Skip to content

Commit afe1a20

Browse files
author
chenyuebiao
committed
lib: add signalMonitor event for process
1 parent c202696 commit afe1a20

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

doc/api/process.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,17 @@ added:
625625

626626
The `'worker'` event is emitted after a new {Worker} thread has been created.
627627

628+
### Event: `'signalMonitor'`
629+
630+
<!-- YAML
631+
added:
632+
- REPLACEME
633+
-->
634+
635+
* `signal` {string} The signal name, such as `SIGINT`.
636+
637+
The `'signalMonitor'` event is emitted after a signal is received.
638+
628639
### Signal events
629640

630641
<!--type=event-->

lib/events.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const {
8383
validateString,
8484
} = require('internal/validators');
8585
const { addAbortListener } = require('internal/events/abort_listener');
86+
const { signals } = internalBinding('constants').os;
8687

8788
const kCapture = Symbol('kCapture');
8889
const kErrorMonitor = Symbol('events.errorMonitor');
@@ -454,6 +455,10 @@ function enhanceStackTrace(err, own) {
454455
* @returns {boolean}
455456
*/
456457
EventEmitter.prototype.emit = function emit(type, ...args) {
458+
if (this === process && signals[type] !== undefined) {
459+
process.emit('signalMonitor', type);
460+
}
461+
457462
let doError = (type === 'error');
458463

459464
const events = this._events;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const EventEmitter = require('events');
5+
6+
// Test that EventEmitter.signalMonitor is not called on EE.
7+
const EE = new EventEmitter();
8+
EE.on('signalMonitor', common.mustNotCall());
9+
EE.emit('SIGINT');
10+
11+
// Test that EventEmitter.signalMonitor is called on process.
12+
process.on('signalMonitor', common.mustCall(function(signal) {
13+
assert.strictEqual(signal, 'SIGINT');
14+
}, 1));
15+
process.emit('SIGINT');
16+
17+
// Test that EventEmitter.signalMonitor is not called on other events.
18+
process.emit('test');

0 commit comments

Comments
 (0)