Worked examples and recipes
Copyable flow traces and a programmatic TypeScript tracer example for teaching state changes.
Version scope — v0.0.2 (historical schema 1). This page and its examples use @0xbuff/throughline@0.0.2, the version pinned by this documentation app. They do not describe the unreleased schema-2 API intended for v0.2.0. See compatibility and upgrading.
These examples demonstrate the five-domain model without claiming to execute the algorithms or systems being explained. Each block is valid input to compile(). Use the Flow reference when editing event syntax.
Recipe 1: a queue receiving and consuming items
Show two distinct changes: enqueueing an item, then consuming the first element.
```thl flow
group "Queue" as row
node pending "Pending items" in "Queue" as array = ["A"]
node active "Active job" in "Queue" = null
link take pending -> active "dequeue"
step "Enqueue B"
set pending = ["A","B"]
focus pending
note "B joins the end of the queue."
step "Take A"
send take = "A"
focus pending, active
note "A is selected for processing."
step "Update state"
set pending = ["B"]
set active = "A"
focus pending, active
note "A leaves the queue and becomes active."
```Why two beats for taking a job? A transfer event does not mutate either endpoint. The separate update makes the before-and-after state visible.
Recipe 2: a staged deployment
Show the distinction between a prepared artifact, a live deployment, and a successful verification.
```thl flow
group "Release" as row
node build "Artifact" in "Release" = "built"
node stage "Staging" in "Release" = "idle"
node probe "Health check" in "Release" hidden = null
link deploy build -> stage "deploy"
link check stage -> probe "probe" hidden
step "Deploy to staging"
send deploy = "artifact"
set stage = "deploying"
focus stage
step "Deployment complete"
set stage = "running"
note "The staged artifact is running."
step "Verify"
show node probe
show link check
send check = "request"
set probe = "passing"
focus probe
note "The health check has passed."
```This is a conceptual workflow. Throughline neither deploys services nor performs real health checks.
Recipe 3: show a replacement node without erasing history
```thl flow
node before "Old value" = 1
node after "New value" hidden = 2
link replace before -> after "replaced by" hidden
step "Prepare replacement"
show node after
show link replace
focus before, after
send replace = 2
step "Commit replacement"
hide node before
hide link replace
focus after
note "The old object disappears from the scene, not from earlier frames."
```The previous snapshots remain available for seeking. Hidden nodes also retain their values.
Recipe 4: generate a trace in TypeScript
Use tracer() if an algorithm is easier to express as code but you want explicit educational snapshots rather than automatic instrumentation:
import { compileProgram, tracer } from "@0xbuff/throughline";
const t = tracer();
t.node("values", "Values", [3, 1, 2], { view: "array" });
t.step("Read first pair", (s) => {
s.focus("values[0]", "values[1]");
s.note("The first two values are out of order.");
});
t.step("Swap first pair", (s) => {
s.set("values[0]", 1);
s.set("values[1]", 3);
s.focus("values[0]", "values[1]");
s.note("The first pair is now ordered.");
});
const trace = compileProgram(t.program());
console.log(trace.frames.at(-1)?.state.entities[0].value); // [1, 3, 2]Calling t.get("values") between completed steps reads the current detached value. The step callback is synchronous; all events within it form one atomic beat. You are responsible for the correctness of the narrated algorithm.
Other starting points
Throughline ships three reference fixtures that can be imported rather than copied from prose:
import { EXAMPLES } from "@0xbuff/throughline/examples";
import { compile } from "@0xbuff/throughline";
const reconciliation = EXAMPLES.find((example) => example.id === "reconciliation");
if (!reconciliation) throw new Error("Example missing");
const trace = compile(reconciliation.source);The current package includes Reconciliation, Shared references, and Matrix multiplication.
For built-in computations, use the dedicated bindings, binary, matrix, or callstack domain guides instead of hand-authoring every arithmetic step.