@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.js CHANGED
@@ -538,11 +538,12 @@ var ws = null;
538
538
  var isConnected = false;
539
539
  var isConnecting = false;
540
540
  var requestId = 0;
541
- var intentionalClose = false;
542
541
  var reconnectAttempts = 0;
543
542
  var isBlockSubscribed = false;
544
543
  var lastBlockHeader = null;
545
544
  var pingTimer = null;
545
+ var reconnectTimer = null;
546
+ var connectionEpoch = 0;
546
547
  var pending = {};
547
548
  var blockSubscribers = [];
548
549
  var connectionCallbacks = [];
@@ -613,6 +614,18 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
613
614
  return waitForConnection();
614
615
  }
615
616
  isConnecting = true;
617
+ if (ws) {
618
+ try {
619
+ ws.onopen = null;
620
+ ws.onclose = null;
621
+ ws.onerror = null;
622
+ ws.onmessage = null;
623
+ ws.close();
624
+ } catch {
625
+ }
626
+ ws = null;
627
+ }
628
+ const epoch = ++connectionEpoch;
616
629
  return new Promise((resolve, reject) => {
617
630
  let hasResolved = false;
618
631
  try {
@@ -624,6 +637,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
624
637
  return;
625
638
  }
626
639
  ws.onopen = () => {
640
+ if (epoch !== connectionEpoch) return;
627
641
  isConnected = true;
628
642
  isConnecting = false;
629
643
  reconnectAttempts = 0;
@@ -637,6 +651,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
637
651
  connectionCallbacks.length = 0;
638
652
  };
639
653
  ws.onclose = () => {
654
+ if (epoch !== connectionEpoch) return;
640
655
  isConnected = false;
641
656
  isBlockSubscribed = false;
642
657
  stopPingTimer();
@@ -645,16 +660,6 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
645
660
  req.reject(new Error("WebSocket connection closed"));
646
661
  });
647
662
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
648
- if (intentionalClose) {
649
- intentionalClose = false;
650
- isConnecting = false;
651
- reconnectAttempts = 0;
652
- if (!hasResolved) {
653
- hasResolved = true;
654
- reject(new Error("WebSocket connection closed intentionally"));
655
- }
656
- return;
657
- }
658
663
  if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
659
664
  logger.error("L1", "Max reconnect attempts reached. Giving up.");
660
665
  isConnecting = false;
@@ -676,7 +681,8 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
676
681
  "L1",
677
682
  `WebSocket closed unexpectedly. Reconnecting in ${delay}ms (attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})...`
678
683
  );
679
- setTimeout(() => {
684
+ reconnectTimer = setTimeout(() => {
685
+ reconnectTimer = null;
680
686
  connect(endpoint).then(() => {
681
687
  if (!hasResolved) {
682
688
  hasResolved = true;
@@ -823,8 +829,16 @@ async function getCurrentBlockHeight() {
823
829
  }
824
830
  function disconnect() {
825
831
  stopPingTimer();
832
+ if (reconnectTimer) {
833
+ clearTimeout(reconnectTimer);
834
+ reconnectTimer = null;
835
+ }
836
+ connectionEpoch++;
826
837
  if (ws) {
827
- intentionalClose = true;
838
+ ws.onopen = null;
839
+ ws.onclose = null;
840
+ ws.onerror = null;
841
+ ws.onmessage = null;
828
842
  ws.close();
829
843
  ws = null;
830
844
  }
@@ -832,12 +846,15 @@ function disconnect() {
832
846
  isConnecting = false;
833
847
  reconnectAttempts = 0;
834
848
  isBlockSubscribed = false;
849
+ const disconnectError = new Error("WebSocket disconnected");
835
850
  Object.values(pending).forEach((req) => {
836
851
  if (req.timeoutId) clearTimeout(req.timeoutId);
852
+ req.reject(disconnectError);
837
853
  });
838
854
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
839
855
  connectionCallbacks.forEach((cb) => {
840
856
  if (cb.timeoutId) clearTimeout(cb.timeoutId);
857
+ cb.reject(disconnectError);
841
858
  });
842
859
  connectionCallbacks.length = 0;
843
860
  blockSubscribers.length = 0;