-
Notifications
You must be signed in to change notification settings - Fork 279
/
index.ts
165 lines (138 loc) · 5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { EventEmitter } from 'events';
import { ExtendedDevice } from './device';
import * as usb from './bindings';
if (usb.INIT_ERROR) {
/* eslint-disable no-console */
console.warn('Failed to initialize libusb.');
}
Object.setPrototypeOf(usb, EventEmitter.prototype);
Object.defineProperty(usb, 'pollHotplug', {
value: false,
writable: true
});
Object.defineProperty(usb, 'pollHotplugDelay', {
value: 500,
writable: true
});
// `usb.Device` is not defined when `usb.INIT_ERROR` is true
if (usb.Device) {
Object.getOwnPropertyNames(ExtendedDevice.prototype).forEach(name => {
Object.defineProperty(usb.Device.prototype, name, Object.getOwnPropertyDescriptor(ExtendedDevice.prototype, name) || Object.create(null));
});
}
interface EventListeners<T> {
newListener: keyof T;
removeListener: keyof T;
}
interface DeviceIds {
idVendor: number;
idProduct: number;
}
declare module './bindings' {
/* eslint-disable @typescript-eslint/no-empty-interface */
interface Device extends ExtendedDevice { }
interface DeviceEvents extends EventListeners<DeviceEvents> {
attach: Device;
detach: Device;
attachIds: DeviceIds;
detachIds: DeviceIds;
}
function addListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
function removeListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
function on<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
function off<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
function once<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
function listeners<K extends keyof DeviceEvents>(event: K): ((arg: DeviceEvents[K]) => void)[];
function rawListeners<K extends keyof DeviceEvents>(event: K): ((arg: DeviceEvents[K]) => void)[];
function removeAllListeners<K extends keyof DeviceEvents>(event?: K): void;
function emit<K extends keyof DeviceEvents>(event: K, arg: DeviceEvents[K]): boolean;
function listenerCount<K extends keyof DeviceEvents>(event: K): number;
}
// Devices delta support for non-libusb hotplug events
let hotPlugDevices = new Set<usb.Device>();
// This method needs to be used for attach/detach IDs (hotplugSupportType === 2) rather than a lookup because vid/pid are not unique
const emitHotplugEvents = () => {
// Collect current devices
const devices = new Set(usb.getDeviceList());
// Find attached devices
for (const device of devices) {
if (!hotPlugDevices.has(device)) {
usb.emit('attach', device);
}
}
// Find detached devices
for (const device of hotPlugDevices) {
if (!devices.has(device)) {
usb.emit('detach', device);
}
}
hotPlugDevices = devices;
};
// Polling mechanism for checking device changes where hotplug detection is not available
let pollingHotplug = false;
const pollHotplug = (start = false) => {
if (start) {
pollingHotplug = true;
} else if (!pollingHotplug) {
return;
} else {
emitHotplugEvents();
}
setTimeout(() => pollHotplug(), usb.pollHotplugDelay);
};
// Devices changed event handler
const devicesChanged = () => setTimeout(() => emitHotplugEvents(), usb.pollHotplugDelay);
// Hotplug control
let hotplugSupported = 0;
const startHotplug = () => {
hotplugSupported = usb.pollHotplug ? 0 : usb._supportedHotplugEvents();
if (hotplugSupported !== 1) {
// Collect initial devices when not using libusb
hotPlugDevices = new Set(usb.getDeviceList());
}
if (hotplugSupported) {
// Use hotplug event emitters
usb._enableHotplugEvents();
if (hotplugSupported === 2) {
// Use hotplug ID events to trigger a change check
usb.on('attachIds', devicesChanged);
usb.on('detachIds', devicesChanged);
}
} else {
// Fallback to using polling to check for changes
pollHotplug(true);
}
};
const stopHotplug = () => {
if (hotplugSupported) {
// Disable hotplug events
usb._disableHotplugEvents();
if (hotplugSupported === 2) {
// Remove hotplug ID event listeners
usb.off('attachIds', devicesChanged);
usb.off('detachIds', devicesChanged);
}
} else {
// Stop polling
pollingHotplug = false;
}
};
usb.on('newListener', event => {
if (event !== 'attach' && event !== 'detach') {
return;
}
const listenerCount = usb.listenerCount('attach') + usb.listenerCount('detach');
if (listenerCount === 0) {
startHotplug();
}
});
usb.on('removeListener', event => {
if (event !== 'attach' && event !== 'detach') {
return;
}
const listenerCount = usb.listenerCount('attach') + usb.listenerCount('detach');
if (listenerCount === 0) {
stopHotplug();
}
});
export = usb;