UNPKG

@thi.ng/csp

Version:

Primitives & operators for Communicating Sequential Processes based on async/await and async iterables

66 lines 2.42 kB
import type { IObjectOf } from "@thi.ng/api"; import type { IClosable, IWriteable, TopicFn } from "./api.js"; import { Channel } from "./channel.js"; import { Mult } from "./mult.js"; /** * Syntax sugar for {@link PubSub} ctor. Creates a new `PubSub` which allows * multiple child subscriptions, based on given topic function. * * @remarks * The topic function will be called for each received value and its result is * used to determine which child subscription should receive the value. New * topic (un)subscriptions can be created dynamically via * {@link PubSub.subscribeTopic} and {@link PubSub.unsubscribeTopic} or * {@link PubSub.unsubscribeAll}. Each topic subscription is a {@link Mult}, * which itself allows for multiple child subscriptions. * * @param fn */ export declare function pubsub<T>(fn: TopicFn<T>): PubSub<T>; export declare function pubsub<T>(src: Channel<T>, fn: TopicFn<T>): PubSub<T>; export declare class PubSub<T> implements IWriteable<T>, IClosable { protected src: Channel<T>; protected fn: TopicFn<T>; protected topics: IObjectOf<Mult<any>>; /** * See {@link pubsub} for reference. * * @param fn */ constructor(fn: TopicFn<T>); constructor(src: Channel<T>, fn: TopicFn<T>); writable(): boolean; write(val: T): Promise<boolean>; close(): void; closed(): boolean; /** * Creates a new topic subscription channel and returns it. Each topic is * managed by its own {@link Mult} and can have arbitrary number of * subscribers. * * @param id - topic id */ subscribeTopic<S extends T = T>(id: string): Channel<S>; /** * Attempts to remove a subscription channel for given topic `id`. Returns * true if successful. If `close` is true (default), the given channel will * also be closed (only if unsubscription was successful). * * @remarks * See {@link Mult.subscribe} for reverse op. * * @param id * @param ch * @param close */ unsubscribeTopic<S extends T = T>(id: string, ch: Channel<S>, close?: boolean): boolean; /** * Removes all child subscription channels for given topic `id` and if * `close` is true (default) also closes them. * * @param close */ unsubscribeAll(id: string, close?: boolean): void; protected process(): Promise<void>; } //# sourceMappingURL=pubsub.d.ts.map