diff options
author | 2019-04-13 20:58:00 +0200 | |
---|---|---|
committer | 2019-04-13 20:58:00 +0200 | |
commit | 6cf3217f7f5da5b8ecfbb52e7c761ab91b1c4c97 (patch) | |
tree | 790582d844f8e9eba2263d11ceb08fb77c6b2415 /src/features | |
parent | Remove copy & paste issues (diff) | |
download | ferdium-app-6cf3217f7f5da5b8ecfbb52e7c761ab91b1c4c97.tar.gz ferdium-app-6cf3217f7f5da5b8ecfbb52e7c761ab91b1c4c97.tar.zst ferdium-app-6cf3217f7f5da5b8ecfbb52e7c761ab91b1c4c97.zip |
Use store reactions
Diffstat (limited to 'src/features')
-rwxr-xr-x | src/features/settingsWS/store.js | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/src/features/settingsWS/store.js b/src/features/settingsWS/store.js index 8e1d455f1..0f1feebb9 100755 --- a/src/features/settingsWS/store.js +++ b/src/features/settingsWS/store.js | |||
@@ -1,4 +1,4 @@ | |||
1 | import { observable, reaction } from 'mobx'; | 1 | import { observable } from 'mobx'; |
2 | import WebSocket from 'ws'; | 2 | import WebSocket from 'ws'; |
3 | import ms from 'ms'; | 3 | import ms from 'ms'; |
4 | 4 | ||
@@ -19,13 +19,12 @@ export class SettingsWSStore extends Store { | |||
19 | constructor(stores, api, actions, state) { | 19 | constructor(stores, api, actions, state) { |
20 | super(stores, api, actions); | 20 | super(stores, api, actions); |
21 | this.state = state; | 21 | this.state = state; |
22 | } | ||
23 | 22 | ||
24 | setup() { | 23 | this.registerReactions([ |
25 | reaction(() => this.stores.user.data.id, this.connect.bind(this), { | 24 | this._initialize.bind(this), |
26 | fireImmediately: true, | 25 | this._reconnect.bind(this), |
27 | }); | 26 | this._close.bind(this), |
28 | reaction(() => !this.connected, this.reconnect.bind(this)); | 27 | ]); |
29 | } | 28 | } |
30 | 29 | ||
31 | connect() { | 30 | connect() { |
@@ -72,7 +71,7 @@ export class SettingsWSStore extends Store { | |||
72 | clearTimeout(this.pingTimeout); | 71 | clearTimeout(this.pingTimeout); |
73 | 72 | ||
74 | this.pingTimeout = setTimeout(() => { | 73 | this.pingTimeout = setTimeout(() => { |
75 | debug('Terminating connection reconnecting in 35'); | 74 | debug('Terminating connection, reconnecting in 35'); |
76 | this.ws.terminate(); | 75 | this.ws.terminate(); |
77 | 76 | ||
78 | this.connected = false; | 77 | this.connected = false; |
@@ -88,7 +87,15 @@ export class SettingsWSStore extends Store { | |||
88 | } | 87 | } |
89 | } | 88 | } |
90 | 89 | ||
91 | reconnect() { | 90 | // Reactions |
91 | |||
92 | _initialize() { | ||
93 | if (this.stores.user.data.id) { | ||
94 | this.connect(); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | _reconnect() { | ||
92 | if (!this.connected) { | 99 | if (!this.connected) { |
93 | debug('Trying to reconnect in 30s'); | 100 | debug('Trying to reconnect in 30s'); |
94 | this.reconnectTimeout = setInterval(() => { | 101 | this.reconnectTimeout = setInterval(() => { |
@@ -100,6 +107,14 @@ export class SettingsWSStore extends Store { | |||
100 | clearInterval(this.reconnectTimeout); | 107 | clearInterval(this.reconnectTimeout); |
101 | } | 108 | } |
102 | } | 109 | } |
110 | |||
111 | _close() { | ||
112 | if (!this.stores.user.isLoggedIn && this.ws) { | ||
113 | debug('Terminating connection'); | ||
114 | this.ws.terminate(); | ||
115 | this.ws = null; | ||
116 | } | ||
117 | } | ||
103 | } | 118 | } |
104 | 119 | ||
105 | export default SettingsWSStore; | 120 | export default SettingsWSStore; |