Skip to content

Commit 45b5803

Browse files
Refactor socket, network_monitor, ...
1 parent e367cdb commit 45b5803

File tree

7 files changed

+474
-450
lines changed

7 files changed

+474
-450
lines changed

src/javascript/_common/base/client_base.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const LocalStore = require('../storage').LocalStore;
55
const State = require('../storage').State;
66
const getPropertyValue = require('../utility').getPropertyValue;
77
const isEmptyObject = require('../utility').isEmptyObject;
8-
const BinarySocket = require('../../app/base/socket');
98

109
const ClientBase = (() => {
1110
const storage_key = 'client.accounts';
@@ -172,13 +171,6 @@ const ClientBase = (() => {
172171
return true;
173172
};
174173

175-
const sendLogoutRequest = (show_login_page) => {
176-
if (show_login_page) {
177-
sessionStorage.setItem('showLoginPage', 1);
178-
}
179-
BinarySocket.send({ logout: '1' });
180-
};
181-
182174
const currentLandingCompany = () => {
183175
const landing_company_response = State.getResponse('landing_company') || {};
184176
const this_shortcode = get('landing_company_shortcode');
@@ -302,7 +294,6 @@ const ClientBase = (() => {
302294
shouldAcceptTnc,
303295
clearAllAccounts,
304296
setNewAccount,
305-
sendLogoutRequest,
306297
currentLandingCompany,
307298
shouldCompleteTax,
308299
getMT5AccountType,
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
const BinarySocket = require('./socket_base');
2+
3+
/*
4+
* Monitors the network status and initialises the WebSocket connection
5+
* 1. online : check the WS status (init/send: blink after timeout, open/message: online)
6+
* 2. offline: it is offline
7+
*/
8+
const NetworkMonitorBase = (() => {
9+
const status_config = {
10+
online : { class: 'online', tooltip: 'Online' },
11+
offline : { class: 'offline', tooltip: 'Offline' },
12+
blinking: { class: 'blinker', tooltip: 'Connecting to server' },
13+
};
14+
const pendings = {};
15+
const pending_keys = {
16+
ws_init : 'ws_init',
17+
ws_request: 'ws_request',
18+
};
19+
const pending_timeouts = {
20+
[pending_keys.ws_init] : 5000,
21+
[pending_keys.ws_request]: 10000,
22+
};
23+
24+
let ws_config,
25+
network_status,
26+
updateUI;
27+
28+
const init = (socket_general_functions, fncUpdateUI) => {
29+
updateUI = fncUpdateUI;
30+
ws_config = Object.assign({ wsEvent, isOnline }, socket_general_functions);
31+
32+
if ('onLine' in navigator) {
33+
window.addEventListener('online', setStatus);
34+
window.addEventListener('offline', setStatus);
35+
} else { // if not supported, default to online and fallback to WS checks
36+
navigator.onLine = true;
37+
}
38+
39+
if (isOnline()) {
40+
BinarySocket.init(ws_config);
41+
}
42+
43+
setStatus(isOnline() ? 'online' : 'offline');
44+
};
45+
46+
const isOnline = () => navigator.onLine;
47+
48+
const wsReconnect = () => {
49+
if (isOnline() && BinarySocket.hasReadyState(2, 3)) { // CLOSING or CLOSED
50+
BinarySocket.init(ws_config);
51+
} else {
52+
BinarySocket.send({ ping: 1 }); // trigger a request to get stable status sooner
53+
}
54+
};
55+
56+
const setStatus = (status) => {
57+
if (!isOnline()) {
58+
network_status = 'offline';
59+
} else if (pending_keys[status] || network_status === 'offline') {
60+
network_status = 'blinking';
61+
wsReconnect();
62+
} else {
63+
network_status = 'online';
64+
}
65+
66+
if (typeof updateUI === 'function') {
67+
updateUI(status_config[network_status], isOnline());
68+
}
69+
};
70+
71+
const ws_events_map = {
72+
init : () => setPending(pending_keys.ws_init),
73+
open : () => clearPendings(pending_keys.ws_init),
74+
send : () => setPending(pending_keys.ws_request),
75+
message: () => clearPendings(),
76+
close : () => setPending(pending_keys.ws_init),
77+
};
78+
79+
const wsEvent = (event) => {
80+
if (typeof ws_events_map[event] === 'function') {
81+
ws_events_map[event]();
82+
}
83+
};
84+
85+
const setPending = (key) => {
86+
if (!pendings[key]) {
87+
pendings[key] = setTimeout(() => {
88+
pendings[key] = undefined;
89+
setStatus(key);
90+
}, pending_timeouts[key]);
91+
}
92+
};
93+
94+
const clearPendings = (key) => {
95+
const clear = (k) => {
96+
clearTimeout(pendings[k]);
97+
pendings[k] = undefined;
98+
if (k === pending_keys.ws_request) {
99+
setStatus('online');
100+
}
101+
};
102+
103+
if (key) {
104+
clear(key);
105+
} else {
106+
Object.keys(pendings).forEach(clear);
107+
}
108+
};
109+
110+
return {
111+
init,
112+
wsEvent,
113+
};
114+
})();
115+
116+
module.exports = NetworkMonitorBase;

0 commit comments

Comments
 (0)