Skip to main content

Emitter <Events>

Cross-platform, lightweight type-safe event emitter.

@example
type Events = {
error: (error: Error) => void
message: (from: string, content: string) => void
}

const emitter = new Emitter<Events>()
emitter.emit('error', 'x') // will catch this type error

Implements

Index

Constructors

constructor

  • new Emitter<Events>(): Emitter<Events>

Methods

emit

  • emit<Event>(event: Event, ...args: Parameters<Events[Event]>): boolean
  • Emits an event with a given name and passes any number of arguments to the listeners. Returns a boolean indicating whether the event had listeners attached.


    Type parameters

    • Event: string | number | symbol

    Parameters

    • event: Event

      The name of the event to be emitted.

    • rest...args: Parameters<Events[Event]>

      Any number of arguments to be passed to the listeners.

    Returns boolean

    A boolean indicating whether the event had listeners attached.

eventNames

  • eventNames(): (string | symbol | keyof Events)[]
  • Returns an array of all the event names that have listeners.


    Returns (string | symbol | keyof Events)[]

    An array of event names.

listenerCount

  • listenerCount<Event>(event: Event): number
  • Returns the number of listeners for the given event.


    Type parameters

    • Event: string | number | symbol

    Parameters

    • event: Event

      The event name.

    Returns number

    The number of listeners for the given event.

listeners

  • listeners<Event>(event: Event): Events[Event][]
  • Returns an array of listeners for the specified event.


    Type parameters

    • Event: string | number | symbol

    Parameters

    • event: Event

      The event name to retrieve listeners for.

    Returns Events[Event][]

    An array of listeners for the specified event.

off

  • off<Event>(event: Event, listener: Events[Event]): this
  • Removes a listener from the specified event.


    Type parameters

    • Event: string | number | symbol

    Parameters

    • event: Event

      the event to remove the listener from.

    • listener: Events[Event]

      the listener to remove from the event.

    Returns this

    The class instance for chaining.

on

  • on<Event>(event: Event, listener: Events[Event]): this
  • Adds a listener for the specified event.


    Type parameters

    • Event: string | number | symbol

    Parameters

    • event: Event

      The event name.

    • listener: Events[Event]

      The listener function.

    Returns this

    Returns this instance for chaining.

once

  • once<Event>(event: Event, listener: Events[Event]): Events[Event]
  • Returns a proxy function that when called, invokes the original listener function and removes the listener from the event emitter.


    Type parameters

    • Event: string | number | symbol

    Parameters

    • event: Event

      The event to listen to.

    • listener: Events[Event]

      The listener function to be called once.

    Returns Events[Event]

    The original listener function.

removeAllListeners

  • removeAllListeners<Event>(event?: Event): this
  • Removes all listeners for the specified event or all events if none specified.


    Type parameters

    • Event: string | number | symbol

    Parameters

    • optionalevent: Event

      The event to remove listeners for.

    Returns this

    Returns the instance for chaining.