Skip to content

Commit cf0fa96

Browse files
bnoordhuisisaacs
authored andcommitted
events: add EventEmitter.defaultMaxListeners
Class property that controls the maximum number of listeners for all instances of EventEmitter. Fixes #3014. Conflicts: lib/events.js
1 parent 2efb6cf commit cf0fa96

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

doc/api/events.markdown

+13-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,19 @@ Removes all listeners, or those of the specified event.
6868
### emitter.setMaxListeners(n)
6969

7070
By default EventEmitters will print a warning if more than 10 listeners are
71-
added for a particular event. This is a useful default which helps finding memory leaks.
72-
Obviously not all Emitters should be limited to 10. This function allows
73-
that to be increased. Set to zero for unlimited.
71+
added for a particular event. This is a useful default which helps finding
72+
memory leaks. Obviously not all Emitters should be limited to 10. This function
73+
allows that to be increased. Set to zero for unlimited.
74+
75+
76+
### EventEmitter.defaultMaxListeners
77+
78+
`emitter.setMaxListeners(n)` sets the maximum on a per-instance basis.
79+
This class property lets you set it for *all* `EventEmitter` instances,
80+
current and future, effective immediately. Use with care.
81+
82+
Note that `emitter.setMaxListeners(n)` still has precedence over
83+
`EventEmitter.defaultMaxListeners`.
7484

7585

7686
### emitter.listeners(event)

lib/events.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ function EventEmitter() {
3333
}
3434
}
3535
this._events = this._events || {};
36-
this._maxListeners = this._maxListeners || defaultMaxListeners;
36+
this._maxListeners = this._maxListeners || undefined;
3737
}
3838
exports.EventEmitter = EventEmitter;
3939

40-
// By default EventEmitters will print a warning if more than
41-
// 10 listeners are added to it. This is a useful default which
42-
// helps finding memory leaks.
43-
//
40+
41+
// By default EventEmitters will print a warning if more than 10 listeners are
42+
// added to it. This is a useful default which helps finding memory leaks.
43+
EventEmitter.defaultMaxListeners = 10;
44+
45+
4446
// Obviously not all Emitters should be limited to 10. This function allows
4547
// that to be increased. Set to zero for unlimited.
46-
var defaultMaxListeners = 10;
4748
EventEmitter.prototype.setMaxListeners = function(n) {
4849
if (typeof n !== 'number' || n < 0)
4950
throw TypeError('n must be a positive number');
@@ -150,7 +151,13 @@ EventEmitter.prototype.addListener = function(type, listener) {
150151

151152
// Check for listener leak
152153
if (typeof this._events[type] === 'object' && !this._events[type].warned) {
153-
m = this._maxListeners;
154+
var m;
155+
if (this._maxListeners !== undefined) {
156+
m = this._maxListeners;
157+
} else {
158+
m = EventEmitter.defaultMaxListeners;
159+
}
160+
154161
if (m && m > 0 && this._events[type].length > m) {
155162
this._events[type].warned = true;
156163
console.error('(node) warning: possible EventEmitter memory ' +

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

+26
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,29 @@ for (var i = 0; i < 1000; i++) {
5656
}
5757
assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));
5858

59+
// process-wide
60+
events.EventEmitter.defaultMaxListeners = 42;
61+
e = new events.EventEmitter();
62+
63+
for (var i = 0; i < 42; ++i) {
64+
e.on('fortytwo', function() {});
65+
}
66+
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
67+
e.on('fortytwo', function() {});
68+
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
69+
delete e._events['fortytwo'].warned;
70+
71+
events.EventEmitter.defaultMaxListeners = 44;
72+
e.on('fortytwo', function() {});
73+
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
74+
e.on('fortytwo', function() {});
75+
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
76+
77+
// but _maxListeners still has precedence over defaultMaxListeners
78+
events.EventEmitter.defaultMaxListeners = 42;
79+
e = new events.EventEmitter();
80+
e.setMaxListeners(1);
81+
e.on('uno', function() {});
82+
assert.ok(!e._events['uno'].hasOwnProperty('warned'));
83+
e.on('uno', function() {});
84+
assert.ok(e._events['uno'].hasOwnProperty('warned'));

0 commit comments

Comments
 (0)