Skip to content

Commit

Permalink
Merge pull request #120 from shafin-deriv/shafin/BOT-2380/chore-reloa…
Browse files Browse the repository at this point in the history
…d-modal-issue

chore: fix reload on device wakeup if bot was running
  • Loading branch information
shafin-deriv authored Nov 11, 2024
2 parents d13aad2 + 1975201 commit 2b2ed56
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
16 changes: 10 additions & 6 deletions src/app/CoreStoreProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,24 @@ const CoreStoreProvider: React.FC<{ children: React.ReactNode }> = observer(({ c
}
}, [client, common, isAuthorizing]);

const handleMessage = useCallback(
const handleMessages = useCallback(
(res: Record<string, unknown>) => {
if (!res) return;
const data = res.data as TSocketResponseData<'balance'>;
const { msg_type, error } = data;

if (error?.code === 'AuthorizationRequired') {
client.logout();
}

if (msg_type === 'balance' && data && !error) {
const balance = data.balance;
if (balance?.accounts) {
client.setAllAccountsBalance(balance);
} else if (balance?.loginid) {
if (!client?.all_accounts_balance?.accounts || !balance?.loginid) return;
const accounts = { ...client.all_accounts_balance.accounts };
const currentLoggedInBalance = accounts[balance.loginid];
const currentLoggedInBalance = { ...accounts[balance.loginid] };
currentLoggedInBalance.balance = balance.balance;

const updatedAccounts = {
Expand All @@ -112,17 +116,17 @@ const CoreStoreProvider: React.FC<{ children: React.ReactNode }> = observer(({ c
);

useEffect(() => {
if (!isAuthorizing && isAuthorized && client) {
const subscription = api_base.api.onMessage().subscribe(handleMessage);
msg_listener.current = { unsubscribe: subscription.unsubscribe };
if (!isAuthorizing && client) {
const subscription = api_base?.api?.onMessage().subscribe(handleMessages);
msg_listener.current = { unsubscribe: subscription?.unsubscribe };
}

return () => {
if (msg_listener.current) {
msg_listener.current.unsubscribe();
}
};
}, [connectionStatus, handleMessage, isAuthorizing, isAuthorized, client]);
}, [connectionStatus, handleMessages, isAuthorizing, isAuthorized, client]);

useEffect(() => {
if (!isAuthorizing && isAuthorized && !accountInitialization.current && client) {
Expand Down
13 changes: 1 addition & 12 deletions src/components/layout/header/account-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ const RenderAccountItems = ({ isVirtual, modifiedAccountList, switchAccount }: T
className='deriv-account-switcher__logout'
onClick={() => {
client.logout();
api_base?.api?.logout();
}}
>
<Text color='prominent' size='xs' align='left' className='deriv-account-switcher__logout-text'>
Expand All @@ -131,17 +130,7 @@ const RenderAccountItems = ({ isVirtual, modifiedAccountList, switchAccount }: T

const AccountSwitcher = observer(({ activeAccount }: TAccountSwitcher) => {
const { accountList } = useApiBase();
const { ui, run_panel, client } = useStore() ?? {
ui: {
account_switcher_disabled_message: '',
},
run_panel: {
is_stop_button_visible: false,
},
client: {
all_accounts_balance: {},
},
};
const { ui, run_panel, client } = useStore();
const { account_switcher_disabled_message } = ui;
const { is_stop_button_visible } = run_panel;

Expand Down
9 changes: 0 additions & 9 deletions src/external/bot-skeleton/services/api/api-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ class APIBase {

if (this.time_interval) clearInterval(this.time_interval);
this.time_interval = null;
this.getTime();

if (V2GetActiveToken()) {
setIsAuthorizing(true);
Expand Down Expand Up @@ -274,14 +273,6 @@ class APIBase {
clearTimeout(i);
});
}

getTime() {
if (!this.time_interval) {
this.time_interval = setInterval(() => {
this.api?.send({ time: 1 });
}, 30000);
}
}
}

export const api_base = new APIBase();
43 changes: 23 additions & 20 deletions src/pages/main/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import MobileWrapper from '@/components/shared_ui/mobile-wrapper';
import Tabs from '@/components/shared_ui/tabs/tabs';
import TradingViewModal from '@/components/trading-view-chart/trading-view-modal';
import { DBOT_TABS, TAB_IDS } from '@/constants/bot-contents';
import { updateWorkspaceName } from '@/external/bot-skeleton';
import dbot from '@/external/bot-skeleton/scratch/dbot';
import { api_base } from '@/external/bot-skeleton/services/api/api-base';
import { api_base, updateWorkspaceName } from '@/external/bot-skeleton';
import { CONNECTION_STATUS } from '@/external/bot-skeleton/services/api/observables/connection-status-stream';
import { isDbotRTL } from '@/external/bot-skeleton/utils/workspace';
import { useApiBase } from '@/hooks/useApiBase';
import { useStore } from '@/hooks/useStore';
import {
LabelPairedChartLineCaptionRegularIcon,
Expand All @@ -30,6 +30,7 @@ const Chart = lazy(() => import('../chart'));
const Tutorial = lazy(() => import('../tutorials'));

const AppWrapper = observer(() => {
const { connectionStatus } = useApiBase();
const { dashboard, load_modal, run_panel, quick_strategy, summary_card } = useStore();
const {
active_tab,
Expand All @@ -42,8 +43,15 @@ const AppWrapper = observer(() => {
setTourDialogVisibility,
} = dashboard;
const { onEntered, dashboard_strategies } = load_modal;
const { is_dialog_open, is_drawer_open, dialog_options, onCancelButtonClick, onCloseDialog, onOkButtonClick } =
run_panel;
const {
is_dialog_open,
is_drawer_open,
dialog_options,
onCancelButtonClick,
onCloseDialog,
onOkButtonClick,
stopBot,
} = run_panel;
const { is_open } = quick_strategy;
const { cancel_button_text, ok_button_text, title, message } = dialog_options as { [key: string]: string };
const { clear } = summary_card;
Expand All @@ -62,22 +70,17 @@ const AppWrapper = observer(() => {
};
const active_hash_tab = GetHashedValue(active_tab);

const checkAndHandleConnection = () => {
const api_status = api_base.getConnectionStatus();
const web_socket_status = ['Connecting', 'Closing', 'Closed'];
//added this check because after sleep mode all the store values refresh and is_running is false.
const is_bot_running = document.getElementById('db-animation__stop-button') !== null;
if (is_bot_running && web_socket_status.includes(api_status)) {
dbot.terminateBot();
clear();
setWebSocketState(false);
}
};

React.useEffect(() => {
window.addEventListener('focus', checkAndHandleConnection);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
if (connectionStatus !== CONNECTION_STATUS.OPENED) {
const is_bot_running = document.getElementById('db-animation__stop-button') !== null;
if (is_bot_running) {
clear();
stopBot();
api_base.setIsRunning(false);
setWebSocketState(false);
}
}
}, [clear, connectionStatus, setWebSocketState, stopBot]);

React.useEffect(() => {
if (is_open) {
Expand Down

0 comments on commit 2b2ed56

Please sign in to comment.