Skip to content

Commit

Permalink
events: implement EventEmitter#getMaxListeners()
Browse files Browse the repository at this point in the history
tellnes committed Dec 5, 2014

Verified

This commit was signed with the committer’s verified signature. The key has expired.
addaleax Anna Henningsen
1 parent dca1ee4 commit 9de06aa
Showing 3 changed files with 56 additions and 7 deletions.
14 changes: 14 additions & 0 deletions doc/api/events.markdown
Original file line number Diff line number Diff line change
@@ -84,6 +84,20 @@ allows that to be increased. Set to zero for unlimited.

Returns emitter, so calls can be chained.

### emitter.getMaxListeners()

Returns the current max listener value for the emitter which is either set by
`emitter.setMaxListeners(n)` or defaults to `EventEmitter.defaultMaxListeners`.

This can be useful to increment/decrement max listeners to avoid the warning
while not being irresponsible and setting a too big number.

emitter.setMaxListeners(emitter.getMaxListeners() + 1);
emitter.once('event', function () {
// do stuff
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
});

### EventEmitter.defaultMaxListeners

`emitter.setMaxListeners(n)` sets the maximum on a per-instance basis.
15 changes: 8 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
@@ -67,6 +67,13 @@ EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
return this;
};

EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
if (!util.isUndefined(this._maxListeners))
return this._maxListeners;
else
return EventEmitter.defaultMaxListeners;
};

EventEmitter.prototype.emit = function emit(type) {
var er, handler, len, args, i, listeners;

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

// Check for listener leak
if (util.isObject(this._events[type]) && !this._events[type].warned) {
var m;
if (!util.isUndefined(this._maxListeners)) {
m = this._maxListeners;
} else {
m = EventEmitter.defaultMaxListeners;
}

var m = this.getMaxListeners();
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
34 changes: 34 additions & 0 deletions test/simple/test-event-emitter-get-max-listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright io.js contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var EventEmitter = require('events');

var emitter = new EventEmitter();

assert.strictEqual(emitter.getMaxListeners(), EventEmitter.defaultMaxListeners);

emitter.setMaxListeners(0)
assert.strictEqual(emitter.getMaxListeners(), 0)

emitter.setMaxListeners(3)
assert.strictEqual(emitter.getMaxListeners(), 3)

0 comments on commit 9de06aa

Please sign in to comment.