Skip to content

Commit

Permalink
Merge pull request #166 from getamis/ASL-4232/wait-until-ready-will-n…
Browse files Browse the repository at this point in the history
…ot-cancel

[ASL-4232] fixed userRejectedRequest will not trigger
  • Loading branch information
VANITAX authored Jun 1, 2023
2 parents d5dd5ee + 02bd1b9 commit cd00e65
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
37 changes: 29 additions & 8 deletions packages/browser/src/middlewares/PopupWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import BrowserBridge from '../utils/BrowserBridge';
import Modal from '../ui/Modal';

const DETECT_IF_POPUP_WINDOW_CLOSED_INTERVAL_MS = 500;
const WAITING_FOR_WALLET_TIMEOUT_LIMIT_MIN = 5;

class PopupWindow implements Messenger {
public bridge: BrowserBridge;
Expand All @@ -31,7 +32,6 @@ class PopupWindow implements Messenger {
this.walletUrl = walletUrl;
this.apiConfig = apiConfig;
this.disableFastSignup = disableFastSignup;

this.newWindowReminderModal = new Modal({
description: t('popup-window-hint'),
cancelText: t('no'),
Expand Down Expand Up @@ -136,6 +136,10 @@ class PopupWindow implements Messenger {
}
};

private successHandler?: () => void;
private cancelHandler?: () => void;
private waitUntilReadyTimeout?: NodeJS.Timeout;

private waitUntilReady = (): Promise<void> => {
const { bridge, proxy, isReady, show } = this;
return new Promise((resolve, reject) => {
Expand All @@ -144,13 +148,30 @@ class PopupWindow implements Messenger {
return;
}

bridge.once(BridgeEvent.ready, resolve);
show({
onReminderModalCancel: () => {
bridge.removeListener(BridgeEvent.ready, resolve);
reject();
},
});
this.successHandler = () => {
if (!this?.cancelHandler) return;
bridge.removeListener(BridgeEvent.hide, this.cancelHandler);
resolve();
};

this.cancelHandler = () => {
if (!this?.successHandler) return;
bridge.removeListener(BridgeEvent.ready, this.successHandler);
reject();
};

bridge.once(BridgeEvent.ready, this.successHandler);
bridge.once(BridgeEvent.hide, this.cancelHandler);
show({ onReminderModalCancel: this.cancelHandler });

if (this.waitUntilReadyTimeout) clearTimeout(this.waitUntilReadyTimeout);
this.waitUntilReadyTimeout = setTimeout(() => {
if (!this?.cancelHandler || !this.successHandler) return;
bridge.removeListener(BridgeEvent.hide, this.cancelHandler);
bridge.removeListener(BridgeEvent.ready, this.successHandler);
reject();
if (this.waitUntilReadyTimeout) clearTimeout(this.waitUntilReadyTimeout);
}, 1000 * 60 * WAITING_FOR_WALLET_TIMEOUT_LIMIT_MIN);
});
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd00e65

Please sign in to comment.