diff --git a/lib/widget.js b/lib/widget.js index 7b9e5c0..a2d13de 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -17,6 +17,8 @@ var colors = require('./colors') , program = require('./program') , widget = exports; +var nextTick = global.setImmediate || process.nextTick.bind(process); + /** * Node */ @@ -352,12 +354,6 @@ function Screen(options) { this.enter(); - function reset() { - if (reset.done) return; - reset.done = true; - self.leave(); - } - this._maxListeners = Infinity; Screen.total++; @@ -366,10 +362,12 @@ function Screen(options) { if (process.listeners('uncaughtException').length > Screen.total) { return; } - reset(); + self.leave(); err = err || new Error('Uncaught Exception.'); console.error(err.stack ? err.stack + '' : err + ''); - return process.exit(1); + nextTick(function() { + process.exit(1); + }); }); ['SIGTERM', 'SIGINT', 'SIGQUIT'].forEach(function(signal) { @@ -377,12 +375,14 @@ function Screen(options) { if (process.listeners(signal).length > Screen.total) { return; } - return process.exit(0); + nextTick(function() { + process.exit(0); + }); }); }); process.on('exit', function() { - reset(); + self.leave(); }); this.on('newListener', function fn(type) {