@unicitylabs/sphere-sdk 0.6.10-dev.3 → 0.6.10-dev.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.
package/dist/index.cjs CHANGED
@@ -559,6 +559,33 @@ function waitForConnection() {
559
559
  connectionCallbacks.push(callback);
560
560
  });
561
561
  }
562
+ function startPingTimer() {
563
+ stopPingTimer();
564
+ pingTimer = setInterval(() => {
565
+ if (!ws || ws.readyState !== WebSocket.OPEN) return;
566
+ try {
567
+ const id = ++requestId;
568
+ ws.send(JSON.stringify({ jsonrpc: "2.0", id, method: "server.ping", params: [] }));
569
+ pending[id] = {
570
+ resolve: () => {
571
+ },
572
+ reject: () => {
573
+ }
574
+ };
575
+ const timeoutId = setTimeout(() => {
576
+ delete pending[id];
577
+ }, 1e4);
578
+ pending[id].timeoutId = timeoutId;
579
+ } catch {
580
+ }
581
+ }, PING_INTERVAL);
582
+ }
583
+ function stopPingTimer() {
584
+ if (pingTimer) {
585
+ clearInterval(pingTimer);
586
+ pingTimer = null;
587
+ }
588
+ }
562
589
  function connect(endpoint = DEFAULT_ENDPOINT) {
563
590
  if (isConnected) {
564
591
  return Promise.resolve();
@@ -581,6 +608,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
581
608
  isConnected = true;
582
609
  isConnecting = false;
583
610
  reconnectAttempts = 0;
611
+ startPingTimer();
584
612
  hasResolved = true;
585
613
  resolve();
586
614
  connectionCallbacks.forEach((cb) => {
@@ -592,6 +620,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
592
620
  ws.onclose = () => {
593
621
  isConnected = false;
594
622
  isBlockSubscribed = false;
623
+ stopPingTimer();
595
624
  Object.values(pending).forEach((req) => {
596
625
  if (req.timeoutId) clearTimeout(req.timeoutId);
597
626
  req.reject(new Error("WebSocket connection closed"));
@@ -774,6 +803,7 @@ async function getCurrentBlockHeight() {
774
803
  }
775
804
  }
776
805
  function disconnect() {
806
+ stopPingTimer();
777
807
  if (ws) {
778
808
  intentionalClose = true;
779
809
  ws.close();
@@ -794,7 +824,7 @@ function disconnect() {
794
824
  blockSubscribers.length = 0;
795
825
  lastBlockHeader = null;
796
826
  }
797
- var DEFAULT_ENDPOINT, ws, isConnected, isConnecting, requestId, intentionalClose, reconnectAttempts, isBlockSubscribed, lastBlockHeader, pending, blockSubscribers, connectionCallbacks, MAX_RECONNECT_ATTEMPTS, BASE_DELAY, MAX_DELAY, RPC_TIMEOUT, CONNECTION_TIMEOUT;
827
+ var DEFAULT_ENDPOINT, ws, isConnected, isConnecting, requestId, intentionalClose, reconnectAttempts, isBlockSubscribed, lastBlockHeader, pingTimer, pending, blockSubscribers, connectionCallbacks, MAX_RECONNECT_ATTEMPTS, BASE_DELAY, MAX_DELAY, RPC_TIMEOUT, CONNECTION_TIMEOUT, PING_INTERVAL;
798
828
  var init_network = __esm({
799
829
  "l1/network.ts"() {
800
830
  "use strict";
@@ -810,6 +840,7 @@ var init_network = __esm({
810
840
  reconnectAttempts = 0;
811
841
  isBlockSubscribed = false;
812
842
  lastBlockHeader = null;
843
+ pingTimer = null;
813
844
  pending = {};
814
845
  blockSubscribers = [];
815
846
  connectionCallbacks = [];
@@ -818,6 +849,7 @@ var init_network = __esm({
818
849
  MAX_DELAY = 6e4;
819
850
  RPC_TIMEOUT = 3e4;
820
851
  CONNECTION_TIMEOUT = 3e4;
852
+ PING_INTERVAL = 3e4;
821
853
  }
822
854
  });
823
855
 
@@ -12528,18 +12560,15 @@ var GroupChatModule = class {
12528
12560
  }
12529
12561
  }
12530
12562
  }
12531
- async destroy() {
12563
+ destroy() {
12532
12564
  this.destroyConnection();
12533
12565
  if (this.persistTimer) {
12534
12566
  clearTimeout(this.persistTimer);
12535
12567
  this.persistTimer = null;
12536
- }
12537
- if (this.deps) {
12538
- try {
12539
- if (this.persistPromise) await this.persistPromise;
12540
- await this.doPersistAll();
12541
- } catch (err) {
12542
- logger.debug("GroupChat", "Persist on destroy failed", err);
12568
+ if (this.deps) {
12569
+ this.doPersistAll().catch(
12570
+ (err) => logger.debug("GroupChat", "Persist on destroy failed", err)
12571
+ );
12543
12572
  }
12544
12573
  }
12545
12574
  this.groups.clear();
@@ -13441,15 +13470,9 @@ var GroupChatModule = class {
13441
13470
  let latest = 0;
13442
13471
  for (const gid of groupIds) {
13443
13472
  const msgs = this.messages.get(gid);
13444
- if (msgs) {
13445
- for (const m of msgs) {
13446
- const ts = Math.floor(m.timestamp / 1e3);
13447
- if (ts > latest) latest = ts;
13448
- }
13449
- }
13450
- const group = this.groups.get(gid);
13451
- if (group) {
13452
- const ts = Math.floor((group.updatedAt || group.createdAt) / 1e3);
13473
+ if (!msgs) continue;
13474
+ for (const m of msgs) {
13475
+ const ts = Math.floor(m.timestamp / 1e3);
13453
13476
  if (ts > latest) latest = ts;
13454
13477
  }
13455
13478
  }