Skip to content

Commit

Permalink
add USB connection wait
Browse files Browse the repository at this point in the history
  • Loading branch information
mholtzman committed Oct 6, 2021
1 parent 3aa1d45 commit dcdf9fa
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion main/signers/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { EventEmitter } from 'stream'
import usb from 'usb'
import HID from 'node-hid'

function wait (ms: number) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}

export class SignerAdapter extends EventEmitter {
adapterType: string;

Expand Down Expand Up @@ -35,7 +41,14 @@ export class UsbSignerAdapter extends SignerAdapter {
constructor (name: string) {
super(name)

this.attachListener = usbDevice => {
this.attachListener = async usbDevice => {
// it seems like the 'attach' event can be fired just before
// the USB device is ready to be read from, 200ms seems to
// always be enough time to wait
// TODO: is there a low-level programmatic way to try to communicate
// with the USB device and wait until its ready?
await wait(200)

if (this.supportsDevice(usbDevice)) {
this.handleAttachedDevice(usbDevice)
}
Expand Down

0 comments on commit dcdf9fa

Please sign in to comment.