Skip to content

Commit af256db

Browse files
committed
Bug Fixes
- FIXED: Issue where calling stop() would not remove listeners to the 'attach' event on usb. (#12) - FIXED: Issue where removing a device would use an old index and possibly remove the wrong device when an earlier device had already been removed. (#13) - Updated dependencies. - Version now 1.5.1.
1 parent 12b3b62 commit af256db

File tree

3 files changed

+2065
-760
lines changed

3 files changed

+2065
-760
lines changed

lib/Shuttle.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,14 @@ class Shuttle extends EventEmitter {
2727
// Methods
2828
start (watchUsb = true) {
2929
if (watchUsb) {
30-
usb.on('attach', (d) => {
31-
// Delay connection by 1 second because
32-
// it takes a second to load on macOS and Linux
33-
setTimeout(() => {
34-
this._search()
35-
}, 1000)
36-
})
30+
usb.on('attach', this._listener)
3731
// Find already connected devices
3832
this._search()
3933
}
4034
}
4135

4236
stop () {
43-
usb.unrefHotplugEvents()
37+
usb.off('attach', this._listener)
4438
if (this._hid.length > 0) {
4539
this._hid.forEach((device) => {
4640
device.hid.close()
@@ -133,7 +127,8 @@ class Shuttle extends EventEmitter {
133127
})
134128
device.hid.on('error', (error) => {
135129
device.hid.close()
136-
this._hid.splice(deviceIdx, 1)
130+
const index = this._hid.findIndex(ele => ele.id === device.id)
131+
this._hid.splice(index, 1)
137132
this.emit('disconnected', device.id)
138133
})
139134
}
@@ -221,6 +216,14 @@ class Shuttle extends EventEmitter {
221216

222217
return value
223218
}
219+
220+
_listener = (d) => {
221+
// Delay connection by 1 second because
222+
// it takes a second to load on macOS and Linux
223+
setTimeout(() => {
224+
this._search()
225+
}, 1000)
226+
}
224227
}
225228

226229
module.exports = new Shuttle()

0 commit comments

Comments
 (0)