Class Bus<EventType>

A Bus instance provides transportation of events across any part of a browser, server, mobile or native app. The 𝗥𝘅𝑓𝑥 bus also allows type-safe ways of triggering, concurrency controlling, and canceling async side-effects in a framework-independent, pure JavaScript way, akin to RxJS.

Type Parameters

  • EventType

Constructors

Properties

createRemovalSub: any
errors: Subject<string | Error>

Contains any un-rescued sync or async errors from listeners. Listener errors terminate their listener when unrescued, but are not propogated back to the trigger call that prompted them, rather they are consumable via bus.errors. Errors in one listener do not affect the trigger-er, or any other listener. In contrast, guard errors are raised to the trigger-er.

See

Bus.listen Bus.trigger Bus.guard

Example

`bus.errors.subscribe(ex => { console.error(ex) })`
events: any
filters: any
getHandlingResult: any
guards: any
handlings: any
resets: Subject<void>

A Subject that notifies each time bus.reset() is called

spies: any

Methods

  • Run a function synchronously for all runtime events, after guards, and prior to spies and listeners. A filter may modify, or replace an event. However the filter must return an event, or that event will not be seen by spies or listeners, and it will be as if the event was never triggered. This is what is meant to 'filter' out events. Throwing an exception will raise to the triggerer, but not terminate the filter.

    Type Parameters

    Parameters

    Returns Subscription

    A subscription that can be used to deactivate the filter.

  • Run a function synchronously for all runtime events, prior to all filters, spies and listeners. Throwing an exception will raise to the triggerer, but not terminate the guard.

    Type Parameters

    Parameters

    Returns Subscription

    A subscription that can be used to deactivate the guard.

  • Assigns a side-effect producing function to matching events in Concurrency Mode "Immediate". Newly returned effects are begun immediately, and so may complete in any order (ala mergeMap), or consume resources unboundedly.

    Type Parameters

    Parameters

    • matcher: Matcher<EventType, MatchType>

      A predicate run upon every event on the bus. The handler function is only executed if the predicate returns true. If the matcher provides a type guard, the handler will see its events as narrowed to that type.

    • handler: EventHandler<MatchType, HandlerReturnType>

      The side-effect producing function which will "Return The Work", as an ObservableInput (A Promise, Observable, or async generator)

    • Optional observer: EffectObserver<HandlerReturnType>

      An EffectObserver which provides functions to be called upon notifications of the handler

    Returns Subscription & ActivityTracked

    A Subscription that can be used to unsubscribe the listener from future events. If the handler returned an Observable of work, any in-progress work will be canceled upon unsubscribe().

    Summary

    immediate mode

  • Assigns a side-effect producing function to matching events in Concurrency Mode "Blocking" (aka singleton). A new effect is not begun if one is in progress. (ala exhaustMap).

    Type Parameters

    Parameters

    • matcher: Matcher<EventType, MatchType>

      A predicate run upon every event on the bus. The handler function is only executed if the predicate returns true. If the matcher provides a type guard, the handler will see its events as narrowed to that type.

    • handler: EventHandler<MatchType, HandlerReturnType>

      The side-effect producing function which will "Return The Work", as an ObservableInput (A Promise, Observable, or async generator)

    • Optional observer: EffectObserver<HandlerReturnType>

      An EffectObserver which provides functions to be called upon notifications of the handler

    Returns Subscription & ActivityTracked

    A Subscription that can be used to unsubscribe the listener from future events. If the handler returned an Observable of work, any in-progress work will be canceled upon unsubscribe().

    Summary

    blocking mode

  • Assigns a side-effect producing function to matching events in Concurrency Mode "Queueing". Newly returned effects are enqueued and always complete in the order they were triggered (ala concatMap).

    Type Parameters

    Parameters

    • matcher: Matcher<EventType, MatchType>

      A predicate run upon every event on the bus. The handler function is only executed if the predicate returns true. If the matcher provides a type guard, the handler will see its events as narrowed to that type.

    • handler: EventHandler<MatchType, HandlerReturnType>

      The side-effect producing function which will "Return The Work", as an ObservableInput (A Promise, Observable, or async generator)

    • Optional observer: EffectObserver<HandlerReturnType>

      An EffectObserver which provides functions to be called upon notifications of the handler

    Returns Subscription & ActivityTracked

    A Subscription that can be used to unsubscribe the listener from future events. If the handler returned an Observable of work, any in-progress work will be canceled upon unsubscribe().

    Summary

    queueing mode

  • Assigns a side-effect producing function to matching events in Concurrency Mode "Switching". Any existing effect is canceled (if it is an Observable, not a Promise) before the new effect is begun (ala switchMap).

    Type Parameters

    Parameters

    • matcher: Matcher<EventType, MatchType>

      A predicate run upon every event on the bus. The handler function is only executed if the predicate returns true. If the matcher provides a type guard, the handler will see its events as narrowed to that type.

    • handler: EventHandler<MatchType, HandlerReturnType>

      The side-effect producing function which will "Return The Work", as an ObservableInput (A Promise, Observable, or async generator)

    • Optional observer: EffectObserver<HandlerReturnType>

      An EffectObserver which provides functions to be called upon notifications of the handler

    Returns Subscription & ActivityTracked

    A Subscription that can be used to unsubscribe the listener from future events. If the handler returned an Observable of work, any in-progress work will be canceled upon unsubscribe().

    Summary

    switching mode

  • Triggers effects upon matching events, using a Toggling (gate) Concurrency Strategy. A new effect is not begun if one is in progress, and the existing effect is canceled.

    Type Parameters

    Parameters

    Returns Subscription & ActivityTracked

    A subscription that can be used to unsubscribe the listener, thereby canceling work in progress.

    Summary

    toggling mode

  • Alias for Bus.trigger

    Parameters

    Returns void

  • Returns a Promise for the first event for which the predicate returns true. The returned Promise will be rejected upon a bus.reset. If the predicate is a type guard, the returned Promise will be narrowed to the matching type.

    Type Parameters

    Parameters

    Returns Promise<MatchType>

  • Returns an Observable of events for which the predicate returns true. The returned Observable completes upon a bus.reset. If the predicate is a type guard, the returned Observable will be narrowed to the matching type.

    Type Parameters

    Parameters

    Returns Observable<EventType>

    See

    Bus.reset

    Example

    `bus.query(() => true).subscribe(console.log)`
    
  • Unsubscribes all guards, filters, spies and listeners, canceling handlings-in-progress if they were returned Observables, and reverting the bus to how it was when newly constructed.

    Returns void

  • Run a function (synchronously) for all runtime events, prior to all listeners. Throwing an exception will terminate the spy.

    Parameters

    • fn: ((item) => void)
        • (item): void
        • Parameters

          Returns void

    Returns Subscription

    A subscription that can be used to deactivate the spy.

  • Puts an event onto the bus, firing any listeners whose predicate returns true. Events go first through guards, then filters, then spies, then listeners.

    Parameters

    • item: EventType

      The item to send to listeners, once it clears guards, filters, and spies.

    Returns void

    Throws

    if a guard, or filter throws an exception. Listener exceptions or errors do not throw, but appear on bus.errors, and terminate that listener.

    See

    Bus.errors Bus.filter Bus.guard Bus.spy Bus.listen