From b7c58415cd2de0fc9771dc4a49382fc62aefa7e8 Mon Sep 17 00:00:00 2001 From: Jeroen Wijenbergh Date: Mon, 25 Apr 2022 17:32:14 +0200 Subject: FSM: Add the ability to run callbacks in a goroutine --- internal/fsm.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'internal/fsm.go') diff --git a/internal/fsm.go b/internal/fsm.go index fadc7c9..d0925c5 100644 --- a/internal/fsm.go +++ b/internal/fsm.go @@ -142,7 +142,7 @@ func (fsm *FSM) writeGraph() { f.WriteString(graph) } -func (fsm *FSM) GoTransitionWithData(newState FSMStateID, data string) bool { +func (fsm *FSM) GoTransitionWithData(newState FSMStateID, data string, background bool) bool { ok := fsm.HasTransition(newState) if ok { @@ -151,14 +151,18 @@ func (fsm *FSM) GoTransitionWithData(newState FSMStateID, data string) bool { if fsm.Debug { fsm.writeGraph() } - fsm.StateCallback(oldState.String(), newState.String(), data) + if background { + go fsm.StateCallback(oldState.String(), newState.String(), data) + } else { + fsm.StateCallback(oldState.String(), newState.String(), data) + } } return ok } func (fsm *FSM) GoTransition(newState FSMStateID) bool { - return fsm.GoTransitionWithData(newState, "") + return fsm.GoTransitionWithData(newState, "", false) } func (fsm *FSM) generateMermaidGraph() string { -- cgit v1.2.3