Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct error handling in connect method #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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