UNPKG

4.01 kBJavaScriptView Raw
1var __read = (this && this.__read) || function (o, n) {
2 var m = typeof Symbol === "function" && o[Symbol.iterator];
3 if (!m) return o;
4 var i = m.call(o), r, ar = [], e;
5 try {
6 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7 }
8 catch (error) { e = { error: error }; }
9 finally {
10 try {
11 if (r && !r.done && (m = i["return"])) m.call(i);
12 }
13 finally { if (e) throw e.error; }
14 }
15 return ar;
16};
17var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18 if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19 if (ar || !(i in from)) {
20 if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21 ar[i] = from[i];
22 }
23 }
24 return to.concat(ar || Array.prototype.slice.call(from));
25};
26import EventEmitter from "events";
27import { getDevices } from "@ledgerhq/hw-transport-node-hid-noevents";
28import { log } from "@ledgerhq/logs";
29import usb from "usb";
30import debounce from "lodash/debounce";
31export default (function (delay, listenDevicesPollingSkip) {
32 var events = new EventEmitter();
33 events.setMaxListeners(0);
34 var listDevices = getDevices();
35 var flatDevice = function (d) { return d.path; };
36 var getFlatDevices = function () { return __spreadArray([], __read(new Set(getDevices().map(function (d) { return flatDevice(d); }))), false); };
37 var getDeviceByPaths = function (paths) {
38 return listDevices.find(function (d) { return paths.includes(flatDevice(d)); });
39 };
40 var lastDevices = getFlatDevices();
41 var poll = function () {
42 if (!listenDevicesPollingSkip()) {
43 log("hid-listen", "Polling for added or removed devices");
44 var changeFound = false;
45 var currentDevices_1 = getFlatDevices();
46 var newDevices = currentDevices_1.filter(function (d) { return !lastDevices.includes(d); });
47 if (newDevices.length > 0) {
48 log("hid-listen", "New device found:", newDevices);
49 listDevices = getDevices();
50 events.emit("add", getDeviceByPaths(newDevices));
51 changeFound = true;
52 }
53 else {
54 log("hid-listen", "No new device found");
55 }
56 var removeDevices_1 = lastDevices.filter(function (d) { return !currentDevices_1.includes(d); });
57 if (removeDevices_1.length > 0) {
58 log("hid-listen", "Removed device found:", removeDevices_1);
59 events.emit("remove", getDeviceByPaths(removeDevices_1));
60 listDevices = listDevices.filter(function (d) { return !removeDevices_1.includes(flatDevice(d)); });
61 changeFound = true;
62 }
63 else {
64 log("hid-listen", "No removed device found");
65 }
66 if (changeFound) {
67 lastDevices = currentDevices_1;
68 }
69 }
70 else {
71 log("hid-listen", "Polling skipped, re-debouncing");
72 debouncedPoll();
73 }
74 };
75 var debouncedPoll = debounce(poll, delay);
76 var attachDetected = function (device) {
77 log("hid-listen", "Device add detected:", device);
78 debouncedPoll();
79 };
80 usb.on("attach", attachDetected);
81 log("hid-listen", "attach listener added");
82 var detachDetected = function (device) {
83 log("hid-listen", "Device removal detected:", device);
84 debouncedPoll();
85 };
86 usb.on("detach", detachDetected);
87 log("hid-listen", "detach listener added");
88 return {
89 stop: function () {
90 log("hid-listen", "Stop received, removing listeners and cancelling pending debounced polls");
91 debouncedPoll.cancel();
92 usb.removeListener("attach", attachDetected);
93 usb.removeListener("detach", detachDetected);
94 },
95 events: events
96 };
97});
98//# sourceMappingURL=listenDevices.js.map
\No newline at end of file