@unicitylabs/sphere-sdk 0.6.10-dev.7 → 0.6.11

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
@@ -627,11 +627,12 @@ var ws = null;
627
627
  var isConnected = false;
628
628
  var isConnecting = false;
629
629
  var requestId = 0;
630
- var intentionalClose = false;
631
630
  var reconnectAttempts = 0;
632
631
  var isBlockSubscribed = false;
633
632
  var lastBlockHeader = null;
634
633
  var pingTimer = null;
634
+ var reconnectTimer = null;
635
+ var connectionEpoch = 0;
635
636
  var pending = {};
636
637
  var blockSubscribers = [];
637
638
  var connectionCallbacks = [];
@@ -702,6 +703,18 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
702
703
  return waitForConnection();
703
704
  }
704
705
  isConnecting = true;
706
+ if (ws) {
707
+ try {
708
+ ws.onopen = null;
709
+ ws.onclose = null;
710
+ ws.onerror = null;
711
+ ws.onmessage = null;
712
+ ws.close();
713
+ } catch {
714
+ }
715
+ ws = null;
716
+ }
717
+ const epoch = ++connectionEpoch;
705
718
  return new Promise((resolve, reject) => {
706
719
  let hasResolved = false;
707
720
  try {
@@ -713,6 +726,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
713
726
  return;
714
727
  }
715
728
  ws.onopen = () => {
729
+ if (epoch !== connectionEpoch) return;
716
730
  isConnected = true;
717
731
  isConnecting = false;
718
732
  reconnectAttempts = 0;
@@ -726,6 +740,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
726
740
  connectionCallbacks.length = 0;
727
741
  };
728
742
  ws.onclose = () => {
743
+ if (epoch !== connectionEpoch) return;
729
744
  isConnected = false;
730
745
  isBlockSubscribed = false;
731
746
  stopPingTimer();
@@ -734,16 +749,6 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
734
749
  req.reject(new Error("WebSocket connection closed"));
735
750
  });
736
751
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
737
- if (intentionalClose) {
738
- intentionalClose = false;
739
- isConnecting = false;
740
- reconnectAttempts = 0;
741
- if (!hasResolved) {
742
- hasResolved = true;
743
- reject(new Error("WebSocket connection closed intentionally"));
744
- }
745
- return;
746
- }
747
752
  if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
748
753
  logger.error("L1", "Max reconnect attempts reached. Giving up.");
749
754
  isConnecting = false;
@@ -765,7 +770,8 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
765
770
  "L1",
766
771
  `WebSocket closed unexpectedly. Reconnecting in ${delay}ms (attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})...`
767
772
  );
768
- setTimeout(() => {
773
+ reconnectTimer = setTimeout(() => {
774
+ reconnectTimer = null;
769
775
  connect(endpoint).then(() => {
770
776
  if (!hasResolved) {
771
777
  hasResolved = true;
@@ -912,8 +918,16 @@ async function getCurrentBlockHeight() {
912
918
  }
913
919
  function disconnect() {
914
920
  stopPingTimer();
921
+ if (reconnectTimer) {
922
+ clearTimeout(reconnectTimer);
923
+ reconnectTimer = null;
924
+ }
925
+ connectionEpoch++;
915
926
  if (ws) {
916
- intentionalClose = true;
927
+ ws.onopen = null;
928
+ ws.onclose = null;
929
+ ws.onerror = null;
930
+ ws.onmessage = null;
917
931
  ws.close();
918
932
  ws = null;
919
933
  }
@@ -921,12 +935,15 @@ function disconnect() {
921
935
  isConnecting = false;
922
936
  reconnectAttempts = 0;
923
937
  isBlockSubscribed = false;
938
+ const disconnectError = new Error("WebSocket disconnected");
924
939
  Object.values(pending).forEach((req) => {
925
940
  if (req.timeoutId) clearTimeout(req.timeoutId);
941
+ req.reject(disconnectError);
926
942
  });
927
943
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
928
944
  connectionCallbacks.forEach((cb) => {
929
945
  if (cb.timeoutId) clearTimeout(cb.timeoutId);
946
+ cb.reject(disconnectError);
930
947
  });
931
948
  connectionCallbacks.length = 0;
932
949
  blockSubscribers.length = 0;