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

state in progress, and can't do transition. #125

Open
sunq0001 opened this issue Jul 14, 2017 · 2 comments
Open

state in progress, and can't do transition. #125

sunq0001 opened this issue Jul 14, 2017 · 2 comments

Comments

@sunq0001
Copy link

sunq0001 commented Jul 14, 2017

i used it by another way, because for me it is easier to understand when function is in state rather than in transition.
but the problem comes if i don't use setTimeout, it will show error.
"transition is invalid while previous transition is still in progress"
but if i add setTimeout(function(){}, 0); error is gone.
could you help solve it?

var FSM = StateMachine.factory({
    init: 'start',
    transitions: [
        { name: 'step', from: 'start', to: 'A' },
        { name: 'step', from: 'A', to: 'B' },
        { name: 'step', from: 'B', to: 'C' },
        { name: 'step', from: 'C', to: 'D' },
        { name: 'step', from: 'D', to: 'start' }
    ],

    methods: {
        onStep: function () {
            var self = this;
            var obj = {
                'start': function () {
                    console.log(`i am in ${self.state} state`);
                    **setTimeout(function () {
                        self.step();
                    }, 0);**
                },
                'A': function () {
                    console.log(`i am in ${self.state} state`);
                    **setTimeout(function () {
                        self.step();
                    }, 0);**
                },
                'B': function () {
                    console.log(`i am in ${self.state} state`);
                    **setTimeout(function () {
                        self.step();
                    }, 0);**
                },
                'C': function () {
                    console.log(`i am in ${self.state} state`);
                    **setTimeout(function () {
                        self.step();
                    }, 0);**
                },

                'D': function () {
                    console.log(`i am in ${self.state} state`);
                    **setTimeout(function () {
                        self.step();
                    }, 0);**
                }
            }
            obj[self.state]();
        }
    }

});

var fsm1 = new FSM();  // 'init()' transition fires from 'none' to 'A' for fsm1
fsm1.step();
@adami
Copy link

adami commented Jul 14, 2017

i don't really understand why you used the fsm like that...

i think what happens is:

  • onStep is a method called when you start the transition by calling fsm1.step()
  • calling this method is just 1 part of the transition process, not the end of it
  • if you are calling 'step' again inside this function, you are actually trying to make the fsm start a new 'step' transition, while still in the previous transition
  • if you put the setTimeout, it is only called after the transition is over so you're ok, but i think you'll actually cause the state machine to never stop 'step'ping from state to state
    was that your intention?

@sunq0001
Copy link
Author

sunq0001 commented Jul 17, 2017

It is just a demo that shows my questions.
my suggestion is that there should be some function that can force state transition although it is still in progress. and i think this is quite common situtation. the state is interrupted by some action and have to transit immediately.

BTW:
Below is game which used state-machine, and i have to use setTimout(fn, 0) to dismiss the error message.
https://github.com/sunq0001/Tetris

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

2 participants