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

Error "transition is invalid in current state" when returning promise from event of type "any" #175

Open
beltschatsar opened this issue Apr 4, 2019 · 1 comment

Comments

@beltschatsar
Copy link

beltschatsar commented Apr 4, 2019

I get error "​​transition is invalid in current state​​" when using a Promise in following "any" event:

  • onBeforeTransition
  • onLeaveState
  • onTransition

Repro case:

import * as StateMachine from 'javascript-state-machine';

var fsm = new StateMachine({
  init: 'solid',
  transitions: [
		{ name: 'melt', from: 'solid', to: 'liquid' },
		{ name: 'freeze', from: 'liquid', to: 'solid' },
		{ name: 'vaporize', from: 'liquid', to: 'gas' },
		{ name: 'condense', from: 'gas', to: 'liquid' }
	],
	methods: {
		onMelt: function () { console.log('I melted'); return new Promise((resolve, reject) => setTimeout(() => {console.log('onMelt'); resolve(true);}, 1000)); },
		onFreeze: function () { console.log('I froze') },
		onVaporize: function () { console.log('I vaporized') },
		onCondense: function () { console.log('I condensed') },
		onBeforeMelt: function () { return new Promise((resolve, reject) => setTimeout(() => {console.log('onBeforeMelt'); resolve(true);}, 1000)) },
		onLeaveSolid: function () { return new Promise((resolve, reject) => setTimeout(() => {console.log('onLeaveSolid'); resolve(true);}, 1000)) },
		// Following ​​Throws "transition is invalid in current state​​"
		onBeforeTransition: function () { return new Promise((resolve, reject) => setTimeout(() => {console.log('onBeforeTransition'); resolve(true);}, 1000)) },
		onLeaveState: function () { return new Promise((resolve, reject) => setTimeout(() => {console.log('onLeaveState'); resolve(true);}, 1000)) },
		onTransition: function () { return new Promise((resolve, reject) => setTimeout(() => {console.log('onTransition'); resolve(true);}, 1000)) },
	}
});

fsm.melt();

I would expect the output to be:

onBeforeTransition
​​​​​onBeforeMelt​​​​​
onLeaveState
​​​​​onLeaveSolid​​​​​
onTransition
I melted
​​​​​onMelt​​​​​

I got:

​​transition is invalid in current state​​
@NelsonFrancisco
Copy link

NelsonFrancisco commented May 15, 2019

I would say this happens because:

  • You are entering on solid
  • So, some method callbacks will be called. Some of which are returning promises that must be resolved before you make the next transition, otherwise you will never leave solid state
  • But you are making a transition fsm.melt() immediately after building a new StateMachine.
  • You never waited for onLeaveSolid or onLeaveState promise to be resolved

As for the solution, I'm not sure. I cannot solve this problem too: When you return a promise in one of the initial state's methods, how do you resolve it?

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