@liberfi.io/react-predict 0.3.47 → 0.3.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.
package/dist/index.mjs CHANGED
@@ -548,6 +548,13 @@ var DEFAULT_RECONNECT_BASE = 1e3;
548
548
  var DEFAULT_RECONNECT_MAX = 3e4;
549
549
  var DEFAULT_PING_INTERVAL = 2e4;
550
550
  var DEFAULT_PONG_TIMEOUT = 1e4;
551
+ var MAX_SUBSCRIPTION_FRAME_BYTES = 3500;
552
+ function byteLength(str) {
553
+ if (typeof TextEncoder !== "undefined") {
554
+ return new TextEncoder().encode(str).length;
555
+ }
556
+ return str.length;
557
+ }
551
558
  var PredictWsClient = class {
552
559
  ws = null;
553
560
  wsUrl;
@@ -665,7 +672,7 @@ var PredictWsClient = class {
665
672
  const set = this.subs.channels.get(ch);
666
673
  for (const slug of marketSlugs) set.add(slug);
667
674
  }
668
- this.send({ type: "subscribe", channels, market_slugs: marketSlugs });
675
+ this.sendChunkedSubscription("subscribe", channels, marketSlugs);
669
676
  }
670
677
  /**
671
678
  * Unsubscribe from one or more channels for the given market slugs.
@@ -675,7 +682,7 @@ var PredictWsClient = class {
675
682
  const set = this.subs.channels.get(ch);
676
683
  for (const slug of marketSlugs) set.delete(slug);
677
684
  }
678
- this.send({ type: "unsubscribe", channels, market_slugs: marketSlugs });
685
+ this.sendChunkedSubscription("unsubscribe", channels, marketSlugs);
679
686
  }
680
687
  // -------------------------------------------------------------------------
681
688
  // Subscription — convenience (single channel)
@@ -831,13 +838,39 @@ var PredictWsClient = class {
831
838
  restoreSubscriptions() {
832
839
  for (const [channel, slugs] of this.subs.channels.entries()) {
833
840
  if (slugs.size > 0) {
834
- this.send({
835
- type: "subscribe",
836
- channels: [channel],
837
- market_slugs: Array.from(slugs)
838
- });
841
+ this.sendChunkedSubscription("subscribe", [channel], Array.from(slugs));
842
+ }
843
+ }
844
+ }
845
+ /**
846
+ * Send a subscribe/unsubscribe message, splitting `marketSlugs` across
847
+ * multiple frames so no single frame exceeds the server read limit.
848
+ */
849
+ sendChunkedSubscription(type, channels, marketSlugs) {
850
+ if (marketSlugs.length === 0) {
851
+ this.send({ type, channels, market_slugs: [] });
852
+ return;
853
+ }
854
+ const envelopeBytes = byteLength(
855
+ JSON.stringify({ type, channels, market_slugs: [] })
856
+ );
857
+ const budget = MAX_SUBSCRIPTION_FRAME_BYTES - envelopeBytes;
858
+ let chunk = [];
859
+ let chunkBytes = 0;
860
+ const flush = () => {
861
+ if (chunk.length > 0) {
862
+ this.send({ type, channels, market_slugs: chunk });
863
+ chunk = [];
864
+ chunkBytes = 0;
839
865
  }
866
+ };
867
+ for (const slug of marketSlugs) {
868
+ const slugBytes = byteLength(JSON.stringify(slug)) + 1;
869
+ if (chunk.length > 0 && chunkBytes + slugBytes > budget) flush();
870
+ chunk.push(slug);
871
+ chunkBytes += slugBytes;
840
872
  }
873
+ flush();
841
874
  }
842
875
  startPing() {
843
876
  this.stopPing();