From 097a5093ed10c5d7176e4dd4987037ea996e851b Mon Sep 17 00:00:00 2001 From: Wonhee Lee Date: Mon, 2 Dec 2024 20:24:11 +0900 Subject: [PATCH] fix: correct error handling in connect method --- projects/modal-sign-html/src/client.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/projects/modal-sign-html/src/client.ts b/projects/modal-sign-html/src/client.ts index 398e6cc..0f42d3e 100644 --- a/projects/modal-sign-html/src/client.ts +++ b/projects/modal-sign-html/src/client.ts @@ -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(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((_, modalReject) => { + unsubscribeModal = this.#modal.subscribeModal(state => { + if (!state.open) { + modalReject(new Error('Modal closed')) + } + }) }) const { uri, approval } = await this.#signClient!.connect(args) @@ -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() } })