UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

22 lines (21 loc) 518 B
import { __optsWithID } from "./idgen.js"; import { stream } from "./stream.js"; const fromAtom = (atom, opts) => { opts = __optsWithID("atom", { emitFirst: true, changed: (a, b) => a !== b, ...opts }); return stream((stream2) => { atom.addWatch(stream2.id, (_, prev, curr) => { if (opts.changed(prev, curr)) { stream2.next(curr); } }); opts.emitFirst && stream2.next(atom.deref()); return () => atom.removeWatch(stream2.id); }, opts); }; export { fromAtom };