Skip to content

Commit

Permalink
fix: remove listeners on watcher.stop()
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed May 15, 2021
1 parent a137551 commit c8d36a3
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/node/src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ export class XKeysWatcher extends EventEmitter {
this.updateConnectedDevices()
}
public stop(): void {
// Note: This is really the wrong way to do it, since it leaves open the listener (this is a memory leak).
// We can't do anything else right now, because usb-detection currently doesn't expose a way to remove listeners from events.
this.isMonitoring = false

// Remove the listeners:
// @ts-expect-error usb-detection exposes wrong types:
USBDetect().removeListener(`add:${XKEYS_VENDOR_ID}`, this.onAddedUSBDevice)
// @ts-expect-error usb-detection exposes wrong types:
USBDetect().removeListener(`remove:${XKEYS_VENDOR_ID}`, this.onRemovedUSBDevice)

watcherCount--
if (watcherCount === 0) {
USBDetect().stopMonitoring()
Expand Down

0 comments on commit c8d36a3

Please sign in to comment.