qmuntal / stateless

Go library for creating finite state machines
BSD 2-Clause "Simplified" License
942 stars 49 forks source link

document idiomatic in-state looping #26

Closed soypat closed 2 years ago

soypat commented 3 years ago

It's fairly common to want to loop over some code when in a state and exit once exiting.

Say you are controlling a orbital launcher, for example. You'd want to loop over a set of instructions that measure position and acceleration and perform some actuation to control trajectory (state controlled_ascent). When your rocket fuel is empty you'd want to switch to a new piece of code that continuously reads sensor data and detects when below a certain altitude->deploy parachutes (state prepare_chute).

Given this problem, what would be an idiomatic way of implementing this state machine with stateless?

qmuntal commented 3 years ago

You could do something similar to the phonecall example using OnEntry and OnExit actions:

https://github.com/qmuntal/stateless/blob/f14dd0ecd3650a38f0fb6437386b73de0c190c00/example_test.go#L46-L50