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