Skip to content

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.

import { checkout, type CheckoutContext } from "../checkout.ts";
const c: CheckoutContext = checkout.context;

Typed at compile time. No runtime cost.

Handlers get a context handle:

  • context.get() returns the live record
  • context.set(s) writes it back (any call triggers change, even same reference)
  • Mutate what get() returned, then set() 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)).

checkout.context.basicInfo?.email;

Effects get the same handle. Read, mutate, set(), then emit().

Child actors (regions) have their own context. They communicate via events. See States.