请问EventEmitter的事件队列的大小可以修改吗
发布于 7 年前 作者 LinkXSystem 3098 次浏览 来自 问答

请问EventEmitter的事件队列的大小可以修改吗

5 回复

可以,自己看 API

可是我就是在API没看到才来问的,请问在哪,谢谢

@LinkXSystem

直接翻源码lib/event.js


// By default EventEmitters will print a warning if more than 10 listeners are
// added to it. This is a useful default which helps finding memory leaks.
var defaultMaxListeners = 10;

Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
  enumerable: true,
  get: function() {
    return defaultMaxListeners;
  },
  set: function(arg) {
    // force global console to be compiled.
    // see https://github.com/nodejs/node/issues/4467
    console;
    // check whether the input is a positive number (whose value is zero or
    // greater and not a NaN).
    if (typeof arg !== 'number' || arg < 0 || arg !== arg)
      throw new TypeError('"defaultMaxListeners" must be a positive number');
    defaultMaxListeners = arg;
  }
});

@i5ting @chickencyj 按以上的回答,我疑惑的是EventEmitter的listeners 的作用就是监听的最大事件数吗 ?我所理解的是** defaultMaxListeners 指的是 emitter.on 方法所能设置的数量**,而我正在实践的问题是由emitter.emit所产生的事件(并发情况, 数量大约5000),** emitter.on** 方法最大能缓存多少事件(如果处理不过来的情况下)

回到顶部