Skip to content

How to cancel a transition in callback? #101

Description

@wenchaoliao

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions