Skip to content

Commit

Permalink
fix: hack to fix issue in Electron
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed May 23, 2021
1 parent f69b599 commit 501f06d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/node/src/node-hid-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export class NodeHIDDevice extends EventEmitter implements HIDDevice {
public async close(): Promise<void> {
this.device.close()

// For some unknown reason, we need to wait a bit before returning because it
// appears that the HID-device isn't actually closed properly until after a short while.
// (This issue has been observed in Electron, where a app.quit() causes the application to crash with "Exit status 3221226505".)
await new Promise((resolve) => setTimeout(resolve, 300))

this.device.removeListener('error', this._handleError)
this.device.removeListener('data', this._handleData)
}
Expand Down
6 changes: 4 additions & 2 deletions packages/node/src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class XKeysWatcher extends EventEmitter {
* Stop the watcher
* @param closeAllDevices Set to false in order to NOT close all devices. Use this if you only want to stop the watching. Defaults to true
*/
public async stop(closeAllDevices: boolean = true): Promise<void> {
public async stop(closeAllDevices = true): Promise<void> {
this.isMonitoring = false

// Remove the listeners:
Expand All @@ -105,9 +105,11 @@ export class XKeysWatcher extends EventEmitter {
if (closeAllDevices) {
// In order for an application to close gracefully,
// we need to close all devices that we've called setupXkeysPanel() on:
const ps: Promise<void>[] = []
for (const xKeysPanel of this.setupXkeysPanels) {
await xKeysPanel.close()
ps.push(xKeysPanel.close())
}
await Promise.all(ps)
}
}
private onAddedUSBDevice = (_device: USBDetectNS.Device) => {
Expand Down

0 comments on commit 501f06d

Please sign in to comment.