An EventGenerator is a type of EventEmitter that exposes the 'emit' method, which allows it to generate events.

Type Parameters

  • EventMap extends Record<string, unknown[]>

Implements

Constructors

Properties

allEventListeners: AllEventListener<keyof EventMap, void>[] = []
eventListeners: {
    [K in string | number | symbol]?: EventListener<EventMap[K], void>[]
} = {}
isEmitting: (keyof EventMap)[] = []
name: string = 'event-emitter'

The name of the event emitter. It will be used in the output logs when errors occur in the event listeners.

Methods

  • Start listening to all events. The provided listener will be invoked each time any event is emitted.

    Parameters

    • listener: AllEventListener<keyof EventMap, void>

      The callback that will be invoked when any event is emitted. It will be invoked with the event name and the event information in its arguments.

    Returns Unsubscribe

  • Type Parameters

    • K extends string | number | symbol

    Parameters

    Returns unknown[]

  • Start listening to a specific event. The provided listener will be invoked each time the event is emitted.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • eventName: K

      The name of the event to listen to.

    • listener: EventListener<EventMap[K], void>

      The callback that will be invoked when the event is emitted. It will be invoked with the event information in its arguments.

    Returns Unsubscribe

  • Start listening to a specific event only once. The provided listener will be invoked the first time the event is emitted.

    If a predicate is provided, the listener will only be invoked if the predicate returns true. The listener will be removed after the first time it is invoked. If the predicate returns false, the event subscription will be kept intact, until the predicate returns true.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • eventName: K

      The name of the event to listen to.

    • listener: EventListener<EventMap[K], void>

      The callback that will be invoked when the event is emitted. It will be invoked with the event information in its arguments.

    • Optional predicate: EventListener<EventMap[K], boolean>

      An optional predicate that can be used to filter out events. If the predicate returns true, the listener will be invoked.

    Returns Unsubscribe

  • Create new event emitter based on the existing event emitter with custom logic to define which events are passed through or transformed.

    Type Parameters

    • NextEventMap extends Record<string, unknown[]>

    Parameters

    • configurePipe: ((pipe) => void)

      In the configure tap callback function you can create custom logic to pass or transform events to

      Example:

      type MyEventMap = { 'hello': [name: string], 'one': [], 'unused': [value: number] }
      const myEmitter: EventEmitter<MyEventMap> = ...;
      type MyPipedEventMap = { 'hello': [name: string], 'two': [] }
      const myPipedEmitter = myEmitter.pipe<MyPipedEventMap>(pipe => {
      pipe.pass('hello');
      pipe.on('one' () => {
      pipe.emit('two');
      });
      });
      myPipedEmitter.on('hello', (name: string) => { ... })
      myPipedEmitter.on('two', () => { ... })

      TODO: Ensure that the pipe has a similar unsubscribe interface as on and once.

    Returns EventEmitter<NextEventMap>

  • Wait for a specific event. The returned promise will resolve when the event is emitted. The promise will resolve with the event information.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • eventName: K

      The name of the event to wait for.

    • Optional predicate: EventListener<EventMap[K], boolean>

      Optionally provides a predicate that is invoked with every occurrence of the event. The promise will only resolve once the predicate returns true. It will be invoked with the event information in its arguments. If the predicate throws an error, the promise will be rejected with that error.

    Returns Promise<EventMap[K]>