diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-05-02 13:06:50 +0200 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2023-09-25 09:43:37 +0200 |
| commit | d0f4303ee5ccecc876fdfae1ce858369e3a323b7 (patch) | |
| tree | 64bda3971398aa56f0672bab49a63a077bf6ae7c /internal | |
| parent | dc7425beb85ea7d35e860a5df2bc0d8ddda8c28a (diff) | |
Client + FSM: Check transitions and add SetState
Also make sure GotConfig can be used to go back to
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/fsm/fsm.go | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/internal/fsm/fsm.go b/internal/fsm/fsm.go index 3cc8edb..cae8833 100644 --- a/internal/fsm/fsm.go +++ b/internal/fsm/fsm.go @@ -131,7 +131,14 @@ func (fsm *FSM) writeGraph() { // If this transition is not handled by the client, it returns an error. func (fsm *FSM) GoTransitionRequired(newState StateID, data interface{}) error { oldState := fsm.Current - if !fsm.GoTransitionWithData(newState, data) { + + handled, err := fsm.GoTransitionWithData(newState, data) + // transition ios not possible + if err != nil { + return err + } + // transition is not handled + if !handled { return errors.Errorf("fsm failed transition from '%v' to '%v', is this required transition handled?", fsm.GetStateName(oldState), fsm.GetStateName(newState)) } return nil @@ -139,9 +146,9 @@ func (fsm *FSM) GoTransitionRequired(newState StateID, data interface{}) error { // GoTransitionWithData is a helper that transitions the state machine toward the 'newState' with associated state data 'data' // It returns whether or not the transition is handled by the client. -func (fsm *FSM) GoTransitionWithData(newState StateID, data interface{}) bool { - if fsm.CheckTransition(newState) != nil { - return false +func (fsm *FSM) GoTransitionWithData(newState StateID, data interface{}) (bool, error) { + if err := fsm.CheckTransition(newState); err != nil { + return false, err } prev := fsm.Current @@ -150,11 +157,11 @@ func (fsm *FSM) GoTransitionWithData(newState StateID, data interface{}) bool { fsm.writeGraph() } - return fsm.StateCallback(prev, newState, data) + return fsm.StateCallback(prev, newState, data), nil } // GoTransition is an alias to call GoTransitionWithData but have an empty string as data. -func (fsm *FSM) GoTransition(newState StateID) bool { +func (fsm *FSM) GoTransition(newState StateID) (bool, error) { // No data means the callback is never required return fsm.GoTransitionWithData(newState, "") } |
