From 4274e6b7d0f5eb3559dd8f91941ce47d9a68e370 Mon Sep 17 00:00:00 2001 From: Jonas Pfenniger Date: Fri, 14 May 2010 09:59:31 +0200 Subject: [PATCH] FIX: signal-unregister bug We now restore the default signal behavior when all listeners are removed for a given signal. Re-enabled test-signal-unregister --- src/node.js | 51 +++++++++++++++++----- .../{disabled => simple}/test-signal-unregister.js | 0 2 files changed, 39 insertions(+), 12 deletions(-) rename test/{disabled => simple}/test-signal-unregister.js (100%) diff --git a/src/node.js b/src/node.js index 0ec16f2..e9bb4b5 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 -- 2.7.4