]> git.codecow.com Git - libnemo.git/commitdiff
Extract Ledger polling. next/fta
authorChris Duncan <chris@codecow.com>
Mon, 18 May 2026 21:42:28 +0000 (14:42 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 18 May 2026 21:42:28 +0000 (14:42 -0700)
src/lib/ledger/index.ts
src/lib/ledger/poll.ts [new file with mode: 0644]

index 23d4a4d736498335eec236bcf5b9623daeb0f931..cda83f7c159d3e64c8cb8a299b96190c867ea893 100644 (file)
@@ -16,6 +16,7 @@ import { _connect } from './connect'
 import { _disconnect } from './disconnect'
 import { LedgerEvent } from './event'
 import { _open } from './open'
+import { _poll } from './poll'
 import { queue } from './queue'
 import { _sign } from './sign'
 import { _verify } from './verify'
@@ -153,9 +154,7 @@ export class Ledger {
                                this.#transport = TransportBLE
                        }
                        if (api === 'usb' && this.#transport !== TransportUSB) {
-                               this.#transport = typeof navigator.hid?.getDevices === 'function'
-                                       ? TransportHID
-                                       : TransportUSB
+                               this.#transport = TransportUSB
                        }
                }
                const status = await queue(async () => _connect(this.#transport))
@@ -172,7 +171,7 @@ export class Ledger {
         * connection updates.
         */
        static async disconnect (): Promise<void> {
-               queue(async () => {
+               return queue(async () => {
                        _disconnect()
                        this.#isPolling = false
                        this.status = 'DISCONNECTED'
@@ -316,21 +315,7 @@ export class Ledger {
         * device.
         */
        static async #poll (): Promise<void> {
-               try {
-                       const isHidPaired = (await navigator.hid?.getDevices?.() ?? [])
-                               .some(device => device.vendorId === LEDGER_VENDOR_ID)
-                       const isUsbPaired = (await navigator.usb?.getDevices?.() ?? [])
-                               .some(device => device.vendorId === LEDGER_VENDOR_ID)
-                       if ((this.#transport === TransportHID && isHidPaired)
-                               || (this.#transport === TransportUSB && isUsbPaired)) {
-                               await this.connect()
-                       } else {
-                               this.status = 'DISCONNECTED'
-                       }
-                       this.#isPolling ? setTimeout(() => this.#poll(), 500) : void 0
-               } catch {
-                       console.warn('Error polling Ledger device')
-                       this.status = 'DISCONNECTED'
-               }
+               this.status = await queue(async () => _poll(this.#transport))
+               this.#isPolling ? setTimeout(() => this.#poll(), 500) : void 0
        }
 }
diff --git a/src/lib/ledger/poll.ts b/src/lib/ledger/poll.ts
new file mode 100644 (file)
index 0000000..c9a61f7
--- /dev/null
@@ -0,0 +1,24 @@
+//! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
+//! SPDX-License-Identifier: GPL-3.0-or-later
+
+import TransportHID from '@ledgerhq/hw-transport-webhid'
+import TransportUSB from '@ledgerhq/hw-transport-webusb'
+import { LEDGER_VENDOR_ID, Ledger, LedgerStatus, LedgerTransport } from '.'
+
+export async function _poll (transport: LedgerTransport): Promise<LedgerStatus> {
+       let status: LedgerStatus = 'DISCONNECTED'
+       try {
+               const isHidPaired = (await navigator.hid?.getDevices?.() ?? [])
+                       .some(device => device.vendorId === LEDGER_VENDOR_ID)
+               const isUsbPaired = (await navigator.usb?.getDevices?.() ?? [])
+                       .some(device => device.vendorId === LEDGER_VENDOR_ID)
+               if ((transport === TransportHID && isHidPaired)
+                       || (transport === TransportUSB && isUsbPaired)) {
+                       status = await Ledger.connect()
+               }
+       } catch {
+               console.warn('Error polling Ledger device')
+       } finally {
+               return status
+       }
+}