From 25fc276d5e3f754f915500e91229b8607febc478 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Tue, 9 Apr 2019 01:20:35 +0200 Subject: Add settings websocket --- src/features/settingsWS/index.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 src/features/settingsWS/index.js (limited to 'src/features/settingsWS/index.js') diff --git a/src/features/settingsWS/index.js b/src/features/settingsWS/index.js new file mode 100755 index 000000000..1e268f184 --- /dev/null +++ b/src/features/settingsWS/index.js @@ -0,0 +1,35 @@ +import { reaction, runInAction } from 'mobx'; +import { SettingsWSStore } from './store'; +import state, { resetState } from './state'; + +const debug = require('debug')('Franz:feature:settingsWS'); + +let store = null; + +export default function initAnnouncements(stores, actions) { + const { features } = stores; + + // Toggle workspace feature + reaction( + () => ( + features.features.isSettingsWSEnabled + ), + (isEnabled) => { + if (isEnabled) { + debug('Initializing `settingsWS` feature'); + store = new SettingsWSStore(stores, null, actions, state); + store.initialize(); + runInAction(() => { state.isFeatureActive = true; }); + } else if (store) { + debug('Disabling `settingsWS` feature'); + runInAction(() => { state.isFeatureActive = false; }); + store.teardown(); + store = null; + resetState(); // Reset state to default + } + }, + { + fireImmediately: true, + }, + ); +} -- cgit v1.2.3-70-g09d2 From 6aea1d4c7d3f7ca44356bc8186fb662483580fa9 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Sat, 13 Apr 2019 13:52:14 +0200 Subject: Remove copy & paste issues --- src/features/settingsWS/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/features/settingsWS/index.js') diff --git a/src/features/settingsWS/index.js b/src/features/settingsWS/index.js index 1e268f184..4049ae814 100755 --- a/src/features/settingsWS/index.js +++ b/src/features/settingsWS/index.js @@ -6,10 +6,10 @@ const debug = require('debug')('Franz:feature:settingsWS'); let store = null; -export default function initAnnouncements(stores, actions) { +export default function initSettingsWebSocket(stores, actions) { const { features } = stores; - // Toggle workspace feature + // Toggle SettingsWebSocket feature reaction( () => ( features.features.isSettingsWSEnabled -- cgit v1.2.3-70-g09d2 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/index.js') 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