-
-
Notifications
You must be signed in to change notification settings - Fork 57
add args to Transition #27
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
base: master
Are you sure you want to change the base?
Conversation
Can you specify why you need the arguments in the |
I want write log once state changed |
I'm hesitant to add the Yet your use case is valid, so I will think on a way to cover it. Other proposals are also welcomed. |
@qmuntal is any better way here? |
7acb6d9
to
640ab36
Compare
640ab36
to
d121196
Compare
@qmuntal is any better way here after 2 years? |
@okhowang var smState stateless.State
var sm = stateless.NewStateMachineWithExternalStorageAndArgs(func(ctx context.Context) (stateless.State, []any, error) {
return smState, nil, nil
}, func(ctx context.Context, state stateless.State, args ...any) error {
log.Info(fmt.Sprintf("now transitioning %s -> %s (args: %v)", smState, state, args))
smState = state
return nil
}, stateless.FiringQueued
) |
for example var smState stateless.State = "init"
var sm = stateless.NewStateMachineWithExternalStorageAndArgs(func(ctx context.Context) (stateless.State, []any, error) {
return smState, nil, nil
}, func(ctx context.Context, state stateless.State, args ...any) error {
log.Printf("state transitioning %s -> %s (args: %v)", smState, state, args)
smState = state
return nil
}, stateless.FiringQueued,
)
sm.Configure("init").
Permit("start", "running").
Permit("startThenStop", "running")
sm.Configure("running").
InitialTransition("sub-running").
Permit("stop", "stopped").
OnEntryFrom("startThenStop", func(ctx context.Context, args ...any) error {
return sm.Fire("stop")
})
sm.Configure("sub-running").SubstateOf("running")
sm.OnTransitioning(func(ctx context.Context, transition stateless.Transition) {
log.Printf("on transitioning %s -> %s", transition.Source, transition.Destination)
})
sm.OnTransitioned(func(ctx context.Context, transition stateless.Transition) {
log.Printf("on transitioned %s -> %s", transition.Source, transition.Destination)
})
_ = sm.Fire("startThenStop") will output
|
related #16