-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
54 lines (39 loc) · 1.28 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
const EventEmitter = require('events').EventEmitter;
const debug = process.env.NODE_DEBUG && process.env.NODE_DEBUG.includes('node_nfc_nci');
const native_nci = require(`./build/${debug ? 'Debug' : 'Release'}/node_nfc_nci`);
class NCIListener extends EventEmitter {
constructor() {
super();
this.emitter = new EventEmitter();
this.context = null;
this.emitter.on("arrived", tag => {
tag.write = (type, content) => this.context.immediateWrite({ type, content });
this.emit("arrived", tag);
});
this.emitter.on("error", error => {
this.emit("error", error);
});
this.emitter.on("departed", tag => {
this.emit("departed", tag);
});
this.emitter.on("written", (tag, previous) => {
this.emit("written", tag, previous);
});
}
setNextWrite(type, content) {
this.context.setNextWrite({ type, content });
}
clearNextWrite() {
this.context.clearNextWrite();
}
hasNextWrite() {
return this.context.hasNextWrite();
}
listen(cb) {
this.context = native_nci.listen(this.emitter.emit.bind(this.emitter));
if (cb) {
cb(this);
}
}
}
module.exports = new NCIListener();