EventEmitter3

repository·master·Indexed 26 days ago

https://github.com/primus/eventemitter3

A high-performance, micro-optimized EventEmitter for Node.js and browsers. It serves as a faster, drop-in replacement for the standard Node.js EventEmitter API, featuring support for custom context bindings to avoid fn.bind overhead and TypeScript support for typed events. Version 5.0.4.

Tokens
1.2K
Snippets
3
Records
14
Agent score
85%

What's inside eventemitter3

  1. Use contextual emits to set 'this' context

    master

    The on, once, and removeListener methods accept an optional extra argument to serve as the context (the this value) for the listener. This avoids the performance overhead of using fn.bind.

    var EE = new EventEmitter()
      , context = { foo: 'bar' };
    
    function emitted() {
      console.log(this === context); // true
    }
    
    EE.once('event-name', emitted, context);
    EE.on('another-event', emitted, context);
    EE.removeListener('another-event', emitted, context);
  2. Important API differences from Node.js EventEmitter

    master

    While eventemitter3 is a high-performance drop-in replacement for Node.js EventEmitter, note the following differences:

    • Removed features: Domain support, newListener and removeListener events, setMaxListeners, getMaxListeners, prependListener, and prependOnceListener methods.
    • Error handling: It does not throw an error when an error event is emitted and no listeners are registered.
    • Listener removal: The removeListener method removes all matching listeners, not just the first one.
    • Context: Supports custom context for events directly in the registration methods.
  3. Inspect event listeners and names

    master

    Use the following methods to inspect the state of the emitter:

    • eventNames(): Returns an array of all event names that have registered listeners.
    • listeners(event): Returns an array of the listener functions registered for a specific event.
    • listenerCount(event): Returns the number of listeners currently registered for a specific event.
  4. Use the EventEmitter class

    master
    The EventEmitter class provides a high-performance event handling interface. It supports typed events by passing an interface defining event names and their argument types. You can also specify a Context type to be used for the this binding in listeners.
  5. Import EventEmitter from the ES module entry point

    master
    You can import the EventEmitter class from the eventemitter3 package using either a named import or a default import. This class is the primary interface for managing event listeners and emitting events.
  6. Define typed events for EventEmitter

    master

    To get full TypeScript support for event names and arguments, pass an object to the EventEmitter generic. The object should map event names to either an array of argument types or a function signature.

    interface MyEvents {
      'event-with-parameters': any[]; // Using array for arguments
      'event-with-example-handler': (...args: string[]) => void; // Using function signature
    }
  7. Remove event listeners with off() and removeListener()

    master
    Use off() or removeListener() to remove listeners. You can remove a specific function, or if fn is omitted, remove all listeners for that event. You can also specify a context and a once flag to target specific listener instances.
  8. Trigger events with emit()

    master

    The emit(event, ...args) method calls all listeners registered for the specified event. It supports passing up to 5 arguments directly for performance optimization, and handles additional arguments via an array.

    • Returns true if the event had listeners, otherwise false.
    • Arguments passed to emit are passed to the listener functions.