From: Chris Duncan Date: Mon, 22 Sep 2025 21:17:45 +0000 (-0700) Subject: Await device close. X-Git-Tag: v0.10.5~12^2~10 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=14c79e60f11358c0266a2d9a50aa5c030b09f37d;p=libnemo.git Await device close. --- 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'