Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add args to Transition #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Transition struct {
Source State
Destination State
Trigger Trigger
Arguments []interface{}

isInitial bool
}
Expand Down Expand Up @@ -344,27 +345,27 @@ 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:
if source == t.Destination {
// 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...)
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions statemachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@ func Test_stateRepresentation_Enter_EnteringActionsExecuted_Error(t *testing.T)
func Test_stateRepresentation_Enter_LeavingActionsNotExecuted(t *testing.T) {
sr := newstateRepresentation(stateA)
transition := Transition{Source: stateA, Destination: stateB, Trigger: triggerX}
var actualTransition Transition
var actualTransition *Transition
sr.ExitActions = append(sr.ExitActions, actionBehaviour{
Action: func(_ context.Context, _ ...any) error {
actualTransition = transition
actualTransition = &transition
return nil
},
})
sr.Enter(context.Background(), transition)
if actualTransition != (Transition{}) {
if actualTransition != nil {
t.Error("expected transition to not be passed to action")
}
}
Expand Down Expand Up @@ -329,41 +329,41 @@ func Test_stateRepresentation_Enter_Substate_SuperstateEntryActionsExecuteBefore
func Test_stateRepresentation_Exit_EnteringActionsNotExecuted(t *testing.T) {
sr := newstateRepresentation(stateB)
transition := Transition{Source: stateA, Destination: stateB, Trigger: triggerX}
var actualTransition Transition
var actualTransition *Transition
sr.EntryActions = append(sr.EntryActions, actionBehaviour{
Action: func(_ context.Context, _ ...any) error {
actualTransition = transition
actualTransition = &transition
return nil
},
})
sr.Exit(context.Background(), transition)
if actualTransition != (Transition{}) {
if actualTransition != nil {
t.Error("expected transition to not be passed to action")
}
}

func Test_stateRepresentation_Exit_LeavingActionsExecuted(t *testing.T) {
sr := newstateRepresentation(stateA)
transition := Transition{Source: stateA, Destination: stateB, Trigger: triggerX}
var actualTransition Transition
var actualTransition *Transition
sr.ExitActions = append(sr.ExitActions, actionBehaviour{
Action: func(_ context.Context, _ ...any) error {
actualTransition = transition
actualTransition = &transition
return nil
},
})
if err := sr.Exit(context.Background(), transition); err != nil {
t.Error(err)
}
if actualTransition != transition {
if actualTransition != &transition {
t.Error("expected transition to be passed to leaving actions")
}
}

func Test_stateRepresentation_Exit_LeavingActionsExecuted_Error(t *testing.T) {
sr := newstateRepresentation(stateA)
transition := Transition{Source: stateA, Destination: stateB, Trigger: triggerX}
var actualTransition Transition
var actualTransition *Transition
sr.ExitActions = append(sr.ExitActions, actionBehaviour{
Action: func(_ context.Context, _ ...any) error {
return errors.New("")
Expand All @@ -372,7 +372,7 @@ func Test_stateRepresentation_Exit_LeavingActionsExecuted_Error(t *testing.T) {
if err := sr.Exit(context.Background(), transition); err == nil {
t.Error("expected error")
}
if actualTransition == transition {
if actualTransition == &transition {
t.Error("expected transition to not be passed to leaving actions")
}
}
Expand Down
Loading