@jgomo3/jflow
v0.1.1
Published
Minimal flow/state-machine pipeline library for JavaScript
Readme
jflow
Minimal flow/state-machine pipeline library for JavaScript and TypeScript.
Install
pnpm add jflowQuickstart
import { createFlow, done, next, run } from "jflow";
type Data = { count: number };
const flow = createFlow<Data, undefined, "start" | "step" | "done">({
initial: "start",
states: {
start: (data) => next("step", { count: data.count + 1 }),
step: (data) => next("done", { count: data.count + 1 }),
done: (data) => done(data)
}
});
const result = await run(flow, { count: 0 });
console.log(result.count);API
createFlow
createFlow({ initial, states })initial: starting state keystates: map of state handlers
run
run(flow, input, options?)options.context: context passed to handlersoptions.maxSteps: guard for infinite loops (default 10000)options.onState: called before each handleroptions.onTransition: called after each transitionoptions.onError: called when a handler throws or returnserror
Transition helpers
next(state, data)
done(data)
error(err, data?)Demo
Order processing example:
pnpm demoThe demo source is in demo/index.ts.
Appendix
This project is inspired by Clojure's flow project and adapts similar ideas to a minimal JS/TS API.
