@unicitylabs/sphere-sdk 0.6.10-dev.4 → 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/l1/index.js CHANGED
@@ -542,6 +542,7 @@ var intentionalClose = false;
542
542
  var reconnectAttempts = 0;
543
543
  var isBlockSubscribed = false;
544
544
  var lastBlockHeader = null;
545
+ var pingTimer = null;
545
546
  var pending = {};
546
547
  var blockSubscribers = [];
547
548
  var connectionCallbacks = [];
@@ -550,6 +551,7 @@ var BASE_DELAY = 2e3;
550
551
  var MAX_DELAY = 6e4;
551
552
  var RPC_TIMEOUT = 3e4;
552
553
  var CONNECTION_TIMEOUT = 3e4;
554
+ var PING_INTERVAL = 3e4;
553
555
  function isWebSocketConnected() {
554
556
  return isConnected && ws !== null && ws.readyState === WebSocket.OPEN;
555
557
  }
@@ -576,6 +578,33 @@ function waitForConnection() {
576
578
  connectionCallbacks.push(callback);
577
579
  });
578
580
  }
581
+ function startPingTimer() {
582
+ stopPingTimer();
583
+ pingTimer = setInterval(() => {
584
+ if (!ws || ws.readyState !== WebSocket.OPEN) return;
585
+ try {
586
+ const id = ++requestId;
587
+ ws.send(JSON.stringify({ jsonrpc: "2.0", id, method: "server.ping", params: [] }));
588
+ pending[id] = {
589
+ resolve: () => {
590
+ },
591
+ reject: () => {
592
+ }
593
+ };
594
+ const timeoutId = setTimeout(() => {
595
+ delete pending[id];
596
+ }, 1e4);
597
+ pending[id].timeoutId = timeoutId;
598
+ } catch {
599
+ }
600
+ }, PING_INTERVAL);
601
+ }
602
+ function stopPingTimer() {
603
+ if (pingTimer) {
604
+ clearInterval(pingTimer);
605
+ pingTimer = null;
606
+ }
607
+ }
579
608
  function connect(endpoint = DEFAULT_ENDPOINT) {
580
609
  if (isConnected) {
581
610
  return Promise.resolve();
@@ -598,6 +627,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
598
627
  isConnected = true;
599
628
  isConnecting = false;
600
629
  reconnectAttempts = 0;
630
+ startPingTimer();
601
631
  hasResolved = true;
602
632
  resolve();
603
633
  connectionCallbacks.forEach((cb) => {
@@ -609,6 +639,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
609
639
  ws.onclose = () => {
610
640
  isConnected = false;
611
641
  isBlockSubscribed = false;
642
+ stopPingTimer();
612
643
  Object.values(pending).forEach((req) => {
613
644
  if (req.timeoutId) clearTimeout(req.timeoutId);
614
645
  req.reject(new Error("WebSocket connection closed"));
@@ -791,6 +822,7 @@ async function getCurrentBlockHeight() {
791
822
  }
792
823
  }
793
824
  function disconnect() {
825
+ stopPingTimer();
794
826
  if (ws) {
795
827
  intentionalClose = true;
796
828
  ws.close();