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 handle triggers that are allowed in all states #5

Open
luxl opened this issue Mar 11, 2020 · 2 comments
Open

How to handle triggers that are allowed in all states #5

luxl opened this issue Mar 11, 2020 · 2 comments

Comments

@luxl
Copy link

luxl commented Mar 11, 2020

Hi, thanks for this great state machine library and I meent a problem when using it.
I have a trigger named "shutdown machine", and it can be fired at any state, which will result in back to the very original state. How to implement this? Adding the trigger to every state?

@qmuntal
Copy link
Owner

qmuntal commented Mar 11, 2020

Hi @luxl, there is no way to set a general trigger for all the states, so will have to do it one by one when defining the state machine.

Anyway, I'll consider this feature a nice to have for future versions.

@floriaanpost
Copy link

I know this is a long time ago, but you can also achieve this by using substates. See the test below as an example:

func TestOnOffSuperstates(t *testing.T) {
	fsm := stateless.NewStateMachine("on1")

	// configure the on superstate
	fsm.Configure("on").
		InitialTransition("on1").
		Permit("shutdown", "off")

	// configure the off superstate
	fsm.Configure("off").
		Permit("turn-on", "on")

	// configure on1, substate of on
	fsm.Configure("on1").
		SubstateOf("on")

	// configure on2, substate of on
	fsm.Configure("on2").
		SubstateOf("on")

	// we are on, firing shutdown should get us in the off state
	fsm.Fire("shutdown")

	if fsm.MustState() != "off" {
		t.Error("Expected state to be off")
	}

	// we are off, firing turn-on should get us in the on -> on1 state
	fsm.Fire("turn-on")
	if fsm.MustState() != "on1" {
		t.Error("Expected state to be on1")
	}
}

You still need to tell each substate that it is a sustate of on, but I think is more elegant than defining the trigger on every state.

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

3 participants