jakesgordon / javascript-state-machine

A javascript finite state machine library
MIT License
8.71k stars 966 forks source link

Sequential transitions #158

Open beshur opened 6 years ago

beshur commented 6 years ago

Hi.

I want the state machine to travel through the states itself (and not on Route 66).

Say, I have these transitions:

transitions: [
    { name: 'doParse', from: 'none', to: 'Parse' },
    { name: 'toEmptyInput', from: 'Parse', to: 'emptyInput'},
    { name: 'doStart', from 'Parse', to: 'Start' }
]

If in Parse I detect no input, I want to go to emptyInput, otherwise if Start conditions are met, I want to call doStart transition.

However, if I call a doStart method inside a state method, I get a 'transition is invalid while previous transition is still in progress' error. What is the correct way to do it?

Guys, I'm really not getting it here, but the custom FSMs we did on a different project allowed me to do it.

Thanks.

bxk21 commented 5 years ago

Correct me if I'm interpreting this incorrectly.

You have logic inside the 'methods' of a transition that determine what the end state should be. When it determines that, the method calls its parent state machine to do another transition. But, because the method occurs during the first transition, it throws an error.

If that's what's happening, I see two choices:

Move the logic to outside of the state machine's 'methods' and instead, determine the new state before doing any transition. NOT IDEAL: Change the onPendingTransition() 'method' to not throw an error.