From a5a811811d516029436a4c4a97ec8aac4182e9c0 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Tue, 23 Jan 2018 18:18:04 -0500 Subject: [PATCH] process: clean up signal handler setup PR-URL: https://github.com/nodejs/node/pull/18330 Reviewed-By: James M Snell Reviewed-By: Joyee Cheung Reviewed-By: Jon Moss --- lib/internal/process.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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]; }