@unicitylabs/sphere-sdk 0.5.6 → 0.5.7

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.
@@ -8983,13 +8983,16 @@ var GroupChatModule = class {
8983
8983
  logger.error("GroupChat", "Max reconnection attempts reached");
8984
8984
  return;
8985
8985
  }
8986
+ const maxDelay = this.config.reconnectDelayMs * 16;
8987
+ const delay = Math.min(this.config.reconnectDelayMs * Math.pow(2, this.reconnectAttempts), maxDelay);
8986
8988
  this.reconnectAttempts++;
8989
+ logger.debug("GroupChat", `Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.config.maxReconnectAttempts})`);
8987
8990
  this.reconnectTimer = setTimeout(() => {
8988
8991
  this.reconnectTimer = null;
8989
8992
  if (this.deps) {
8990
8993
  this.connect().catch((err) => logger.error("GroupChat", "Reconnect failed:", err));
8991
8994
  }
8992
- }, this.config.reconnectDelayMs);
8995
+ }, delay);
8993
8996
  }
8994
8997
  // ===========================================================================
8995
8998
  // Subscription Management
@@ -12326,6 +12329,7 @@ var secp256k1 = /* @__PURE__ */ ecdsa(Pointk1, sha2564);
12326
12329
 
12327
12330
  // modules/market/MarketModule.ts
12328
12331
  init_errors();
12332
+ init_logger();
12329
12333
  var DEFAULT_MARKET_API_URL = "https://market-api.unicity.network";
12330
12334
  function hexToBytes3(hex) {
12331
12335
  const len = hex.length >> 1;
@@ -12498,16 +12502,54 @@ var MarketModule = class {
12498
12502
  */
12499
12503
  subscribeFeed(listener) {
12500
12504
  const wsUrl = this.apiUrl.replace(/^http/, "ws") + "/ws/feed";
12501
- const ws2 = new WebSocket(wsUrl);
12502
- ws2.onmessage = (event) => {
12503
- try {
12504
- const raw = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
12505
- listener(mapFeedMessage(raw));
12506
- } catch {
12505
+ const BASE_DELAY2 = 2e3;
12506
+ const MAX_DELAY2 = 3e4;
12507
+ const MAX_ATTEMPTS = 10;
12508
+ let ws2 = null;
12509
+ let reconnectAttempts2 = 0;
12510
+ let reconnectTimer = null;
12511
+ let destroyed = false;
12512
+ function connect2() {
12513
+ if (destroyed) return;
12514
+ ws2 = new WebSocket(wsUrl);
12515
+ ws2.onopen = () => {
12516
+ reconnectAttempts2 = 0;
12517
+ logger.debug("Market", "Feed WebSocket connected");
12518
+ };
12519
+ ws2.onmessage = (event) => {
12520
+ try {
12521
+ const raw = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
12522
+ listener(mapFeedMessage(raw));
12523
+ } catch {
12524
+ }
12525
+ };
12526
+ ws2.onclose = () => {
12527
+ if (destroyed) return;
12528
+ scheduleReconnect();
12529
+ };
12530
+ ws2.onerror = () => {
12531
+ };
12532
+ }
12533
+ function scheduleReconnect() {
12534
+ if (destroyed) return;
12535
+ if (reconnectAttempts2 >= MAX_ATTEMPTS) {
12536
+ logger.warn("Market", `Feed WebSocket: gave up after ${MAX_ATTEMPTS} reconnect attempts`);
12537
+ return;
12507
12538
  }
12508
- };
12539
+ const delay = Math.min(BASE_DELAY2 * Math.pow(2, reconnectAttempts2), MAX_DELAY2);
12540
+ reconnectAttempts2++;
12541
+ logger.debug("Market", `Feed WebSocket reconnecting in ${delay}ms (attempt ${reconnectAttempts2}/${MAX_ATTEMPTS})`);
12542
+ reconnectTimer = setTimeout(connect2, delay);
12543
+ }
12544
+ connect2();
12509
12545
  return () => {
12510
- ws2.close();
12546
+ destroyed = true;
12547
+ if (reconnectTimer) {
12548
+ clearTimeout(reconnectTimer);
12549
+ reconnectTimer = null;
12550
+ }
12551
+ ws2?.close();
12552
+ ws2 = null;
12511
12553
  };
12512
12554
  }
12513
12555
  // ---------------------------------------------------------------------------