This project is part of the @thi.ng/umbrella monorepo.
Declarative, reactive dataflow graph construction using @thi.ng/rstream, @thi.ng/atom and @thi.ng/transducers primitives.
Stream subscription types act as graph nodes and attached transducers as graph edges, transforming data for downstream consumers / nodes. Theoretically, allows cycles and is not restricted to DAG topologies, but care must be taken to avoid CPU hogging (user's responsibility).
yarn add @thi.ng/rstream-graph
Small(ish), fully commented projects can be found in the /examples
folder:
- Dataflow circles - Source, Live version
- SVG grid gen - Source, Live version
More basic:
import { Atom } from "@thi.ng/atom";
import * as rs from "@thi.ng/rstream";
import * as rsg from "@thi.ng/rstream-graph";
// (optional) state atom to source value change streams from
const state = new Atom({a: 1, b: 2});
// graph declaration / definition
const graph = rsg.initGraph(state, {
// this node sources both of its inputs
// from values in the state atom
add: {
fn: rsg.add,
ins: {
a: { path: "a" },
b: { path: "b" }
},
},
// this node receives values from the `add` node
// and the given iterable
mul: {
fn: rsg.mul,
ins: {
a: { stream: "add" },
b: { stream: () => rs.fromIterable([10, 20, 30]) }
},
}
});
// (optional) subscribe to individual nodes
graph.mul.subscribe({
next: (x) => console.log("result:", x)
});
// result: 30
// result: 60
// result: 90
// changes in subscribed atom values flow through the graph
setTimeout(() => state.resetIn("a", 10), 1000);
// result: 360
Please documentation in the source code for further details.
- Karsten Schmidt
© 2018 Karsten Schmidt // Apache Software License 2.0