qmuntal / stateless

Go library for creating finite state machines
https://wikipedia.org/wiki/Finite-state_machine
BSD 2-Clause "Simplified" License
974 stars 53 forks source link

How To Fail A State Transition? #82

Open billyb2 opened 3 months ago

billyb2 commented 3 months ago

Hi! While working with this library, I noticed that it isn't really possible to fail a state transition. If a state's OnEntry or OnExit return an error, it looks like the state of the FSM is still updated. Is there a way to automatically set the state back to how it previously was, if the transition fails? I of course could do that by having another trigger, but having that happen automatically could be useful.

Thank you for this amazing library :)

daniel-sullivan commented 1 month ago

An interesting thing I've just discovered while going through the code and considering writing my own implementation is that the problem you'll always get is that the OnExit of the previous state has already been called so you'd have to re-enter it. I also noticed though that returning an error on OnExit will stop the state from transitioning.

In our case, we can put the bootstrap code for the next state in the OnExit of the previous state (very simple enabled / disabled flow)