Events
An event is a typed message that triggers a transition. event("back")()
declares one.
Declare
Section titled “Declare”const back = event("back")();const submitBasicInfo = event("submitBasicInfo")<{ email: string; name: string }>();back.type; // "back"Payload must be object | void. Default: no payload.
Create instances
Section titled “Create instances”back.create(); // { type: "back" }submitBasicInfo.create({ email: "a@b.com", name: "A" });Check identity
Section titled “Check identity”back.is(someEvent); // boolean (type guard)Use in transitions
Section titled “Use in transitions”m.on(shippingAddress, submitShipping, (event, { context }) => { const s = context.get(); s.shippingAddress = event.payload; context.set(s); return { state: payment };});event.payload is typed from the event ref.
Guards (conditional transitions)
Section titled “Guards (conditional transitions)”No guard syntax. Plain if in the handler picks which { state } to return.
Return {} to stay put.
m.on(scanning, scanDone, (event) => ({ state: event.payload.risky ? flagged : cleared,}));Internal events
Section titled “Internal events”Declared in internal, not inputs. Emitted by effects. Processed after the
current transition. Nothing external can send them via send().
const paymentOk = event("paymentOk")<{ orderId: string }>();const paymentFail = event("paymentFail")<{ reason: string }>();Intercepts an event in every state. State-specific handler runs first;
onAny only fires if no state handler matched.
m.onAny(back, (_event, { context }) => { const s = checkout.state.name; if (s === "payment") { const cur = context.get(); cur.paymentInfo = undefined; context.set(cur); return { state: shippingAddress }; } if (s === "error") return { state: payment }; return {};});