@tonconnect/sdk 3.0.4-beta.0 → 3.0.4-beta.1

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/lib/esm/index.mjs CHANGED
@@ -534,7 +534,7 @@ function timeout(fn, options) {
534
534
  const timeout = options === null || options === void 0 ? void 0 : options.timeout;
535
535
  const signal = options === null || options === void 0 ? void 0 : options.signal;
536
536
  const abortController = createAbortController(signal);
537
- return new Promise((resolve, reject) => {
537
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
538
538
  if (abortController.signal.aborted) {
539
539
  reject(new TonConnectError('Operation aborted'));
540
540
  return;
@@ -551,8 +551,14 @@ function timeout(fn, options) {
551
551
  reject(new TonConnectError('Operation aborted'));
552
552
  }, { once: true });
553
553
  const deferOptions = { timeout, abort: abortController.signal };
554
- fn(resolve, reject, deferOptions).finally(() => clearTimeout(timeoutId));
555
- });
554
+ yield fn((...args) => {
555
+ clearTimeout(timeoutId);
556
+ resolve(...args);
557
+ }, () => {
558
+ clearTimeout(timeoutId);
559
+ reject();
560
+ }, deferOptions);
561
+ }));
556
562
  }
557
563
 
558
564
  class BridgeGateway {
@@ -665,12 +671,11 @@ class BridgeGateway {
665
671
  return response;
666
672
  });
667
673
  }
668
- errorsHandler(eventSource, e) {
674
+ errorsHandler(eventSource, e, connectionEstablished) {
669
675
  return __awaiter(this, void 0, void 0, function* () {
670
676
  if (this.isConnecting) {
671
677
  eventSource.close();
672
- logDebug(`Bridge reconnecting, ${this.defaultReconnectDelay}ms delay`);
673
- return yield this.eventSource.recreate(this.defaultReconnectDelay);
678
+ throw new TonConnectError('Bridge error, failed to connect');
674
679
  }
675
680
  if (this.isReady) {
676
681
  try {
@@ -679,11 +684,15 @@ class BridgeGateway {
679
684
  catch (e) { }
680
685
  return;
681
686
  }
682
- if (this.isClosed) {
687
+ if (this.isClosed && connectionEstablished) {
683
688
  eventSource.close();
684
689
  logDebug(`Bridge reconnecting, ${this.defaultReconnectDelay}ms delay`);
685
690
  return yield this.eventSource.recreate(this.defaultReconnectDelay);
686
691
  }
692
+ if (this.isClosed && !connectionEstablished) {
693
+ eventSource.close();
694
+ throw new TonConnectError('Bridge error, failed to connect');
695
+ }
687
696
  throw new TonConnectError('Bridge error, unknown state');
688
697
  });
689
698
  }
@@ -731,6 +740,7 @@ function createEventSource(config) {
731
740
  reject(new TonConnectError('Bridge connection aborted'));
732
741
  return;
733
742
  }
743
+ let connectionEstablished = false;
734
744
  const eventSource = new EventSource(url.toString());
735
745
  eventSource.onerror = (reason) => __awaiter(this, void 0, void 0, function* () {
736
746
  if (signal.aborted) {
@@ -739,7 +749,7 @@ function createEventSource(config) {
739
749
  return;
740
750
  }
741
751
  try {
742
- const newInstance = yield config.errorHandler(eventSource, reason);
752
+ const newInstance = yield config.errorHandler(eventSource, reason, connectionEstablished);
743
753
  if (newInstance !== eventSource) {
744
754
  eventSource.close();
745
755
  }
@@ -753,6 +763,7 @@ function createEventSource(config) {
753
763
  }
754
764
  });
755
765
  eventSource.onopen = () => {
766
+ connectionEstablished = true;
756
767
  if (signal.aborted) {
757
768
  eventSource.close();
758
769
  reject(new TonConnectError('Bridge connection aborted'));
@@ -1282,11 +1293,12 @@ class BridgeProvider {
1282
1293
  return gateway;
1283
1294
  });
1284
1295
  yield Promise.allSettled(this.pendingGateways.map(bridge => callForSuccess((_options) => {
1296
+ var _a;
1285
1297
  if (!this.pendingGateways.some(item => item === bridge)) {
1286
1298
  return bridge.close();
1287
1299
  }
1288
1300
  return bridge.registerSession({
1289
- openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
1301
+ openingDeadlineMS: (_a = options === null || options === void 0 ? void 0 : options.openingDeadlineMS) !== null && _a !== void 0 ? _a : this.defaultOpeningDeadlineMS,
1290
1302
  signal: _options.signal
1291
1303
  });
1292
1304
  }, {
@@ -2426,7 +2438,7 @@ class TonConnectTracker {
2426
2438
  }
2427
2439
  }
2428
2440
 
2429
- const tonConnectSdkVersion = "3.0.4-beta.0";
2441
+ const tonConnectSdkVersion = "3.0.4-beta.1";
2430
2442
 
2431
2443
  class TonConnect {
2432
2444
  constructor(options) {
@@ -2612,7 +2624,7 @@ class TonConnect {
2612
2624
  provider = null;
2613
2625
  };
2614
2626
  abortController.signal.addEventListener('abort', onAbortRestore);
2615
- return yield callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2627
+ const restoreConnectionTask = callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2616
2628
  yield (provider === null || provider === void 0 ? void 0 : provider.restoreConnection({
2617
2629
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
2618
2630
  signal: _options.signal
@@ -2626,9 +2638,12 @@ class TonConnect {
2626
2638
  }
2627
2639
  }), {
2628
2640
  attempts: Number.MAX_SAFE_INTEGER,
2629
- delayMs: 5000,
2641
+ delayMs: 2000,
2630
2642
  signal: options === null || options === void 0 ? void 0 : options.signal
2631
2643
  });
2644
+ const restoreConnectionTimeout = new Promise(resolve => setTimeout(() => resolve(), 12000) // connection deadline
2645
+ );
2646
+ return Promise.race([restoreConnectionTask, restoreConnectionTimeout]);
2632
2647
  });
2633
2648
  }
2634
2649
  sendTransaction(transaction, optionsOrOnRequestSent) {