@eos3/connect 0.2.0 → 0.2.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/dist/ui.js +82 -4
- package/package.json +1 -1
package/dist/ui.js
CHANGED
|
@@ -348,6 +348,7 @@ const EOS_CONNECT_UI_MESSAGES = {
|
|
|
348
348
|
closeEosConnect: '关闭 EOS Connect'
|
|
349
349
|
}
|
|
350
350
|
};
|
|
351
|
+
const EOS_CONNECT_QUICK_PAY_REFRESH_INTERVAL_MS = 1500;
|
|
351
352
|
function normalizeEosConnectUiLocale(locale) {
|
|
352
353
|
return String(locale ?? '').toLowerCase().startsWith('zh') ? 'zh-CN' : 'en';
|
|
353
354
|
}
|
|
@@ -471,6 +472,9 @@ function isQuickPaymentReadyView(view) {
|
|
|
471
472
|
function isQuickPaymentReadyState(state) {
|
|
472
473
|
return state.status === 'connected' && state.canUse === true;
|
|
473
474
|
}
|
|
475
|
+
function isQuickPaymentTerminalView(view) {
|
|
476
|
+
return view?.status === 'error' || view?.status === 'unsupported';
|
|
477
|
+
}
|
|
474
478
|
function globalTelegramWebApp() {
|
|
475
479
|
const maybeWindow = globalThis;
|
|
476
480
|
return maybeWindow.window?.Telegram?.WebApp ?? maybeWindow.Telegram?.WebApp ?? null;
|
|
@@ -565,6 +569,8 @@ function createModalElement(options) {
|
|
|
565
569
|
unsubscribe = null;
|
|
566
570
|
client = null;
|
|
567
571
|
flowState = 'idle';
|
|
572
|
+
flowPromise = null;
|
|
573
|
+
quickPayRefreshTimer = null;
|
|
568
574
|
connectedCallback() {
|
|
569
575
|
this.client = resolveClient(this, options.client, options);
|
|
570
576
|
this.render();
|
|
@@ -573,15 +579,22 @@ function createModalElement(options) {
|
|
|
573
579
|
dispatchEosConnectError(this, { error });
|
|
574
580
|
});
|
|
575
581
|
this.addEventListener('click', this.handleClick);
|
|
582
|
+
globalThis.addEventListener?.('focus', this.handleForeground);
|
|
583
|
+
globalThis.document?.addEventListener?.('visibilitychange', this.handleForeground);
|
|
576
584
|
}
|
|
577
585
|
disconnectedCallback() {
|
|
586
|
+
this.clearQuickPayRefresh();
|
|
578
587
|
this.unsubscribe?.();
|
|
579
588
|
this.removeEventListener('click', this.handleClick);
|
|
589
|
+
globalThis.removeEventListener?.('focus', this.handleForeground);
|
|
590
|
+
globalThis.document?.removeEventListener?.('visibilitychange', this.handleForeground);
|
|
580
591
|
}
|
|
581
592
|
handleClick = (event) => {
|
|
582
593
|
const target = event.target;
|
|
583
594
|
if (target?.closest('[data-eos-connect-close]')) {
|
|
584
595
|
this.flowState = 'idle';
|
|
596
|
+
this.flowPromise = null;
|
|
597
|
+
this.clearQuickPayRefresh();
|
|
585
598
|
this.setAttribute('hidden', '');
|
|
586
599
|
this.dispatchEvent(new CustomEvent('eos-connect-close', { bubbles: true, composed: true }));
|
|
587
600
|
return;
|
|
@@ -593,10 +606,14 @@ function createModalElement(options) {
|
|
|
593
606
|
return;
|
|
594
607
|
}
|
|
595
608
|
if (provider === 'telegram') {
|
|
609
|
+
if (this.flowState !== 'idle' || this.flowPromise) {
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
596
612
|
this.flowState = 'checking';
|
|
597
613
|
this.render();
|
|
614
|
+
this.scheduleQuickPayRefresh();
|
|
598
615
|
}
|
|
599
|
-
|
|
616
|
+
this.flowPromise = connectEosProvider(this.client, provider, {
|
|
600
617
|
...options.telegramOptions,
|
|
601
618
|
botUsername: options.telegramOptions?.botUsername ?? options.botUsername,
|
|
602
619
|
assetLimits: options.telegramOptions?.assetLimits ?? options.assetLimits,
|
|
@@ -604,20 +621,81 @@ function createModalElement(options) {
|
|
|
604
621
|
openExternal: options.openExternal
|
|
605
622
|
}).then((view) => {
|
|
606
623
|
if (provider === 'telegram') {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
624
|
+
if (isQuickPaymentReadyView(view) || isQuickPaymentReadyState(this.client.getSnapshot())) {
|
|
625
|
+
this.showQuickPaySuccess();
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
if (isQuickPaymentTerminalView(view)) {
|
|
629
|
+
this.flowState = 'idle';
|
|
630
|
+
this.clearQuickPayRefresh();
|
|
631
|
+
this.render();
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
this.flowState = 'checking';
|
|
610
635
|
this.render();
|
|
636
|
+
this.scheduleQuickPayRefresh();
|
|
611
637
|
}
|
|
612
638
|
}).catch((error) => {
|
|
613
639
|
if (provider === 'telegram') {
|
|
614
640
|
this.flowState = 'idle';
|
|
641
|
+
this.clearQuickPayRefresh();
|
|
615
642
|
this.render();
|
|
616
643
|
}
|
|
617
644
|
dispatchEosConnectError(this, { error, provider });
|
|
645
|
+
}).finally(() => {
|
|
646
|
+
this.flowPromise = null;
|
|
618
647
|
});
|
|
619
648
|
}
|
|
620
649
|
};
|
|
650
|
+
handleForeground = () => {
|
|
651
|
+
if (globalThis.document?.visibilityState && globalThis.document.visibilityState !== 'visible') {
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
if (this.flowState !== 'checking') {
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
this.clearQuickPayRefresh();
|
|
658
|
+
void this.refreshQuickPayStatus();
|
|
659
|
+
};
|
|
660
|
+
scheduleQuickPayRefresh(delayMs = EOS_CONNECT_QUICK_PAY_REFRESH_INTERVAL_MS) {
|
|
661
|
+
if (!this.client || this.flowState !== 'checking' || this.quickPayRefreshTimer) {
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
this.quickPayRefreshTimer = globalThis.setTimeout(() => {
|
|
665
|
+
this.quickPayRefreshTimer = null;
|
|
666
|
+
void this.refreshQuickPayStatus();
|
|
667
|
+
}, delayMs);
|
|
668
|
+
}
|
|
669
|
+
clearQuickPayRefresh() {
|
|
670
|
+
if (this.quickPayRefreshTimer) {
|
|
671
|
+
globalThis.clearTimeout(this.quickPayRefreshTimer);
|
|
672
|
+
this.quickPayRefreshTimer = null;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
async refreshQuickPayStatus() {
|
|
676
|
+
if (!this.client || this.flowState !== 'checking') {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
try {
|
|
680
|
+
const quickPay = await this.client.checkQuickPay();
|
|
681
|
+
if (this.flowState !== 'checking') {
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
if (quickPay.enabled) {
|
|
685
|
+
this.showQuickPaySuccess();
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
catch (error) {
|
|
690
|
+
dispatchEosConnectError(this, { error, provider: 'telegram' });
|
|
691
|
+
}
|
|
692
|
+
this.scheduleQuickPayRefresh();
|
|
693
|
+
}
|
|
694
|
+
showQuickPaySuccess() {
|
|
695
|
+
this.flowState = 'success';
|
|
696
|
+
this.clearQuickPayRefresh();
|
|
697
|
+
this.render();
|
|
698
|
+
}
|
|
621
699
|
render() {
|
|
622
700
|
if (!this.client)
|
|
623
701
|
return;
|