diff --git a/src/node.js b/src/node.js index 0ec16f2692..e9bb4b5da0 100644 --- a/src/node.js +++ b/src/node.js @@ -65,21 +65,48 @@ process.compile("(function (exports) {" // module.require("events"); // Signal Handlers +(function() { + var signalWatchers = {}; + addListener = process.addListener, + removeListener = process.removeListener; + + function isSignal (event) { + return event.slice(0, 3) === 'SIG' && process.hasOwnProperty(event); + }; + + // Wrap addListener for the special signal types + process.addListener = function (type, listener) { + var ret = addListener.apply(this, arguments); + if (isSignal(type)) { + if (!signalWatchers.hasOwnProperty(type)) { + var b = process.binding('signal_watcher'), + w = new b.SignalWatcher(process[type]); + w.callback = function () { + process.emit(type); + } + signalWatchers[type] = w; + w.start(); + } else if (this.listeners(type).length === 1) { + signalWatchers[event].start(); + } + } + + return ret; + } -function isSignal (event) { - return event.slice(0, 3) === 'SIG' && process.hasOwnProperty(event); -}; + process.removeListener = function (type, listener) { + var ret = removeListener.apply(this, arguments); + if (isSignal(type)) { + process.assert(signalWatchers.hasOwnProperty(type)); -process.addListener("newListener", function (event) { - if (isSignal(event) && process.listeners(event).length === 0) { - var b = process.binding('signal_watcher'); - var w = new b.SignalWatcher(process[event]); - w.callback = function () { - process.emit(event); - }; - w.start(); + if (this.listeners(type).length === 0) { + signalWatchers[type].stop(); + } + } + + return ret; } -}); +})(); // Timers function addTimerListener (callback) { diff --git a/test/disabled/test-signal-unregister.js b/test/simple/test-signal-unregister.js similarity index 100% rename from test/disabled/test-signal-unregister.js rename to test/simple/test-signal-unregister.js