Deviopath › TypeScript

State Machine with Guards

Hard · Modelling

Write runMachine(config, events) that runs a finite state machine described by typed data (see the starter).

Start in config.initial. For each event (with index i):

- If the current state is listed in config.final, the event is rejected.

- Otherwise use the FIRST transition (in list order) whose from equals the current state, whose event equals event.type, and whose guard passes. A missing guard always passes; a guard passes when event.value <op> guard.value is true.

- If a transition is found, move to its to state and append it to path. If not, push i to rejected.

Return { state, path, rejected } where path starts with the initial state.

Next: Kanban Board Reducer