From d0f4303ee5ccecc876fdfae1ce858369e3a323b7 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Tue, 2 May 2023 13:06:50 +0200 Subject: Client + FSM: Check transitions and add SetState Also make sure GotConfig can be used to go back to --- internal/fsm/fsm.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'internal') 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, "") } -- cgit v1.2.3