UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

78 lines 3.42 kB
import type { Fn, Predicate2 } from "@thi.ng/api"; import { EquivMap } from "@thi.ng/associative/equiv-map"; import type { Transducer } from "@thi.ng/transducers"; import type { ISubscriber, ISubscription, TransformableOpts, WithErrorHandlerOpts } from "./api.js"; import { Subscription } from "./subscription.js"; export interface PubSubOpts<A, B, T> { /** * Topic function. Incoming values will be routed to topic subscriptions * using this function's return value. */ topic: Fn<B, T>; /** * Optional transformer for incoming values. If given, `xform` will be * applied first and the transformed value passed to the `topic` fn. */ xform?: Transducer<A, B>; /** * Equivalence check for topic values. Should return truthy result if given * topics are considered equal. */ equiv?: Predicate2<T>; /** * Optional subscription ID for the PubSub instance. */ id?: string; } /** * Topic based stream splitter. Applies `topic` function to each received value * and only forwards it to the child subscriptions of the returned topic. * * @remarks * The actual topic (return value from `topic` fn) can be of any type `T`, or * `undefined`. If the latter is returned, the incoming value will not be * processed further. Complex topics (e.g objects / arrays) are allowed and * they're matched against registered topics using * [`equiv`](https://docs.thi.ng/umbrella/equiv/functions/equiv.html) by default * (but customizable via `equiv` option). Each topic can have any number of * subscribers. * * If a `xform` transducer is given, it is always applied prior to passing the * input to the topic function. I.e. in this case the topic function will * receive the transformed inputs. * * {@link PubSub} supports dynamic topic subscriptions and unsubscriptions via * {@link PubSub.subscribeTopic} and {@link PubSub.unsubscribeTopic}. However, * the standard {@link ISubscribable.subscribe} / * {@link ISubscribable.unsubscribe} methods are NOT supported (since * meaningless) and will throw an error! `unsubscribe()` can only be called * WITHOUT argument to unsubscribe the entire `PubSub` instance (incl. all topic * subscriptions) from the parent stream. * * @param opts - */ export declare const pubsub: <A, B = A, T = any>(opts: PubSubOpts<A, B, T>) => PubSub<A, B, T>; /** * See {@link pubsub} for reference & examples. */ export declare class PubSub<A, B = A, T = any> extends Subscription<A, B> { topicfn: Fn<B, T>; topics: EquivMap<T, Subscription<B, B>>; constructor(opts: PubSubOpts<A, B, T>); /** * Unsupported. Use {@link PubSub.subscribeTopic} instead. */ subscribe(): Subscription<B, any>; /** * Unsupported. Use {@link PubSub.subscribeTopic} instead. */ transform(): Subscription<B, any>; subscribeTopic<C>(topicID: T, opts?: Partial<TransformableOpts<B, C>>): ISubscription<B, C>; subscribeTopic<C>(topicID: T, sub: ISubscriber<C>, opts?: Partial<TransformableOpts<B, C>>): ISubscription<B, C>; transformTopic<C>(topicID: T, xform: Transducer<B, C>, opts?: Partial<WithErrorHandlerOpts>): ISubscription<B, B>; unsubscribeTopic(topicID: T, sub: ISubscription<B, any>): boolean; unsubscribe(sub?: ISubscription<B, any>): boolean; done(): void; protected dispatch(x: B): boolean | undefined; } //# sourceMappingURL=pubsub.d.ts.map