Context
Context is the actor’s mutable data record. Pass it in at construction, read and replace it during the run. It survives transitions.
State decides where the machine is. Context decides what it knows.
Define
Section titled “Define”import { checkout, type CheckoutContext } from "../checkout.ts";
const c: CheckoutContext = checkout.context;Typed at compile time. No runtime cost.
Read and write
Section titled “Read and write”Handlers get a context handle:
context.get()returns the live recordcontext.set(s)writes it back (any call triggerschange, even same reference)- Mutate what
get()returned, thenset()it
m.on(basicInfo, submitBasicInfo, (event, { context }) => { const s = context.get(); s.basicInfo = event.payload; context.set(s); return { state: shippingAddress };});Nested mutations work the same way (s.items.push(item); context.set(s)).
Read outside transitions
Section titled “Read outside transitions”checkout.context.basicInfo?.email;Effects
Section titled “Effects”Effects get the same handle. Read, mutate, set(), then emit().
Children
Section titled “Children”Child actors (regions) have their own context. They communicate via events. See States.