exaroton 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exaroton",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "exaroton API client",
5
5
  "homepage": "https://exaroton.com",
6
6
  "main": "index.js",
@@ -51,7 +51,11 @@ export default class WebsocketClient extends EventEmitter {
51
51
  */
52
52
  reconnectTimeout = 3000;
53
53
 
54
- #reconnectInterval;
54
+ /**
55
+ * Timeout for reconnecting to the websocket
56
+ * @type {number|null}
57
+ */
58
+ #reconnectTimeout = null;
55
59
 
56
60
  /**
57
61
  * @type {boolean}
@@ -98,9 +102,15 @@ export default class WebsocketClient extends EventEmitter {
98
102
  }
99
103
 
100
104
  /**
101
- * Connect to websocket
105
+ * Connect to websocket if not already connected
102
106
  */
103
107
  connect() {
108
+ if (!this.#shouldConnect) {
109
+ this.#connect();
110
+ }
111
+ }
112
+
113
+ #connect() {
104
114
  this.#shouldConnect = true;
105
115
  this.#websocket = new WebSocket(this.url, {headers: {authorization: "Bearer " + this.#client.getAPIToken()}});
106
116
  this.#websocket.on('open', this.onOpen.bind(this));
@@ -110,6 +120,7 @@ export default class WebsocketClient extends EventEmitter {
110
120
  if (!this.streamRetryInterval) {
111
121
  this.streamRetryInterval = setInterval(this.tryToStartStreams.bind(this), 15000);
112
122
  }
123
+ this.#connected = true;
113
124
  }
114
125
 
115
126
  /**
@@ -119,14 +130,15 @@ export default class WebsocketClient extends EventEmitter {
119
130
  this.#shouldConnect = false;
120
131
  this.#websocket.close();
121
132
  this.#streams = {};
122
- clearInterval(this.#reconnectInterval);
133
+
134
+ clearTimeout(this.#reconnectTimeout);
135
+ this.#reconnectTimeout = null;
136
+
123
137
  clearInterval(this.streamRetryInterval);
124
138
  this.streamRetryInterval = null;
125
139
  }
126
140
 
127
141
  onOpen() {
128
- this.#connected = true;
129
- clearInterval(this.#reconnectInterval);
130
142
  this.emit('open');
131
143
  }
132
144
 
@@ -134,7 +146,7 @@ export default class WebsocketClient extends EventEmitter {
134
146
  this.emit('close');
135
147
  this.#ready = false;
136
148
  if (this.autoReconnect && this.#shouldConnect) {
137
- this.#reconnectInterval = setInterval(this.connect.bind(this), this.reconnectTimeout);
149
+ this.#reconnectTimeout = setTimeout(this.#connect.bind(this), this.reconnectTimeout);
138
150
  } else {
139
151
  this.#connected = false;
140
152
  }
@@ -181,6 +193,7 @@ export default class WebsocketClient extends EventEmitter {
181
193
  }
182
194
 
183
195
  /**
196
+ * This method returns true if the websocket is connected (or trying to connect).
184
197
  * @return {boolean}
185
198
  */
186
199
  isConnected() {