diff options
author | Abin Mn <abinmn619@gmail.com> | 2021-10-26 21:18:20 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-26 21:18:20 +0530 |
commit | 91c69428ed0dc2dd26b00c6dd5a6684f25515a34 (patch) | |
tree | 062a9a4946aa1367263533682ee59e795ea97234 /src/features/settingsWS/store.js | |
parent | Add feature to display service name under service icon when the toggle for it... (diff) | |
download | ferdium-app-91c69428ed0dc2dd26b00c6dd5a6684f25515a34.tar.gz ferdium-app-91c69428ed0dc2dd26b00c6dd5a6684f25515a34.tar.zst ferdium-app-91c69428ed0dc2dd26b00c6dd5a6684f25515a34.zip |
Cleanup/remove feature toggle for todo, workspace, service proxy (#2134)
* Remove DEFAULT_FEATURES_CONFIG from config
* Remove static controller
Co-authored-by: Madhuri B <MadhuriBandanadam@users.noreply.github.com>
Diffstat (limited to 'src/features/settingsWS/store.js')
-rwxr-xr-x | src/features/settingsWS/store.js | 132 |
1 files changed, 0 insertions, 132 deletions
diff --git a/src/features/settingsWS/store.js b/src/features/settingsWS/store.js deleted file mode 100755 index 3b9e10825..000000000 --- a/src/features/settingsWS/store.js +++ /dev/null | |||
@@ -1,132 +0,0 @@ | |||
1 | import { observable } from 'mobx'; | ||
2 | import WebSocket from 'ws'; | ||
3 | import ms from 'ms'; | ||
4 | |||
5 | import { FeatureStore } from '../utils/FeatureStore'; | ||
6 | import { createReactions } from '../../stores/lib/Reaction'; | ||
7 | import { WS_API } from '../../environment-remote'; | ||
8 | |||
9 | const debug = require('debug')('Ferdi:feature:settingsWS:store'); | ||
10 | |||
11 | export class SettingsWSStore extends FeatureStore { | ||
12 | stores = null; | ||
13 | |||
14 | actions = null; | ||
15 | |||
16 | ws = null; | ||
17 | |||
18 | pingTimeout = null; | ||
19 | |||
20 | reconnectTimeout = null; | ||
21 | |||
22 | @observable connected = false; | ||
23 | |||
24 | start(stores, actions) { | ||
25 | this.stores = stores; | ||
26 | this.actions = actions; | ||
27 | |||
28 | this._registerReactions( | ||
29 | createReactions([ | ||
30 | this._initialize.bind(this), | ||
31 | this._reconnect.bind(this), | ||
32 | this._close.bind(this), | ||
33 | ]), | ||
34 | ); | ||
35 | } | ||
36 | |||
37 | connect() { | ||
38 | try { | ||
39 | const wsURL = `${WS_API}/ws/${this.stores.user.data.id}`; | ||
40 | debug('Setting up WebSocket to', wsURL); | ||
41 | |||
42 | this.ws = new WebSocket(wsURL); | ||
43 | |||
44 | this.ws.on('open', () => { | ||
45 | debug('Opened WebSocket'); | ||
46 | this.send({ | ||
47 | action: 'authorize', | ||
48 | token: this.stores.user.authToken, | ||
49 | }); | ||
50 | |||
51 | this.connected = true; | ||
52 | |||
53 | this.heartbeat(); | ||
54 | }); | ||
55 | |||
56 | this.ws.on('message', data => { | ||
57 | const resp = JSON.parse(data); | ||
58 | debug('Received message', resp); | ||
59 | |||
60 | if (resp.id) { | ||
61 | this.stores.user.getUserInfoRequest.patch(result => { | ||
62 | if (!result) return; | ||
63 | |||
64 | debug('Patching user object with new values'); | ||
65 | Object.assign(result, resp); | ||
66 | }); | ||
67 | } | ||
68 | }); | ||
69 | |||
70 | this.ws.on('ping', this.heartbeat.bind(this)); | ||
71 | } catch (error) { | ||
72 | console.error(error); | ||
73 | } | ||
74 | } | ||
75 | |||
76 | heartbeat() { | ||
77 | debug('Heartbeat'); | ||
78 | clearTimeout(this.pingTimeout); | ||
79 | |||
80 | this.pingTimeout = setTimeout(() => { | ||
81 | debug('Terminating connection, reconnecting in 35'); | ||
82 | this.ws.terminate(); | ||
83 | |||
84 | this.connected = false; | ||
85 | }, ms('35s')); | ||
86 | } | ||
87 | |||
88 | send(data) { | ||
89 | if (this.ws && this.ws.readyState === 1) { | ||
90 | this.ws.send(JSON.stringify(data)); | ||
91 | debug('Sending data', data); | ||
92 | } else { | ||
93 | debug('WebSocket is not initialized'); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | // Reactions | ||
98 | |||
99 | _initialize() { | ||
100 | if (this.stores.user.data.id && !this.ws) { | ||
101 | this.connect(); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | _reconnect() { | ||
106 | if (!this.connected) { | ||
107 | debug('Trying to reconnect in 30s'); | ||
108 | this.reconnectTimeout = setInterval(() => { | ||
109 | debug('Trying to reconnect'); | ||
110 | this.connect(); | ||
111 | }, ms('30s')); | ||
112 | } else { | ||
113 | debug('Clearing reconnect interval'); | ||
114 | clearInterval(this.reconnectTimeout); | ||
115 | } | ||
116 | } | ||
117 | |||
118 | _close() { | ||
119 | if (!this.stores.user.isLoggedIn) { | ||
120 | debug('Stopping reactions'); | ||
121 | this._stopReactions(); | ||
122 | |||
123 | if (this.ws) { | ||
124 | debug('Terminating connection'); | ||
125 | this.ws.terminate(); | ||
126 | this.ws = null; | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | |||
132 | export default SettingsWSStore; | ||