From e6535eb6d3b8700335076950a8ca531be8cde175 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Tue, 16 Apr 2019 14:17:31 +0200 Subject: Refactor settingsWS Store to new FeatureStore --- src/features/settingsWS/index.js | 16 +++++----------- src/features/settingsWS/store.js | 40 +++++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 26 deletions(-) (limited to 'src/features/settingsWS') 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 @@ -import { reaction, runInAction } from 'mobx'; +import { reaction } from 'mobx'; import { SettingsWSStore } from './store'; -import state, { resetState } from './state'; const debug = require('debug')('Franz:feature:settingsWS'); -let store = null; +export const settingsStore = new SettingsWSStore(); export default function initSettingsWebSocket(stores, actions) { const { features } = stores; @@ -17,15 +16,10 @@ export default function initSettingsWebSocket(stores, actions) { (isEnabled) => { if (isEnabled) { debug('Initializing `settingsWS` feature'); - store = new SettingsWSStore(stores, null, actions, state); - store.initialize(); - runInAction(() => { state.isFeatureActive = true; }); - } else if (store) { + settingsStore.start(stores, actions); + } else if (settingsStore) { debug('Disabling `settingsWS` feature'); - runInAction(() => { state.isFeatureActive = false; }); - store.teardown(); - store = null; - resetState(); // Reset state to default + settingsStore.stop(); } }, { 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'; import WebSocket from 'ws'; import ms from 'ms'; -import Store from '../../stores/lib/Store'; +import { FeatureStore } from '../utils/FeatureStore'; +import { createReactions } from '../../stores/lib/Reaction'; import { WS_API } from '../../environment'; const debug = require('debug')('Franz:feature:settingsWS:store'); -export class SettingsWSStore extends Store { - ws = null; +export class SettingsWSStore extends FeatureStore { + stores = null; - @observable connected = false; + actions = null; + + ws = null; pingTimeout = null; reconnectTimeout = null; - constructor(stores, api, actions, state) { - super(stores, api, actions); - this.state = state; + @observable connected = false; + + start(stores, actions) { + this.stores = stores; + this.actions = actions; - this.registerReactions([ + this._registerReactions(createReactions([ this._initialize.bind(this), this._reconnect.bind(this), this._close.bind(this), - ]); + ])); } connect() { @@ -79,7 +84,7 @@ export class SettingsWSStore extends Store { } send(data) { - if (this.ws) { + if (this.ws && this.ws.readyState === 1) { this.ws.send(JSON.stringify(data)); debug('Sending data', data); } else { @@ -90,7 +95,7 @@ export class SettingsWSStore extends Store { // Reactions _initialize() { - if (this.stores.user.data.id) { + if (this.stores.user.data.id && !this.ws) { this.connect(); } } @@ -109,10 +114,15 @@ export class SettingsWSStore extends Store { } _close() { - if (!this.stores.user.isLoggedIn && this.ws) { - debug('Terminating connection'); - this.ws.terminate(); - this.ws = null; + if (!this.stores.user.isLoggedIn) { + debug('Stopping reactions'); + this._stopReactions(); + + if (this.ws) { + debug('Terminating connection'); + this.ws.terminate(); + this.ws = null; + } } } } -- cgit v1.2.3-70-g09d2