forked from MadLittleMods/node-usb-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (90 loc) · 2.81 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
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
//var SegfaultHandler = require('segfault-handler');
//SegfaultHandler.registerHandler();
var index = require('./package.json');
function isFunction(functionToCheck) {
return typeof functionToCheck === 'function';
}
if(global[index.name] && global[index.name].version === index.version) {
module.exports = global[index.name];
} else {
var detection = require('bindings')('detection.node');
var EventEmitter2 = require('eventemitter2').EventEmitter2;
var detector = new EventEmitter2({
wildcard: true,
delimiter: ':',
maxListeners: 1000 // default would be 10!
});
//detector.find = detection.find;
detector.find = function(vid, pid, callback) {
// Suss out the optional parameters
if(isFunction(vid) && !pid && !callback) {
callback = vid;
vid = undefined;
} else if(isFunction(pid) && !callback) {
callback = pid;
pid = undefined;
}
return new Promise(function(resolve, reject) {
// Assemble the optional args into something we can use with `apply`
var args = [];
if(vid) {
args = args.concat(vid);
}
if(pid) {
args = args.concat(pid);
}
// Tack on our own callback that takes care of things
args = args.concat(function(err, devices) {
// We call the callback if they passed one
if(callback) {
callback.call(callback, err, devices);
}
// But also do the promise stuff
if(err) {
reject(err);
return;
}
resolve(devices);
});
// Fire off the `find` function that actually does all of the work
detection.find.apply(detection, args);
});
};
detection.registerAdded(function(device) {
detector.emit('add:' + device.vendorId + ':' + device.productId, device);
detector.emit('insert:' + device.vendorId + ':' + device.productId, device);
detector.emit('add:' + device.vendorId, device);
detector.emit('insert:' + device.vendorId, device);
detector.emit('add', device);
detector.emit('insert', 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 = false;
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;
}