UNPKG

@thi.ng/transducers-async

Version:

Async versions of various highly composable transducers, reducers and iterators

83 lines 2.49 kB
import type { Fn, Fn0, Maybe } from "@thi.ng/api"; /** * Creates a new {@link Mult} instance which allows splitting a single `src` * async iterable into multiple parallel subscribers. Iteration only starts when * the first subscriber is attached (via {@link Mult.subscribe}) and back * pressure is handled by waiting for **all** child subscribers to deliver their * values before the next value from `src` is consumed. `Mult` allows dynamic * subscriptions and unsubscriptions and will stop consuming from `src` when no * further subscribers are attached. * * @example * ```ts tangle:../export/mult.ts * import { map, mult, run, wait } from "@thi.ng/transducers-async"; * * const root = mult( * (async function* () { * yield "hello"; * await wait(1000); * yield "world"; * await wait(1000); * yield "good bye"; * })() * ); * * // 1st subscriber (vanilla JS) * (async () => { * for await (let x of root.subscribe()) console.log("vanilla:", x); * })(); * * // 2nd subscriber (transducer), attached with delay * setTimeout( * () => * run( * map(async (x) => { * console.log("tx", x); * await wait(1500); * }), * root.subscribe() * ), * 900 * ); * * // vanilla: hello * // vanilla: world * // tx world * // vanilla: good bye * // tx good bye * ``` * * @param src */ export declare const mult: <T>(src: AsyncIterable<T>) => Mult<T>; export declare class Mult<T> { src: AsyncIterable<T>; protected subs: MSub<T>[]; protected isActive: boolean; constructor(src: AsyncIterable<T>); /** * Creates a new subscription (aka custom `AsyncIterable`) which will * receive any future values from `src`. The returned subscription can be * removed again via {@link Mult.unsubscribe}. */ subscribe(): AsyncIterable<T>; /** * Attempts to remove given child subscription (presumably created via * {@link Mult.subscribe}). Returns true if removal was successful. * * @param sub */ unsubscribe(sub: AsyncIterable<T>): boolean; } /** @internal */ export declare class MSub<T> { valueP: Promise<Maybe<T>>; notifyP: Promise<void>; resolve: Fn<Maybe<T>, void>; notify: Fn0<void>; active: boolean; constructor(); [Symbol.asyncIterator](): AsyncGenerator<Awaited<Awaited<T> & null> | Awaited<Awaited<T> & {}>, void, unknown>; protected $await(): void; } //# sourceMappingURL=mult.d.ts.map