Browse Source

Fix event listener leak check timing

Fixes #1041.
v0.7.4-release
koichik 14 years ago
committed by Ryan Dahl
parent
commit
80c2fe9456
  1. 6
      lib/events.js
  2. 29
      test/simple/test-event-emitter-check-listener-leaks.js

6
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];

29
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'));
Loading…
Cancel
Save