From 14c79e60f11358c0266a2d9a50aa5c030b09f37d Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 22 Sep 2025 14:17:45 -0700 Subject: [PATCH] Await device close. --- src/lib/ledger.ts | 35 +++++++++++++++++++++-------------- src/lib/wallet/lock.ts | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/lib/ledger.ts b/src/lib/ledger.ts index a0fc5f0..1406213 100644 --- a/src/lib/ledger.ts +++ b/src/lib/ledger.ts @@ -189,28 +189,35 @@ export class Ledger { : TransportUSB } } - const status = await this.#connect() - if (!this.isUnsupported && !this.#isPolling) { - this.#isPolling = true - this.#poll().catch(() => { }) + try { + return await this.#connect() + } catch (err) { + throw new Error('Ledger.connect()', { cause: err }) + } finally { + if (!this.isUnsupported && !this.#isPolling) { + this.#isPolling = true + this.#poll().catch(() => { }) + } } - return status } /** * Clears Ledger connections from HID and USB interfaces and stops polling for * connection updates. */ - static disconnect (): void { + static async disconnect (): Promise { this.#isPolling = false - setTimeout(async () => { - const hidDevices = (await navigator?.hid?.getDevices?.() ?? []) - .filter(device => device.vendorId === this.ledgerVendorId) - .map(device => device.close()) - const usbDevices = (await navigator?.usb?.getDevices?.() ?? []) - .filter(device => device.vendorId === this.ledgerVendorId) - .map(device => device.close()) - this.#status = 'DISCONNECTED' + return new Promise(resolve => { + setTimeout(async () => { + await (await navigator?.hid?.getDevices?.() ?? []) + .filter(device => device.vendorId === this.ledgerVendorId) + .map(device => device.close()) + await (await navigator?.usb?.getDevices?.() ?? []) + .filter(device => device.vendorId === this.ledgerVendorId) + .map(device => device.close()) + this.#status = 'DISCONNECTED' + resolve() + }) }) } diff --git a/src/lib/wallet/lock.ts b/src/lib/wallet/lock.ts index e647655..55ad514 100644 --- a/src/lib/wallet/lock.ts +++ b/src/lib/wallet/lock.ts @@ -8,7 +8,7 @@ import { Wallet } from '../wallet' export async function _lock (wallet: Wallet, vault: Vault): Promise { try { if (wallet.type === 'Ledger') { - Ledger.disconnect() + await Ledger.disconnect() } else { await vault.request({ action: 'lock' -- 2.47.3