Skip to content

Dispatching events

Dispatch is how state changes. You address a machine by its imported definition and send a typed event — 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.

  • In a template handleron:click={() => cart.send({ type: 'ADD_ITEM', productId: id })} (the read instance’s send).
  • In an API routedispatch(Machine, event) from the handler’s helpers.

Both run server-side today.

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.

The dispatch triggers a recompute; the response is a patch list targeting only the changed slots.

From a client island, dispatch to a server machine over /__events:

dispatch(CartMachine, { type: 'ADD_ITEM', productId: id })