Skip to content

Commit

Permalink
events: implement EventEmitter#getMaxListeners()
Browse files Browse the repository at this point in the history
Fixes nodejs/node-v0.x-archive#8237.

PR-URL: nodejs#82
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
tellnes authored and bnoordhuis committed Dec 5, 2014
1 parent 993fadb commit 2931348
Show file tree
Hide file tree
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
Expand Up @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ' +
Expand Down
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 2931348

Please sign in to comment.