active-connect-ng2 0.2.46 → 0.2.48

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.
@@ -102,7 +102,8 @@ class WebsocketClient {
102
102
  url = protocol + '//' + document.location.hostname + port + '/wss';
103
103
  }
104
104
  this.url = url;
105
- this.disconnect(false);
105
+ // Initial setup: nothing to tear down yet, just open the first connection.
106
+ this.closed = false;
106
107
  this.connect(this.url);
107
108
  }
108
109
  connect(url) {
@@ -110,10 +111,12 @@ class WebsocketClient {
110
111
  this.target.___connectionId = 0;
111
112
  }
112
113
  this.target.___connectionId++;
113
- if (!this.subject) {
114
- this.create(url);
114
+ if (this.ws &&
115
+ (this.ws.readyState === WebSocket.CONNECTING ||
116
+ this.ws.readyState === WebSocket.OPEN)) {
117
+ return;
115
118
  }
116
- return this.subject;
119
+ this.create(url);
117
120
  }
118
121
  initTabSuspendedListener() {
119
122
  if (document) {
@@ -124,6 +127,7 @@ class WebsocketClient {
124
127
  }
125
128
  else if (document.visibilityState === 'visible') {
126
129
  console.log('ActiveConnect: Reconnecting WebSocket...');
130
+ this.closed = false;
127
131
  this.connect(this.url);
128
132
  }
129
133
  });
@@ -155,7 +159,7 @@ class WebsocketClient {
155
159
  setTimeout(() => {
156
160
  this.resetRequestedState();
157
161
  this.requestStack.forEach((e) => {
158
- this.sendToSocket(e.method, e.data);
162
+ this.sendToSocket(e.method, e.data, false, e.messageId);
159
163
  this.requestStack = this.requestStack.filter((e1) => e1 != e);
160
164
  });
161
165
  }, 2000);
@@ -169,7 +173,7 @@ class WebsocketClient {
169
173
  if (this.connectionEstablishedOnce) {
170
174
  // reset requested state for outbounds
171
175
  WebsocketClient.resetRequestedStateCallbacks.forEach((reset) => {
172
- reset();
176
+ reset(this);
173
177
  });
174
178
  }
175
179
  else {
@@ -180,6 +184,20 @@ class WebsocketClient {
180
184
  WebsocketClient.resetRequestedStateCallbacks.push(callback);
181
185
  }
182
186
  create(url) {
187
+ if (this.ws) {
188
+ // Detach handlers first so the old socket's close event can't trigger
189
+ // another reconnect cycle once it's no longer the "current" socket.
190
+ this.ws.onopen = null;
191
+ this.ws.onmessage = null;
192
+ this.ws.onerror = null;
193
+ this.ws.onclose = null;
194
+ if (this.ws.readyState === WebSocket.CONNECTING ||
195
+ this.ws.readyState === WebSocket.OPEN) {
196
+ this.ws.close();
197
+ }
198
+ }
199
+ this.clearReconnectTimer();
200
+ this.closed = false;
183
201
  // create new connection
184
202
  this.logEntry('TCP/HTTP:CONNECT-SERVER');
185
203
  this.ws = new WebSocket(url + (this.supportsCache ? '?cache=true' : ''));
@@ -191,7 +209,6 @@ class WebsocketClient {
191
209
  this.ws.onopen = () => {
192
210
  this.logEntry('TCP/HTTP:CONNECTED');
193
211
  this.Connected = true;
194
- this.closed = false;
195
212
  };
196
213
  this.ws.onmessage = (e) => {
197
214
  this.messageReceived(JsonParser.parse(e.data.toString()));
@@ -203,14 +220,22 @@ class WebsocketClient {
203
220
  this.pool.WssConnected = false;
204
221
  this.logEntry('TCP/HTTP:RESET-REQUESTED-OUTBOUND-STATE');
205
222
  this.resetRequestedState();
206
- setTimeout(() => {
223
+ this.clearReconnectTimer();
224
+ this.reconnectTimer = setTimeout(() => {
225
+ this.reconnectTimer = undefined;
207
226
  this.connect(url);
208
227
  }, 1000);
209
228
  }
210
229
  };
211
230
  }
212
- sendToSocket(method, data, dontEnsureTransmission) {
213
- const messageId = ++this.messageId;
231
+ clearReconnectTimer() {
232
+ if (this.reconnectTimer !== undefined) {
233
+ clearTimeout(this.reconnectTimer);
234
+ this.reconnectTimer = undefined;
235
+ }
236
+ }
237
+ sendToSocket(method, data, dontEnsureTransmission, forcedMessageId) {
238
+ const messageId = forcedMessageId ?? ++this.messageId;
214
239
  if (this.ws.readyState != this.ws.OPEN) {
215
240
  if (!dontEnsureTransmission)
216
241
  this.requestStack.push({ method, data, messageId });
@@ -254,7 +279,7 @@ class WebsocketClient {
254
279
  }
255
280
  });
256
281
  }
257
- messageReceived({ method, value, messageId, specificHash, inserted, updated, deleted, length, }) {
282
+ messageReceived({ method, value, messageId, specificHash, inserted, updated, deleted, length }) {
258
283
  this.logEntry('RECEIVED:' + method);
259
284
  if (method == '___cache') {
260
285
  this.handleOutboundCacheRequest(value);
@@ -306,13 +331,13 @@ class WebsocketClient {
306
331
  if (item) {
307
332
  this.send('___cache', {
308
333
  method,
309
- specificHash: item.specificHash || null,
334
+ specificHash: item.specificHash || null
310
335
  });
311
336
  }
312
337
  else {
313
338
  this.send('___cache', {
314
339
  method,
315
- specificHash: null,
340
+ specificHash: null
316
341
  });
317
342
  }
318
343
  });
@@ -320,7 +345,7 @@ class WebsocketClient {
320
345
  catch (e) {
321
346
  this.send('___cache', {
322
347
  method,
323
- specificHash: null,
348
+ specificHash: null
324
349
  });
325
350
  console.error('ActiveConnect: ' + e);
326
351
  }
@@ -328,7 +353,7 @@ class WebsocketClient {
328
353
  else {
329
354
  this.send('___cache', {
330
355
  method,
331
- specificHash: null,
356
+ specificHash: null
332
357
  });
333
358
  console.error('Active-Connect: Caching not possible as the indexedDB has not been initialized');
334
359
  }
@@ -349,7 +374,7 @@ class WebsocketClient {
349
374
  const browser = this.getBrowser();
350
375
  const os = this.getOS();
351
376
  this.send('___browser', {
352
- browser: `${browser.name} ${browser.version} ${os}`,
377
+ browser: `${browser.name} ${browser.version} ${os}`
353
378
  });
354
379
  }
355
380
  getBrowser() {
@@ -371,7 +396,7 @@ class WebsocketClient {
371
396
  }
372
397
  return {
373
398
  name: M[0],
374
- version: M[1],
399
+ version: M[1]
375
400
  };
376
401
  }
377
402
  getOS() {
@@ -410,10 +435,16 @@ class WebsocketClient {
410
435
  });
411
436
  }
412
437
  disconnect(restart) {
413
- if (!restart)
414
- this.closed = true;
438
+ this.closed = true;
439
+ this.clearReconnectTimer();
415
440
  if (this.ws)
416
441
  this.ws.close();
442
+ if (!restart) {
443
+ this.ws.onopen = null;
444
+ this.ws.onmessage = null;
445
+ this.ws.onerror = null;
446
+ this.ws.onclose = null;
447
+ }
417
448
  }
418
449
  initCache() { }
419
450
  get target() {
@@ -588,7 +619,7 @@ function Outbound(method, requestingRequired, cached, sortBy) {
588
619
  .update('outbound', {
589
620
  method,
590
621
  data,
591
- specificHash,
622
+ specificHash
592
623
  })
593
624
  .subscribe(() => { });
594
625
  }
@@ -628,7 +659,7 @@ function Outbound(method, requestingRequired, cached, sortBy) {
628
659
  .update('outbound', {
629
660
  method,
630
661
  data,
631
- specificHash,
662
+ specificHash
632
663
  })
633
664
  .subscribe(() => { });
634
665
  }
@@ -656,9 +687,9 @@ function Outbound(method, requestingRequired, cached, sortBy) {
656
687
  }
657
688
  });
658
689
  if (requestingRequired) {
659
- WebsocketClient.addResetRequestingStateCallback(function callback() {
660
- if (target.___requested) {
661
- target.___requested[propertyKey] = false;
690
+ WebsocketClient.addResetRequestingStateCallback(function callback(client) {
691
+ if (target.___requested && target.___requested[propertyKey]) {
692
+ client.send('request.' + method, null).then();
662
693
  }
663
694
  });
664
695
  }
@@ -688,7 +719,7 @@ function Outbound(method, requestingRequired, cached, sortBy) {
688
719
  if (sortBy && val)
689
720
  val = val.sort(sortBy);
690
721
  return (target.___data[propertyKey] = val);
691
- },
722
+ }
692
723
  };
693
724
  target[propertyKey] = obj;
694
725
  return obj;
@@ -924,7 +955,7 @@ class OutboundObject {
924
955
  method,
925
956
  data: _this.data,
926
957
  specificHash,
927
- length,
958
+ length
928
959
  })
929
960
  .subscribe(() => { });
930
961
  }
@@ -963,7 +994,7 @@ class OutboundObject {
963
994
  method,
964
995
  data,
965
996
  specificHash,
966
- length,
997
+ length
967
998
  })
968
999
  .subscribe(() => { });
969
1000
  }
@@ -1101,7 +1132,7 @@ class OutboundObject {
1101
1132
  : undefined
1102
1133
  : undefined
1103
1134
  : this.previouslyCachedCount || this.initialLoadingCount,
1104
- loaded: this.loadedLength,
1135
+ loaded: this.loadedLength
1105
1136
  });
1106
1137
  }
1107
1138
  else {
@@ -1161,7 +1192,7 @@ class OutboundObject {
1161
1192
  afterServerReconnected() {
1162
1193
  if (this.requested && this.lazyLoaded) {
1163
1194
  this.requested = false;
1164
- this.load(1).then();
1195
+ this.load(this.loadedLength).then();
1165
1196
  }
1166
1197
  if (this.loadedId) {
1167
1198
  this.loadedIdData = null;