Uses a 'handlers' structure to determine when to change
state and actions to take.
The structure is a set of logical conditions: if we're in
this state, then move to this other state etc.
consthandlers = [ { // If we're in the 'sleeping' state, move to next state if:'sleeping', then: { next:true } }, { // If we're in the 'waking' state, randomly either go to 'resting' or 'sleeping' state if:'waking', then: [ () => { if (Math.random() > 0.5) { return { next:'resting' } } else { return { next:'sleeping' } } } ] } ];
Set up the driver, and call run() when you want to get
the machine to change state or take action:
constdriver = awaitStateMachine.driver(states, handlers); setInterval(async () => { awaitdriver.run(); // Note use of 'await' again }, 1000);
Essentially, the 'handlers' structure gets run through each time run()
is called.
Defaults to selecting the highest-ranked result to determine
what to do next.
Drives a state machine.
Uses a 'handlers' structure to determine when to change state and actions to take.
The structure is a set of logical conditions: if we're in this state, then move to this other state etc.
Set up the driver, and call
run()
when you want to get the machine to change state or take action:Essentially, the 'handlers' structure gets run through each time
run()
is called.Defaults to selecting the highest-ranked result to determine what to do next.