@thi.ng/rstream
Version:
Reactive streams & subscription primitives for constructing dataflow graphs / pipelines
58 lines • 1.8 kB
TypeScript
import type { Predicate2 } from "@thi.ng/api";
import type { ReadonlyAtom } from "@thi.ng/atom";
import type { CommonOpts } from "./api.js";
export interface FromAtomOpts<T> extends CommonOpts {
/**
* True, if the current atom value should be emitted when the stream
* activates.
*
* @defaultValue true
*/
emitFirst: boolean;
/**
* User predicate to determine value changes in atom. New values are
* only emitted on stream if the predicate returns true.
*
* @defaultValue `!==`
*/
changed: Predicate2<T>;
}
/**
* Yields {@link Stream} of value changes in given [Atom-like state
* container](https://thi.ng/atom).
*
* @remarks
* [Attaches a
* watch](https://docs.thi.ng/umbrella/api/interfaces/IWatch.html#addWatch) to
* the atom and checks for value changes with given `changed` predicate (`!==`
* by default). If the predicate returns truthy result, the new value is emitted
* on the stream. If `emitFirst` is true (default), also emits atom's current
* value when first subscriber attaches to stream.
*
* Also see {@link fromView}, {@link fromViewUnsafe}
*
* @example
* ```ts tangle:../export/from-atom.ts
* import { defAtom, defCursor } from "@thi.ng/atom";
* import { fromAtom, trace } from "@thi.ng/rstream";
*
* type DB = { a: number; b?: number };
*
* const db = defAtom<DB>({ a: 23, b: 88 });
* const cursor = defCursor(db, ["a"])
*
* fromAtom(cursor).subscribe(trace("cursor val:"))
* // cursor val: 23
*
* cursor.reset(42);
* // cursor val: 42
*
* db.reset({a: 66})
* // cursor val: 66
* ```
*
* @param atom -
* @param opts -
*/
export declare const fromAtom: <T>(atom: ReadonlyAtom<T>, opts?: Partial<FromAtomOpts<T>>) => import("./stream.js").Stream<T>;
//# sourceMappingURL=atom.d.ts.map