An EventEmitter can be used to listen to structured events based on a provided EventMap. The event map describes the possible events. Each event is described as a record where the key is a string representing the event name and the value is a typed array representing the arguments that that event is emitted with.

An EventEmitter can be used to listen to events directly using on and in case of more complex behaviour it can be used to derive new EventEmitters using pipe.

Example:

type MyEventMap = { 'join': [name: string], 'leave': [name: string, reasonCode: number] }
const myEmitter: EventEmitter<MyEventMap> = ...;
myEmitter.on('leave', (name: string, reasonCode: number) => {
// You implementation here that uses `name` and `reasonCode`.
});
interface EventEmitter<EventMap> {
    name: string;
    all(listener): Unsubscribe;
    on<K>(eventName, listener): Unsubscribe;
    once<K>(eventName, listener, predicate?): Unsubscribe;
    pipe<NextEventMap>(configureTap): EventEmitter<NextEventMap>;
    waitFor<K>(eventName, predicate?): Promise<EventMap[K]>;
}

Type Parameters

  • EventMap extends Record<string, unknown[]>

Implemented by

Properties

Methods

Properties

name: string

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

  • 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

    • configureTap: ((tap) => 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.
        • (tap): void
        • Parameters

          • tap: {
                emit<NextK>(eventName, ...args): unknown[];
                on<PrevK>(eventName, listener): void;
                pass<PrevK, MutualEventKey>(...eventNames): void;
            }
            • emit:function
              • Emits an event on the pipe to be received by the new EventEmitter.

                Type Parameters

                • NextK extends string | number | symbol

                Parameters

                • eventName: NextK

                  The name of the event to emit.

                • Rest ...args: NextEventMap[NextK]

                  The arguments of the event to emit.

                Returns unknown[]

            • on:function
              • Start listening to a specific event on the source event emitter.

                Type Parameters

                • PrevK extends string | number | symbol

                Parameters

                • eventName: PrevK

                  The name of the event to listen to.

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

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

                Returns void

            • pass:function
              • Ensures that events (with the provided event names) are passed through as-is to the next event emitter. Note: Only event names that exist on both sides of the pipe and that have the same argument array signature are allowed to be passed.

                Type Parameters

                • PrevK extends string | number | symbol = keyof EventMap
                • MutualEventKey extends string | number | symbol = PrevK extends keyof NextEventMap
                      ? NextEventMap[PrevK] extends EventMap[PrevK]
                          ? PrevK
                          : never
                      : never

                Parameters

                • Rest ...eventNames: MutualEventKey[]

                  The names of the events you want to pass through as-is to the next event emitter. Only events that exist on both sides of the pipe and that have the same argument array signature are allowed to be passed.

                Returns void

          Returns void

    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]>