Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions projects/modal-sign-html/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ export class WalletConnectModalSign {
// -- public ------------------------------------------------------------
public async connect(args: WalletConnectModalSignConnectArguments) {
const { requiredNamespaces, optionalNamespaces } = args
let unsubscribeModal: (() => void) | null = null

return new Promise<WalletConnectModalSignSession>(async (resolve, reject) => {
await this.#initSignClient()

const unsubscribeModal = this.#modal.subscribeModal(state => {
if (!state.open) {
unsubscribeModal()
reject(new Error('Modal closed'))
}
const modalPromise = new Promise<never>((_, modalReject) => {
unsubscribeModal = this.#modal.subscribeModal(state => {
if (!state.open) {
modalReject(new Error('Modal closed'))
}
})
})

const { uri, approval } = await this.#signClient!.connect(args)
Expand All @@ -74,12 +76,12 @@ export class WalletConnectModalSign {
}

try {
const session = await approval()
const session = await Promise.race([modalPromise, approval()])
resolve(session)
} catch (err) {
reject(err)
} finally {
unsubscribeModal()
unsubscribeModal?.()
this.#modal.closeModal()
}
})
Expand Down