aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/settingsWS/store.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/settingsWS/store.js')
-rwxr-xr-xsrc/features/settingsWS/store.js40
1 files changed, 25 insertions, 15 deletions
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}