Class Bus<TEventMap>

Type Parameters

Hierarchy

  • Bus

Implements

Accessors

  • set defaultAllowUnhandledEvents(allow: boolean): void
  • Set the default for Bus.options.allowUnhandledEvents for all instances

    Setter

    boolean

    Parameters

    • allow: boolean

    Returns void

  • set verbose(verbose: boolean): void
  • Set the default Bus.options.verbose for all instances

    Setter

    Partial<ListenerThresholds>

    Parameters

    • verbose: boolean

    Returns void

  • set defaultLogger(logger: Logger): void
  • Set the default logger for all instances to an object that implements Logger interface

    Setter

    Logger

    Parameters

    • logger: Logger

    Returns void

  • get active(): boolean
  • The active state of the bus, i.e. does it have any subscribers. Subscribers include delegates and scanners

    Getter

    boolean

    Returns boolean

  • get name(): string
  • get hasListeners(): boolean
  • Getter

    boolean

    Returns boolean

  • get hasOwnListeners(): boolean
  • Getter

    boolean

    Returns boolean

  • get hasDelegateListeners(): boolean
  • Getter

    boolean

    Returns boolean

  • get listeners(): ReadonlyMap<"*" | keyof TEventMap, ReadonlySet<GenericHandler>>
  • Returns ReadonlyMap<"*" | keyof TEventMap, ReadonlySet<GenericHandler>>

  • get listenerCount(): number
  • Returns number

Constructors

  • new Bus<TEventMap>(options?: Options): Bus<TEventMap>
  • Type Parameters

    • TEventMap extends object = object

    Parameters

    Returns Bus<TEventMap>

Methods

  • handleUnexpectedEvent<T>(event: T, ...payload: TEventMap[T] extends void ? [] | [undefined] | [null] : [TEventMap[T]]): void
  • Declare how the bus should handle events emitted that have no listeners. Will be invoked when an instance's options.allowUnhandledEvents = false (default is true). The default implementation is to throw an error.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • event: T
    • Rest ...payload: TEventMap[T] extends void ? [] | [undefined] | [null] : [TEventMap[T]]

    Returns void

  • Subscribe a callback to event(s). alias of Bus.proxy when invoked with WILDCARD, alias of Bus.any when invoked with an array of events

    Type Parameters

    • T extends string | number | symbol | (keyof TEventMap)[]

    Parameters

    Returns Subscription

  • emit<T>(event: T, ...payload: EventPayload<TEventMap, T>): boolean
  • Type Parameters

    • T extends string | number | symbol

    Parameters

    • event: T
    • Rest ...payload: EventPayload<TEventMap, T>

    Returns boolean

  • Handle multiple events with the same handler. EventHandlers.MultiEventHandler receives raised event as first argument, payload as second argument

    Type Parameters

    • TEvents extends (keyof TEventMap)[]

    Parameters

    Returns Subscription

  • Create a proxy for all events raised. Like Bus.any, handlers receive the raised event as first argument and payload as second argument.

    Parameters

    Returns Subscription

  • next<T>(resolutionTrigger: T, rejectionTrigger?: T extends "*" ? never : T extends (keyof TEventMap)[] ? Exclude<keyof TEventMap, T[number]> | Exclude<keyof TEventMap, T[number]>[] : T extends keyof TEventMap ? Exclude<keyof TEventMap, T> | Exclude<keyof TEventMap, T>[] : never): CancelablePromise<T extends keyof TEventMap ? TEventMap[T] : void>
  • Utility for resolving/rejecting a promise based on the reception of an event. Promise will resolve with event payload, if a single event, or undefined if listening to multiple events.

    Type Parameters

    • T extends string | number | symbol | (keyof TEventMap)[]

    Parameters

    • resolutionTrigger: T

      what event/events should resolve the promise

    • Optional rejectionTrigger: T extends "*" ? never : T extends (keyof TEventMap)[] ? Exclude<keyof TEventMap, T[number]> | Exclude<keyof TEventMap, T[number]>[] : T extends keyof TEventMap ? Exclude<keyof TEventMap, T> | Exclude<keyof TEventMap, T>[] : never

      what event/events should reject the promise. Must be mutually disjoint with resolvingEvent

    Returns CancelablePromise<T extends keyof TEventMap ? TEventMap[T] : void>

  • scan<TEvaluator>(params: { evaluator: TEvaluator; trigger: Listenable<keyof TEventMap>; eager?: boolean; pool?: boolean; timeout?: number }): CancelablePromise<TEvaluator extends Evaluator<U, TEventMap> ? U : any>
  • Utility for resolving/rejecting a promise based on an evaluation done when an event is triggered. If params.eager=true (default), evaluates condition immedately. If evaluator resolves or rejects in the eager evaluation, the scanner does not subscribe to any events

    Type Parameters

    • TEvaluator extends Evaluator<any, TEventMap>

    Parameters

    • params: { evaluator: TEvaluator; trigger: Listenable<keyof TEventMap>; eager?: boolean; pool?: boolean; timeout?: number }
      • evaluator: TEvaluator

        an evaluation function that should check for a certain state and may resolve or reject the scan based on the state.

      • trigger: Listenable<keyof TEventMap>

        event or events that should trigger evaluator

      • Optional eager?: boolean

        should params.evaluator be called immediately; default is true. This eliminates the following anti-pattern:

        if(!someCondition) {
        await this.scan({evaluator: evaluateSomeCondition, trigger: ...});
        }
      • Optional pool?: boolean

        attempt to pool scanners that can be resolved by the same evaluator and trigger; default is true

      • Optional timeout?: number

        cancel the scan after params.timeout milliseconds. Values <= 0 are ignored. Currently pooling timeouts is not supported. If params.timeout is configured, it will disable pooling regardless if params.pool=true

    Returns CancelablePromise<TEvaluator extends Evaluator<U, TEventMap> ? U : any>

  • pipe<TDelegate>(delegate: TDelegate): TDelegate
  • Pipe one bus's events into another bus's subscribers

    Type Parameters

    • TDelegate extends Bus<TEventMap, TDelegate>

    Parameters

    • delegate: TDelegate

    Returns TDelegate

  • unpipe<TDelegate>(delegate: TDelegate): void
  • Type Parameters

    • TDelegate extends Bus<TEventMap, TDelegate>

    Parameters

    • delegate: TDelegate

    Returns void

  • Subscribe to meta changes to the Bus with Lifecycle events

    Type Parameters

    • L extends "error" | "active" | "idle" | "willActivate" | "willIdle" | "willAddListener" | "didAddListener" | "willRemoveListener" | "didRemoveListener" | "willDestroy"

    Parameters

    Returns Subscription

  • monitor(handler: ((activeState: boolean) => void)): Subscription
  • Subscribe to meta states of the Bus, idle and active. Bus becomes idle when it goes from 1 to 0 subscribers, and active when it goes from 0 to 1. The handler receives a boolean indicating if the bus is active (true) or idle (false)

    Parameters

    • handler: ((activeState: boolean) => void)
        • (activeState: boolean): void
        • Parameters

          • activeState: boolean

          Returns void

    Returns Subscription

  • hasListenersFor(event: "*" | keyof TEventMap): boolean
  • Parameters

    • event: "*" | keyof TEventMap

    Returns boolean

  • hasOwnListenersFor(event: "*" | keyof TEventMap): boolean
  • Parameters

    • event: "*" | keyof TEventMap

    Returns boolean

  • hasDelegateListenersFor(event: "*" | keyof TEventMap): boolean
  • Parameters

    • event: "*" | keyof TEventMap

    Returns boolean

  • getListenerCountFor(event: "*" | keyof TEventMap): number
  • Parameters

    • event: "*" | keyof TEventMap

    Returns number

  • getOwnListenerCountFor(event: "*" | keyof TEventMap): number
  • Parameters

    • event: "*" | keyof TEventMap

    Returns number

  • getDelegateListenerCountFor(event: "*" | keyof TEventMap): number
  • Parameters

    • event: "*" | keyof TEventMap

    Returns number

Events

  • destroy(): void
  • Remove all event subscribers, lifecycle subscribers, and delegates triggers lifecycle meta events for all subscribed events before removing lifecycle subscribers

    Emits

    Lifecycle.willDestroy Lifecycle.willDestroy

    Returns void

Generated using TypeDoc