Type a State Machine Class from Its Table
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Write a class Machine, created with a transitions table and an initial state: the table maps each state to the events it handles and the state each one leads to, like { idle: { FETCH: "loading" }, loading: { DONE: "idle" } }. state is the current state; send(event) moves to the state the table gives for that event from the current state, or stays put if the current state doesn't handle it, and returns the state it ends in. Type it from the table alone: state is the union of the table's states, send only accepts events that appear somewhere in the table, and a transition to a state that isn't in the table, or an initial state that isn't in it, is a type error.
Your code must pass
- Moves to the next state on an event
- Ignores an event the current state doesn't handle
- send returns the state it ends in
- Has no type errors
- state is the union of the table's states
- send only takes events from the table
- Rejects unknown target and initial states