-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
69 lines (54 loc) · 1.46 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
const util = require('util')
const EventEmitter = require('events').EventEmitter
const bindings = require('bindings')
const Mouse = bindings('native-mouse').Mouse;
class NativeMouseInstance extends EventEmitter {
constructor () {
super()
this.mouse = null
// Only create mouse if there is a listener
this.once('newListener', this.init)
this.ref = function() {
if(this.mouse) {
this.mouse.ref()
}
}
this.unref = function() {
if(this.mouse) {
this.mouse.unref()
}
}
this.destroy = function() {
if(this.mouse) {
this.mouse.destroy()
}
this.mouse = null;
}
this.init = this.init.bind(this)
}
init() {
this.mouse = new Mouse((type, x, y) => {
this.emit(type, x, y)
})
}
}
// inherit the prototype methods
util.inherits(NativeMouseInstance, EventEmitter);
const instance = new NativeMouseInstance()
// cleanup the message loop
function exitHandler(options, exitCode) {
instance.destroy()
if (options.exit) {
process.exit(exitCode)
}
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
module.exports = instance;