diff --git a/background/services/provider-bridge/index.ts b/background/services/provider-bridge/index.ts index 2a60835b..33c2fa81 100644 --- a/background/services/provider-bridge/index.ts +++ b/background/services/provider-bridge/index.ts @@ -353,11 +353,24 @@ export default class ProviderBridgeService extends BaseService { permissionRequest: PermissionRequest ): Promise { this.emitter.emit("requestPermission", permissionRequest) - await showExtensionPopup(AllowedQueryParamPage.dappPermission) - - return new Promise((resolve) => { + const permissionPromise = new Promise((resolve) => { this.#pendingPermissionsRequests[permissionRequest.origin] = resolve + + showExtensionPopup(AllowedQueryParamPage.dappPermission, {}, () => { + resolve("Time to move on") + }) }) + + const result = await permissionPromise + + if (this.#pendingPermissionsRequests[permissionRequest.origin]) { + // Just in case this is a different promise, go ahead and resolve it with + // the same result. + this.#pendingPermissionsRequests[permissionRequest.origin](result) + delete this.#pendingPermissionsRequests[permissionRequest.origin] + } + + return result } async grantPermission(permission: PermissionRequest): Promise { @@ -374,11 +387,7 @@ export default class ProviderBridgeService extends BaseService { } await this.db.setPermission(permission) - - if (this.#pendingPermissionsRequests[permission.origin]) { - this.#pendingPermissionsRequests[permission.origin](permission) - delete this.#pendingPermissionsRequests[permission.origin] - } + this.#pendingPermissionsRequests[permission.origin]?.(permission) } async denyOrRevokePermission(permission: PermissionRequest): Promise { diff --git a/background/services/provider-bridge/show-popup.ts b/background/services/provider-bridge/show-popup.ts index 54d81dd5..ed090029 100644 --- a/background/services/provider-bridge/show-popup.ts +++ b/background/services/provider-bridge/show-popup.ts @@ -3,7 +3,8 @@ import { AllowedQueryParamPageType } from "@pelagus-provider/provider-bridge-sha export default async function showExtensionPopup( url: AllowedQueryParamPageType, - additionalOptions: { [key: string]: string } = {} + additionalOptions: { [key: string]: string } = {}, + onClose?: () => void, ): Promise { const { left = 0, top, width = 1920 } = await browser.windows.getCurrent() const popupWidth = 384 @@ -24,5 +25,19 @@ export default async function showExtensionPopup( focused: true, } - return browser.windows.create(params) + + const window = await browser.windows.create(params) + + if (onClose !== undefined) { + const listener = (windowId: number) => { + if (windowId === window.id) { + onClose() + + browser.windows.onRemoved.removeListener(listener) + } + } + browser.windows.onRemoved.addListener(listener) + } + + return window } diff --git a/ui/pages/DAppConnectRequest.tsx b/ui/pages/DAppConnectRequest.tsx index 9a8fc852..f3f970bc 100644 --- a/ui/pages/DAppConnectRequest.tsx +++ b/ui/pages/DAppConnectRequest.tsx @@ -50,7 +50,7 @@ export default function DAppConnectRequest(): ReactElement { ) } window.onbeforeunload = null - window.close() + setTimeout(() => window.close(), 300) }, [dispatch, permission, currentAccountTotal]) const deny = useCallback(async () => {