diff --git a/lib/internal/process.js b/lib/internal/process.js index 71db75b91685ea..757c8de8e685f1 100644 --- a/lib/internal/process.js +++ b/lib/internal/process.js @@ -178,24 +178,23 @@ function setupKillAndExit() { function setupSignalHandlers() { - // Load events module in order to access prototype elements on process like - // process.addListener. - const signalWraps = {}; + const signalWraps = Object.create(null); + let Signal; function isSignal(event) { return typeof event === 'string' && constants[event] !== undefined; } // Detect presence of a listener for the special signal types - process.on('newListener', function(type, listener) { - if (isSignal(type) && - !signalWraps.hasOwnProperty(type)) { - const Signal = process.binding('signal_wrap').Signal; + process.on('newListener', function(type) { + if (isSignal(type) && signalWraps[type] === undefined) { + if (Signal === undefined) + Signal = process.binding('signal_wrap').Signal; const wrap = new Signal(); wrap.unref(); - wrap.onsignal = function() { process.emit(type, type); }; + wrap.onsignal = process.emit.bind(process, type, type); const signum = constants[type]; const err = wrap.start(signum); @@ -208,8 +207,8 @@ function setupSignalHandlers() { } }); - process.on('removeListener', function(type, listener) { - if (signalWraps.hasOwnProperty(type) && this.listenerCount(type) === 0) { + process.on('removeListener', function(type) { + if (signalWraps[type] !== undefined && this.listenerCount(type) === 0) { signalWraps[type].close(); delete signalWraps[type]; }