How to use the defaultMaxListeners function from events

Find comprehensive JavaScript events.defaultMaxListeners code examples handpicked from public code repositorys.

events.defaultMaxListeners is a property in the Node.js Events module that sets the maximum number of listeners that can be registered for a single event.

3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
// Check for listener leak
if (isObject(this._events[type]) && !this._events[type].warned) {
  if (!isUndefined(this._maxListeners)) {
    m = this._maxListeners;
  } else {
    m = EventEmitter.defaultMaxListeners;
  }

  if (m && m > 0 && this._events[type].length > m) {
    this._events[type].warned = true;
fork icon2
star icon2
watch icon0

722
723
724
725
726
727
728
729
730
731
732
733
  return this;
};


function $getMaxListeners(that) {
  if (that._maxListeners === undefined)
    return EventEmitter.defaultMaxListeners;
  return that._maxListeners;
}


EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
fork icon0
star icon0
watch icon1

+ 4 other calls in file

How does events.defaultMaxListeners work?

The events.defaultMaxListeners property is used to set the maximum number of listeners that can be added to an event emitter in Node.js. By default, the maximum number of listeners is set to 10, but it can be changed by assigning a different value to events.defaultMaxListeners. If the number of listeners exceeds this limit, a warning is logged to the console.

Ai Example

1
2
const EventEmitter = require("events");
EventEmitter.defaultMaxListeners = 20;

This will allow up to 20 listeners to be added to an event before a warning is issued.