forked from laftho/node-nfc-nci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (44 loc) · 1.37 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
const { EventEmitter } = require("events");
const debug =
process.env.NODE_DEBUG && process.env.NODE_DEBUG.includes("node-nfc-nci");
const moduleFile = debug
? `./build/Debug/node-nfc-nci.node`
: `./build/Release/node-nfc-nci.node`;
const native_nci = require(moduleFile);
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();