From: Chris Duncan Date: Mon, 18 May 2026 21:13:59 +0000 (-0700) Subject: Simplify device support check and status getter. Replace status setting function... X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=32acfbe2c6bbb572e45533dee76a994313055bec;p=libnemo.git Simplify device support check and status getter. Replace status setting function with internal-only setter. --- diff --git a/src/lib/ledger/index.ts b/src/lib/ledger/index.ts index d016e81..487f2b1 100644 --- a/src/lib/ledger/index.ts +++ b/src/lib/ledger/index.ts @@ -68,6 +68,7 @@ export const STATUS_CODES: Readonly> = 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 { - 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' } } }