@unicitylabs/sphere-sdk 0.6.10-dev.4 → 0.6.10-dev.6

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.cjs CHANGED
@@ -631,6 +631,7 @@ var intentionalClose = false;
631
631
  var reconnectAttempts = 0;
632
632
  var isBlockSubscribed = false;
633
633
  var lastBlockHeader = null;
634
+ var pingTimer = null;
634
635
  var pending = {};
635
636
  var blockSubscribers = [];
636
637
  var connectionCallbacks = [];
@@ -639,6 +640,7 @@ var BASE_DELAY = 2e3;
639
640
  var MAX_DELAY = 6e4;
640
641
  var RPC_TIMEOUT = 3e4;
641
642
  var CONNECTION_TIMEOUT = 3e4;
643
+ var PING_INTERVAL = 3e4;
642
644
  function isWebSocketConnected() {
643
645
  return isConnected && ws !== null && ws.readyState === WebSocket.OPEN;
644
646
  }
@@ -665,6 +667,33 @@ function waitForConnection() {
665
667
  connectionCallbacks.push(callback);
666
668
  });
667
669
  }
670
+ function startPingTimer() {
671
+ stopPingTimer();
672
+ pingTimer = setInterval(() => {
673
+ if (!ws || ws.readyState !== WebSocket.OPEN) return;
674
+ try {
675
+ const id = ++requestId;
676
+ ws.send(JSON.stringify({ jsonrpc: "2.0", id, method: "server.ping", params: [] }));
677
+ pending[id] = {
678
+ resolve: () => {
679
+ },
680
+ reject: () => {
681
+ }
682
+ };
683
+ const timeoutId = setTimeout(() => {
684
+ delete pending[id];
685
+ }, 1e4);
686
+ pending[id].timeoutId = timeoutId;
687
+ } catch {
688
+ }
689
+ }, PING_INTERVAL);
690
+ }
691
+ function stopPingTimer() {
692
+ if (pingTimer) {
693
+ clearInterval(pingTimer);
694
+ pingTimer = null;
695
+ }
696
+ }
668
697
  function connect(endpoint = DEFAULT_ENDPOINT) {
669
698
  if (isConnected) {
670
699
  return Promise.resolve();
@@ -687,6 +716,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
687
716
  isConnected = true;
688
717
  isConnecting = false;
689
718
  reconnectAttempts = 0;
719
+ startPingTimer();
690
720
  hasResolved = true;
691
721
  resolve();
692
722
  connectionCallbacks.forEach((cb) => {
@@ -698,6 +728,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
698
728
  ws.onclose = () => {
699
729
  isConnected = false;
700
730
  isBlockSubscribed = false;
731
+ stopPingTimer();
701
732
  Object.values(pending).forEach((req) => {
702
733
  if (req.timeoutId) clearTimeout(req.timeoutId);
703
734
  req.reject(new Error("WebSocket connection closed"));
@@ -880,6 +911,7 @@ async function getCurrentBlockHeight() {
880
911
  }
881
912
  }
882
913
  function disconnect() {
914
+ stopPingTimer();
883
915
  if (ws) {
884
916
  intentionalClose = true;
885
917
  ws.close();