Options
All
  • Public
  • Public/Protected
  • All
Menu

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.

This class is functionally nearly identical to the {@link EventEmitter} class with two exceptions.

  1. The {@link EventEmitter} class only awaits return values from its listeners, it does not return them to the emitter.
  2. This class requires listeners to by synchronous, unless R is specified as to be a promise or other deferred type.
example
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: Record<keyof T, unknown[]>

  • R

Hierarchy

  • EventHooks

Index

Constructors

constructor

  • Type parameters

    • T: Record<keyof T, unknown[]>

    • R

    Returns EventHooks<T, R>

Methods

emit

  • emit<K>(event: K, ...args: T[K]): R[]
  • Emits an event to all currently subscribed listeners.

    Type parameters

    • K: string | number | symbol

    Parameters

    • event: K

      the event to emit.

    • Rest ...args: T[K]

      any arguments required for the event.

    Returns R[]

off

  • off<K>(event: K, listener: (...args: T[K]) => R): void
  • Stops listening to an event.

    Type parameters

    • K: 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.

        • (...args: T[K]): R
        • Parameters

          • Rest ...args: T[K]

          Returns R

    Returns void

on

  • on<K>(event: K, listener: (...args: T[K]) => R, order?: number): void
  • Starts listening to an event.

    Type parameters

    • K: 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.

        • (...args: T[K]): R
        • Parameters

          • Rest ...args: T[K]

          Returns R

    • order: number = 0

      optional order to insert this hook with.

    Returns void

once

  • once<K>(event: K, listener: (...args: T[K]) => R, order?: number): void
  • Listens to a single occurrence of an event.

    Type parameters

    • K: 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.

        • (...args: T[K]): R
        • Parameters

          • Rest ...args: T[K]

          Returns R

    • order: number = 0

      optional order to insert this hook with.

    Returns void

restoreMomento

  • restoreMomento(momento: EventHooksMomento<T, R>): void

saveMomento

  • saveMomento(): EventHooksMomento<T, R>

Generated using TypeDoc