image by katerha
[G] -> [Y] -> [R]
^ |
|_____________/
What if a "left turn only" green arrow existed as well?
[G] <-> [Y]
^ \ / ^
| X |
| / \ |
v v
[R] <-> [B]
States:
Transitions:
We may also need a state transition action: "when the button enters the 'active' state, send a 'click' event and then enter the 'Enabled' state"
The term "state" has several overlapping meanings.
The term "state machine" has several technical variants as well.
let states = {
"green": {canChangeTo: ["yellow"]},
"yellow": {canChangeTo: ["red"]},
"red": {canChangeTo: ["green"]}
}
let currentState = "green";
function enterState(newState) {
let validTransitions = states[currentState].canChangeTo;
if (validTransitions.includes(newState)) {
currentState = newState;
} else {
throw "Invalid state transition attempted - from " + currentState + " to " + newState;
}
}
/