From 640ab36148f8da0609c1f199f62d45ed0cf6dfe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?okhowang=28=E7=8E=8B=E6=B2=9B=E6=96=87=29?= Date: Tue, 1 Jun 2021 11:50:35 +0800 Subject: [PATCH] add args to Transition --- statemachine.go | 17 +++++++++-------- statemachine_test.go | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/statemachine.go b/statemachine.go index 99e4c2a..6e7f54e 100644 --- a/statemachine.go +++ b/statemachine.go @@ -29,6 +29,7 @@ type Transition struct { Source State Destination State Trigger Trigger + Arguments []interface{} isInitial bool } @@ -344,13 +345,13 @@ func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, ar case *ignoredTriggerBehaviour: // ignored case *reentryTriggerBehaviour: - transition := Transition{Source: source, Destination: t.Destination, Trigger: trigger} + transition := Transition{Source: source, Destination: t.Destination, Trigger: trigger, Arguments: args} err = sm.handleReentryTrigger(ctx, representativeState, transition, args...) case *dynamicTriggerBehaviour: var destination any destination, err = t.Destination(ctx, args...) if err == nil { - transition := Transition{Source: source, Destination: destination, Trigger: trigger} + transition := Transition{Source: source, Destination: destination, Trigger: trigger, Arguments: args} err = sm.handleTransitioningTrigger(ctx, representativeState, transition, args...) } case *transitioningTriggerBehaviour: @@ -358,13 +359,13 @@ func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, ar // If a trigger was found on a superstate that would cause unintended reentry, don't trigger. break } - transition := Transition{Source: source, Destination: t.Destination, Trigger: trigger} + transition := Transition{Source: source, Destination: t.Destination, Trigger: trigger, Arguments: args} err = sm.handleTransitioningTrigger(ctx, representativeState, transition, args...) case *internalTriggerBehaviour: var sr *stateRepresentation sr, err = sm.currentState(ctx) if err == nil { - transition := Transition{Source: source, Destination: source, Trigger: trigger} + transition := Transition{Source: source, Destination: source, Trigger: trigger, Arguments: args} err = sr.InternalAction(ctx, transition, args...) } } @@ -377,7 +378,7 @@ func (sm *StateMachine) handleReentryTrigger(ctx context.Context, sr *stateRepre } newSr := sm.stateRepresentation(transition.Destination) if !transition.IsReentry() { - transition = Transition{Source: transition.Destination, Destination: transition.Destination, Trigger: transition.Trigger} + transition = Transition{Source: transition.Destination, Destination: transition.Destination, Trigger: transition.Trigger, Arguments: args} if err := newSr.Exit(ctx, transition, args...); err != nil { return err } @@ -413,7 +414,7 @@ func (sm *StateMachine) handleTransitioningTrigger(ctx context.Context, sr *stat return err } } - callEvents(sm.onTransitionedEvents, ctx, Transition{transition.Source, rep.State, transition.Trigger, false}) + callEvents(sm.onTransitionedEvents, ctx, Transition{transition.Source, rep.State, transition.Trigger, args, false}) return nil } @@ -437,9 +438,9 @@ func (sm *StateMachine) enterState(ctx context.Context, sr *stateRepresentation, if !isValidForInitialState { panic(fmt.Sprintf("stateless: The target (%v) for the initial transition is not a substate.", sr.InitialTransitionTarget)) } - initialTranslation := Transition{Source: transition.Source, Destination: sr.InitialTransitionTarget, Trigger: transition.Trigger, isInitial: true} + initialTranslation := Transition{Source: transition.Source, Destination: sr.InitialTransitionTarget, Trigger: transition.Trigger, isInitial: true, Arguments: args} sr = sm.stateRepresentation(sr.InitialTransitionTarget) - callEvents(sm.onTransitioningEvents, ctx, Transition{transition.Destination, initialTranslation.Destination, transition.Trigger, false}) + callEvents(sm.onTransitioningEvents, ctx, Transition{transition.Destination, initialTranslation.Destination, transition.Trigger, args, false}) sr, err = sm.enterState(ctx, sr, initialTranslation, args...) } return sr, err diff --git a/statemachine_test.go b/statemachine_test.go index a89718e..fb89cd5 100644 --- a/statemachine_test.go +++ b/statemachine_test.go @@ -26,8 +26,8 @@ func TestTransition_IsReentry(t *testing.T) { t *Transition want bool }{ - {"TransitionIsNotChange", &Transition{"1", "1", "0", false}, true}, - {"TransitionIsChange", &Transition{"1", "2", "0", false}, false}, + {"TransitionIsNotChange", &Transition{"1", "1", "0", nil, false}, true}, + {"TransitionIsChange", &Transition{"1", "2", "0", nil, false}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {