forked from jaganathanb/node-usbspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (46 loc) · 1.16 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 hidspyBinding = require("bindings")("hidspy.node");
var events = require("events");
require("es6-promise/auto");
var index = require("./package.json");
if (global[index.name] && global[index.name].version === index.version) {
module.exports = global[index.name];
} else {
var hidspy = new events.EventEmitter();
var ready = false;
hidspy.spyOn = function(callback) {
var promise = new Promise(function(resolve, reject) {
if (!ready) {
hidspyBinding.spyOn(
function(data) {
hidspy.emit("change", data);
},
function(data) {
hidspy.emit("end", data);
},
function() {
resolve(true);
callback(true);
}
);
ready = true;
} else {
reject();
callback(false);
}
});
return promise;
};
hidspy.spyOff = function() {
if (!ready) {
hidspyBinding.spyOff();
ready = false;
}
};
hidspy.status = {
plugin: 1,
pullout: 0,
}
hidspy.version = index.version;
global[index.name] = hidspy;
module.exports = hidspy;
}