@tonconnect/sdk 3.0.1-beta.0 → 3.0.1-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
@@ -346,54 +346,6 @@ function encodeTelegramUrlParameters(parameters) {
346
346
  .replaceAll('%', '--');
347
347
  }
348
348
 
349
- /**
350
- * Creates an AbortController instance with an optional AbortSignal.
351
- *
352
- * @param {AbortSignal} [signal] - An optional AbortSignal to use for aborting the controller.
353
- * @returns {AbortController} - An instance of AbortController.
354
- */
355
- function createAbortController(signal) {
356
- const abortController = new AbortController();
357
- if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
358
- abortController.abort();
359
- }
360
- else {
361
- signal === null || signal === void 0 ? void 0 : signal.addEventListener('abort', () => abortController.abort(), { once: true });
362
- }
363
- return abortController;
364
- }
365
- /**
366
- * Executes a function and provides deferred behavior, allowing for a timeout and abort functionality.
367
- *
368
- * @param {Deferrable<T>} fn - The function to execute. It should return a promise that resolves with the desired result.
369
- * @param {DeferOptions} options - Optional configuration options for the defer behavior.
370
- * @returns {Promise<T>} - A promise that resolves with the result of the executed function, or rejects with an error if it times out or is aborted.
371
- */
372
- function defer(fn, options) {
373
- const timeout = options === null || options === void 0 ? void 0 : options.timeout;
374
- const signal = options === null || options === void 0 ? void 0 : options.signal;
375
- const abortController = createAbortController(signal);
376
- return new Promise((resolve, reject) => {
377
- if (abortController.signal.aborted) {
378
- reject(new TonConnectError('Operation aborted'));
379
- return;
380
- }
381
- let timeoutId;
382
- if (typeof timeout !== 'undefined') {
383
- timeoutId = setTimeout(() => {
384
- reject(new TonConnectError(`Timeout after ${timeout}ms`));
385
- abortController.abort();
386
- }, timeout);
387
- }
388
- abortController.signal.addEventListener('abort', () => {
389
- clearTimeout(timeoutId);
390
- reject(new TonConnectError('Operation aborted'));
391
- }, { once: true });
392
- const deferOptions = { timeout, abort: abortController.signal };
393
- fn(resolve, reject, deferOptions).finally(() => clearTimeout(timeoutId));
394
- });
395
- }
396
-
397
349
  /**
398
350
  * Delays the execution of code for a specified number of milliseconds.
399
351
  * @param {number} timeout - The number of milliseconds to delay the execution.
@@ -402,21 +354,38 @@ function defer(fn, options) {
402
354
  */
403
355
  function delay(timeout, options) {
404
356
  return __awaiter(this, void 0, void 0, function* () {
405
- return yield defer((resolve, reject, options) => __awaiter(this, void 0, void 0, function* () {
357
+ return new Promise((resolve, reject) => {
406
358
  var _a, _b;
407
- if ((_a = options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
359
+ if ((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
408
360
  reject(new TonConnectError('Delay aborted'));
409
361
  return;
410
362
  }
411
363
  const timeoutId = setTimeout(() => resolve(), timeout);
412
- (_b = options.signal) === null || _b === void 0 ? void 0 : _b.addEventListener('abort', () => {
364
+ (_b = options === null || options === void 0 ? void 0 : options.signal) === null || _b === void 0 ? void 0 : _b.addEventListener('abort', () => {
413
365
  clearTimeout(timeoutId);
414
366
  reject(new TonConnectError('Delay aborted'));
415
367
  });
416
- }), { signal: options === null || options === void 0 ? void 0 : options.signal });
368
+ });
417
369
  });
418
370
  }
419
371
 
372
+ /**
373
+ * Creates an AbortController instance with an optional AbortSignal.
374
+ *
375
+ * @param {AbortSignal} [signal] - An optional AbortSignal to use for aborting the controller.
376
+ * @returns {AbortController} - An instance of AbortController.
377
+ */
378
+ function createAbortController(signal) {
379
+ const abortController = new AbortController();
380
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
381
+ abortController.abort();
382
+ }
383
+ else {
384
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener('abort', () => abortController.abort(), { once: true });
385
+ }
386
+ return abortController;
387
+ }
388
+
420
389
  /**
421
390
  * Function to call ton api until we get response.
422
391
  * Because ton network is pretty unstable we need to make sure response is final.
@@ -444,7 +413,9 @@ function callForSuccess(fn, options) {
444
413
  catch (err) {
445
414
  lastError = err;
446
415
  i++;
447
- yield delay(delayMs);
416
+ if (i < attempts) {
417
+ yield delay(delayMs);
418
+ }
448
419
  }
449
420
  }
450
421
  throw lastError;
@@ -489,13 +460,18 @@ function createResource(createFn, disposeFn) {
489
460
  let currentResource = null;
490
461
  let currentArgs = null;
491
462
  let currentPromise = null;
463
+ let currentSignal = null;
492
464
  let abortController = null;
493
465
  // create a new resource
494
- const create = (...args) => __awaiter(this, void 0, void 0, function* () {
466
+ const create = (signal, ...args) => __awaiter(this, void 0, void 0, function* () {
467
+ currentSignal = signal !== null && signal !== void 0 ? signal : null;
495
468
  abortController === null || abortController === void 0 ? void 0 : abortController.abort();
496
- abortController = createAbortController();
497
- currentArgs = args;
498
- const promise = createFn(abortController.signal, ...args);
469
+ abortController = createAbortController(signal);
470
+ if (abortController.signal.aborted) {
471
+ throw new TonConnectError('Resource creation was aborted');
472
+ }
473
+ currentArgs = args !== null && args !== void 0 ? args : null;
474
+ const promise = createFn(signal, ...args);
499
475
  currentPromise = promise;
500
476
  const resource = yield promise;
501
477
  if (currentPromise !== promise) {
@@ -511,20 +487,35 @@ function createResource(createFn, disposeFn) {
511
487
  };
512
488
  // dispose the current resource
513
489
  const dispose = () => __awaiter(this, void 0, void 0, function* () {
514
- const resource = currentResource;
515
- currentResource = null;
516
- const promise = currentPromise;
517
- currentPromise = null;
518
- abortController === null || abortController === void 0 ? void 0 : abortController.abort();
519
- yield Promise.allSettled([
520
- resource ? disposeFn(resource) : Promise.resolve(),
521
- promise ? disposeFn(yield promise) : Promise.resolve()
522
- ]);
490
+ try {
491
+ const resource = currentResource;
492
+ currentResource = null;
493
+ const promise = currentPromise;
494
+ currentPromise = null;
495
+ abortController === null || abortController === void 0 ? void 0 : abortController.abort();
496
+ yield Promise.allSettled([
497
+ resource ? disposeFn(resource) : Promise.resolve(),
498
+ promise ? disposeFn(yield promise) : Promise.resolve()
499
+ ]);
500
+ }
501
+ catch (e) {
502
+ logError('Failed to dispose the resource', e);
503
+ }
523
504
  });
524
505
  // recreate the current resource
525
- const recreate = () => __awaiter(this, void 0, void 0, function* () {
526
- yield dispose();
527
- return create(...(currentArgs !== null && currentArgs !== void 0 ? currentArgs : []));
506
+ const recreate = (delayMs) => __awaiter(this, void 0, void 0, function* () {
507
+ const resource = currentResource;
508
+ const promise = currentPromise;
509
+ const args = currentArgs;
510
+ const signal = currentSignal;
511
+ yield delay(delayMs);
512
+ if (resource === currentResource &&
513
+ promise === currentPromise &&
514
+ args === currentArgs &&
515
+ signal === currentSignal) {
516
+ return create(currentSignal, ...(args !== null && args !== void 0 ? args : []));
517
+ }
518
+ throw new TonConnectError('Resource recreation was aborted by a new resource creation');
528
519
  });
529
520
  return {
530
521
  create,
@@ -534,6 +525,38 @@ function createResource(createFn, disposeFn) {
534
525
  };
535
526
  }
536
527
 
528
+ /**
529
+ * Executes a function and provides deferred behavior, allowing for a timeout and abort functionality.
530
+ *
531
+ * @param {Deferrable<T>} fn - The function to execute. It should return a promise that resolves with the desired result.
532
+ * @param {DeferOptions} options - Optional configuration options for the defer behavior.
533
+ * @returns {Promise<T>} - A promise that resolves with the result of the executed function, or rejects with an error if it times out or is aborted.
534
+ */
535
+ function timeout(fn, options) {
536
+ const timeout = options === null || options === void 0 ? void 0 : options.timeout;
537
+ const signal = options === null || options === void 0 ? void 0 : options.signal;
538
+ const abortController = createAbortController(signal);
539
+ return new Promise((resolve, reject) => {
540
+ if (abortController.signal.aborted) {
541
+ reject(new TonConnectError('Operation aborted'));
542
+ return;
543
+ }
544
+ let timeoutId;
545
+ if (typeof timeout !== 'undefined') {
546
+ timeoutId = setTimeout(() => {
547
+ abortController.abort();
548
+ reject(new TonConnectError(`Timeout after ${timeout}ms`));
549
+ }, timeout);
550
+ }
551
+ abortController.signal.addEventListener('abort', () => {
552
+ clearTimeout(timeoutId);
553
+ reject(new TonConnectError('Operation aborted'));
554
+ }, { once: true });
555
+ const deferOptions = { timeout, abort: abortController.signal };
556
+ fn(resolve, reject, deferOptions).finally(() => clearTimeout(timeoutId));
557
+ });
558
+ }
559
+
537
560
  class BridgeGateway {
538
561
  constructor(storage, bridgeUrl, sessionId, listener, errorsListener) {
539
562
  this.bridgeUrl = bridgeUrl;
@@ -544,7 +567,8 @@ class BridgeGateway {
544
567
  this.postPath = 'message';
545
568
  this.heartbeatMessage = 'heartbeat';
546
569
  this.defaultTtl = 300;
547
- this.eventSource = createResource((signal, options) => __awaiter(this, void 0, void 0, function* () {
570
+ this.defaultReconnectDelay = 5000;
571
+ this.eventSource = createResource((signal, openingDeadlineMS) => __awaiter(this, void 0, void 0, function* () {
548
572
  const eventSourceConfig = {
549
573
  bridgeUrl: this.bridgeUrl,
550
574
  ssePath: this.ssePath,
@@ -552,9 +576,10 @@ class BridgeGateway {
552
576
  bridgeGatewayStorage: this.bridgeGatewayStorage,
553
577
  errorHandler: this.errorsHandler.bind(this),
554
578
  messageHandler: this.messagesHandler.bind(this),
555
- signal: signal
579
+ signal: signal,
580
+ openingDeadlineMS: openingDeadlineMS
556
581
  };
557
- return yield createEventSource(eventSourceConfig, options);
582
+ return yield createEventSource(eventSourceConfig);
558
583
  }), (resource) => __awaiter(this, void 0, void 0, function* () {
559
584
  resource.close();
560
585
  }));
@@ -574,10 +599,11 @@ class BridgeGateway {
574
599
  }
575
600
  registerSession(options) {
576
601
  return __awaiter(this, void 0, void 0, function* () {
577
- yield this.eventSource.create(options);
602
+ yield this.eventSource.create(options === null || options === void 0 ? void 0 : options.signal, options === null || options === void 0 ? void 0 : options.openingDeadlineMS);
578
603
  });
579
604
  }
580
605
  send(message, receiver, topic, ttlOrOptions) {
606
+ var _a;
581
607
  return __awaiter(this, void 0, void 0, function* () {
582
608
  // TODO: remove deprecated method
583
609
  const options = {};
@@ -587,6 +613,7 @@ class BridgeGateway {
587
613
  else {
588
614
  options.ttl = ttlOrOptions === null || ttlOrOptions === void 0 ? void 0 : ttlOrOptions.ttl;
589
615
  options.signal = ttlOrOptions === null || ttlOrOptions === void 0 ? void 0 : ttlOrOptions.signal;
616
+ options.attempts = ttlOrOptions === null || ttlOrOptions === void 0 ? void 0 : ttlOrOptions.attempts;
590
617
  }
591
618
  const url = new URL(addPathToUrl(this.bridgeUrl, this.postPath));
592
619
  url.searchParams.append('client_id', this.sessionId);
@@ -599,21 +626,25 @@ class BridgeGateway {
599
626
  if (!response.ok) {
600
627
  throw new TonConnectError(`Bridge send failed, status ${response.status}`);
601
628
  }
602
- }), { attempts: Number.MAX_SAFE_INTEGER, delayMs: 5000, signal: options === null || options === void 0 ? void 0 : options.signal });
629
+ }), {
630
+ attempts: (_a = options === null || options === void 0 ? void 0 : options.attempts) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER,
631
+ delayMs: 5000,
632
+ signal: options === null || options === void 0 ? void 0 : options.signal
633
+ });
603
634
  });
604
635
  }
605
636
  pause() {
606
- var _a;
607
- (_a = this.eventSource) === null || _a === void 0 ? void 0 : _a.dispose();
637
+ this.eventSource.dispose().catch(e => logError(`Bridge pause failed, ${e}`));
608
638
  }
609
639
  unPause() {
610
640
  return __awaiter(this, void 0, void 0, function* () {
611
- yield this.eventSource.recreate();
641
+ const RECREATE_WITHOUT_DELAY = 0;
642
+ yield this.eventSource.recreate(RECREATE_WITHOUT_DELAY);
612
643
  });
613
644
  }
614
645
  close() {
615
646
  return __awaiter(this, void 0, void 0, function* () {
616
- yield this.eventSource.dispose();
647
+ yield this.eventSource.dispose().catch(e => logError(`Bridge close failed, ${e}`));
617
648
  });
618
649
  }
619
650
  setListener(listener) {
@@ -637,20 +668,22 @@ class BridgeGateway {
637
668
  }
638
669
  errorsHandler(e) {
639
670
  return __awaiter(this, void 0, void 0, function* () {
640
- if (this.isConnecting) {
641
- logError('Bridge error', JSON.stringify(e));
642
- return;
643
- }
644
- if (this.isReady) {
645
- this.errorsListener(e);
646
- return;
647
- }
648
- if (this.isClosed) {
649
- logDebug('Bridge reconnecting, 200ms delay');
650
- yield delay(200);
651
- yield this.eventSource.recreate();
652
- return;
671
+ try {
672
+ if (this.isConnecting) {
673
+ logError('Bridge error', JSON.stringify(e));
674
+ return;
675
+ }
676
+ if (this.isReady) {
677
+ this.errorsListener(e);
678
+ return;
679
+ }
680
+ if (this.isClosed) {
681
+ logDebug(`Bridge reconnecting, ${this.defaultReconnectDelay}ms delay`);
682
+ yield this.eventSource.recreate(this.defaultReconnectDelay);
683
+ return;
684
+ }
653
685
  }
686
+ catch (e) { }
654
687
  });
655
688
  }
656
689
  messagesHandler(e) {
@@ -673,9 +706,13 @@ class BridgeGateway {
673
706
  });
674
707
  }
675
708
  }
676
- function createEventSource(config, options) {
709
+ /**
710
+ * Creates an event source.
711
+ * @param {CreateEventSourceConfig} config - Configuration for creating an event source.
712
+ */
713
+ function createEventSource(config) {
677
714
  return __awaiter(this, void 0, void 0, function* () {
678
- return yield defer((resolve, reject, deferOptions) => __awaiter(this, void 0, void 0, function* () {
715
+ return yield timeout((resolve, reject, deferOptions) => __awaiter(this, void 0, void 0, function* () {
679
716
  var _a;
680
717
  const abortController = createAbortController(deferOptions.signal);
681
718
  const signal = abortController.signal;
@@ -696,6 +733,7 @@ function createEventSource(config, options) {
696
733
  const eventSource = new EventSource(url.toString());
697
734
  eventSource.onerror = (reason) => {
698
735
  if (signal.aborted) {
736
+ eventSource.close();
699
737
  reject(new TonConnectError('Bridge connection aborted'));
700
738
  return;
701
739
  }
@@ -703,6 +741,7 @@ function createEventSource(config, options) {
703
741
  };
704
742
  eventSource.onopen = () => {
705
743
  if (signal.aborted) {
744
+ eventSource.close();
706
745
  reject(new TonConnectError('Bridge connection aborted'));
707
746
  return;
708
747
  }
@@ -711,12 +750,11 @@ function createEventSource(config, options) {
711
750
  eventSource.onmessage = (event) => {
712
751
  config.messageHandler(event);
713
752
  };
714
- (_a = config === null || config === void 0 ? void 0 : config.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', () => {
715
- logError('Bridge connection aborted');
753
+ (_a = config.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', () => {
716
754
  eventSource.close();
717
755
  reject(new TonConnectError('Bridge connection aborted'));
718
756
  });
719
- }), { timeout: options === null || options === void 0 ? void 0 : options.openingDeadlineMS, signal: config === null || config === void 0 ? void 0 : config.signal });
757
+ }), { timeout: config.openingDeadlineMS, signal: config.signal });
720
758
  });
721
759
  }
722
760
 
@@ -908,6 +946,10 @@ class BridgeProvider {
908
946
  });
909
947
  }
910
948
  connect(message, options) {
949
+ var _a;
950
+ const abortController = createAbortController(options === null || options === void 0 ? void 0 : options.signal);
951
+ (_a = this.abortController) === null || _a === void 0 ? void 0 : _a.abort();
952
+ this.abortController = abortController;
911
953
  this.closeGateways();
912
954
  const sessionCrypto = new protocol.SessionCrypto();
913
955
  this.session = {
@@ -923,13 +965,16 @@ class BridgeProvider {
923
965
  sessionCrypto
924
966
  })
925
967
  .then(() => __awaiter(this, void 0, void 0, function* () {
968
+ if (abortController.signal.aborted) {
969
+ return;
970
+ }
926
971
  yield callForSuccess(_options => this.openGateways(sessionCrypto, {
927
972
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
928
973
  signal: _options === null || _options === void 0 ? void 0 : _options.signal
929
974
  }), {
930
975
  attempts: Number.MAX_SAFE_INTEGER,
931
976
  delayMs: 5000,
932
- signal: options === null || options === void 0 ? void 0 : options.signal
977
+ signal: abortController.signal
933
978
  });
934
979
  }));
935
980
  const universalLink = 'universalLink' in this.walletConnectionSource &&
@@ -1008,6 +1053,7 @@ class BridgeProvider {
1008
1053
  else {
1009
1054
  options.onRequestSent = optionsOrOnRequestSent === null || optionsOrOnRequestSent === void 0 ? void 0 : optionsOrOnRequestSent.onRequestSent;
1010
1055
  options.signal = optionsOrOnRequestSent === null || optionsOrOnRequestSent === void 0 ? void 0 : optionsOrOnRequestSent.signal;
1056
+ options.attempts = optionsOrOnRequestSent === null || optionsOrOnRequestSent === void 0 ? void 0 : optionsOrOnRequestSent.attempts;
1011
1057
  }
1012
1058
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
1013
1059
  var _a;
@@ -1019,7 +1065,7 @@ class BridgeProvider {
1019
1065
  logDebug('Send http-bridge request:', Object.assign(Object.assign({}, request), { id }));
1020
1066
  const encodedRequest = this.session.sessionCrypto.encrypt(JSON.stringify(Object.assign(Object.assign({}, request), { id })), protocol.hexToByteArray(this.session.walletPublicKey));
1021
1067
  try {
1022
- yield this.gateway.send(encodedRequest, this.session.walletPublicKey, request.method, { signal: options === null || options === void 0 ? void 0 : options.signal });
1068
+ yield this.gateway.send(encodedRequest, this.session.walletPublicKey, request.method, { attempts: options === null || options === void 0 ? void 0 : options.attempts, signal: options === null || options === void 0 ? void 0 : options.signal });
1023
1069
  (_a = options === null || options === void 0 ? void 0 : options.onRequestSent) === null || _a === void 0 ? void 0 : _a.call(options);
1024
1070
  this.pendingRequests.set(id.toString(), resolve);
1025
1071
  }
@@ -1038,15 +1084,23 @@ class BridgeProvider {
1038
1084
  return __awaiter(this, void 0, void 0, function* () {
1039
1085
  return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
1040
1086
  let called = false;
1087
+ let timeoutId = null;
1041
1088
  const onRequestSent = () => {
1042
- called = true;
1043
- this.removeBridgeAndSession().then(resolve);
1089
+ if (!called) {
1090
+ called = true;
1091
+ this.removeBridgeAndSession().then(resolve);
1092
+ }
1044
1093
  };
1045
1094
  try {
1046
1095
  this.closeGateways();
1096
+ const abortController = createAbortController(options === null || options === void 0 ? void 0 : options.signal);
1097
+ timeoutId = setTimeout(() => {
1098
+ abortController.abort();
1099
+ }, this.defaultOpeningDeadlineMS);
1047
1100
  yield this.sendRequest({ method: 'disconnect', params: [] }, {
1048
1101
  onRequestSent: onRequestSent,
1049
- signal: options === null || options === void 0 ? void 0 : options.signal
1102
+ signal: abortController.signal,
1103
+ attempts: 1,
1050
1104
  });
1051
1105
  }
1052
1106
  catch (e) {
@@ -1055,6 +1109,12 @@ class BridgeProvider {
1055
1109
  this.removeBridgeAndSession().then(resolve);
1056
1110
  }
1057
1111
  }
1112
+ finally {
1113
+ if (timeoutId) {
1114
+ clearTimeout(timeoutId);
1115
+ }
1116
+ onRequestSent();
1117
+ }
1058
1118
  }));
1059
1119
  });
1060
1120
  }
@@ -1189,11 +1249,7 @@ class BridgeProvider {
1189
1249
  return url.toString();
1190
1250
  }
1191
1251
  openGateways(sessionCrypto, options) {
1192
- var _a;
1193
1252
  return __awaiter(this, void 0, void 0, function* () {
1194
- const abortController = createAbortController(options === null || options === void 0 ? void 0 : options.signal);
1195
- (_a = this.abortController) === null || _a === void 0 ? void 0 : _a.abort();
1196
- this.abortController = abortController;
1197
1253
  if (Array.isArray(this.walletConnectionSource)) {
1198
1254
  // close all gateways before opening new ones
1199
1255
  this.pendingGateways.map(bridge => bridge.close().catch(e => console.error(e)));
@@ -1206,6 +1262,9 @@ class BridgeProvider {
1206
1262
  return gateway;
1207
1263
  });
1208
1264
  yield Promise.allSettled(this.pendingGateways.map(bridge => callForSuccess((_options) => {
1265
+ if (!this.pendingGateways.some(item => item === bridge)) {
1266
+ return bridge.close();
1267
+ }
1209
1268
  return bridge.registerSession({
1210
1269
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
1211
1270
  signal: _options.signal
@@ -1213,7 +1272,7 @@ class BridgeProvider {
1213
1272
  }, {
1214
1273
  attempts: Number.MAX_SAFE_INTEGER,
1215
1274
  delayMs: 5000,
1216
- signal: abortController.signal
1275
+ signal: options === null || options === void 0 ? void 0 : options.signal
1217
1276
  })));
1218
1277
  return;
1219
1278
  }
@@ -1225,7 +1284,7 @@ class BridgeProvider {
1225
1284
  this.gateway = new BridgeGateway(this.storage, this.walletConnectionSource.bridgeUrl, sessionCrypto.sessionId, this.gatewayListener.bind(this), this.gatewayErrorsListener.bind(this));
1226
1285
  return yield this.gateway.registerSession({
1227
1286
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
1228
- signal: abortController.signal
1287
+ signal: options === null || options === void 0 ? void 0 : options.signal
1229
1288
  });
1230
1289
  }
1231
1290
  });
@@ -2033,6 +2092,11 @@ class TonConnect {
2033
2092
  }
2034
2093
  (_b = this.provider) === null || _b === void 0 ? void 0 : _b.closeConnection();
2035
2094
  this.provider = this.createProvider(wallet);
2095
+ abortController.signal.addEventListener('abort', () => {
2096
+ var _a;
2097
+ (_a = this.provider) === null || _a === void 0 ? void 0 : _a.closeConnection();
2098
+ this.provider = null;
2099
+ });
2036
2100
  return this.provider.connect(this.createConnectRequest(options === null || options === void 0 ? void 0 : options.request), {
2037
2101
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
2038
2102
  signal: abortController.signal
@@ -2093,6 +2157,10 @@ class TonConnect {
2093
2157
  (_b = this.provider) === null || _b === void 0 ? void 0 : _b.closeConnection();
2094
2158
  this.provider = provider;
2095
2159
  provider.listen(this.walletEventsListener.bind(this));
2160
+ abortController.signal.addEventListener('abort', () => {
2161
+ provider === null || provider === void 0 ? void 0 : provider.closeConnection();
2162
+ provider = null;
2163
+ });
2096
2164
  return yield callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2097
2165
  return provider === null || provider === void 0 ? void 0 : provider.restoreConnection({
2098
2166
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
@@ -2139,21 +2207,22 @@ class TonConnect {
2139
2207
  * Disconnect form thw connected wallet and drop current session.
2140
2208
  */
2141
2209
  disconnect(options) {
2142
- var _a, _b;
2210
+ var _a;
2143
2211
  return __awaiter(this, void 0, void 0, function* () {
2144
2212
  if (!this.connected) {
2145
2213
  throw new WalletNotConnectedError();
2146
2214
  }
2147
2215
  const abortController = createAbortController(options === null || options === void 0 ? void 0 : options.signal);
2148
- (_a = this.abortController) === null || _a === void 0 ? void 0 : _a.abort();
2216
+ const prevAbortController = this.abortController;
2149
2217
  this.abortController = abortController;
2150
2218
  if (abortController.signal.aborted) {
2151
2219
  throw new TonConnectError('Disconnect was aborted');
2152
2220
  }
2153
2221
  this.onWalletDisconnected();
2154
- yield ((_b = this.provider) === null || _b === void 0 ? void 0 : _b.disconnect({
2222
+ yield ((_a = this.provider) === null || _a === void 0 ? void 0 : _a.disconnect({
2155
2223
  signal: abortController.signal
2156
2224
  }));
2225
+ prevAbortController === null || prevAbortController === void 0 ? void 0 : prevAbortController.abort();
2157
2226
  });
2158
2227
  }
2159
2228
  /**
@@ -2188,12 +2257,12 @@ class TonConnect {
2188
2257
  this.pauseConnection();
2189
2258
  }
2190
2259
  else {
2191
- this.unPauseConnection();
2260
+ this.unPauseConnection().catch(e => logError('Cannot unpause connection', e));
2192
2261
  }
2193
2262
  });
2194
2263
  }
2195
2264
  catch (e) {
2196
- console.error('Cannot subscribe to the document.visibilitychange: ', e);
2265
+ logError('Cannot subscribe to the document.visibilitychange: ', e);
2197
2266
  }
2198
2267
  }
2199
2268
  createProvider(wallet) {