forked from MadLittleMods/node-usb-detection
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
55 lines (43 loc) · 1.58 KB
/
index.js
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
var index = require('./package.json');
if (global[index.name] && global[index.name].version === index.version) {
module.exports = global[index.name];
} else {
var detection = require('bindings')('detection.node'),
EventEmitter2 = require('eventemitter2').EventEmitter2;
var detector = new EventEmitter2({
wildcard: true,
delimiter: ':',
maxListeners: 1000 // default would be 10!
});
detector.find = detection.find;
detection.registerAdded(function(device) {
detector.emit('add:' + device.vendorId + ':' + device.productId, device);
detector.emit('add:' + device.vendorId, device);
detector.emit('add', device);
detector.emit('change:' + device.vendorId + ':' + device.productId, device);
detector.emit('change:' + device.vendorId, device);
detector.emit('change', device);
});
detection.registerRemoved(function(device) {
detector.emit('remove:' + device.vendorId + ':' + device.productId, device);
detector.emit('remove:' + device.vendorId, device);
detector.emit('remove', device);
detector.emit('change:' + device.vendorId + ':' + device.productId, device);
detector.emit('change:' + device.vendorId, device);
detector.emit('change', device);
});
var started = true;
detector.startMonitoring = function() {
if (started) return;
started = true;
detection.startMonitoring();
};
detector.stopMonitoring = function() {
if (!started) return;
started = false;
detection.stopMonitoring();
};
detector.version = index.version;
global[index.name] = detector;
module.exports = detector;
}