@hello-terrain/work
v0.1.1
Published
A small reactive task graph library for scheduling dependent computations.
Readme
@hello-terrain/work
A small reactive task graph for typed async computations.
Quick start
import { graph, param, task } from "@hello-terrain/work";
const value = param(2);
const calcSquare = task((get, work) => {
const currentValue = get(value);
return work(() => currentValue * currentValue);
});
const calcGraph = graph();
calcGraph.add(calcSquare);
await calcGraph.run();
calcGraph.get(calcSquare); // 4
value.set(4);
await calcGraph.run();
calcGraph.get(calcSquare); // 16Semantics
- Dependency tracking: tasks discover dependencies by calling
get(ref)beforework(...). - Targets must be registered: tasks passed as
targetsmust be registered viag.add(task). - Upstream tasks referenced by
get(otherTask)are registered automatically when discovered. cache:"none":\n - the task recomputes on every run\n - any downstream tasks are treated as dirty every run\n - within a run, downstream tasks can still depend on values computed earlier in the run
Lanes and laneConcurrency
Tasks can be tagged with a lane (default "cpu"). Lanes become meaningful when you pass
laneConcurrency to graph.run(), which enables per-lane concurrency limits for that run.
- If
laneConcurrencyis omitted (or{}), tasks are not throttled by lane. - If
laneConcurrencyis provided and non-empty, tasks acquire a permit for their lane before running. Lanes not listed inlaneConcurrencydefault to 1 permit.
import { graph, task } from "@hello-terrain/work";
const cpuTask = task((_get, work) => work(() => expensiveCompute()))
.lane("cpu")
.displayName("cpuTask");
const ioTask = task(async (_get, work, ctx) =>
work(async () => fetch("https://example.com", { signal: ctx.signal })),
)
.lane("io")
.displayName("ioTask");
const g = graph();
g.add(cpuTask);
g.add(ioTask);
await g.run({
targets: [cpuTask, ioTask],
laneConcurrency: {
cpu: 2,
io: 8,
},
});Benchmarks
This package uses mitata for microbenchmarks:
pnpm --filter @hello-terrain/work benchProject Architecture
This library uses unbuild for building.
src/index.tsis the main entry point for your library exports- Add your library code in the
srcfolder tests/contains your test files
Libraries
The following libraries are used - checkout the linked docs to learn more
- unbuild - Unified JavaScript build system
Tools
- Vitest - Fast unit test framework powered by Vite
- Oxlint - A fast linter for JavaScript and TypeScript
- Oxfmt - Fast Prettier-compatible code formatter
Development Commands
pnpm installto install the dependenciespnpm run buildto build the library into thedistfolderpnpm run testto run the testspnpm run releaseto build and publish to npm
This library was generated with create-krispya
