summaryrefslogtreecommitdiff
path: root/internal/fsm/fsm.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2023-05-09 14:02:24 +0200
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2023-09-25 09:43:37 +0200
commit7a5593953c47bda301c7a1eccac2a0454c439835 (patch)
tree37814c8a11d17621847d3ada7bc008dc3a0cb97a /internal/fsm/fsm.go
parent98bbc7e60a70fc030846fb16294d813af8131e3a (diff)
FSM: Allow to always go back to the initial state
Diffstat (limited to 'internal/fsm/fsm.go')
-rw-r--r--internal/fsm/fsm.go7
1 files changed, 7 insertions, 0 deletions
diff --git a/internal/fsm/fsm.go b/internal/fsm/fsm.go
index cae8833..c7ae104 100644
--- a/internal/fsm/fsm.go
+++ b/internal/fsm/fsm.go
@@ -71,6 +71,9 @@ type FSM struct {
// GetStateName gets the name of a state as a string
GetStateName func(StateID) string
+
+ // initial is the initial state that we can always go back to
+ initial StateID
}
// Init initializes the state machine and sets it to the given current state.
@@ -88,6 +91,7 @@ func (fsm *FSM) Init(
fsm.Directory = directory
fsm.GetStateName = nameGen
fsm.Generate = generate
+ fsm.initial = current
}
// InState returns whether or not the state machine is in the given 'check' state.
@@ -96,6 +100,9 @@ func (fsm *FSM) InState(check StateID) bool {
}
func (fsm *FSM) CheckTransition(desired StateID) error {
+ if desired == fsm.initial {
+ return nil
+ }
for _, ts := range fsm.States[fsm.Current].Transitions {
if ts.To == desired {
return nil