@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.
@@ -567,6 +567,18 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
567
567
  return waitForConnection();
568
568
  }
569
569
  isConnecting = true;
570
+ if (ws) {
571
+ try {
572
+ ws.onopen = null;
573
+ ws.onclose = null;
574
+ ws.onerror = null;
575
+ ws.onmessage = null;
576
+ ws.close();
577
+ } catch {
578
+ }
579
+ ws = null;
580
+ }
581
+ const epoch = ++connectionEpoch;
570
582
  return new Promise((resolve, reject) => {
571
583
  let hasResolved = false;
572
584
  try {
@@ -578,6 +590,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
578
590
  return;
579
591
  }
580
592
  ws.onopen = () => {
593
+ if (epoch !== connectionEpoch) return;
581
594
  isConnected = true;
582
595
  isConnecting = false;
583
596
  reconnectAttempts = 0;
@@ -591,6 +604,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
591
604
  connectionCallbacks.length = 0;
592
605
  };
593
606
  ws.onclose = () => {
607
+ if (epoch !== connectionEpoch) return;
594
608
  isConnected = false;
595
609
  isBlockSubscribed = false;
596
610
  stopPingTimer();
@@ -599,16 +613,6 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
599
613
  req.reject(new Error("WebSocket connection closed"));
600
614
  });
601
615
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
602
- if (intentionalClose) {
603
- intentionalClose = false;
604
- isConnecting = false;
605
- reconnectAttempts = 0;
606
- if (!hasResolved) {
607
- hasResolved = true;
608
- reject(new Error("WebSocket connection closed intentionally"));
609
- }
610
- return;
611
- }
612
616
  if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
613
617
  logger.error("L1", "Max reconnect attempts reached. Giving up.");
614
618
  isConnecting = false;
@@ -630,7 +634,8 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
630
634
  "L1",
631
635
  `WebSocket closed unexpectedly. Reconnecting in ${delay}ms (attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})...`
632
636
  );
633
- setTimeout(() => {
637
+ reconnectTimer = setTimeout(() => {
638
+ reconnectTimer = null;
634
639
  connect(endpoint).then(() => {
635
640
  if (!hasResolved) {
636
641
  hasResolved = true;
@@ -777,8 +782,16 @@ async function getCurrentBlockHeight() {
777
782
  }
778
783
  function disconnect() {
779
784
  stopPingTimer();
785
+ if (reconnectTimer) {
786
+ clearTimeout(reconnectTimer);
787
+ reconnectTimer = null;
788
+ }
789
+ connectionEpoch++;
780
790
  if (ws) {
781
- intentionalClose = true;
791
+ ws.onopen = null;
792
+ ws.onclose = null;
793
+ ws.onerror = null;
794
+ ws.onmessage = null;
782
795
  ws.close();
783
796
  ws = null;
784
797
  }
@@ -786,18 +799,21 @@ function disconnect() {
786
799
  isConnecting = false;
787
800
  reconnectAttempts = 0;
788
801
  isBlockSubscribed = false;
802
+ const disconnectError = new Error("WebSocket disconnected");
789
803
  Object.values(pending).forEach((req) => {
790
804
  if (req.timeoutId) clearTimeout(req.timeoutId);
805
+ req.reject(disconnectError);
791
806
  });
792
807
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
793
808
  connectionCallbacks.forEach((cb) => {
794
809
  if (cb.timeoutId) clearTimeout(cb.timeoutId);
810
+ cb.reject(disconnectError);
795
811
  });
796
812
  connectionCallbacks.length = 0;
797
813
  blockSubscribers.length = 0;
798
814
  lastBlockHeader = null;
799
815
  }
800
- 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;
816
+ var DEFAULT_ENDPOINT, ws, isConnected, isConnecting, requestId, reconnectAttempts, isBlockSubscribed, lastBlockHeader, pingTimer, reconnectTimer, connectionEpoch, pending, blockSubscribers, connectionCallbacks, MAX_RECONNECT_ATTEMPTS, BASE_DELAY, MAX_DELAY, RPC_TIMEOUT, CONNECTION_TIMEOUT, PING_INTERVAL;
801
817
  var init_network = __esm({
802
818
  "l1/network.ts"() {
803
819
  "use strict";
@@ -809,11 +825,12 @@ var init_network = __esm({
809
825
  isConnected = false;
810
826
  isConnecting = false;
811
827
  requestId = 0;
812
- intentionalClose = false;
813
828
  reconnectAttempts = 0;
814
829
  isBlockSubscribed = false;
815
830
  lastBlockHeader = null;
816
831
  pingTimer = null;
832
+ reconnectTimer = null;
833
+ connectionEpoch = 0;
817
834
  pending = {};
818
835
  blockSubscribers = [];
819
836
  connectionCallbacks = [];
@@ -5242,9 +5259,7 @@ var L1PaymentsModule = class {
5242
5259
  }
5243
5260
  }
5244
5261
  destroy() {
5245
- if (isWebSocketConnected()) {
5246
- disconnect();
5247
- }
5262
+ disconnect();
5248
5263
  this._initialized = false;
5249
5264
  this._identity = void 0;
5250
5265
  this._addresses = [];
@@ -5255,9 +5270,7 @@ var L1PaymentsModule = class {
5255
5270
  */
5256
5271
  disable() {
5257
5272
  this._disabled = true;
5258
- if (isWebSocketConnected()) {
5259
- disconnect();
5260
- }
5273
+ disconnect();
5261
5274
  }
5262
5275
  /**
5263
5276
  * Re-enable this module. Connection will be established lazily on next operation.
@@ -15472,7 +15485,7 @@ var MarketModule = class {
15472
15485
  const MAX_ATTEMPTS = 10;
15473
15486
  let ws2 = null;
15474
15487
  let reconnectAttempts2 = 0;
15475
- let reconnectTimer = null;
15488
+ let reconnectTimer2 = null;
15476
15489
  let destroyed = false;
15477
15490
  function connect2() {
15478
15491
  if (destroyed) return;
@@ -15504,14 +15517,14 @@ var MarketModule = class {
15504
15517
  const delay = Math.min(BASE_DELAY2 * Math.pow(2, reconnectAttempts2), MAX_DELAY2);
15505
15518
  reconnectAttempts2++;
15506
15519
  logger.debug("Market", `Feed WebSocket reconnecting in ${delay}ms (attempt ${reconnectAttempts2}/${MAX_ATTEMPTS})`);
15507
- reconnectTimer = setTimeout(connect2, delay);
15520
+ reconnectTimer2 = setTimeout(connect2, delay);
15508
15521
  }
15509
15522
  connect2();
15510
15523
  return () => {
15511
15524
  destroyed = true;
15512
- if (reconnectTimer) {
15513
- clearTimeout(reconnectTimer);
15514
- reconnectTimer = null;
15525
+ if (reconnectTimer2) {
15526
+ clearTimeout(reconnectTimer2);
15527
+ reconnectTimer2 = null;
15515
15528
  }
15516
15529
  ws2?.close();
15517
15530
  ws2 = null;
@@ -18312,7 +18325,7 @@ var Sphere = class _Sphere {
18312
18325
  if (!this._transport.discoverAddresses) {
18313
18326
  throw new SphereError("Transport provider does not support address discovery", "INVALID_CONFIG");
18314
18327
  }
18315
- const includeL1Scan = options.includeL1Scan ?? true;
18328
+ const includeL1Scan = (options.includeL1Scan ?? true) && !!this._l1Config;
18316
18329
  const transportResult = await discoverAddressesImpl(
18317
18330
  (index) => {
18318
18331
  const addrInfo = this._deriveAddressInternal(index, false);