Model allowed application state transitions
Quick answer
A state machine describes allowed states and the events that move between them. It prevents ambiguous combinations such as a request being both loading and successful by using one explicit state label.
How it works
Define initial state, events, permitted transitions and invalid-event policy. A transition function can return a new state or the original state when an event is ignored. Attach relevant data to each state and keep effects outside transitions. For async work, include request identity if old completion events might arrive after a newer request; the simplified examples have one active request.
Local examples
Run in Node.js v22.16.0 as a CommonJS script. Actual ByteMind editor support is unverified.
(async () => {const step=(s,e)=>e==="RESET"?"idle":s==="idle"&&e==="START"?"loading":s==="loading"&&e==="SUCCESS"?"ready":s==="loading"&&e==="FAILURE"?"error":s;console.log(step("idle","SUCCESS"),step("idle","START"),step("loading","FAILURE"));})()Expected console output:
idle loading errorA success before start is ignored; valid events change the phase.
Run in Node.js v22.16.0 as a CommonJS script. Actual ByteMind editor support is unverified.
(async () => {const table=new Map([["closed",new Map([["OPEN","open"]])],["open",new Map([["CLOSE","closed"]])]]);const step=(s,e)=>table.get(s)?.get(e)??s;let state="closed";for(const e of ["OPEN","OPEN","CLOSE"])state=step(state,e);console.log(state,step(state,"UNKNOWN"));})()Expected console output:
closed closedAn already-open OPEN event and unknown event do not change state.
Common mistakes
Do not combine independent flags for mutually exclusive phases unless you explicitly enforce their invariant. Do not send network requests inside a pure transition function.
Practice
Trace idle, loading, ready and error for START, SUCCESS, FAILURE and RESET; specify which events are ignored in each state.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Model allowed application state transitions — Knowledge Check
Apply the concept and reason about local example output.
What should a state-machine contract specify for an invalid event?
Why use a single phase for mutually exclusive request stages?
Where should network side effects occur in a pure transition design?
What is the exact console output in Model allowed application state transitions example 1?
(async () => {const step=(s,e)=>e==="RESET"?"idle":s==="idle"&&e==="START"?"loading":s==="loading"&&e==="SUCCESS"?"ready":s==="loading"&&e==="FAILURE"?"error":s;console.log(step("idle","SUCCESS"),step("idle","START"),step("loading","FAILURE"));})()What is the exact console output in Model allowed application state transitions example 2?
(async () => {const table=new Map([["closed",new Map([["OPEN","open"]])],["open",new Map([["CLOSE","closed"]])]]);const step=(s,e)=>table.get(s)?.get(e)??s;let state="closed";for(const e of ["OPEN","OPEN","CLOSE"])state=step(state,e);console.log(state,step(state,"UNKNOWN"));})()