diff options
author | Stefan Malzner <stefan@adlk.io> | 2019-04-16 14:17:31 +0200 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2019-04-16 14:17:31 +0200 |
commit | e6535eb6d3b8700335076950a8ca531be8cde175 (patch) | |
tree | 0cf45078f9a0135f11d2c37fbc272b5657087742 | |
parent | fix feature store test (diff) | |
download | ferdium-app-e6535eb6d3b8700335076950a8ca531be8cde175.tar.gz ferdium-app-e6535eb6d3b8700335076950a8ca531be8cde175.tar.zst ferdium-app-e6535eb6d3b8700335076950a8ca531be8cde175.zip |
Refactor settingsWS Store to new FeatureStore
-rwxr-xr-x | src/features/settingsWS/index.js | 16 | ||||
-rwxr-xr-x | src/features/settingsWS/store.js | 40 |
2 files changed, 30 insertions, 26 deletions
diff --git a/src/features/settingsWS/index.js b/src/features/settingsWS/index.js index 4049ae814..2064d2973 100755 --- a/src/features/settingsWS/index.js +++ b/src/features/settingsWS/index.js | |||
@@ -1,10 +1,9 @@ | |||
1 | import { reaction, runInAction } from 'mobx'; | 1 | import { reaction } from 'mobx'; |
2 | import { SettingsWSStore } from './store'; | 2 | import { SettingsWSStore } from './store'; |
3 | import state, { resetState } from './state'; | ||
4 | 3 | ||
5 | const debug = require('debug')('Franz:feature:settingsWS'); | 4 | const debug = require('debug')('Franz:feature:settingsWS'); |
6 | 5 | ||
7 | let store = null; | 6 | export const settingsStore = new SettingsWSStore(); |
8 | 7 | ||
9 | export default function initSettingsWebSocket(stores, actions) { | 8 | export default function initSettingsWebSocket(stores, actions) { |
10 | const { features } = stores; | 9 | const { features } = stores; |
@@ -17,15 +16,10 @@ export default function initSettingsWebSocket(stores, actions) { | |||
17 | (isEnabled) => { | 16 | (isEnabled) => { |
18 | if (isEnabled) { | 17 | if (isEnabled) { |
19 | debug('Initializing `settingsWS` feature'); | 18 | debug('Initializing `settingsWS` feature'); |
20 | store = new SettingsWSStore(stores, null, actions, state); | 19 | settingsStore.start(stores, actions); |
21 | store.initialize(); | 20 | } else if (settingsStore) { |
22 | runInAction(() => { state.isFeatureActive = true; }); | ||
23 | } else if (store) { | ||
24 | debug('Disabling `settingsWS` feature'); | 21 | debug('Disabling `settingsWS` feature'); |
25 | runInAction(() => { state.isFeatureActive = false; }); | 22 | settingsStore.stop(); |
26 | store.teardown(); | ||
27 | store = null; | ||
28 | resetState(); // Reset state to default | ||
29 | } | 23 | } |
30 | }, | 24 | }, |
31 | { | 25 | { |
diff --git a/src/features/settingsWS/store.js b/src/features/settingsWS/store.js index 0f1feebb9..167a70d10 100755 --- a/src/features/settingsWS/store.js +++ b/src/features/settingsWS/store.js | |||
@@ -2,29 +2,34 @@ 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 | ||
5 | import Store from '../../stores/lib/Store'; | 5 | import { FeatureStore } from '../utils/FeatureStore'; |
6 | import { createReactions } from '../../stores/lib/Reaction'; | ||
6 | import { WS_API } from '../../environment'; | 7 | import { WS_API } from '../../environment'; |
7 | 8 | ||
8 | const debug = require('debug')('Franz:feature:settingsWS:store'); | 9 | const debug = require('debug')('Franz:feature:settingsWS:store'); |
9 | 10 | ||
10 | export class SettingsWSStore extends Store { | 11 | export class SettingsWSStore extends FeatureStore { |
11 | ws = null; | 12 | stores = null; |
12 | 13 | ||
13 | @observable connected = false; | 14 | actions = null; |
15 | |||
16 | ws = null; | ||
14 | 17 | ||
15 | pingTimeout = null; | 18 | pingTimeout = null; |
16 | 19 | ||
17 | reconnectTimeout = null; | 20 | reconnectTimeout = null; |
18 | 21 | ||
19 | constructor(stores, api, actions, state) { | 22 | @observable connected = false; |
20 | super(stores, api, actions); | 23 | |
21 | this.state = state; | 24 | start(stores, actions) { |
25 | this.stores = stores; | ||
26 | this.actions = actions; | ||
22 | 27 | ||
23 | this.registerReactions([ | 28 | this._registerReactions(createReactions([ |
24 | this._initialize.bind(this), | 29 | this._initialize.bind(this), |
25 | this._reconnect.bind(this), | 30 | this._reconnect.bind(this), |
26 | this._close.bind(this), | 31 | this._close.bind(this), |
27 | ]); | 32 | ])); |
28 | } | 33 | } |
29 | 34 | ||
30 | connect() { | 35 | connect() { |
@@ -79,7 +84,7 @@ export class SettingsWSStore extends Store { | |||
79 | } | 84 | } |
80 | 85 | ||
81 | send(data) { | 86 | send(data) { |
82 | if (this.ws) { | 87 | if (this.ws && this.ws.readyState === 1) { |
83 | this.ws.send(JSON.stringify(data)); | 88 | this.ws.send(JSON.stringify(data)); |
84 | debug('Sending data', data); | 89 | debug('Sending data', data); |
85 | } else { | 90 | } else { |
@@ -90,7 +95,7 @@ export class SettingsWSStore extends Store { | |||
90 | // Reactions | 95 | // Reactions |
91 | 96 | ||
92 | _initialize() { | 97 | _initialize() { |
93 | if (this.stores.user.data.id) { | 98 | if (this.stores.user.data.id && !this.ws) { |
94 | this.connect(); | 99 | this.connect(); |
95 | } | 100 | } |
96 | } | 101 | } |
@@ -109,10 +114,15 @@ export class SettingsWSStore extends Store { | |||
109 | } | 114 | } |
110 | 115 | ||
111 | _close() { | 116 | _close() { |
112 | if (!this.stores.user.isLoggedIn && this.ws) { | 117 | if (!this.stores.user.isLoggedIn) { |
113 | debug('Terminating connection'); | 118 | debug('Stopping reactions'); |
114 | this.ws.terminate(); | 119 | this._stopReactions(); |
115 | this.ws = null; | 120 | |
121 | if (this.ws) { | ||
122 | debug('Terminating connection'); | ||
123 | this.ws.terminate(); | ||
124 | this.ws = null; | ||
125 | } | ||
116 | } | 126 | } |
117 | } | 127 | } |
118 | } | 128 | } |