diff --git a/lib/events.js b/lib/events.js index 6fe55bb3d26..66aeb8ed416 100644 --- a/lib/events.js +++ b/lib/events.js @@ -105,6 +105,9 @@ EventEmitter.prototype.addListener = function(type, listener) { this._events[type] = listener; } else if (isArray(this._events[type])) { + // If we've already got an array, just append. + this._events[type].push(listener); + // Check for listener leak if (!this._events[type].warned) { var m; @@ -123,9 +126,6 @@ EventEmitter.prototype.addListener = function(type, listener) { console.trace(); } } - - // If we've already got an array, just append. - this._events[type].push(listener); } else { // Adding the second element, need to change to array. this._events[type] = [this._events[type], listener]; diff --git a/test/simple/test-event-emitter-check-listener-leaks.js b/test/simple/test-event-emitter-check-listener-leaks.js new file mode 100644 index 00000000000..66e257cf3e4 --- /dev/null +++ b/test/simple/test-event-emitter-check-listener-leaks.js @@ -0,0 +1,29 @@ +var assert = require('assert'); +var events = require('events'); + +var e = new events.EventEmitter(); + +// default +for (var i = 0; i < 10; i++) { + e.on('default', function() {}); +} +assert.ok(!e._events['default'].hasOwnProperty('warned')); +e.on('default', function() {}); +assert.ok(e._events['default'].warned); + +// specific +e.setMaxListeners(5); +for (var i = 0; i < 5; i++) { + e.on('specific', function() {}); +} +assert.ok(!e._events['specific'].hasOwnProperty('warned')); +e.on('specific', function() {}); +assert.ok(e._events['specific'].warned); + +// unlimited +e.setMaxListeners(0); +for (var i = 0; i < 1000; i++) { + e.on('unlimited', function() {}); +} +assert.ok(!e._events['unlimited'].hasOwnProperty('warned')); +