-
Notifications
You must be signed in to change notification settings - Fork 28.1k
/
signal.js
54 lines (42 loc) · 1.15 KB
/
signal.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
'use strict';
const {
FunctionPrototypeBind,
SafeMap,
} = primordials;
const {
errnoException,
} = require('internal/errors');
const { signals } = internalBinding('constants').os;
let Signal;
const signalWraps = new SafeMap();
function isSignal(event) {
return typeof event === 'string' && signals[event] !== undefined;
}
// Detect presence of a listener for the special signal types
function startListeningIfSignal(type) {
if (isSignal(type) && !signalWraps.has(type)) {
if (Signal === undefined)
Signal = internalBinding('signal_wrap').Signal;
const wrap = new Signal();
wrap.unref();
wrap.onsignal = FunctionPrototypeBind(process.emit, process, type, type);
const signum = signals[type];
const err = wrap.start(signum);
if (err) {
wrap.close();
throw errnoException(err, 'uv_signal_start');
}
signalWraps.set(type, wrap);
}
}
function stopListeningIfSignal(type) {
const wrap = signalWraps.get(type);
if (wrap !== undefined && process.listenerCount(type) === 0) {
wrap.close();
signalWraps.delete(type);
}
}
module.exports = {
startListeningIfSignal,
stopListeningIfSignal,
};