Skip to content

Commit

Permalink
fix: make XKeysWatcher.stop() close all the devices it has called set…
Browse files Browse the repository at this point in the history
…upXkeysPanel() for.
  • Loading branch information
nytamin committed May 23, 2021
1 parent e0cb525 commit f69b599
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ watcher.on('connected', (xkeysPanel) => {
})

// To stop watching, call
// watcher.stop()
// watcher.stop() // Returns a promise
// .catch(console.error)
```

### Connect to a devices manually
Expand Down Expand Up @@ -296,6 +297,14 @@ The most notable changes are:
| `myXkeys.setAllBacklights(...)` | Arguments have changed, see docs |
| `myXkeys.setLED(index, ...)` | `myXkeys.setIndicatorLED(index, ...)` (index 1 = the red, 2 = the green one) |

### 2.1.1

Version `2.1.1` has a minor change for when stopping the XKeysWatcher instance:
```
const watcher = new XKeysWatcher()
await watcher.stop() // Now returns a promise
```

## For developers

This is a mono-repo, using [Lerna](https://github.com/lerna/lerna) and [Yarn](https://yarnpkg.com).
Expand Down
2 changes: 1 addition & 1 deletion packages/node/examples/basic-log-all-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ watcher.on('connected', (xkeysPanel) => {
})

// To stop watching, call
// watcher.stop()
// watcher.stop().catch(console.error)
17 changes: 16 additions & 1 deletion packages/node/src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export class XKeysWatcher extends EventEmitter {
private shouldFindChangedReTries = 0

public debug = false
/** A list of the devices we've called setupXkeysPanels for */
private setupXkeysPanels: XKeys[] = []

constructor() {
super()
Expand All @@ -82,7 +84,11 @@ export class XKeysWatcher extends EventEmitter {
// Also do a sweep for all currently connected X-keys panels:
this.updateConnectedDevices()
}
public stop(): void {
/**
* 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> {
this.isMonitoring = false

// Remove the listeners:
Expand All @@ -95,6 +101,14 @@ export class XKeysWatcher extends EventEmitter {
if (watcherCount === 0) {
USBDetect().stopMonitoring()
}

if (closeAllDevices) {
// In order for an application to close gracefully,
// we need to close all devices that we've called setupXkeysPanel() on:
for (const xKeysPanel of this.setupXkeysPanels) {
await xKeysPanel.close()
}
}
}
private onAddedUSBDevice = (_device: USBDetectNS.Device) => {
// Called whenever a new USB device is added
Expand Down Expand Up @@ -175,6 +189,7 @@ export class XKeysWatcher extends EventEmitter {

setupXkeysPanel(devicePath)
.then((xkeysPanel) => {
this.setupXkeysPanels.push(xkeysPanel)
// Since this is async, check if the panel is still connected
if (this.seenDevicePaths[devicePath]) {
// yes, it is still connected
Expand Down

0 comments on commit f69b599

Please sign in to comment.