]> git.codecow.com Git - libnemo.git/commitdiff
Simplify device support check and status getter. Replace status setting function...
authorChris Duncan <chris@codecow.com>
Mon, 18 May 2026 21:13:59 +0000 (14:13 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 18 May 2026 21:13:59 +0000 (14:13 -0700)
src/lib/ledger/index.ts

index d016e819f5da097a2f020a87deb62711fa39fbfd..487f2b1b507525badf7201f93b13c841f7fe1599 100644 (file)
@@ -68,6 +68,7 @@ export const STATUS_CODES: Readonly<Record<number, string>> = Object.freeze({
  * https://github.com/roosmaa/ledger-app-nano/blob/master/doc/nano.md
  */
 export class Ledger {
+       static #isInternal: boolean = true
        static #isPolling: boolean = false
        static #status: LedgerStatus = 'DISCONNECTED'
        static #transport: typeof TransportHID | typeof TransportBLE | typeof TransportUSB
@@ -83,12 +84,6 @@ export class Ledger {
         * transport type according to the following priorities: HID, Bluetooth, USB.
         */
        static get isUnsupported (): boolean {
-               if (this.#status === 'UNSUPPORTED') {
-                       return true
-               }
-               if (this.#transport !== undefined) {
-                       return false
-               }
                console.log('Checking browser Ledger support...')
                if (typeof globalThis.navigator?.hid?.getDevices === 'function') {
                        this.#transport ??= TransportHID
@@ -102,7 +97,7 @@ export class Ledger {
                        this.#transport ??= TransportUSB
                        return false
                }
-               this.#setStatus('UNSUPPORTED')
+               this.status = 'UNSUPPORTED'
                return true
        }
 
@@ -112,7 +107,19 @@ export class Ledger {
         * UNSUPPORTED | DISCONNECTED | BUSY | LOCKED | CONNECTED
         */
        static get status (): LedgerStatus {
-               return this.isUnsupported ? 'UNSUPPORTED' : this.#status
+               return this.#status
+       }
+
+       /**
+        * Sets the Ledger status and emits an event. Only callable within the class.
+        */
+       static set status (value: LedgerStatus) {
+               if (#isInternal in this && this.#status !== value) {
+                       this.#status = value
+                       const event = new LedgerEvent('ledgerstatuschanged', value)
+                       this.dispatchEvent(event)
+                       console.log(event)
+               }
        }
 
        /**
@@ -138,7 +145,7 @@ export class Ledger {
         * - CONNECTED: Nano app is open and listening
         */
        static async connect (api?: 'hid' | 'ble' | 'usb'): Promise<LedgerStatus> {
-               if (api !== undefined || this.#status !== 'UNSUPPORTED') {
+               if (api !== undefined || !this.isUnsupported) {
                        if (api === 'hid' && this.#transport !== TransportHID) {
                                this.#transport = TransportHID
                        }
@@ -152,12 +159,12 @@ export class Ledger {
                        }
                }
                const status = await queue(async () => _connect(this.#transport))
-               this.#setStatus(status)
+               this.status = status
                if (!this.isUnsupported && !this.#isPolling) {
                        this.#isPolling = true
                        this.#poll().then(() => void 0, () => void 0)
                }
-               return this.#status
+               return this.status
        }
 
        /**
@@ -168,7 +175,7 @@ export class Ledger {
                queue(async () => {
                        _disconnect()
                        this.#isPolling = false
-                       this.#setStatus('DISCONNECTED')
+                       this.status = 'DISCONNECTED'
                })
        }
 
@@ -312,24 +319,12 @@ export class Ledger {
                                || (this.#transport === TransportUSB && isUsbPaired)) {
                                await this.connect()
                        } else {
-                               this.#setStatus('DISCONNECTED')
+                               this.status = 'DISCONNECTED'
                        }
                        this.#isPolling ? setTimeout(() => this.#poll(), 500) : void 0
                } catch {
                        console.warn('Error polling Ledger device')
-                       this.#setStatus('DISCONNECTED')
-               }
-       }
-
-       /**
-        * Sets the Ledger status and emits an event.
-        */
-       static #setStatus (value: LedgerStatus) {
-               if (this.#status !== value) {
-                       this.#status = value
-                       const event = new LedgerEvent('ledgerstatuschanged', value)
-                       this.dispatchEvent(event)
-                       console.log(event)
+                       this.status = 'DISCONNECTED'
                }
        }
 }