Skip to content

Commit 2931348

Browse files
tellnesbnoordhuis
authored andcommitted
events: implement EventEmitter#getMaxListeners()
Fixes nodejs/node-v0.x-archive#8237. PR-URL: #82 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 993fadb commit 2931348

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

doc/api/events.markdown

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ allows that to be increased. Set to zero for unlimited.
8484

8585
Returns emitter, so calls can be chained.
8686

87+
### emitter.getMaxListeners()
88+
89+
Returns the current max listener value for the emitter which is either set by
90+
`emitter.setMaxListeners(n)` or defaults to `EventEmitter.defaultMaxListeners`.
91+
92+
This can be useful to increment/decrement max listeners to avoid the warning
93+
while not being irresponsible and setting a too big number.
94+
95+
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
96+
emitter.once('event', function () {
97+
// do stuff
98+
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
99+
});
100+
87101
### EventEmitter.defaultMaxListeners
88102

89103
`emitter.setMaxListeners(n)` sets the maximum on a per-instance basis.

lib/events.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
6767
return this;
6868
};
6969

70+
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
71+
if (!util.isUndefined(this._maxListeners))
72+
return this._maxListeners;
73+
else
74+
return EventEmitter.defaultMaxListeners;
75+
};
76+
7077
EventEmitter.prototype.emit = function emit(type) {
7178
var er, handler, len, args, i, listeners;
7279

@@ -165,13 +172,7 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
165172

166173
// Check for listener leak
167174
if (util.isObject(this._events[type]) && !this._events[type].warned) {
168-
var m;
169-
if (!util.isUndefined(this._maxListeners)) {
170-
m = this._maxListeners;
171-
} else {
172-
m = EventEmitter.defaultMaxListeners;
173-
}
174-
175+
var m = this.getMaxListeners();
175176
if (m && m > 0 && this._events[type].length > m) {
176177
this._events[type].warned = true;
177178
console.error('(node) warning: possible EventEmitter memory ' +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright io.js contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var EventEmitter = require('events');
25+
26+
var emitter = new EventEmitter();
27+
28+
assert.strictEqual(emitter.getMaxListeners(), EventEmitter.defaultMaxListeners);
29+
30+
emitter.setMaxListeners(0)
31+
assert.strictEqual(emitter.getMaxListeners(), 0)
32+
33+
emitter.setMaxListeners(3)
34+
assert.strictEqual(emitter.getMaxListeners(), 3)

0 commit comments

Comments
 (0)