import { BehaviorSubject, Observable, ObservableInput, PartialObserver, Subject, Subscriber, Subscription, TeardownLogic } from 'rxjs'; import { mergeMap } from 'rxjs/operators'; export type Predicate = (item: T) => boolean; /** A function accepting an item and returning a value, an Observable constructor, an ObservableInput, or void */ export type EventHandler = (item: T) => ((this: Observable, subscriber: Subscriber) => TeardownLogic) | ObservableInput | void; /** Handles events of an effects' subscription, distinct from its Observable's notifications. */ export type SubscriptionObserver = { subscribe: () => void; unsubscribe: () => void; finalize: () => void; }; /** An object of callbacks for `next`,`complete`,`error`,`subscribe` (started), `unsubscribe` (canceled), and `finalize` events of an effect's lifecycle. */ export type EffectObserver = PartialObserver | SubscriptionObserver; export type ObserverKey = keyof PartialObserver | keyof SubscriptionObserver; export type MapFn = (t?: T) => U; export type Mapper = Partial>>; export interface ActivityTracked { isActive: BehaviorSubject; } export type MatchPredicate = ((i: EventType) => i is MatchType) | ((i: EventType) => boolean); export type Matcher = MatchPredicate | { match: MatchPredicate; }; /** * 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. */ export declare class Bus { private events; /** A Subject that notifies each time `bus.reset()` is called */ resets: Subject; private guards; private filters; private spies; private handlings; /** 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 {@link Bus.listen} {@link Bus.trigger} {@link Bus.guard} * @example `bus.errors.subscribe(ex => { console.error(ex) })` */ errors: Subject; constructor(); /** * 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. * @param matcher A predicate to select events for which it returns `true`. * @see { @link Bus.reset } * @example `bus.query(() => true).subscribe(console.log)` */ query(matcher: Matcher): Observable; /** * 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. * @param matcher A predicate to select the first event for which it returns `true`. */ nextEvent(matcher: Matcher): Promise; /** * Puts an event onto the bus, firing any listeners whose predicate returns `true`. * Events go first through guards, then filters, then spies, then listeners. * * @param item The item to send to listeners, once it clears guards, filters, and spies. * @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 { @link Bus.errors } { @link Bus.filter } { @link Bus.guard } { @link Bus.spy } { @link Bus.listen } */ trigger(item: EventType): void; /** Alias for { @link Bus.trigger } */ next(item: EventType): void; /** 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. * @param matcher 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. * @param handler The side-effect producing function which will _"Return The Work"_, as an `ObservableInput` (A Promise, Observable, or async generator) * @param observer An { @link EffectObserver } which provides functions to be called upon notifications of the handler * @param operator Allows a custom RxJS operator to be passed to use its own ConcurrencyMode. * @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](https://d2jksv3bi9fv68.cloudfront.net/rxfx/mode-immediate-sm.png) */ listen(matcher: Matcher, handler: EventHandler, observer?: EffectObserver, /** @ignore */ operator?: typeof mergeMap): Subscription & ActivityTracked; /** 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`). * @param matcher 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. * @param handler The side-effect producing function which will _"Return The Work"_, as an `ObservableInput` (A Promise, Observable, or async generator) * @param observer An { @link EffectObserver } which provides functions to be called upon notifications of the handler * @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](https://d2jksv3bi9fv68.cloudfront.net/rxfx/mode-queueing-sm.png) */ listenQueueing(matcher: Matcher, handler: EventHandler, observer?: EffectObserver): Subscription & ActivityTracked; /** 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`). * @param matcher 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. * @param handler The side-effect producing function which will _"Return The Work"_, as an `ObservableInput` (A Promise, Observable, or async generator) * @param observer An { @link EffectObserver } which provides functions to be called upon notifications of the handler * @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](https://d2jksv3bi9fv68.cloudfront.net/rxfx/mode-switching-sm.png) */ listenSwitching(matcher: Matcher, handler: EventHandler, observer?: EffectObserver): Subscription & ActivityTracked; /** 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`). * @param matcher 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. * @param handler The side-effect producing function which will _"Return The Work"_, as an `ObservableInput` (A Promise, Observable, or async generator) * @param observer An { @link EffectObserver } which provides functions to be called upon notifications of the handler * @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](https://d2jksv3bi9fv68.cloudfront.net/rxfx/mode-blocking-sm.png) */ listenBlocking(matcher: Matcher, handler: EventHandler, observer?: EffectObserver): Subscription & ActivityTracked; /** 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. * @param matcher 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. * @param handler Called for each matching event, returns an ObservableInput (an Iterable,Promise,Observable) * @param observer An { @link EffectObserver } which provides functions to be called upon notifications of the handler * @returns A subscription that can be used to unsubscribe the listener, thereby canceling work in progress. * @summary ![toggling mode](https://d2jksv3bi9fv68.cloudfront.net/rxfx/mode-toggling-sm.png) */ listenToggling(matcher: Matcher, handler: EventHandler, observer?: EffectObserver): Subscription & ActivityTracked; /** 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. * */ guard(matcher: Matcher, fn: (item: MatchType) => void): 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. */ filter(matcher: Matcher, fn: (item: MatchType) => EventType | null | undefined): Subscription; /** 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. */ spy(fn: (item: EventType) => void): Subscription; /** 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. */ reset(): void; /** Creates an {@link 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. */ observeAll(): EffectObserver; /** Creates an {@link 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' }) }) * ); * ``` **/ observeWith(mapper: Mapper): PartialObserver & SubscriptionObserver; private getHandlingResult; private createRemovalSub; } export declare const defaultBus: Bus; /** A predicate returning true for any event. * @example bus.query(ANY).subscribe(...) */ export declare const ANY: (_: any) => boolean; /** A predicate returning true for any event. * @example bus.query(ALL).subscribe(...) */ export declare const ALL: (_: any) => boolean; export declare function becomesInactive(activity: ActivityTracked): Promise;