@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.
@@ -532,6 +532,33 @@ function waitForConnection() {
532
532
  connectionCallbacks.push(callback);
533
533
  });
534
534
  }
535
+ function startPingTimer() {
536
+ stopPingTimer();
537
+ pingTimer = setInterval(() => {
538
+ if (!ws || ws.readyState !== WebSocket.OPEN) return;
539
+ try {
540
+ const id = ++requestId;
541
+ ws.send(JSON.stringify({ jsonrpc: "2.0", id, method: "server.ping", params: [] }));
542
+ pending[id] = {
543
+ resolve: () => {
544
+ },
545
+ reject: () => {
546
+ }
547
+ };
548
+ const timeoutId = setTimeout(() => {
549
+ delete pending[id];
550
+ }, 1e4);
551
+ pending[id].timeoutId = timeoutId;
552
+ } catch {
553
+ }
554
+ }, PING_INTERVAL);
555
+ }
556
+ function stopPingTimer() {
557
+ if (pingTimer) {
558
+ clearInterval(pingTimer);
559
+ pingTimer = null;
560
+ }
561
+ }
535
562
  function connect(endpoint = DEFAULT_ENDPOINT) {
536
563
  if (isConnected) {
537
564
  return Promise.resolve();
@@ -554,6 +581,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
554
581
  isConnected = true;
555
582
  isConnecting = false;
556
583
  reconnectAttempts = 0;
584
+ startPingTimer();
557
585
  hasResolved = true;
558
586
  resolve();
559
587
  connectionCallbacks.forEach((cb) => {
@@ -565,6 +593,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
565
593
  ws.onclose = () => {
566
594
  isConnected = false;
567
595
  isBlockSubscribed = false;
596
+ stopPingTimer();
568
597
  Object.values(pending).forEach((req) => {
569
598
  if (req.timeoutId) clearTimeout(req.timeoutId);
570
599
  req.reject(new Error("WebSocket connection closed"));
@@ -747,6 +776,7 @@ async function getCurrentBlockHeight() {
747
776
  }
748
777
  }
749
778
  function disconnect() {
779
+ stopPingTimer();
750
780
  if (ws) {
751
781
  intentionalClose = true;
752
782
  ws.close();
@@ -767,7 +797,7 @@ function disconnect() {
767
797
  blockSubscribers.length = 0;
768
798
  lastBlockHeader = null;
769
799
  }
770
- 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;
800
+ 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;
771
801
  var init_network = __esm({
772
802
  "l1/network.ts"() {
773
803
  "use strict";
@@ -783,6 +813,7 @@ var init_network = __esm({
783
813
  reconnectAttempts = 0;
784
814
  isBlockSubscribed = false;
785
815
  lastBlockHeader = null;
816
+ pingTimer = null;
786
817
  pending = {};
787
818
  blockSubscribers = [];
788
819
  connectionCallbacks = [];
@@ -791,6 +822,7 @@ var init_network = __esm({
791
822
  MAX_DELAY = 6e4;
792
823
  RPC_TIMEOUT = 3e4;
793
824
  CONNECTION_TIMEOUT = 3e4;
825
+ PING_INTERVAL = 3e4;
794
826
  }
795
827
  });
796
828
 
@@ -12188,18 +12220,15 @@ var GroupChatModule = class {
12188
12220
  }
12189
12221
  }
12190
12222
  }
12191
- async destroy() {
12223
+ destroy() {
12192
12224
  this.destroyConnection();
12193
12225
  if (this.persistTimer) {
12194
12226
  clearTimeout(this.persistTimer);
12195
12227
  this.persistTimer = null;
12196
- }
12197
- if (this.deps) {
12198
- try {
12199
- if (this.persistPromise) await this.persistPromise;
12200
- await this.doPersistAll();
12201
- } catch (err) {
12202
- logger.debug("GroupChat", "Persist on destroy failed", err);
12228
+ if (this.deps) {
12229
+ this.doPersistAll().catch(
12230
+ (err) => logger.debug("GroupChat", "Persist on destroy failed", err)
12231
+ );
12203
12232
  }
12204
12233
  }
12205
12234
  this.groups.clear();
@@ -13101,15 +13130,9 @@ var GroupChatModule = class {
13101
13130
  let latest = 0;
13102
13131
  for (const gid of groupIds) {
13103
13132
  const msgs = this.messages.get(gid);
13104
- if (msgs) {
13105
- for (const m of msgs) {
13106
- const ts = Math.floor(m.timestamp / 1e3);
13107
- if (ts > latest) latest = ts;
13108
- }
13109
- }
13110
- const group = this.groups.get(gid);
13111
- if (group) {
13112
- const ts = Math.floor((group.updatedAt || group.createdAt) / 1e3);
13133
+ if (!msgs) continue;
13134
+ for (const m of msgs) {
13135
+ const ts = Math.floor(m.timestamp / 1e3);
13113
13136
  if (ts > latest) latest = ts;
13114
13137
  }
13115
13138
  }