Browse Source

events: fix checking max listeners with `1`

Fixes #2490.
v0.7.4-release
Ryunosuke SATO 13 years ago
committed by koichik
parent
commit
22d7fe1206
  1. 12
      lib/events.js
  2. 7
      test/simple/test-event-emitter-check-listener-leaks.js

12
lib/events.js

@ -115,8 +115,14 @@ EventEmitter.prototype.addListener = function(type, listener) {
// 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];
}
// Check for listener leak
if (!this._events[type].warned) {
if (isArray(this._events[type]) && !this._events[type].warned) {
var m;
if (this._maxListeners !== undefined) {
m = this._maxListeners;
@ -133,10 +139,6 @@ EventEmitter.prototype.addListener = function(type, listener) {
console.trace();
}
}
} else {
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
}
return this;
};

7
test/simple/test-event-emitter-check-listener-leaks.js

@ -41,6 +41,13 @@ assert.ok(!e._events['specific'].hasOwnProperty('warned'));
e.on('specific', function() {});
assert.ok(e._events['specific'].warned);
// only one
e.setMaxListeners(1);
e.on('only one', function() {});
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
e.on('only one', function() {});
assert.ok(e._events['only one'].hasOwnProperty('warned'));
// unlimited
e.setMaxListeners(0);
for (var i = 0; i < 1000; i++) {

Loading…
Cancel
Save