forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIProvider.tsx
256 lines (215 loc) · 8.65 KB
/
APIProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, { PropsWithChildren, createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';
// @ts-expect-error `@deriv/deriv-api` is not in TypeScript, Hence we ignore the TS error.
import DerivAPIBasic from '@deriv/deriv-api/dist/DerivAPIBasic';
import { getAppId, getSocketURL, useWS } from '@deriv/shared';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import {
TSocketEndpointNames,
TSocketError,
TSocketRequestPayload,
TSocketResponseData,
TSocketSubscribableEndpointNames,
} from '../types';
import { hashObject } from './utils';
type TSendFunction = <T extends TSocketEndpointNames>(
name: T,
payload?: TSocketRequestPayload<T>
) => Promise<TSocketResponseData<T> & TSocketError<T>>;
type TSubscribeFunction = <T extends TSocketSubscribableEndpointNames>(
name: T,
payload?: TSocketRequestPayload<T>
) => Promise<{ id: string; subscription: DerivAPIBasic['subscribe'] }>;
type TUnsubscribeFunction = (id: string) => void;
type APIContextData = {
derivAPI: DerivAPIBasic | null;
switchEnvironment: (loginid: string | null | undefined) => void;
send: TSendFunction;
subscribe: TSubscribeFunction;
unsubscribe: TUnsubscribeFunction;
queryClient: QueryClient;
};
const APIContext = createContext<APIContextData | null>(null);
declare global {
interface Window {
ReactQueryClient?: QueryClient;
DerivAPI?: Record<string, DerivAPIBasic>;
WSConnections?: Record<string, WebSocket>;
}
}
// This is a temporary workaround to share a single `QueryClient` instance between all the packages.
const getSharedQueryClientContext = (): QueryClient => {
if (!window.ReactQueryClient) {
window.ReactQueryClient = new QueryClient();
}
return window.ReactQueryClient;
};
/**
* Retrieves the WebSocket URL based on the current environment.
* @returns {string} The WebSocket URL.
*/
const getWebSocketURL = () => {
const endpoint = getSocketURL();
const app_id = getAppId();
const language = localStorage.getItem('i18n_language');
const brand = 'deriv';
const wss_url = `wss://${endpoint}/websockets/v3?app_id=${app_id}&l=${language}&brand=${brand}`;
return wss_url;
};
/**
* Retrieves or initializes a WebSocket instance based on the provided URL.
* @param {string} wss_url - The WebSocket URL.
* @returns {WebSocket} The WebSocket instance associated with the provided URL.
*/
const getWebsocketInstance = (wss_url: string, onWSClose: () => void) => {
if (!window.WSConnections) {
window.WSConnections = {};
}
const existingWebsocketInstance = window.WSConnections[wss_url];
if (
!existingWebsocketInstance ||
!(existingWebsocketInstance instanceof WebSocket) ||
[2, 3].includes(existingWebsocketInstance.readyState)
) {
window.WSConnections[wss_url] = new WebSocket(wss_url);
window.WSConnections[wss_url].addEventListener('close', () => {
if (typeof onWSClose === 'function') onWSClose();
});
}
return window.WSConnections[wss_url];
};
/**
* Retrieves the active WebSocket instance.
* @returns {WebSocket} The WebSocket instance associated with the provided URL.
*/
export const getActiveWebsocket = () => {
const wss_url = getWebSocketURL();
return window?.WSConnections?.[wss_url];
};
/**
* Initializes a DerivAPI instance for the global window. This enables a standalone connection
* without causing race conditions with deriv-app core stores.
* @returns {DerivAPIBasic} The initialized DerivAPI instance.
*/
const initializeDerivAPI = (onWSClose: () => void): DerivAPIBasic => {
if (!window.DerivAPI) {
window.DerivAPI = {};
}
const wss_url = getWebSocketURL();
const websocketConnection = getWebsocketInstance(wss_url, onWSClose);
if (!window.DerivAPI?.[wss_url] || window.DerivAPI?.[wss_url].isConnectionClosed()) {
window.DerivAPI[wss_url] = new DerivAPIBasic({ connection: websocketConnection });
}
return window.DerivAPI?.[wss_url];
};
const queryClient = getSharedQueryClientContext();
/**
* Determines the WS environment based on the login ID and custom server URL.
* @param {string | null | undefined} loginid - The login ID (can be a string, null, or undefined).
* @returns {string} Returns the WS environment: 'custom', 'real', or 'demo'.
*/
/**
* @deprecated Please use 'WebSocketUtils.getEnvironmentFromLoginid' from '@deriv-com/utils' instead of this.
*/
const getEnvironment = (loginid: string | null | undefined) => {
const customServerURL = window.localStorage.getItem('config.server_url');
if (customServerURL) return 'custom';
if (loginid && !/^(VRT|VRW)/.test(loginid)) return 'real';
return 'demo';
};
type TAPIProviderProps = {
/** If set to true, the APIProvider will instantiate it's own socket connection. */
standalone?: boolean;
};
const APIProvider = ({ children, standalone = false }: PropsWithChildren<TAPIProviderProps>) => {
const WS = useWS();
const [reconnect, setReconnect] = useState(false);
const activeLoginid =
window.sessionStorage.getItem('active_loginid') || window.localStorage.getItem('active_loginid');
const [environment, setEnvironment] = useState(getEnvironment(activeLoginid));
const standaloneDerivAPI = useRef(standalone ? initializeDerivAPI(() => setReconnect(true)) : null);
const subscriptions = useRef<Record<string, DerivAPIBasic['subscribe']>>();
const send: TSendFunction = (name, payload) => {
return standaloneDerivAPI.current?.send({ [name]: 1, ...payload });
};
const subscribe: TSubscribeFunction = async (name, payload) => {
const id = await hashObject({ name, payload });
const matchingSubscription = subscriptions.current?.[id];
if (matchingSubscription) return { id, subscription: matchingSubscription };
const { payload: _payload } = payload ?? {};
const subscription = standaloneDerivAPI.current?.subscribe({
[name]: 1,
subscribe: 1,
...(_payload ?? {}),
});
subscriptions.current = { ...(subscriptions.current ?? {}), ...{ [id]: subscription } };
return { id, subscription };
};
const unsubscribe: TUnsubscribeFunction = id => {
const matchingSubscription = subscriptions.current?.[id];
if (matchingSubscription) matchingSubscription.unsubscribe();
};
useEffect(() => {
const currentDerivApi = standaloneDerivAPI.current;
const currentSubscriptions = subscriptions.current;
return () => {
if (currentSubscriptions) {
Object.keys(currentSubscriptions).forEach(key => {
currentSubscriptions[key].unsubscribe();
});
}
if (currentDerivApi && currentDerivApi.connection.readyState === 1) currentDerivApi.disconnect();
};
}, []);
const switchEnvironment = useCallback(
(loginid: string | null | undefined) => {
if (!standalone) return;
const currentEnvironment = getEnvironment(loginid);
if (currentEnvironment !== 'custom' && currentEnvironment !== environment) {
setEnvironment(currentEnvironment);
}
},
[environment, standalone]
);
useEffect(() => {
let interval_id: ReturnType<typeof setInterval>;
if (standalone) {
interval_id = setInterval(() => standaloneDerivAPI.current?.send({ ping: 1 }), 10000);
}
return () => clearInterval(interval_id);
}, [standalone]);
useEffect(() => {
let reconnectTimerId: NodeJS.Timeout;
if (standalone || reconnect) {
standaloneDerivAPI.current = initializeDerivAPI(() => {
reconnectTimerId = setTimeout(() => setReconnect(true), 500);
});
setReconnect(false);
}
return () => clearTimeout(reconnectTimerId);
}, [environment, reconnect, standalone]);
return (
<APIContext.Provider
value={{
derivAPI: standalone ? standaloneDerivAPI.current : WS,
switchEnvironment,
send,
subscribe,
unsubscribe,
queryClient,
}}
>
<QueryClientProvider client={queryClient}>
{children}
{/* <ReactQueryDevtools /> */}
</QueryClientProvider>
</APIContext.Provider>
);
};
export const useAPIContext = () => {
const context = useContext(APIContext);
if (!context) {
throw new Error('useAPIContext must be used within APIProvider');
}
return context;
};
export default APIProvider;