Class EventHooks<T, R>

Event emitter which allows listeners to return a value.

This is beneficial for the themes since it allows plugins to modify the HTML output without doing unsafe text replacement.

Very simple event emitter class which collects the return values of its listeners.

const x = new EventHooks<{ a: [string] }, string>()
x.on('a', a => a.repeat(123)) // ok, returns a string
x.on('b', console.log) // error, 'b' is not assignable to 'a'
x.on('a' a => 1) // error, returns a number but expected a string

Type Parameters

  • T extends Record<keyof T, unknown[]>
  • R

Constructors

  • Type Parameters

    • T extends Record<keyof T, unknown[]>
    • R

    Returns EventHooks<T, R>

Methods

  • Emits an event to all currently subscribed listeners.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • event: K

      the event to emit.

    • ...args: T[K]

      any arguments required for the event.

    Returns R[]

  • Stops listening to an event.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • event: K

      the event to stop listening to.

    • listener: (...args: T[K]) => R

      the function to remove from the listener array.

    Returns void

  • Starts listening to an event.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • event: K

      the event to listen to.

    • listener: (...args: T[K]) => R

      function to be called when an this event is emitted.

    • order: number = 0

      optional order to insert this hook with.

    Returns void

  • Listens to a single occurrence of an event.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • event: K

      the event to listen to.

    • listener: (...args: T[K]) => R

      function to be called when an this event is emitted.

    • order: number = 0

      optional order to insert this hook with.

    Returns void

  • Parameters

    • momento: EventHooksMomento<T, R>

    Returns void