Dispatching events
Dispatch is how state changes. You address a machine by its imported definition and send a typed event — no magic strings.
Typed, no magic strings
Section titled “Typed, no magic strings”dispatch(CartMachine, { type: 'ADD_ITEM', productId: id })The event is checked against the machine’s declared union (EventOf<typeof CartMachine>). A wrong type or a missing field is a compile error.
Where dispatch happens
Section titled “Where dispatch happens”- In a template handler —
on:click={() => cart.send({ type: 'ADD_ITEM', productId: id })}(the read instance’ssend). - In an API route —
dispatch(Machine, event)from the handler’s helpers.
Both run server-side today.
Cross-machine fan-out
Section titled “Cross-machine fan-out”A dispatch records every touched machine. Subscribers (subscribes/emits) react, their machines are persisted, and on a live route the changes fan out to connected sessions.
How patches come back
Section titled “How patches come back”The dispatch triggers a recompute; the response is a patch list targeting only the changed slots.
Client dispatch
Section titled “Client dispatch”From a client island, dispatch to a server machine over /__events:
dispatch(CartMachine, { type: 'ADD_ITEM', productId: id })