Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Fix event listener leak check timing
Browse files Browse the repository at this point in the history
Fixes #1041.
  • Loading branch information
koichik authored and ry committed May 14, 2011
1 parent 56aa2fd commit 80c2fe9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down
29 changes: 29 additions & 0 deletions test/simple/test-event-emitter-check-listener-leaks.js
Original file line number Diff line number Diff line change
@@ -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'));

0 comments on commit 80c2fe9

Please sign in to comment.