Skip to content

Commit

Permalink
add args to Transition
Browse files Browse the repository at this point in the history
  • Loading branch information
okhowang committed Jun 1, 2021
1 parent 5454b26 commit 7acb6d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Transition struct {
Source State
Destination State
Trigger Trigger
Arguments []interface{}

isInitial bool
}
Expand Down Expand Up @@ -366,24 +367,24 @@ 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:
destination, ok := t.ResultsInTransitionFrom(ctx, source, args...)
if !ok {
err = fmt.Errorf("stateless: Dynamic handler for trigger %s in state %s has failed", trigger, source)
} else {
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:
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 @@ -396,7 +397,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 @@ -432,7 +433,7 @@ func (sm *StateMachine) handleTransitioningTrigger(ctx context.Context, sr *stat
return err
}
}
sm.onTransitionedEvents.Invoke(ctx, Transition{transition.Source, rep.State, transition.Trigger, false})
sm.onTransitionedEvents.Invoke(ctx, Transition{transition.Source, rep.State, transition.Trigger, args, false})
return nil
}

Expand All @@ -456,9 +457,9 @@ func (sm *StateMachine) enterState(ctx context.Context, sr *stateRepresentation,
if !isValidForInitialState {
panic(fmt.Sprintf("stateless: The target (%s) 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)
sm.onTransitioningEvents.Invoke(ctx, Transition{transition.Destination, initialTranslation.Destination, transition.Trigger, false})
sm.onTransitioningEvents.Invoke(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 @@ -28,8 +28,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

0 comments on commit 7acb6d9

Please sign in to comment.