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

Hierarchy

  • Bus

Constructors

  • Type Parameters

    • EventType

    Returns Bus<EventType>

Properties

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

listen trigger guard

Example

bus.errors.subscribe(ex => { console.error(ex) })

events: Subject<EventType>
filters: [Predicate<EventType>, ((item: EventType) => undefined | null | EventType)][]
guards: [Predicate<EventType>, ((item: EventType) => void)][]
handlings: Subject<Observable<void>>
resets: Subject<void>
spies: [Predicate<EventType>, ((item: EventType) => void)][]

Methods

  • Parameters

    • matcher: Function
    • fn: Function
    • all: [Predicate<EventType>, ((item: EventType) => unknown)][]

    Returns Subscription

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

    Returns

    A subscription that can be used to deactivate the filter.

    Type Parameters

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => boolean) | ((i: EventType) => i is MatchType)
    • fn: ((item: MatchType) => undefined | null | EventType)
        • (item: MatchType): undefined | null | EventType
        • Parameters

          • item: MatchType

          Returns undefined | null | EventType

    Returns Subscription

  • Type Parameters

    • HandlerReturnType

    Parameters

    • handler: EventHandler<EventType, HandlerReturnType>
    • event: EventType

    Returns Observable<HandlerReturnType>

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

    Returns

    A subscription that can be used to deactivate the guard.

    Type Parameters

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => boolean) | ((i: EventType) => i is MatchType)
    • fn: ((item: MatchType) => void)
        • (item: MatchType): void
        • Parameters

          • item: MatchType

          Returns void

    Returns Subscription

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

    Returns

    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

    Type Parameters

    • HandlerReturnType

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => i is MatchType) | ((i: EventType) => boolean)

      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 & {
        isActive: BehaviorSubject<boolean>;
    }

  • 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).

    Returns

    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

    Type Parameters

    • HandlerReturnType

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => boolean) | ((i: EventType) => i is 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 & {
        isActive: BehaviorSubject<boolean>;
    }

  • 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).

    Returns

    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

    Type Parameters

    • HandlerReturnType

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => boolean) | ((i: EventType) => i is 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 & {
        isActive: BehaviorSubject<boolean>;
    }

  • 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).

    Returns

    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

    Type Parameters

    • HandlerReturnType

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => boolean) | ((i: EventType) => i is 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 & {
        isActive: BehaviorSubject<boolean>;
    }

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

    Returns

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

    Summary

    toggling mode

    Type Parameters

    • HandlerReturnType

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => boolean) | ((i: EventType) => i is 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>

      Called for each matching event, returns an ObservableInput (an Iterable,Promise,Observable)

    • Optional observer: EffectObserver<HandlerReturnType>

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

    Returns Subscription & {
        isActive: BehaviorSubject<boolean>;
    }

  • Alias for trigger

    Parameters

    • item: EventType

    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

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => boolean) | ((i: EventType) => i is MatchType)

      A predicate to select the first event for which it returns true.

    Returns Promise<MatchType>

  • Creates an EffectObserver which triggers each value from the handler back onto the bus. Use this when the listener returns items suitable for putting directly onto the bus.

    Type Parameters

    • HandlerReturnType

    Returns EffectObserver<HandlerReturnType>

  • Creates an EffectObserver which triggers the handlers' lifecycle events, after running through a mapping function. Use this when the listener's values are not compatible with the bus, or to capture lifecycle events.

    Example

    bus.listen(
    isSearchRequest,
    () => from([{ result: 'foo' }]),
    bus.observeWith({ subscribe: () => ({ type: 'search/started' }) })
    );

    Type Parameters

    • HandlerReturnType

    Parameters

    Returns Object

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

    See

    reset

    Example

    bus.query(() => true).subscribe(console.log)

    Type Parameters

    • MatchType = EventType

    Parameters

    • matcher: ((i: EventType) => boolean) | ((i: EventType) => i is MatchType)

      A predicate to select events for which it returns true.

    Returns Observable<EventType>

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

    Returns

    A subscription that can be used to deactivate the spy.

    Parameters

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

          • item: EventType

          Returns void

    Returns Subscription

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

    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

    errors filter guard spy listen

    Parameters

    • item: EventType

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

    Returns void

Generated using TypeDoc