From d868fab7550ee02b3057626579420a0ef23a1186 Mon Sep 17 00:00:00 2001 From: hirad-deriv Date: Tue, 7 Jan 2025 13:27:52 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"Jim/wall=205227/switch=20ws=20connect?= =?UTF-8?q?ion=20when=20clients=20switch=20between=20real=20a=E2=80=A6"=20?= =?UTF-8?q?(#17943)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ac6d910ca17a12f47717d262d5874e2183c7af87. --- packages/api-v2/src/APIProvider.tsx | 31 +++++----------- packages/api-v2/src/AuthProvider.tsx | 41 +++++----------------- packages/api-v2/src/constants/index.ts | 1 - packages/api-v2/src/constants/platforms.ts | 1 - packages/api-v2/src/ws-client/ws-client.ts | 16 +++------ packages/shared/src/utils/config/config.ts | 8 ++--- packages/wallets/src/App.tsx | 2 +- 7 files changed, 25 insertions(+), 75 deletions(-) delete mode 100644 packages/api-v2/src/constants/platforms.ts diff --git a/packages/api-v2/src/APIProvider.tsx b/packages/api-v2/src/APIProvider.tsx index d8f77d2833d3..3ecb76ef7b4a 100644 --- a/packages/api-v2/src/APIProvider.tsx +++ b/packages/api-v2/src/APIProvider.tsx @@ -1,14 +1,10 @@ -import React, { createContext, PropsWithChildren, useCallback, useContext, useEffect, useRef, useState } from 'react'; - +import React, { PropsWithChildren, createContext, useContext, useEffect, useRef, useState, useCallback } from 'react'; import { getAppId, getSocketURL } from '@deriv/shared'; import { getInitialLanguage } from '@deriv-com/translations'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; - import { TSocketRequestPayload, TSocketResponseData, TSocketSubscribableEndpointNames } from '../types'; - -import WSClient from './ws-client/ws-client'; -import { PLATFORMS } from './constants'; import { hashObject } from './utils'; +import WSClient from './ws-client/ws-client'; type TSubscribeFunction = ( name: T, @@ -31,14 +27,14 @@ type APIContextData = { setOnConnected: (onConnected: () => void) => void; connection: WebSocket; wsClient: WSClient; - createNewWSConnection: () => void; }; /** * Retrieves the WebSocket URL based on the current environment. * @returns {string} The WebSocket URL. */ -const getWebSocketURL = (endpoint: string) => { +const getWebSocketURL = () => { + const endpoint = getSocketURL(); const app_id = getAppId(); const language = getInitialLanguage(); return `wss://${endpoint}/websockets/v3?app_id=${app_id}&l=${language}&brand=deriv`; @@ -49,8 +45,8 @@ const APIContext = createContext(null); /** * @returns {WebSocket} The initialized WebSocket instance. */ -const initializeConnection = (endpoint: string, onWSClose: () => void, onOpen?: () => void): WebSocket => { - const wss_url = getWebSocketURL(endpoint); +const initializeConnection = (onWSClose: () => void, onOpen?: () => void): WebSocket => { + const wss_url = getWebSocketURL(); const connection = new WebSocket(wss_url); connection.addEventListener('close', () => { @@ -71,13 +67,12 @@ const initializeConnection = (endpoint: string, onWSClose: () => void, onOpen?: type TAPIProviderProps = { /** If set to true, the APIProvider will instantiate it's own socket connection. */ standalone?: boolean; - platform?: string; }; type SubscribeReturnType = ReturnType; // This captures the entire return type of TSubscribeFunction type UnwrappedSubscription = Awaited; -const APIProvider = ({ children, platform }: PropsWithChildren) => { +const APIProvider = ({ children }: PropsWithChildren) => { const [reconnect, setReconnect] = useState(false); const connectionRef = useRef(); const subscriptionsRef = useRef>(); @@ -92,7 +87,6 @@ const APIProvider = ({ children, platform }: PropsWithChildren(language); - const endpoint = getSocketURL(platform === PLATFORMS.WALLETS); useEffect(() => { isMounted.current = true; @@ -115,7 +109,6 @@ const APIProvider = ({ children, platform }: PropsWithChildren { if (isMounted.current) setReconnect(true); }, @@ -125,7 +118,6 @@ const APIProvider = ({ children, platform }: PropsWithChildren { reconnectTimerId = setTimeout(() => { if (isMounted.current) { @@ -218,7 +209,6 @@ const APIProvider = ({ children, platform }: PropsWithChildren clearTimeout(reconnectTimerId); - }, [endpoint, reconnect]); + }, [reconnect]); // reconnects to latest WS url for new language only when language changes useEffect(() => { @@ -239,15 +229,10 @@ const APIProvider = ({ children, platform }: PropsWithChildren { - setReconnect(true); - }, []); - return ( ) => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any subscribe: (onData: (response: any) => void) => Promise<{ unsubscribe: () => Promise }>; }; }; @@ -112,7 +108,7 @@ const AuthProvider = ({ loginIDKey, children, cookieTimeout, selectDefaultAccoun const { mutateAsync } = useMutation('authorize'); - const { queryClient, setOnReconnected, setOnConnected, wsClient, createNewWSConnection } = useAPIContext(); + const { queryClient, setOnReconnected, setOnConnected, wsClient } = useAPIContext(); const [isLoading, setIsLoading] = useState(true); const [isSwitching, setIsSwitching] = useState(false); @@ -129,7 +125,6 @@ const AuthProvider = ({ loginIDKey, children, cookieTimeout, selectDefaultAccoun const subscribe = useCallback( (name: T, payload?: TSocketRequestPayload) => { return { - // eslint-disable-next-line @typescript-eslint/no-explicit-any subscribe: (onData: (response: any) => void) => { return wsClient?.subscribe(name, payload, onData); }, @@ -154,15 +149,8 @@ const AuthProvider = ({ loginIDKey, children, cookieTimeout, selectDefaultAccoun if (!activeAccount) return; localStorage.setItem(loginIDKey ?? 'active_loginid', activeLoginID); - const isDemo = activeAccount.is_virtual; - const shouldCreateNewWSConnection = - (isDemo && wsClient?.endpoint === AppIDConstants.environments.real) || - (!isDemo && wsClient?.endpoint === AppIDConstants.environments.demo); - if (shouldCreateNewWSConnection) { - createNewWSConnection(); - } }, - [loginIDKey, wsClient?.endpoint, createNewWSConnection] + [loginIDKey] ); useEffect(() => { @@ -279,21 +267,8 @@ const AuthProvider = ({ loginIDKey, children, cookieTimeout, selectDefaultAccoun isInitializing, subscribe, logout, - createNewWSConnection, }; - }, [ - data, - switchAccount, - refetch, - isLoading, - isError, - isFetching, - isSuccess, - loginid, - logout, - createNewWSConnection, - subscribe, - ]); + }, [data, switchAccount, refetch, isLoading, isError, isFetching, isSuccess, loginid, logout, subscribe]); return {children}; }; diff --git a/packages/api-v2/src/constants/index.ts b/packages/api-v2/src/constants/index.ts index 4460fc78c0de..aa7dd211925f 100644 --- a/packages/api-v2/src/constants/index.ts +++ b/packages/api-v2/src/constants/index.ts @@ -1,4 +1,3 @@ export * from './countries'; export * from './onfido'; export * from './errorCodes'; -export * from './platforms'; diff --git a/packages/api-v2/src/constants/platforms.ts b/packages/api-v2/src/constants/platforms.ts deleted file mode 100644 index ff77f49b7f7b..000000000000 --- a/packages/api-v2/src/constants/platforms.ts +++ /dev/null @@ -1 +0,0 @@ -export const PLATFORMS = { WALLETS: 'wallets' } as const; diff --git a/packages/api-v2/src/ws-client/ws-client.ts b/packages/api-v2/src/ws-client/ws-client.ts index 76990332f1fb..6c153bba4890 100644 --- a/packages/api-v2/src/ws-client/ws-client.ts +++ b/packages/api-v2/src/ws-client/ws-client.ts @@ -1,13 +1,12 @@ +import SubscriptionsManager from './subscriptions-manager'; +import request from './request'; import { - TSocketEndpointNames, - TSocketRequestPayload, TSocketResponse, + TSocketRequestPayload, + TSocketEndpointNames, TSocketSubscribableEndpointNames, } from '../../types'; -import request from './request'; -import SubscriptionsManager from './subscriptions-manager'; - /** * WSClient as main instance */ @@ -16,7 +15,6 @@ export default class WSClient { subscriptionManager: SubscriptionsManager; isAuthorized = false; onAuthorized?: () => void; - endpoint?: string; constructor(onAuthorized?: () => void) { this.onAuthorized = onAuthorized; @@ -30,10 +28,6 @@ export default class WSClient { } } - setEndpoint(endpoint: string) { - this.endpoint = endpoint; - } - private onWebsocketAuthorized() { if (!this.ws) { return; @@ -53,7 +47,7 @@ export default class WSClient { return Promise.reject(new Error('WS is not set')); } return request(this.ws, name, payload).then((response: TSocketResponse) => { - if ('msg_type' in response && response.msg_type === 'authorize') { + if ((response as unknown as any).msg_type === 'authorize') { this.onWebsocketAuthorized(); } diff --git a/packages/shared/src/utils/config/config.ts b/packages/shared/src/utils/config/config.ts index 742ed41d1f8c..6d51850df093 100644 --- a/packages/shared/src/utils/config/config.ts +++ b/packages/shared/src/utils/config/config.ts @@ -77,7 +77,7 @@ export const getAppId = () => { return app_id; }; -export const getSocketURL = (is_wallets = false) => { +export const getSocketURL = () => { const local_storage_server_url = window.localStorage.getItem('config.server_url'); if (local_storage_server_url) return local_storage_server_url; @@ -87,10 +87,8 @@ export const getSocketURL = (is_wallets = false) => { const params = new URLSearchParams(document.location.search.substring(1)); active_loginid_from_url = params.get('acct1'); } - const local_storage_loginid = is_wallets - ? window.localStorage.getItem('active_wallet_loginid') - : window.localStorage.getItem('active_loginid'); - const loginid = local_storage_loginid || active_loginid_from_url; + + const loginid = window.localStorage.getItem('active_loginid') || active_loginid_from_url; const is_real = loginid && !/^(VRT|VRW)/.test(loginid); const server = is_real ? 'green' : 'blue'; diff --git a/packages/wallets/src/App.tsx b/packages/wallets/src/App.tsx index ead52b96cb88..41793dc31d1b 100644 --- a/packages/wallets/src/App.tsx +++ b/packages/wallets/src/App.tsx @@ -44,7 +44,7 @@ const App: React.FC = ({ const defaultLanguage = preferredLanguage ?? language; return ( - + }>