@grvt/client 1.5.4 → 1.5.5

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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/ws/ws.d.ts +5 -1
  3. package/ws/ws.js +53 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grvt/client",
3
- "version": "1.5.4",
3
+ "version": "1.5.5",
4
4
  "description": "Node.js & JavaScript client for GRVT REST APIs & WebSockets",
5
5
  "repository": {
6
6
  "type": "git",
package/ws/ws.d.ts CHANGED
@@ -16,6 +16,7 @@ interface IOptions {
16
16
  }
17
17
  export declare class WS {
18
18
  private _retries;
19
+ private _paused;
19
20
  private _connecting;
20
21
  private readonly _version;
21
22
  private readonly _options;
@@ -55,7 +56,8 @@ export declare class WS {
55
56
  onClose(callback: (e: CloseEvent) => void): () => void;
56
57
  private readonly _onErrors;
57
58
  onError(callback: (e: Event | WSError) => void): () => void;
58
- private _subscribeCurrentPairs;
59
+ private _pause;
60
+ private _resume;
59
61
  private _subscribe;
60
62
  /**
61
63
  * Only supports one feed
@@ -66,5 +68,7 @@ export declare class WS {
66
68
  connect(): this;
67
69
  disconnect(): this;
68
70
  ready(delay?: number): Promise<unknown>;
71
+ pause(): this;
72
+ resume(): this;
69
73
  }
70
74
  export {};
package/ws/ws.js CHANGED
@@ -55,6 +55,7 @@ class WS {
55
55
  }
56
56
  constructor(options) {
57
57
  this._retries = 0;
58
+ this._paused = false;
58
59
  this._connecting = true;
59
60
  this._version = 'v1';
60
61
  /**
@@ -130,7 +131,7 @@ class WS {
130
131
  this._close(currentWs);
131
132
  return;
132
133
  }
133
- this._subscribeCurrentPairs();
134
+ this._resume();
134
135
  });
135
136
  currentWs.addEventListener('close', () => {
136
137
  // only keep the latest ws
@@ -198,6 +199,12 @@ class WS {
198
199
  return;
199
200
  }
200
201
  const consumers = (() => {
202
+ /**
203
+ * If paused, do not send any message to consumers
204
+ */
205
+ if (this._paused) {
206
+ return [];
207
+ }
201
208
  switch (stream) {
202
209
  case interfaces_1.EStream.DEPOSIT:
203
210
  case interfaces_1.EStream.TRANSFER:
@@ -207,11 +214,6 @@ class WS {
207
214
  return this._getSelectorConsumers({ stream, selector });
208
215
  }
209
216
  })();
210
- if (!(consumers === null || consumers === void 0 ? void 0 : consumers.length)) {
211
- // There are delay in sending unsubscribe message so clients might receive messages after unsubscribing
212
- // console.log('TODO: send unsubscribe with by message:', message, result)
213
- return;
214
- }
215
217
  if (result && (consumers === null || consumers === void 0 ? void 0 : consumers.length)) {
216
218
  for (const consumer of consumers) {
217
219
  typeof consumer === 'function' && consumer(result);
@@ -586,10 +588,14 @@ class WS {
586
588
  }
587
589
  const primaryGroup = this._pairs[key];
588
590
  const _a = primaryGroup, _b = consumerKey, _ = _a[_b], keep = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
589
- this._pairs[key] = keep;
590
591
  if (Object.keys(keep).length) {
592
+ this._pairs[key] = keep;
591
593
  needUnsubscribe = false;
592
594
  }
595
+ else {
596
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
597
+ delete this._pairs[key];
598
+ }
593
599
  }
594
600
  if (needUnsubscribe) {
595
601
  if (!toUnsubscribe[stream]) {
@@ -645,7 +651,38 @@ class WS {
645
651
  this._onErrors.splice(this._onErrors.indexOf(callback), 1);
646
652
  };
647
653
  }
648
- _subscribeCurrentPairs() {
654
+ _pause() {
655
+ this._paused = true;
656
+ const pairs = Object.keys(this._pairs);
657
+ const groupStreams = {};
658
+ // keep last primary stream
659
+ for (const pair of pairs.reverse()) {
660
+ const { stream, feed } = this._parsePair(pair);
661
+ if (!groupStreams[stream]) {
662
+ groupStreams[stream] = [];
663
+ }
664
+ const primaryFeed = feed.split('@')[0];
665
+ if (!groupStreams[stream].some((f) => f.startsWith(`${primaryFeed}@`))) {
666
+ groupStreams[stream].push(feed);
667
+ }
668
+ }
669
+ const streams = Object.keys(groupStreams);
670
+ for (const stream of streams) {
671
+ this._sendMessage({
672
+ method: 'unsubscribe',
673
+ stream,
674
+ feed: groupStreams[stream]
675
+ });
676
+ }
677
+ }
678
+ _resume() {
679
+ /**
680
+ * If not paused, do nothing
681
+ */
682
+ if (!this._paused) {
683
+ return;
684
+ }
685
+ this._paused = false;
649
686
  const pairs = Object.keys(this._pairs);
650
687
  const groupStreams = {};
651
688
  // keep last primary stream
@@ -832,5 +869,13 @@ class WS {
832
869
  }, delay);
833
870
  });
834
871
  }
872
+ pause() {
873
+ this._pause();
874
+ return this;
875
+ }
876
+ resume() {
877
+ this._resume();
878
+ return this;
879
+ }
835
880
  }
836
881
  exports.WS = WS;