]> git.codecow.com Git - libnemo.git/commitdiff
Await device close.
authorChris Duncan <chris@zoso.dev>
Mon, 22 Sep 2025 21:17:45 +0000 (14:17 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 22 Sep 2025 21:17:45 +0000 (14:17 -0700)
src/lib/ledger.ts
src/lib/wallet/lock.ts

index a0fc5f0a6dbb6881336e2a6ad666e69a38457c6a..14062133bfdad0b424730805da0348b5497250fa 100644 (file)
@@ -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<void> {
                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()
+                       })
                })
        }
 
index e647655e0f8f3bc5ecea7273cc53d866c8084f83..55ad51465496d81eae94f291c401145ddf79f5d0 100644 (file)
@@ -8,7 +8,7 @@ import { Wallet } from '../wallet'
 export async function _lock (wallet: Wallet, vault: Vault): Promise<void> {
        try {
                if (wallet.type === 'Ledger') {
-                       Ledger.disconnect()
+                       await Ledger.disconnect()
                } else {
                        await vault.request({
                                action: 'lock'