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

Fix event listener leak check timing #1041

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));