Class EventEmitter<EventMap>

Base class for objects that dispatches events.

Example

var emitter = new EventEmitter();
emitter.on('myEvent', function(evt){
console.log(evt.message);
});
emitter.emit({
type: 'myEvent',
message: 'Hello world!'
});

Type Parameters

  • EventMap extends Record<string, {
        type: string;
    }>

Hierarchy

Constructors

Methods

Constructors

  • Type Parameters

    • EventMap extends Record<string, {
          type: string;
      }>

    Returns EventEmitter<EventMap>

Methods

  • Emit an event.

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • event: EventMap[E]

    Returns EventEmitter<EventMap>

    The self object, for chainability.

    Example

    emitter.emit({
    type: 'myEvent',
    customData: 123
    });
  • Check if an event listener is added

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • type: E
    • Optional listener: ((e) => void)
        • (e): void
        • Parameters

          • e: EventMap[E]

          Returns void

    Returns boolean

  • Remove an event listener

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • type: E
    • listener: ((e) => void)
        • (e): void
        • Parameters

          • e: EventMap[E]

          Returns void

    Returns EventEmitter<EventMap>

    The self object, for chainability.

    Example

    emitter.on('myEvent', handler); // Add handler
    emitter.off('myEvent', handler); // Remove handler
  • Add an event listener

    Type Parameters

    • E extends string | number | symbol

    Parameters

    • type: E
    • listener: ((e) => void)
        • (e): void
        • Parameters

          • e: EventMap[E]

          Returns void

    Returns EventEmitter<EventMap>

    The self object, for chainability.

    Example

    emitter.on('myEvent', function(evt){
    console.log('myEvt was triggered!');
    });