A timer that can be ready, focusing, paused or on a break has rules: you can't
pause something that hasn't started, or resume something that isn't paused.
With a few booleans, such as isRunning and isPaused, those rules are spread
through the event handlers, and sooner or later two flags disagree. A state
machine writes the rules down in one place.
Each state lists the events it accepts, and the state each one leads
to. That's the whole set of rules. transition(status, event) looks the next
state up, and returns the same state for an event that isn't listed:
JavaScript
export function transition(status, event) { return machine[status][event] ?? status;}export function can(status, event) { return event in machine[status];}
Everything follows from the table
The reducer is just transition. There are no ifs in it:
JavaScript
function reducer(status, event) { const next = transition(status, event); console.log(`${status} + ${event} → ${next}`); return next;}
The buttons ask the machine: disabled={!can(status, event)}. They can
never offer something the machine would ignore.
Impossible states are impossible.status is one string, so "paused and
on a break" can't be represented.
Predict
The disabled buttons hide the machine's other safeguard. Add a button that is
always enabled, <button onClick={() => send("RESUME")}>Force resume</button>,
and press it while the timer is Ready. What does the console say?
Check your prediction
idle + RESUME → idle. The machine ignores an event its current state doesn't
list, so a stray event can't push it somewhere invalid. A boolean version would
happily set isRunning = true from anywhere.
When to use one
Reach for an explicit machine when a feature has several modes with rules
between them: a timer, an upload, a checkout, a multi-step form, a media player.
For bigger machines, with nested states, guards and delays, libraries such as
XState give you the same idea with more tools.
Exercise
A long break every fourth session
Real Pomodoro timers give a long break after every fourth focus session. That
needs the machine to remember data, not just which state it's in.
Count completed focus sessions, and show Sessions completed: 2 under the
heading. A session counts only when finish ends it, not reset.
After every fourth completed session, finish leads to a new state,
longBreak, labelled On a long break, instead of onBreak. From
longBreak, FINISH and RESET both go to idle.
machine.js stays the one place that says which events each state accepts.
After four rounds of start, finish, finish, the fourth round's first
finish should log:
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
The table can say which events a state accepts and where they usually lead,
but not "every fourth time": that depends on a number the status string doesn't
hold. Machines keep such data next to the status, as extended state, and use
it in a guard, a condition that chooses between two targets for the same
event.
Hint 2: a first step
Make the reducer's state an object with status and completed, starting at
"idle" and 0, and change the heading and the buttons to read state.status.
Everything should behave exactly as before: that proves the refactor. Then find
the one transition that needs the count, focusing + FINISH, and handle it
in the reducer. Don't forget the table entry for the new state.
Show one solution
In machine.js, add the new state. focusing still lists FINISH: "onBreak",
the usual target:
JavaScript
longBreak: { FINISH: "idle", RESET: "idle" },
In App.jsx:
JavaScript
const labels = { idle: "Ready", focusing: "Focusing", paused: "Paused", onBreak: "On a break", longBreak: "On a long break" };function reducer(state, event) { let next = transition(state.status, event); let completed = state.completed; if (state.status === "focusing" && event === "FINISH") { completed += 1; if (completed % 4 === 0) next = "longBreak"; } console.log(`${state.status} + ${event} → ${next}`); return { status: next, completed };}
and can(state.status, event) for the buttons. The table still decides
whether FINISH is allowed at all; the guard only picks between two targets
once it is. The On Your Own drill on Day 7 needs the same pattern: a machine
that carries an index and a score alongside its status.
Completion is saved in this browser only. It does not measure mastery.