Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return Promise.resolve().then(() => {
if (transportInstance) {
log("hid-verbose", "reusing opened transport instance");
return transportInstance;
}
const device = getDevices()[0];
if (!device) throw new CantOpenDevice("no device found");
log("hid-verbose", "new HID transport");
transportInstance = new TransportNodeHidSingleton(
new HID.HID(device.path)
);
const unlisten = listenDevices(
() => {},
() => {
// assume any ledger disconnection concerns current transport
if (transportInstance) {
transportInstance.emit("disconnect");
}
}
);
const onDisconnect = () => {
if (!transportInstance) return;
static listen = (observer: Observer>): Subscription => {
let unsubscribed;
Promise.resolve(getDevices()).then(devices => {
// this needs to run asynchronously so the subscription is defined during this phase
for (const device of devices) {
if (!unsubscribed) {
const deviceModel = identifyUSBProductId(device.productId);
observer.next({
type: "add",
descriptor: "",
device: { name: device.deviceName },
deviceModel
});
}
}
});
const onAdd = device => {
const deviceModel = identifyUSBProductId(device.productId);
static listen = (
observer: Observer>
): Subscription => {
let unsubscribed = false;
Promise.resolve(getDevices()).then(devices => {
// this needs to run asynchronously so the subscription is defined during this phase
for (const device of devices) {
if (!unsubscribed) {
const descriptor: string = device.path;
const deviceModel = identifyUSBProductId(device.productId);
observer.next({ type: "add", descriptor, device, deviceModel });
}
}
});
const { events, stop } = listenDevices(
listenDevicesDebounce,
listenDevicesPollingSkip
);
const onAdd = device => {
if (unsubscribed || !device) return;
const poll = () => {
if (!listenDevicesPollingSkip()) {
log("hid-listen", "Polling for added or removed devices");
let changeFound = false;
const currentDevices = getFlatDevices();
const newDevices = currentDevices.filter(d => !lastDevices.includes(d));
if (newDevices.length > 0) {
log("hid-listen", "New device found:", newDevices);
listDevices = getDevices();
events.emit("add", getDeviceByPaths(newDevices));
changeFound = true;
} else {
log("hid-listen", "No new device found");
}
const removeDevices = lastDevices.filter(
d => !currentDevices.includes(d)
);
if (removeDevices.length > 0) {
log("hid-listen", "Removed device found:", removeDevices);
events.emit("remove", getDeviceByPaths(removeDevices));
listDevices = listDevices.filter(
return Promise.resolve().then(() => {
if (path) {
return new TransportNodeHid(new HID.HID(path));
}
const device = getDevices()[0];
if (!device) throw new TransportError("NoDevice", "NoDevice");
return new TransportNodeHid(new HID.HID(device.path));
});
}
export default (
delay: number,
listenDevicesPollingSkip: () => boolean
): ({
events: EventEmitter,
stop: () => void
}) => {
const events = new EventEmitter();
events.setMaxListeners(0);
let listDevices = getDevices();
const flatDevice = d => d.path;
const getFlatDevices = () => [
...new Set(getDevices().map(d => flatDevice(d)))
];
const getDeviceByPaths = paths =>
listDevices.find(d => paths.includes(flatDevice(d)));
let lastDevices = getFlatDevices();
const poll = () => {
if (!listenDevicesPollingSkip()) {
log("hid-listen", "Polling for added or removed devices");
const getFlatDevices = () => [
...new Set(getDevices().map(d => flatDevice(d)))
];