Skip to content

Commit

Permalink
Pull minor taho updates + add window close timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuddi authored and rileystephens28 committed Nov 1, 2024
1 parent da4aca3 commit 4ece1ee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
25 changes: 17 additions & 8 deletions background/services/provider-bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,24 @@ export default class ProviderBridgeService extends BaseService<Events> {
permissionRequest: PermissionRequest
): Promise<unknown> {
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<void> {
Expand All @@ -374,11 +387,7 @@ export default class ProviderBridgeService extends BaseService<Events> {
}

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<void> {
Expand Down
19 changes: 17 additions & 2 deletions background/services/provider-bridge/show-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<browser.Windows.Window> {
const { left = 0, top, width = 1920 } = await browser.windows.getCurrent()
const popupWidth = 384
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion ui/pages/DAppConnectRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 4ece1ee

Please sign in to comment.