@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/cjs/index.cjs CHANGED
@@ -537,7 +537,7 @@ function timeout(fn, options) {
537
537
  const timeout = options === null || options === void 0 ? void 0 : options.timeout;
538
538
  const signal = options === null || options === void 0 ? void 0 : options.signal;
539
539
  const abortController = createAbortController(signal);
540
- return new Promise((resolve, reject) => {
540
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
541
541
  if (abortController.signal.aborted) {
542
542
  reject(new TonConnectError('Operation aborted'));
543
543
  return;
@@ -554,8 +554,14 @@ function timeout(fn, options) {
554
554
  reject(new TonConnectError('Operation aborted'));
555
555
  }, { once: true });
556
556
  const deferOptions = { timeout, abort: abortController.signal };
557
- fn(resolve, reject, deferOptions).finally(() => clearTimeout(timeoutId));
558
- });
557
+ yield fn((...args) => {
558
+ clearTimeout(timeoutId);
559
+ resolve(...args);
560
+ }, () => {
561
+ clearTimeout(timeoutId);
562
+ reject();
563
+ }, deferOptions);
564
+ }));
559
565
  }
560
566
 
561
567
  class BridgeGateway {
@@ -668,12 +674,11 @@ class BridgeGateway {
668
674
  return response;
669
675
  });
670
676
  }
671
- errorsHandler(eventSource, e) {
677
+ errorsHandler(eventSource, e, connectionEstablished) {
672
678
  return __awaiter(this, void 0, void 0, function* () {
673
679
  if (this.isConnecting) {
674
680
  eventSource.close();
675
- logDebug(`Bridge reconnecting, ${this.defaultReconnectDelay}ms delay`);
676
- return yield this.eventSource.recreate(this.defaultReconnectDelay);
681
+ throw new TonConnectError('Bridge error, failed to connect');
677
682
  }
678
683
  if (this.isReady) {
679
684
  try {
@@ -682,11 +687,15 @@ class BridgeGateway {
682
687
  catch (e) { }
683
688
  return;
684
689
  }
685
- if (this.isClosed) {
690
+ if (this.isClosed && connectionEstablished) {
686
691
  eventSource.close();
687
692
  logDebug(`Bridge reconnecting, ${this.defaultReconnectDelay}ms delay`);
688
693
  return yield this.eventSource.recreate(this.defaultReconnectDelay);
689
694
  }
695
+ if (this.isClosed && !connectionEstablished) {
696
+ eventSource.close();
697
+ throw new TonConnectError('Bridge error, failed to connect');
698
+ }
690
699
  throw new TonConnectError('Bridge error, unknown state');
691
700
  });
692
701
  }
@@ -734,6 +743,7 @@ function createEventSource(config) {
734
743
  reject(new TonConnectError('Bridge connection aborted'));
735
744
  return;
736
745
  }
746
+ let connectionEstablished = false;
737
747
  const eventSource = new EventSource(url.toString());
738
748
  eventSource.onerror = (reason) => __awaiter(this, void 0, void 0, function* () {
739
749
  if (signal.aborted) {
@@ -742,7 +752,7 @@ function createEventSource(config) {
742
752
  return;
743
753
  }
744
754
  try {
745
- const newInstance = yield config.errorHandler(eventSource, reason);
755
+ const newInstance = yield config.errorHandler(eventSource, reason, connectionEstablished);
746
756
  if (newInstance !== eventSource) {
747
757
  eventSource.close();
748
758
  }
@@ -756,6 +766,7 @@ function createEventSource(config) {
756
766
  }
757
767
  });
758
768
  eventSource.onopen = () => {
769
+ connectionEstablished = true;
759
770
  if (signal.aborted) {
760
771
  eventSource.close();
761
772
  reject(new TonConnectError('Bridge connection aborted'));
@@ -1285,11 +1296,12 @@ class BridgeProvider {
1285
1296
  return gateway;
1286
1297
  });
1287
1298
  yield Promise.allSettled(this.pendingGateways.map(bridge => callForSuccess((_options) => {
1299
+ var _a;
1288
1300
  if (!this.pendingGateways.some(item => item === bridge)) {
1289
1301
  return bridge.close();
1290
1302
  }
1291
1303
  return bridge.registerSession({
1292
- openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
1304
+ openingDeadlineMS: (_a = options === null || options === void 0 ? void 0 : options.openingDeadlineMS) !== null && _a !== void 0 ? _a : this.defaultOpeningDeadlineMS,
1293
1305
  signal: _options.signal
1294
1306
  });
1295
1307
  }, {
@@ -2429,7 +2441,7 @@ class TonConnectTracker {
2429
2441
  }
2430
2442
  }
2431
2443
 
2432
- const tonConnectSdkVersion = "3.0.4-beta.0";
2444
+ const tonConnectSdkVersion = "3.0.4-beta.1";
2433
2445
 
2434
2446
  class TonConnect {
2435
2447
  constructor(options) {
@@ -2615,7 +2627,7 @@ class TonConnect {
2615
2627
  provider = null;
2616
2628
  };
2617
2629
  abortController.signal.addEventListener('abort', onAbortRestore);
2618
- return yield callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2630
+ const restoreConnectionTask = callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2619
2631
  yield (provider === null || provider === void 0 ? void 0 : provider.restoreConnection({
2620
2632
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
2621
2633
  signal: _options.signal
@@ -2629,9 +2641,12 @@ class TonConnect {
2629
2641
  }
2630
2642
  }), {
2631
2643
  attempts: Number.MAX_SAFE_INTEGER,
2632
- delayMs: 5000,
2644
+ delayMs: 2000,
2633
2645
  signal: options === null || options === void 0 ? void 0 : options.signal
2634
2646
  });
2647
+ const restoreConnectionTimeout = new Promise(resolve => setTimeout(() => resolve(), 12000) // connection deadline
2648
+ );
2649
+ return Promise.race([restoreConnectionTask, restoreConnectionTimeout]);
2635
2650
  });
2636
2651
  }
2637
2652
  sendTransaction(transaction, optionsOrOnRequestSent) {