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

How to cancel a transition in callback? #101

Open
wenchaoliao opened this issue Nov 10, 2023 · 0 comments
Open

How to cancel a transition in callback? #101

wenchaoliao opened this issue Nov 10, 2023 · 0 comments

Comments

@wenchaoliao
Copy link

wenchaoliao commented Nov 10, 2023

I use the CancelTransition method and want to cancel a transition while do some thing failed in the callback , but it complain event run1 inappropriate because previous transition did not complete after I cancel the transition and input a new event.

func main() {
	f := fsm.NewFSM(
		"start",
		fsm.Events{
			{Name: "run_async", Src: []string{"start"}, Dst: "end"},
			{Name: "run", Src: []string{"start"}, Dst: "end"},
		},
		fsm.Callbacks{
			"leave_start": func(_ context.Context, e *fsm.Event) {
				if e.Event == "run_async" {
					e.Async()
				}
			},
		},
	)

	err := f.Event(context.Background(), "run_async")

	asyncError, ok := err.(fsm.AsyncError)
	if !ok {
		panic("!ok")
	}
      //do something failed
	asyncError.CancelTransition()

	if err := f.Transition(); err != nil {
		panic(fmt.Sprintf("Transition err:%v", err))
	}

	if err := f.Event(context.Background(), "run"); err != nil {
		panic(fmt.Sprintf("run event err:%v", err))
	}
}

it complain panic: run event err:event run inappropriate because previous transition did not complete

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

No branches or pull requests

1 participant