-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (52 loc) · 1.29 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
/* global Bare */
const EventEmitter = require('bare-events')
const os = require('bare-os')
const binding = require('./binding')
const errors = require('./lib/errors')
const signals = os.constants.signals
module.exports = exports = class Signal extends EventEmitter {
constructor(signum) {
super()
if (typeof signum === 'string') {
if (signum in signals === false) {
throw errors.UNKNOWN_SIGNAL('Unknown signal: ' + signum)
}
signum = signals[signum]
}
this._signum = signum
this._handle = binding.init(this, this._onsignal, this._onclose)
this._closing = null
}
_onsignal() {
this.emit('signal', this._signum)
}
_onclose() {
this._handle = null
this.emit('close')
}
start() {
binding.start(this._handle, this._signum)
}
stop() {
if (this._closing) return
binding.stop(this._handle)
}
ref() {
binding.ref(this._handle)
}
unref() {
binding.unref(this._handle)
}
close() {
if (this._closing) return this._closing
this._closing = EventEmitter.once(this, 'close')
binding.close(this._handle)
return this._closing
}
static send(signum, pid = os.pid()) {
os.kill(pid, signum)
}
}
exports.Emitter = require('./lib/emitter')
exports.constants = signals
exports.errors = errors