centrifuge 5.2.0 → 5.2.2

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/README.md CHANGED
@@ -522,7 +522,7 @@ You can call `unsubscribe` method to unsubscribe from a channel:
522
522
  sub.unsubscribe();
523
523
  ```
524
524
 
525
- **Important thing to know** is that unsubscribing from subscription does not remove event handlers you already set to that Subscription object. This allows to simply subscribe to channel again later calling `.subscribe()` method of subscription (see below). But there are cases when your code structured in a way that you need to remove event handlers after unsubscribe **to prevent them be executed twice** in the future. To do this remove event listeners explicitly after calling `unsubscribe()`:
525
+ **Important thing to mention** is that unsubscribing from subscription does not remove event handlers you already set to that Subscription object. This allows to simply subscribe to channel again later calling `.subscribe()` method of subscription (see below). But there are cases when your code structured in a way that you need to remove event handlers after unsubscribe **to prevent them be executed twice** in the future. To do this remove event listeners explicitly after calling `unsubscribe()`:
526
526
 
527
527
  ```javascript
528
528
  sub.unsubscribe();
@@ -127,7 +127,7 @@ export declare class Centrifuge extends Centrifuge_base {
127
127
  private _getRefreshRetryDelay;
128
128
  private _refreshResponse;
129
129
  private _removeSubscription;
130
- protected _unsubscribe(sub: Subscription): void;
130
+ protected _unsubscribe(sub: Subscription): Promise<void>;
131
131
  private _getSub;
132
132
  private _isServerSub;
133
133
  private _sendSubscribeCommands;
package/build/index.js CHANGED
@@ -1,5 +1,37 @@
1
1
  'use strict';
2
2
 
3
+ /******************************************************************************
4
+ Copyright (c) Microsoft Corporation.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
+ ***************************************************************************** */
17
+ /* global Reflect, Promise, SuppressedError, Symbol */
18
+
19
+
20
+ function __awaiter(thisArg, _arguments, P, generator) {
21
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
+ return new (P || (P = Promise))(function (resolve, reject) {
23
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ }
29
+
30
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
31
+ var e = new Error(message);
32
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
33
+ };
34
+
3
35
  function getDefaultExportFromCjs (x) {
4
36
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
5
37
  }
@@ -614,6 +646,7 @@ class Subscription extends EventEmitter$1 {
614
646
  this._delta = '';
615
647
  this._delta_negotiated = false;
616
648
  this._prevValue = null;
649
+ this._unsubPromise = Promise.resolve();
617
650
  this._setOptions(options);
618
651
  // @ts-ignore – we are hiding some symbols from public API autocompletion.
619
652
  if (this._centrifuge._debugEnabled) {
@@ -664,7 +697,7 @@ class Subscription extends EventEmitter$1 {
664
697
  }
665
698
  /** unsubscribe from a channel, keeping position state.*/
666
699
  unsubscribe() {
667
- this._setUnsubscribed(exports.unsubscribedCodes.unsubscribeCalled, 'unsubscribe called', true);
700
+ this._unsubPromise = this._setUnsubscribed(exports.unsubscribedCodes.unsubscribeCalled, 'unsubscribe called', true);
668
701
  }
669
702
  /** publish data to a channel.*/
670
703
  publish(data) {
@@ -783,16 +816,25 @@ class Subscription extends EventEmitter$1 {
783
816
  }
784
817
  }
785
818
  _setSubscribing(code, reason) {
786
- if (this._isSubscribing()) {
787
- return;
788
- }
789
- if (this._isSubscribed()) {
790
- this._clearSubscribedState();
791
- }
792
- if (this._setState(exports.SubscriptionState.Subscribing)) {
793
- this.emit('subscribing', { channel: this.channel, code: code, reason: reason });
794
- }
795
- this._subscribe();
819
+ return __awaiter(this, void 0, void 0, function* () {
820
+ if (this._isSubscribing()) {
821
+ return;
822
+ }
823
+ if (this._isSubscribed()) {
824
+ this._clearSubscribedState();
825
+ }
826
+ if (this._setState(exports.SubscriptionState.Subscribing)) {
827
+ this.emit('subscribing', { channel: this.channel, code: code, reason: reason });
828
+ }
829
+ // @ts-ignore – for performance reasons only await _unsubPromise for emulution case where it's required.
830
+ if (this._centrifuge._transport && this._centrifuge._transport.emulation()) {
831
+ yield this._unsubPromise;
832
+ }
833
+ if (!this._isSubscribing()) {
834
+ return;
835
+ }
836
+ this._subscribe();
837
+ });
796
838
  }
797
839
  _subscribe() {
798
840
  // @ts-ignore – we are hiding some symbols from public API autocompletion.
@@ -947,19 +989,20 @@ class Subscription extends EventEmitter$1 {
947
989
  }
948
990
  _setUnsubscribed(code, reason, sendUnsubscribe) {
949
991
  if (this._isUnsubscribed()) {
950
- return;
992
+ return Promise.resolve();
951
993
  }
994
+ let promise = Promise.resolve();
952
995
  if (this._isSubscribed()) {
953
996
  if (sendUnsubscribe) {
954
997
  // @ts-ignore – we are hiding some methods from public API autocompletion.
955
- this._centrifuge._unsubscribe(this);
998
+ promise = this._centrifuge._unsubscribe(this);
956
999
  }
957
1000
  this._clearSubscribedState();
958
1001
  }
959
- if (this._isSubscribing()) {
1002
+ else if (this._isSubscribing()) {
960
1003
  if (this._inflight && sendUnsubscribe) {
961
1004
  // @ts-ignore – we are hiding some methods from public API autocompletion.
962
- this._centrifuge._unsubscribe(this);
1005
+ promise = this._centrifuge._unsubscribe(this);
963
1006
  }
964
1007
  this._clearSubscribingState();
965
1008
  }
@@ -967,6 +1010,7 @@ class Subscription extends EventEmitter$1 {
967
1010
  this.emit('unsubscribed', { channel: this.channel, code: code, reason: reason });
968
1011
  }
969
1012
  this._rejectPromises({ code: exports.errorCodes.subscriptionUnsubscribed, message: this.state });
1013
+ return promise;
970
1014
  }
971
1015
  _handlePublication(pub) {
972
1016
  if (this._delta && this._delta_negotiated) {
@@ -1605,38 +1649,6 @@ class SseTransport {
1605
1649
  }
1606
1650
  }
1607
1651
 
1608
- /******************************************************************************
1609
- Copyright (c) Microsoft Corporation.
1610
-
1611
- Permission to use, copy, modify, and/or distribute this software for any
1612
- purpose with or without fee is hereby granted.
1613
-
1614
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
1615
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1616
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
1617
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1618
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1619
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1620
- PERFORMANCE OF THIS SOFTWARE.
1621
- ***************************************************************************** */
1622
- /* global Reflect, Promise, SuppressedError, Symbol */
1623
-
1624
-
1625
- function __awaiter(thisArg, _arguments, P, generator) {
1626
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1627
- return new (P || (P = Promise))(function (resolve, reject) {
1628
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1629
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1630
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1631
- step((generator = generator.apply(thisArg, _arguments || [])).next());
1632
- });
1633
- }
1634
-
1635
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
1636
- var e = new Error(message);
1637
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1638
- };
1639
-
1640
1652
  /** @internal */
1641
1653
  class WebtransportTransport {
1642
1654
  constructor(endpoint, options) {
@@ -1862,7 +1874,9 @@ class Writer {
1862
1874
  }
1863
1875
  // Copy from array at start to end.
1864
1876
  putArray(a, start, end) {
1865
- this.a.push(...a.slice(start, end));
1877
+ // TODO: optimize.
1878
+ for (let i = start; i < end; i++)
1879
+ this.a.push(a[i]);
1866
1880
  }
1867
1881
  }
1868
1882
  // Return a 32-bit checksum of the array.
@@ -3202,25 +3216,30 @@ class Centrifuge extends EventEmitter$1 {
3202
3216
  }
3203
3217
  _unsubscribe(sub) {
3204
3218
  if (!this._transportIsOpen) {
3205
- return;
3219
+ return Promise.resolve();
3206
3220
  }
3207
3221
  const req = {
3208
3222
  channel: sub.channel
3209
3223
  };
3210
3224
  const cmd = { unsubscribe: req };
3211
3225
  const self = this;
3212
- this._call(cmd, false).then(resolveCtx => {
3213
- // @ts-ignore - improve later.
3214
- if (resolveCtx.next) {
3226
+ const unsubscribePromise = new Promise((resolve, _) => {
3227
+ this._call(cmd, false).then(resolveCtx => {
3228
+ resolve();
3215
3229
  // @ts-ignore - improve later.
3216
- resolveCtx.next();
3217
- }
3218
- }, rejectCtx => {
3219
- if (rejectCtx.next) {
3220
- rejectCtx.next();
3221
- }
3222
- self._disconnect(exports.connectingCodes.unsubscribeError, 'unsubscribe error', true);
3230
+ if (resolveCtx.next) {
3231
+ // @ts-ignore - improve later.
3232
+ resolveCtx.next();
3233
+ }
3234
+ }, rejectCtx => {
3235
+ resolve();
3236
+ if (rejectCtx.next) {
3237
+ rejectCtx.next();
3238
+ }
3239
+ self._disconnect(exports.connectingCodes.unsubscribeError, 'unsubscribe error', true);
3240
+ });
3223
3241
  });
3242
+ return unsubscribePromise;
3224
3243
  }
3225
3244
  _getSub(channel) {
3226
3245
  const sub = this._subs[channel];
package/build/index.mjs CHANGED
@@ -1,3 +1,35 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol */
16
+
17
+
18
+ function __awaiter(thisArg, _arguments, P, generator) {
19
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
20
+ return new (P || (P = Promise))(function (resolve, reject) {
21
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
22
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
23
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
24
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
25
+ });
26
+ }
27
+
28
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
29
+ var e = new Error(message);
30
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
31
+ };
32
+
1
33
  function getDefaultExportFromCjs (x) {
2
34
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
3
35
  }
@@ -612,6 +644,7 @@ class Subscription extends EventEmitter$1 {
612
644
  this._delta = '';
613
645
  this._delta_negotiated = false;
614
646
  this._prevValue = null;
647
+ this._unsubPromise = Promise.resolve();
615
648
  this._setOptions(options);
616
649
  // @ts-ignore – we are hiding some symbols from public API autocompletion.
617
650
  if (this._centrifuge._debugEnabled) {
@@ -662,7 +695,7 @@ class Subscription extends EventEmitter$1 {
662
695
  }
663
696
  /** unsubscribe from a channel, keeping position state.*/
664
697
  unsubscribe() {
665
- this._setUnsubscribed(unsubscribedCodes.unsubscribeCalled, 'unsubscribe called', true);
698
+ this._unsubPromise = this._setUnsubscribed(unsubscribedCodes.unsubscribeCalled, 'unsubscribe called', true);
666
699
  }
667
700
  /** publish data to a channel.*/
668
701
  publish(data) {
@@ -781,16 +814,25 @@ class Subscription extends EventEmitter$1 {
781
814
  }
782
815
  }
783
816
  _setSubscribing(code, reason) {
784
- if (this._isSubscribing()) {
785
- return;
786
- }
787
- if (this._isSubscribed()) {
788
- this._clearSubscribedState();
789
- }
790
- if (this._setState(SubscriptionState.Subscribing)) {
791
- this.emit('subscribing', { channel: this.channel, code: code, reason: reason });
792
- }
793
- this._subscribe();
817
+ return __awaiter(this, void 0, void 0, function* () {
818
+ if (this._isSubscribing()) {
819
+ return;
820
+ }
821
+ if (this._isSubscribed()) {
822
+ this._clearSubscribedState();
823
+ }
824
+ if (this._setState(SubscriptionState.Subscribing)) {
825
+ this.emit('subscribing', { channel: this.channel, code: code, reason: reason });
826
+ }
827
+ // @ts-ignore – for performance reasons only await _unsubPromise for emulution case where it's required.
828
+ if (this._centrifuge._transport && this._centrifuge._transport.emulation()) {
829
+ yield this._unsubPromise;
830
+ }
831
+ if (!this._isSubscribing()) {
832
+ return;
833
+ }
834
+ this._subscribe();
835
+ });
794
836
  }
795
837
  _subscribe() {
796
838
  // @ts-ignore – we are hiding some symbols from public API autocompletion.
@@ -945,19 +987,20 @@ class Subscription extends EventEmitter$1 {
945
987
  }
946
988
  _setUnsubscribed(code, reason, sendUnsubscribe) {
947
989
  if (this._isUnsubscribed()) {
948
- return;
990
+ return Promise.resolve();
949
991
  }
992
+ let promise = Promise.resolve();
950
993
  if (this._isSubscribed()) {
951
994
  if (sendUnsubscribe) {
952
995
  // @ts-ignore – we are hiding some methods from public API autocompletion.
953
- this._centrifuge._unsubscribe(this);
996
+ promise = this._centrifuge._unsubscribe(this);
954
997
  }
955
998
  this._clearSubscribedState();
956
999
  }
957
- if (this._isSubscribing()) {
1000
+ else if (this._isSubscribing()) {
958
1001
  if (this._inflight && sendUnsubscribe) {
959
1002
  // @ts-ignore – we are hiding some methods from public API autocompletion.
960
- this._centrifuge._unsubscribe(this);
1003
+ promise = this._centrifuge._unsubscribe(this);
961
1004
  }
962
1005
  this._clearSubscribingState();
963
1006
  }
@@ -965,6 +1008,7 @@ class Subscription extends EventEmitter$1 {
965
1008
  this.emit('unsubscribed', { channel: this.channel, code: code, reason: reason });
966
1009
  }
967
1010
  this._rejectPromises({ code: errorCodes.subscriptionUnsubscribed, message: this.state });
1011
+ return promise;
968
1012
  }
969
1013
  _handlePublication(pub) {
970
1014
  if (this._delta && this._delta_negotiated) {
@@ -1603,38 +1647,6 @@ class SseTransport {
1603
1647
  }
1604
1648
  }
1605
1649
 
1606
- /******************************************************************************
1607
- Copyright (c) Microsoft Corporation.
1608
-
1609
- Permission to use, copy, modify, and/or distribute this software for any
1610
- purpose with or without fee is hereby granted.
1611
-
1612
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
1613
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1614
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
1615
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1616
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1617
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1618
- PERFORMANCE OF THIS SOFTWARE.
1619
- ***************************************************************************** */
1620
- /* global Reflect, Promise, SuppressedError, Symbol */
1621
-
1622
-
1623
- function __awaiter(thisArg, _arguments, P, generator) {
1624
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1625
- return new (P || (P = Promise))(function (resolve, reject) {
1626
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1627
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1628
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1629
- step((generator = generator.apply(thisArg, _arguments || [])).next());
1630
- });
1631
- }
1632
-
1633
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
1634
- var e = new Error(message);
1635
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1636
- };
1637
-
1638
1650
  /** @internal */
1639
1651
  class WebtransportTransport {
1640
1652
  constructor(endpoint, options) {
@@ -1860,7 +1872,9 @@ class Writer {
1860
1872
  }
1861
1873
  // Copy from array at start to end.
1862
1874
  putArray(a, start, end) {
1863
- this.a.push(...a.slice(start, end));
1875
+ // TODO: optimize.
1876
+ for (let i = start; i < end; i++)
1877
+ this.a.push(a[i]);
1864
1878
  }
1865
1879
  }
1866
1880
  // Return a 32-bit checksum of the array.
@@ -3200,25 +3214,30 @@ class Centrifuge extends EventEmitter$1 {
3200
3214
  }
3201
3215
  _unsubscribe(sub) {
3202
3216
  if (!this._transportIsOpen) {
3203
- return;
3217
+ return Promise.resolve();
3204
3218
  }
3205
3219
  const req = {
3206
3220
  channel: sub.channel
3207
3221
  };
3208
3222
  const cmd = { unsubscribe: req };
3209
3223
  const self = this;
3210
- this._call(cmd, false).then(resolveCtx => {
3211
- // @ts-ignore - improve later.
3212
- if (resolveCtx.next) {
3224
+ const unsubscribePromise = new Promise((resolve, _) => {
3225
+ this._call(cmd, false).then(resolveCtx => {
3226
+ resolve();
3213
3227
  // @ts-ignore - improve later.
3214
- resolveCtx.next();
3215
- }
3216
- }, rejectCtx => {
3217
- if (rejectCtx.next) {
3218
- rejectCtx.next();
3219
- }
3220
- self._disconnect(connectingCodes.unsubscribeError, 'unsubscribe error', true);
3228
+ if (resolveCtx.next) {
3229
+ // @ts-ignore - improve later.
3230
+ resolveCtx.next();
3231
+ }
3232
+ }, rejectCtx => {
3233
+ resolve();
3234
+ if (rejectCtx.next) {
3235
+ rejectCtx.next();
3236
+ }
3237
+ self._disconnect(connectingCodes.unsubscribeError, 'unsubscribe error', true);
3238
+ });
3221
3239
  });
3240
+ return unsubscribePromise;
3222
3241
  }
3223
3242
  _getSub(channel) {
3224
3243
  const sub = this._subs[channel];
@@ -127,7 +127,7 @@ export declare class Centrifuge extends Centrifuge_base {
127
127
  private _getRefreshRetryDelay;
128
128
  private _refreshResponse;
129
129
  private _removeSubscription;
130
- protected _unsubscribe(sub: Subscription): void;
130
+ protected _unsubscribe(sub: Subscription): Promise<void>;
131
131
  private _getSub;
132
132
  private _isServerSub;
133
133
  private _sendSubscribeCommands;