diff --git a/doc/api/events.markdown b/doc/api/events.markdown index 895debba026859..0923ef44c659e2 100644 --- a/doc/api/events.markdown +++ b/doc/api/events.markdown @@ -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. diff --git a/lib/events.js b/lib/events.js index f8c651feac6921..3ee16791dd808f 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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 ' + diff --git a/test/simple/test-event-emitter-get-max-listeners.js b/test/simple/test-event-emitter-get-max-listeners.js new file mode 100644 index 00000000000000..8e47ef7a794141 --- /dev/null +++ b/test/simple/test-event-emitter-get-max-listeners.js @@ -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)