Documentation
    Preparing search index...

    Class Emitter<EventsList>

    Event emitter is built on top of emittery with support class based events and listeners

    Type Parameters

    • EventsList extends Record<string | symbol | number, any>

    Implements

    Index

    Constructors

    • Creates a new Emitter instance

      Type Parameters

      • EventsList extends Record<string | number | symbol, any>

      Parameters

      • app: Application<any>

        The AdonisJS application instance

      Returns Emitter<EventsList>

    Accessors

    • get eventsListeners(): Map<
          AllowedEventTypes,
          Map<Listener<any, Constructor<any>>, ListenerMethod<any>>,
      >

      Returns a map of events and their registered listeners. The map key is the event name and the value is another map of listeners.

      The listeners map key is the original binding listener and the value is a callback function.

      Returns Map<
          AllowedEventTypes,
          Map<Listener<any, Constructor<any>>, ListenerMethod<any>>,
      >

      The events listeners map

    Methods

    • Register a global error handler

      Parameters

      • callback: (event: Constructor<any> | keyof EventsList, error: any, data: any) => void

        The error handler callback

      Returns this

      The emitter instance for method chaining

    • Listen for an event. The method returns the unsubscribe function.

      Type Parameters

      • Event extends Constructor<any>
      • ListenerClass extends Constructor<any>

      Parameters

      Returns UnsubscribeFunction

      The unsubscribe function

    • Listen for an event. The method returns the unsubscribe function.

      Type Parameters

      • Name extends string | number | symbol
      • ListenerClass extends Constructor<any>

      Parameters

      Returns UnsubscribeFunction

      The unsubscribe function

    • Listen for an event depending on a condition

      Type Parameters

      • Event extends Constructor<any>
      • ListenerClass extends Constructor<any>

      Parameters

      • condition: boolean | (() => boolean)

        The condition to check before listening

      • event: Event

        The event to listen for

      • listener: Listener<InstanceType<Event>, ListenerClass>

        The listener to register

      Returns UnsubscribeFunction

      The unsubscribe function

    • Listen for an event depending on a condition

      Type Parameters

      • Name extends string | number | symbol
      • ListenerClass extends Constructor<any>

      Parameters

      Returns UnsubscribeFunction

      The unsubscribe function

    • Attach a listener to listen for all the events. Wildcard listeners can only be defined as inline callbacks.

      Parameters

      • listener: (event: AllowedEventTypes, data: any) => any

        The wildcard listener callback

      Returns UnsubscribeFunction

      The unsubscribe function

    • Emit event. The event listeners will be called asynchronously in parallel.

      You can await this method to wait for events listeners to finish

      Type Parameters

      • Event extends Constructor<any>

      Parameters

      • event: Event

        The event to emit

      • data: InstanceType<Event>

        The data to pass to listeners

      Returns Promise<void>

    • Emit event. The event listeners will be called asynchronously in parallel.

      You can await this method to wait for events listeners to finish

      Type Parameters

      • Name extends string | number | symbol

      Parameters

      Returns Promise<void>

    • Emit events serially. The event listeners will be called asynchronously in the same sequence as they are registered.

      You can await this method to wait for events listeners to finish

      Type Parameters

      • Event extends Constructor<any>

      Parameters

      • event: Event

        The event to emit

      • data: InstanceType<Event>

        The data to pass to listeners

      Returns Promise<void>

    • Emit events serially. The event listeners will be called asynchronously in the same sequence as they are registered.

      You can await this method to wait for events listeners to finish

      Type Parameters

      • Name extends string | number | symbol

      Parameters

      Returns Promise<void>

    • Remove a specific listener for an event

      Parameters

      • event: Constructor<any> | keyof EventsList

        The event to remove listener from

      • listener: Listener<any, Constructor<any>>

        The listener to remove

      Returns void

    • Remove a specific listener listening for all the events

      Parameters

      • listener: (event: Constructor<any> | keyof EventsList, data: any) => any

        The wildcard listener to remove

      Returns this

      The emitter instance for method chaining

    • Remove a specific listener for an event

      Parameters

      • event: Constructor<any> | keyof EventsList

        The event to remove listener from

      • listener: Listener<any, Constructor<any>>

        The listener to remove

      Returns void

    • Clear all listeners for a specific event

      Parameters

      • event: Constructor<any> | keyof EventsList

        The event to clear listeners for

      Returns void

    • Clear all listeners for all the events

      Returns void

    • Get count of listeners for a given event or all the events

      Parameters

      • Optionalevent: Constructor<any> | keyof EventsList

        The event to count listeners for (optional)

      Returns number

      The number of listeners

    • Find if an event has one or more listeners

      Parameters

      • Optionalevent: Constructor<any> | keyof EventsList

        The event to check listeners for (optional)

      Returns boolean

      True if the event has listeners

    • Fake one or more events. The listeners for faked events will not be invoked.

      The return value is an events buffer that collects all the events within memory.

      Calling this method one than once drops the existing fakes and creates new one.

      Parameters

      • Optionalevents: (Constructor<any> | keyof EventsList)[]

        Array of events to fake (optional, defaults to all events)

      Returns EventsBuffer<EventsList>

      The events buffer for assertions