Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

okhowang
Copy link
Contributor

@okhowang okhowang commented Jun 1, 2021

related #16

@qmuntal
Copy link
Owner

qmuntal commented Jun 3, 2021

Can you specify why you need the arguments in the Transition? They are normally already passed in the function call-backs.

@okhowang
Copy link
Contributor Author

okhowang commented Jun 3, 2021

I want write log once state changed
for debugging state machine.
And args is the detail info of transition.

@qmuntal
Copy link
Owner

qmuntal commented Jun 13, 2021

I'm hesitant to add the Arguments property to Transition. It does make sense but there are already many callbacks whose parameters are a transition and the arguments, so it would be confusing to have the arguments in both place.

Yet your use case is valid, so I will think on a way to cover it. Other proposals are also welcomed.

@okhowang
Copy link
Contributor Author

okhowang commented Aug 3, 2022

@qmuntal is any better way here?

@okhowang okhowang force-pushed the transition-arguments branch from 7acb6d9 to 640ab36 Compare April 29, 2024 07:27
@okhowang okhowang force-pushed the transition-arguments branch from 640ab36 to d121196 Compare April 29, 2024 07:37
@okhowang
Copy link
Contributor Author

okhowang commented May 3, 2024

@qmuntal is any better way here after 2 years?

@daniel-sullivan
Copy link
Contributor

daniel-sullivan commented Jan 25, 2025

@okhowang
This feature: #92 added the args to the external state handler. You can use a locally-scoped variable rather than an "external" state then add your log to this callback. e.g:

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
)

@okhowang
Copy link
Contributor Author

okhowang commented Feb 5, 2025

stateMutator in stateless.NewStateMachineWithExternalStorageAndArgs is equal to OnTransitioning.
but there is no alternative for OnTransitioned which behaviour different with OnTransitioning, especially for sub state

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

2025/02/05 11:48:09 on transitioning init -> running
2025/02/05 11:48:09 state transitioning init -> running (args: [])
2025/02/05 11:48:09 on transitioning running -> sub-running
2025/02/05 11:48:09 state transitioning running -> sub-running (args: [])
2025/02/05 11:48:09 on transitioned init -> sub-running
2025/02/05 11:48:09 on transitioning sub-running -> stopped
2025/02/05 11:48:09 state transitioning sub-running -> stopped (args: [])
2025/02/05 11:48:09 on transitioned sub-running -> stopped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants