Browse Source

Simplify listenDevices

master
Gaëtan Renaudeau 7 years ago
parent
commit
caa1930180
  1. 1
      README.md
  2. 57
      src/commands/listenDevices.js
  3. 3
      src/config/constants.js

1
README.md

@ -96,7 +96,6 @@ SYNC_ALL_INTERVAL=60000
CHECK_APP_INTERVAL_WHEN_INVALID=600
CHECK_APP_INTERVAL_WHEN_VALID=1200
CHECK_UPDATE_DELAY=5000
DEVICE_DISCONNECT_DEBOUNCE=500
```
### Launch storybook

57
src/commands/listenDevices.js

@ -4,58 +4,17 @@ import logger from 'logger'
import { createCommand } from 'helpers/ipc'
import { Observable } from 'rxjs'
import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import { DEVICE_DISCONNECT_DEBOUNCE, LISTEN_DEVICES_POLLING_INTERVAL } from 'config/constants'
import { LISTEN_DEVICES_DEBOUNCE } from 'config/constants'
CommNodeHid.setListenDevicesPollingInterval(LISTEN_DEVICES_POLLING_INTERVAL)
CommNodeHid.setListenDevicesDebug(true)
CommNodeHid.setListenDevicesPollingInterval(LISTEN_DEVICES_DEBOUNCE)
const cmd = createCommand('listenDevices', () =>
Observable.create(o => {
const pendingRemovePerPath = {}
const sub = CommNodeHid.listen({
next: e => {
// debounce the add/remove in case we see quick `remove,add` events on same path.
switch (e.type) {
case 'add': {
const pendingRemove = pendingRemovePerPath[e.descriptor]
if (pendingRemove) {
logger.warn(`Skipping remove/add usb event for ${e.descriptor}`)
// there where a recent "remove" event, we don't emit add because we didn't emit "remove" yet.
clearTimeout(pendingRemove)
delete pendingRemovePerPath[e.descriptor]
} else {
// if there were no recent "remove", we just emit the "add"
o.next(e)
}
break
}
case 'remove': {
// we we always debounce the "remove" event. emit it a bit later in case a "add" of same descriptor happen soon.
if (pendingRemovePerPath[e.descriptor]) {
clearTimeout(pendingRemovePerPath[e.descriptor])
}
pendingRemovePerPath[e.descriptor] = setTimeout(() => {
delete pendingRemovePerPath[e.descriptor]
o.next(e)
}, DEVICE_DISCONNECT_DEBOUNCE)
break
}
default:
o.next(e)
}
},
complete: () => {
o.complete()
},
error: err => {
o.error(err)
},
})
return () => {
Object.keys(pendingRemovePerPath).map(k => clearTimeout(pendingRemovePerPath[k]))
sub.unsubscribe()
}
CommNodeHid.setListenDevicesDebug((msg, ...args) =>
logger.debug(msg, {
type: 'listenDevices',
args,
}),
)
const cmd = createCommand('listenDevices', () => Observable.create(CommNodeHid.listen))
export default cmd

3
src/config/constants.js

@ -32,13 +32,12 @@ export const MIN_HEIGHT = intFromEnv('LEDGER_MIN_HEIGHT', 700)
export const CHECK_APP_INTERVAL_WHEN_INVALID = 600
export const CHECK_APP_INTERVAL_WHEN_VALID = 1200
export const CHECK_UPDATE_DELAY = 5000
export const DEVICE_DISCONNECT_DEBOUNCE = intFromEnv('LEDGER_DEVICE_DISCONNECT_DEBOUNCE', 1000)
export const DEVICE_INFOS_TIMEOUT = intFromEnv('DEVICE_INFOS_TIMEOUT', 5 * 1000)
export const GENUINE_CACHE_DELAY = intFromEnv('GENUINE_CACHE_DELAY', 1000)
export const GENUINE_TIMEOUT = intFromEnv('GENUINE_TIMEOUT', 120 * 1000)
export const GET_CALLS_RETRY = intFromEnv('GET_CALLS_RETRY', 2)
export const GET_CALLS_TIMEOUT = intFromEnv('GET_CALLS_TIMEOUT', 30 * 1000)
export const LISTEN_DEVICES_POLLING_INTERVAL = intFromEnv('LISTEN_DEVICES_POLLING_INTERVAL', 1000)
export const LISTEN_DEVICES_DEBOUNCE = intFromEnv('LISTEN_DEVICES_DEBOUNCE', 200)
// NB: technically speaking OUTDATED_CONSIDERED_DELAY should be set to ZERO.
// but we'll only do that when we're sure the sync is performant and all is working smoothly
export const OUTDATED_CONSIDERED_DELAY = intFromEnv('OUTDATED_CONSIDERED_DELAY', 2 * 60 * 1000)

Loading…
Cancel
Save