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.
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;
// 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 ' +
--- /dev/null
+// 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)