Skip to content

State Machines & Statecharts

A state machine: one machine in exactly one state at a time, moving only on declared events. A statechart adds nesting, parallelism, and data. Every machine built with Mantaq is a statechart.

Plain flows use scattered flags (currentStep, booleans) that drift out of sync. A state machine removes that: the situation is the state, the only way to change it is a declared transition triggered by a declared event. No hidden in-between situations exist because they were never defined.

A state is one known situation. The checkout has basicInfo, payment, success. Always exactly one.

An event is something that happens and can move the machine (submitPayment, back). Only events move the machine.

A transition is a move from one state to another, triggered by an event.

A guard decides whether a transition fires. In Mantaq, the guard lives inside the handler (no separate guard syntax).

The machine is always in exactly one state. Being in payment means not in submitting. This is what makes it different from a bag of flags: flags can disagree, a machine cannot.

  • Initial state: where the machine starts (basicInfo).
  • Final state: the machine is done and won’t accept anything else (success).
basicInfo → shippingAddress → payment → submitting → success (final)
↕ (back) ↕ ↕ (back)
error

Each arrow is a transition. Each box is a state. back moves to an earlier step. From error the machine returns to payment for a retry.

  • Nesting. A state contains a whole sub-machine. Parent and child each keep their own states.
  • Parallelism. Several child flows run at once. Neither blocks the other.
  • Extended state. Data alongside the current state. Without it you’d need a separate state for every data combination.
TermMeaning
StateOne known situation the machine can be in.
StatechartState machine with nesting, parallelism, and data.
EventSomething that happens and can move the machine.
TransitionA move from one state to another, triggered by an event.
GuardCondition a transition must pass to fire.
Initial stateWhere the machine starts.
Final stateEnd of the machine; nothing accepted after it.
Extended stateData the machine carries alongside its current state.
Entry actionRuns when a state is entered.
Exit actionRuns when a state is left; leaving cancels in-flight work.
Compound stateA state containing its own nested flow.
Orthogonal regionsParallel parts of a machine, running at once.
DeterminismSame state + event + data = same result, always.

For a full glossary, see GLOSSARY.md.