-
Notifications
You must be signed in to change notification settings - Fork 382
Only poll USB devices list when a change is detected #191
Changes from 9 commits
33a2dee
4442a98
f070025
8566412
9483bd9
199a2ee
9b5451b
3b897f3
83f13c3
c862d38
b98dfe6
c440e5e
64dad39
ae6f5c5
666f6ba
96c1e35
a1987cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
// @flow | ||
|
||
import EventEmitter from "events"; | ||
import usbDetect from "usb-detection"; | ||
import debounce from "lodash.debounce"; | ||
import getDevices from "./getDevices"; | ||
|
||
const VENDOR_ID = 11415; // Ledger's Vendor ID for filtering | ||
|
||
export default ( | ||
delay: number, | ||
listenDevicesPollingSkip: () => boolean | ||
listenDevicesPollingSkip: () => boolean, | ||
debug: (...any) => void | ||
): { | ||
events: EventEmitter, | ||
stop: () => void | ||
} => { | ||
const events = new EventEmitter(); | ||
events.setMaxListeners(0); | ||
|
||
let timeoutDetection; | ||
let listDevices = getDevices(); | ||
|
||
const flatDevice = d => d.path; | ||
|
@@ -27,37 +31,75 @@ export default ( | |
|
||
let lastDevices = getFlatDevices(); | ||
|
||
const checkDevices = () => { | ||
const poll = () => { | ||
if (!listenDevicesPollingSkip()) { | ||
const currentDevices = getFlatDevices(); | ||
debug("Polling for added or removed devices"); | ||
|
||
let changeFound = false; | ||
const currentDevices = getFlatDevices(); | ||
const newDevices = currentDevices.filter(d => !lastDevices.includes(d)); | ||
const removeDevices = lastDevices.filter( | ||
d => !currentDevices.includes(d) | ||
); | ||
|
||
if (newDevices.length > 0) { | ||
debug("New device found:", newDevices); | ||
|
||
listDevices = getDevices(); | ||
events.emit("add", getDeviceByPaths(newDevices)); | ||
|
||
changeFound = true; | ||
} else { | ||
debug("No new device found"); | ||
} | ||
|
||
const removeDevices = lastDevices.filter( | ||
d => !currentDevices.includes(d) | ||
); | ||
|
||
if (removeDevices.length > 0) { | ||
debug("Removed device found:", removeDevices); | ||
|
||
events.emit("remove", getDeviceByPaths(removeDevices)); | ||
listDevices = listDevices.filter( | ||
d => !removeDevices.includes(flatDevice(d)) | ||
); | ||
|
||
changeFound = true; | ||
} else { | ||
debug("No removed device found"); | ||
} | ||
|
||
lastDevices = currentDevices; | ||
if (changeFound) { | ||
lastDevices = currentDevices; | ||
} | ||
} else { | ||
debug("Polling skipped, re-debouncing"); | ||
debouncedPoll(); | ||
} | ||
setTimeout(checkDevices, delay); | ||
}; | ||
|
||
timeoutDetection = setTimeout(checkDevices, delay); | ||
const debouncedPoll = debounce(poll, delay); | ||
|
||
debug("Starting to monitor USB for ledger devices"); | ||
usbDetect.startMonitoring(); | ||
|
||
// Detect add | ||
usbDetect.on(`add:${VENDOR_ID}`, device => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should filter like it was here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pre-filter, taking advantage of the vendor ID filtering offered by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👌 |
||
debug("Device add detected:", device.deviceName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this device don't have the path ? 😢 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it doesn't 😭 MadLittleMods/node-usb-detection#19 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🆗 |
||
|
||
debouncedPoll(); | ||
}); | ||
|
||
// Detect remove | ||
usbDetect.on(`remove:${VENDOR_ID}`, device => { | ||
debug("Device removal detected:", device.deviceName); | ||
|
||
debouncedPoll(); | ||
}); | ||
|
||
return { | ||
stop: () => { | ||
clearTimeout(timeoutDetection); | ||
debug("Stopping USB monitoring"); | ||
debouncedPoll.cancel(); | ||
usbDetect.stopMonitoring(); | ||
}, | ||
events | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a lil detail, but I would prefer we depend on
lodash
and doingimport debounce from "lodash/debounce"
, like on live