events: implement EventEmitter#getMaxListeners()

Fixes https://github.com/joyent/node/issues/8237.

PR-URL: https://github.com/iojs/io.js/pull/82
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
pull/49/head
Christian Tellnes 2014-12-05 14:50:05 +01:00 committed by Ben Noordhuis
parent 993fadb1f2
commit 2931348372
3 changed files with 56 additions and 7 deletions

View File

@ -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.

View File

@ -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 ' +

View File

@ -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)