Skip to content

Commit ca17e4c

Browse files
committed
Remove disconnected devices from cache
1 parent 4edcb83 commit ca17e4c

File tree

2 files changed

+12
-32
lines changed

2 files changed

+12
-32
lines changed

.vscode/launch.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
{
99
"type": "node",
1010
"request": "launch",
11-
"name": "example",
11+
"name": "Launch Example",
1212
"program": "${workspaceFolder}/examples/selector.js",
13+
"outFiles": [
14+
"${workspaceFolder}/examples/**/*.js",
15+
"${workspaceFolder}/dist/**/*.js"
16+
],
1317
"sourceMaps": true
1418
}
1519
]
16-
}
20+
}

src/usb.ts

+6-30
Original file line numberDiff line numberDiff line change
@@ -57,45 +57,21 @@ export class USB extends EventDispatcher {
5757
options = options || {};
5858
this.devicesFound = options.devicesFound;
5959

60-
const deviceConnectCallback = device => {
60+
adapter.addListener(USBAdapter.EVENT_DEVICE_CONNECT, device => {
6161
if (this.replaceAllowedDevice(device)) {
6262
this.emit(USB.EVENT_DEVICE_CONNECT, device);
6363
}
64-
};
64+
});
6565

66-
const deviceDisconnectCallback = handle => {
66+
adapter.addListener(USBAdapter.EVENT_DEVICE_DISCONNECT, handle => {
6767
const allowedDevice = this.allowedDevices.find(allowedDevices => allowedDevices._handle === handle);
6868

6969
if (allowedDevice) {
7070
this.emit(USB.EVENT_DEVICE_DISCONNECT, allowedDevice);
71-
}
72-
};
73-
74-
this.on("newListener", event => {
75-
const listenerCount = this.listenerCount(event);
76-
77-
if (listenerCount !== 0) {
78-
return;
79-
}
80-
81-
if (event === USB.EVENT_DEVICE_CONNECT) {
82-
adapter.addListener(USBAdapter.EVENT_DEVICE_CONNECT, deviceConnectCallback);
83-
} else if (event === USB.EVENT_DEVICE_DISCONNECT) {
84-
adapter.addListener(USBAdapter.EVENT_DEVICE_DISCONNECT, deviceDisconnectCallback);
85-
}
86-
});
87-
88-
this.on("removeListener", event => {
89-
const listenerCount = this.listenerCount(event);
90-
91-
if (listenerCount !== 0) {
92-
return;
93-
}
9471

95-
if (event === USB.EVENT_DEVICE_CONNECT) {
96-
adapter.removeListener(USBAdapter.EVENT_DEVICE_CONNECT, deviceConnectCallback);
97-
} else if (event === USB.EVENT_DEVICE_DISCONNECT) {
98-
adapter.removeListener(USBAdapter.EVENT_DEVICE_DISCONNECT, deviceDisconnectCallback);
72+
// Delete device from cache
73+
const index = this.allowedDevices.indexOf(allowedDevice);
74+
this.allowedDevices.splice(index, 1);
9975
}
10076
});
10177
}

0 commit comments

Comments
 (0)