Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Fixes HID transport to Promise.reject errors instead of throwing them
Browse files Browse the repository at this point in the history
also solves it in 2 sibling libs
  • Loading branch information
gre committed Jul 10, 2019
1 parent 2efaa3b commit 39b3ca4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
54 changes: 29 additions & 25 deletions packages/hw-transport-node-hid-singleton/src/TransportNodeHid.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,38 @@ export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents
* if path="" is not provided, the library will take the first device
*/
static open(): Promise<TransportNodeHidSingleton> {
if (transportInstance) {
log("hid-verbose", "reusing opened transport instance");
return Promise.resolve(transportInstance);
}
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 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;
log("hid-verbose", "transport instance was disconnected");
transportInstance.off("disconnect", onDisconnect);
transportInstance = null;
unlisten();
};
transportInstance.on("disconnect", onDisconnect);
);
const onDisconnect = () => {
if (!transportInstance) return;
log("hid-verbose", "transport instance was disconnected");
transportInstance.off("disconnect", onDisconnect);
transportInstance = null;
unlisten();
};
transportInstance.on("disconnect", onDisconnect);

return Promise.resolve(transportInstance);
return transportInstance;
});
}

close() {
Expand Down
14 changes: 8 additions & 6 deletions packages/hw-transport-node-hid/src/TransportNodeHid.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ export default class TransportNodeHid extends TransportNodeHidNoEvents {
* if path="" is not provided, the library will take the first device
*/
static open(path: ?string) {
if (path) {
return Promise.resolve(new TransportNodeHid(new HID.HID(path)));
}
const device = getDevices()[0];
if (!device) throw new TransportError("NoDevice", "NoDevice");
return Promise.resolve(new TransportNodeHid(new HID.HID(device.path)));
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));
});
}
}

0 comments on commit 39b3ca4

Please sign in to comment.