aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Stefan Malzner <stefan@adlk.io>2019-04-16 14:17:31 +0200
committerLibravatar Stefan Malzner <stefan@adlk.io>2019-04-16 14:17:31 +0200
commite6535eb6d3b8700335076950a8ca531be8cde175 (patch)
tree0cf45078f9a0135f11d2c37fbc272b5657087742
parentfix feature store test (diff)
downloadferdium-app-e6535eb6d3b8700335076950a8ca531be8cde175.tar.gz
ferdium-app-e6535eb6d3b8700335076950a8ca531be8cde175.tar.zst
ferdium-app-e6535eb6d3b8700335076950a8ca531be8cde175.zip
Refactor settingsWS Store to new FeatureStore
-rwxr-xr-xsrc/features/settingsWS/index.js16
-rwxr-xr-xsrc/features/settingsWS/store.js40
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 @@
1import { reaction, runInAction } from 'mobx'; 1import { reaction } from 'mobx';
2import { SettingsWSStore } from './store'; 2import { SettingsWSStore } from './store';
3import state, { resetState } from './state';
4 3
5const debug = require('debug')('Franz:feature:settingsWS'); 4const debug = require('debug')('Franz:feature:settingsWS');
6 5
7let store = null; 6export const settingsStore = new SettingsWSStore();
8 7
9export default function initSettingsWebSocket(stores, actions) { 8export 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';
2import WebSocket from 'ws'; 2import WebSocket from 'ws';
3import ms from 'ms'; 3import ms from 'ms';
4 4
5import Store from '../../stores/lib/Store'; 5import { FeatureStore } from '../utils/FeatureStore';
6import { createReactions } from '../../stores/lib/Reaction';
6import { WS_API } from '../../environment'; 7import { WS_API } from '../../environment';
7 8
8const debug = require('debug')('Franz:feature:settingsWS:store'); 9const debug = require('debug')('Franz:feature:settingsWS:store');
9 10
10export class SettingsWSStore extends Store { 11export 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}