From: Chris Duncan Date: Thu, 18 Sep 2025 03:56:57 +0000 (-0700) Subject: Remove extra event listeners. X-Git-Tag: v0.10.5~12^2~41 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=25d31d723648f89b5552ff0b053c43394bef3924;p=libnemo.git Remove extra event listeners. --- diff --git a/src/lib/ledger.ts b/src/lib/ledger.ts index 1904907..75d3a3d 100644 --- a/src/lib/ledger.ts +++ b/src/lib/ledger.ts @@ -43,7 +43,7 @@ interface LedgerSignResponse extends LedgerResponse { export class Ledger { static #listenTimeout: 30000 = 30000 static #openTimeout: 3000 = 3000 - static #polling: number | NodeJS.Timeout = 0 + static #polling: number | NodeJS.Timeout static #status: LedgerStatus = 'DISCONNECTED' static #transport: typeof TransportHID | typeof TransportBLE | typeof TransportUSB static #ADPU_CODES: { [key: string]: number } = Object.freeze({ @@ -176,33 +176,28 @@ export class Ledger { } try { const version = await this.#version() - globalThis.navigator?.hid?.addEventListener('connect', console.log) - globalThis.navigator?.usb?.addEventListener('connect', console.log) - globalThis.navigator?.hid?.addEventListener('disconnect', console.log) - globalThis.navigator?.usb?.addEventListener('disconnect', console.log) - globalThis.navigator?.hid?.addEventListener('connect', this.#onConnectHid) - globalThis.navigator?.usb?.addEventListener('connect', this.#onConnectUsb) - globalThis.navigator?.hid?.addEventListener('disconnect', this.#onDisconnectHid) - globalThis.navigator?.usb?.addEventListener('disconnect', this.#onDisconnectUsb) if (version.status !== 'OK') { this.#status = 'DISCONNECTED' - } else if (version.name === 'Nano') { - const { status } = await this.account() - if (status === 'OK') { - this.#status = 'CONNECTED' - } else if (status === 'SECURITY_STATUS_NOT_SATISFIED') { - this.#status = 'LOCKED' + } else { + this.#polling ??= setInterval(() => this.connect(), 1000) + if (version.name === 'Nano') { + const { status } = await this.account() + if (status === 'OK') { + this.#status = 'CONNECTED' + } else if (status === 'SECURITY_STATUS_NOT_SATISFIED') { + this.#status = 'LOCKED' + } else { + this.#status = 'DISCONNECTED' + } } else { - this.#status = 'DISCONNECTED' + this.#status = 'BUSY' } - } else { - this.#status = 'BUSY' } } catch (err) { console.error(err) + clearInterval(this.#polling) this.#status = 'DISCONNECTED' } - console.log(this.status) return this.status } @@ -210,6 +205,7 @@ export class Ledger { * Clears Ledger connections from all device interfaces. */ static disconnect (): void { + clearInterval(this.#polling) setTimeout(async () => { const hidDevices = await globalThis.navigator?.hid?.getDevices?.() ?? [] for (const device of hidDevices) { @@ -407,60 +403,6 @@ export class Ledger { return new Promise(r => setTimeout(r, 1000, { status: this.#STATUS_CODES[response] })) } - static #onConnectHid = async (e: HIDConnectionEvent): Promise => { - console.log('onConnectHid') - console.log(e) - if (e.device?.vendorId === this.UsbVendorId) { - console.log('Ledger connected via HID') - await this.connect() - this.#polling = setInterval(this.connect, 1000) - const { hid } = globalThis.navigator - hid.addEventListener('disconnect', this.#onDisconnectHid) - hid.removeEventListener('connect', this.#onConnectHid) - } - } - - static #onDisconnectHid = async (e: HIDConnectionEvent): Promise => { - console.log('onDisconnectHid') - console.log(e) - if (e.device?.vendorId === this.UsbVendorId) { - console.log('Ledger disconnected via HID') - clearInterval(this.#polling) - await this.connect() - const { hid } = globalThis.navigator - hid.addEventListener('connect', this.#onConnectHid) - hid.removeEventListener('disconnect', this.#onDisconnectHid) - this.#status = 'DISCONNECTED' - } - } - - static #onConnectUsb = async (e: USBConnectionEvent): Promise => { - console.log('onConnectUsb') - console.log(e) - if (e.device?.vendorId === this.UsbVendorId) { - console.log('Ledger connected via USB') - await this.connect() - this.#polling = setInterval(this.connect, 1000) - const { usb } = globalThis.navigator - usb.addEventListener('disconnect', this.#onDisconnectUsb) - usb.removeEventListener('connect', this.#onConnectUsb) - } - } - - static #onDisconnectUsb = async (e: USBConnectionEvent): Promise => { - console.log('onDisconnectUsb') - console.log(e) - if (e.device?.vendorId === this.UsbVendorId) { - console.log('Ledger disconnected via USB') - clearInterval(this.#polling) - await this.connect() - const { usb } = globalThis.navigator - usb.addEventListener('connect', this.#onConnectUsb) - usb.removeEventListener('disconnect', this.#onDisconnectUsb) - this.#status = 'DISCONNECTED' - } - } - /** * Open the Nano app by launching a user flow. *