Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EventEmitter<Events>

Classes extending EventEmitter can emit events, and listeners can listen on events using on.

remarks

Inspired by nanoevents, but refactored as a class to better fit into our class hierarchy.

Type Parameters

  • Events extends EventsRecord

    An interface mapping event names to event types.

Hierarchy

Index

Constructors

Methods

Constructors

Methods

  • emit<K>(eventName: K, event: Events[K]): void
  • Emits an event, which triggers all the registered event handlers.

    Event handlers are called in the order they are added. Errors in event handlers are captured and logged (with console.error), not propagated to the caller.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • eventName: K

      Name of the event to emit.

    • event: Events[K]

      Event object to pass to the event handlers.

    Returns void

  • on<K>(eventName: K, handler: ((event: Events[K], caller: EventEmitter<Events>) => void), options?: { once?: boolean }): (() => void)
  • Registers an event handler that is triggered when the event happens.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • eventName: K

      Name of the event to listen on.

    • handler: ((event: Events[K], caller: EventEmitter<Events>) => void)

      Callback that handles the event.

        • Parameters

          Returns void

    • Optional options: { once?: boolean }
      • Optional once?: boolean

        If true, the event handler is triggered at most once (the next time the event happens), then unsubscribed.

    Returns (() => void)

    An "off" function that removes the event handler when called.

      • (): void
      • Registers an event handler that is triggered when the event happens.

        Returns void

        An "off" function that removes the event handler when called.

Generated using TypeDoc