NodeEventTarget 对比 EventEmitter


NodeEventTarget 对象实现了 EventEmitter API 的修改子集,允许它在某些情况下接近地模拟 EventEmitterNodeEventTarget 不是 EventEmitter 的实例,在大多数情况下不能代替 EventEmitter

  1. EventEmitter 不同,任何给定的 listener 最多可以在每个事件 type 中注册一次。 尝试多次注册 listener 将被忽略。
  2. NodeEventTarget 不模拟完整的 EventEmitter API。 特别是 prependListener()prependOnceListener()rawListeners()setMaxListeners()getMaxListeners()errorMonitor API 没有被模拟。 'newListener''removeListener' 事件也不会触发。
  3. NodeEventTarget 没有为类型为 'error' 的事件实现任何特殊的默认行为。
  4. NodeEventTarget 支持 EventListener 对象以及作为所有事件类型句柄的函数。

The NodeEventTarget object implements a modified subset of the EventEmitter API that allows it to closely emulate an EventEmitter in certain situations. A NodeEventTarget is not an instance of EventEmitter and cannot be used in place of an EventEmitter in most cases.

  1. Unlike EventEmitter, any given listener can be registered at most once per event type. Attempts to register a listener multiple times are ignored.
  2. The NodeEventTarget does not emulate the full EventEmitter API. Specifically the prependListener(), prependOnceListener(), rawListeners(), setMaxListeners(), getMaxListeners(), and errorMonitor APIs are not emulated. The 'newListener' and 'removeListener' events will also not be emitted.
  3. The NodeEventTarget does not implement any special default behavior for events with type 'error'.
  4. The NodeEventTarget supports EventListener objects as well as functions as handlers for all event types.