javascript-state-machine icon indicating copy to clipboard operation
javascript-state-machine copied to clipboard

state in progress, and can't do transition.

Open sunq0001 opened this issue 7 years ago • 2 comments

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();

sunq0001 avatar Jul 14 '17 05:07 sunq0001

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?

adami avatar Jul 14 '17 14:07 adami

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

sunq0001 avatar Jul 17 '17 06:07 sunq0001