@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.d.cts CHANGED
@@ -3679,7 +3679,7 @@ declare class GroupChatModule {
3679
3679
  constructor(config?: GroupChatModuleConfig);
3680
3680
  initialize(deps: GroupChatModuleDependencies): void;
3681
3681
  load(): Promise<void>;
3682
- destroy(): Promise<void>;
3682
+ destroy(): void;
3683
3683
  private destroyConnection;
3684
3684
  connect(): Promise<void>;
3685
3685
  getConnectionStatus(): boolean;
package/dist/index.d.ts CHANGED
@@ -3679,7 +3679,7 @@ declare class GroupChatModule {
3679
3679
  constructor(config?: GroupChatModuleConfig);
3680
3680
  initialize(deps: GroupChatModuleDependencies): void;
3681
3681
  load(): Promise<void>;
3682
- destroy(): Promise<void>;
3682
+ destroy(): void;
3683
3683
  private destroyConnection;
3684
3684
  connect(): Promise<void>;
3685
3685
  getConnectionStatus(): boolean;
package/dist/index.js CHANGED
@@ -542,6 +542,33 @@ function waitForConnection() {
542
542
  connectionCallbacks.push(callback);
543
543
  });
544
544
  }
545
+ function startPingTimer() {
546
+ stopPingTimer();
547
+ pingTimer = setInterval(() => {
548
+ if (!ws || ws.readyState !== WebSocket.OPEN) return;
549
+ try {
550
+ const id = ++requestId;
551
+ ws.send(JSON.stringify({ jsonrpc: "2.0", id, method: "server.ping", params: [] }));
552
+ pending[id] = {
553
+ resolve: () => {
554
+ },
555
+ reject: () => {
556
+ }
557
+ };
558
+ const timeoutId = setTimeout(() => {
559
+ delete pending[id];
560
+ }, 1e4);
561
+ pending[id].timeoutId = timeoutId;
562
+ } catch {
563
+ }
564
+ }, PING_INTERVAL);
565
+ }
566
+ function stopPingTimer() {
567
+ if (pingTimer) {
568
+ clearInterval(pingTimer);
569
+ pingTimer = null;
570
+ }
571
+ }
545
572
  function connect(endpoint = DEFAULT_ENDPOINT) {
546
573
  if (isConnected) {
547
574
  return Promise.resolve();
@@ -564,6 +591,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
564
591
  isConnected = true;
565
592
  isConnecting = false;
566
593
  reconnectAttempts = 0;
594
+ startPingTimer();
567
595
  hasResolved = true;
568
596
  resolve();
569
597
  connectionCallbacks.forEach((cb) => {
@@ -575,6 +603,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
575
603
  ws.onclose = () => {
576
604
  isConnected = false;
577
605
  isBlockSubscribed = false;
606
+ stopPingTimer();
578
607
  Object.values(pending).forEach((req) => {
579
608
  if (req.timeoutId) clearTimeout(req.timeoutId);
580
609
  req.reject(new Error("WebSocket connection closed"));
@@ -757,6 +786,7 @@ async function getCurrentBlockHeight() {
757
786
  }
758
787
  }
759
788
  function disconnect() {
789
+ stopPingTimer();
760
790
  if (ws) {
761
791
  intentionalClose = true;
762
792
  ws.close();
@@ -777,7 +807,7 @@ function disconnect() {
777
807
  blockSubscribers.length = 0;
778
808
  lastBlockHeader = null;
779
809
  }
780
- 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;
810
+ 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;
781
811
  var init_network = __esm({
782
812
  "l1/network.ts"() {
783
813
  "use strict";
@@ -793,6 +823,7 @@ var init_network = __esm({
793
823
  reconnectAttempts = 0;
794
824
  isBlockSubscribed = false;
795
825
  lastBlockHeader = null;
826
+ pingTimer = null;
796
827
  pending = {};
797
828
  blockSubscribers = [];
798
829
  connectionCallbacks = [];
@@ -801,6 +832,7 @@ var init_network = __esm({
801
832
  MAX_DELAY = 6e4;
802
833
  RPC_TIMEOUT = 3e4;
803
834
  CONNECTION_TIMEOUT = 3e4;
835
+ PING_INTERVAL = 3e4;
804
836
  }
805
837
  });
806
838
 
@@ -12381,18 +12413,15 @@ var GroupChatModule = class {
12381
12413
  }
12382
12414
  }
12383
12415
  }
12384
- async destroy() {
12416
+ destroy() {
12385
12417
  this.destroyConnection();
12386
12418
  if (this.persistTimer) {
12387
12419
  clearTimeout(this.persistTimer);
12388
12420
  this.persistTimer = null;
12389
- }
12390
- if (this.deps) {
12391
- try {
12392
- if (this.persistPromise) await this.persistPromise;
12393
- await this.doPersistAll();
12394
- } catch (err) {
12395
- logger.debug("GroupChat", "Persist on destroy failed", err);
12421
+ if (this.deps) {
12422
+ this.doPersistAll().catch(
12423
+ (err) => logger.debug("GroupChat", "Persist on destroy failed", err)
12424
+ );
12396
12425
  }
12397
12426
  }
12398
12427
  this.groups.clear();
@@ -13294,15 +13323,9 @@ var GroupChatModule = class {
13294
13323
  let latest = 0;
13295
13324
  for (const gid of groupIds) {
13296
13325
  const msgs = this.messages.get(gid);
13297
- if (msgs) {
13298
- for (const m of msgs) {
13299
- const ts = Math.floor(m.timestamp / 1e3);
13300
- if (ts > latest) latest = ts;
13301
- }
13302
- }
13303
- const group = this.groups.get(gid);
13304
- if (group) {
13305
- const ts = Math.floor((group.updatedAt || group.createdAt) / 1e3);
13326
+ if (!msgs) continue;
13327
+ for (const m of msgs) {
13328
+ const ts = Math.floor(m.timestamp / 1e3);
13306
13329
  if (ts > latest) latest = ts;
13307
13330
  }
13308
13331
  }