jakesgordon / javascript-state-machine

A javascript finite state machine library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

onAfterTransition is fired before transition is completed

anticore opened this issue · comments

According to the documentation:

onAfterTransition - fired after any transition

However, when I fire a new transition inside my implementation of onAfterTransition, I get the following error:

transition is invalid while previous transition is still in progress

Is this the expected behaviour? If so, how can I catch a transition AFTER it completes, so I can fire new transitions?

You need to fire the new transition in a setTimeout();

Take a look at #143.

This seems like a weird solution, and I don't understand why the fix isn't implemented on the lib's side. It's also not referred to anywhere in the documentation.

Not really. As I explained in #143, it's pretty standard for any event-driven system to have to back out of the event loop before you can fire another event. The timeout achieves this.

@rickbsgu Okay, but here's my perspective, for what it's worth:

  • If onAfterTransition is fired after the transition concludes, why does it block transitions within it as if the transition is still happening? The way the lib works right now, there appears to be no difference between onTransition and onAfterTransition.
  • If this timeout method is an accepted and encouraged solution to the problem, why isn't it implemented in some way on the lib's side?

After- and Before- transitions are events, not the transition itself. Events have to be handled as events.

How would it be solved on the lib side?

Maybe if could send it in a timeout, but you're still in an event when you want to fire a new one, so you can be stuck with the same issue.

If you've ever done any Windows or other event-based programming, you still run into the same situation - you have to back out to the event loop before you can fire another event (usually...)

@rickbsgu The first paragraph is not really making sense to me, sorry...
Let me try to clarify my question again:

  • I declare a state machine with three states: A, B and C and two transitions AtoB and BtoC;
  • A is the initial state;
  • I implement the onAfterTransition method to fire the BtoC transition;
  • I fire the AtoB transition, changing the current state from A to B;
  • onAfterTransition is called, tries to fire the BtoC transition, but fails, because the AtoB transition isn't complete yet.

The problem here, from my point of view, is that the onAfterTransition method should be called when the AtoB transition is completed, according to the documentation. It shouldn't, therefore, have any problems firing a new transition, ie. BtoC, since the previous one is completed.

If this isn't the case, and onAfterTransition is called before AtoB is completed, what's the point of having both onTransition and onAfterTransition?

Also, if the fix is to wrap the BtoC transition call in a timeout event, why isn't this wrapping being done by the lib, if the transition AtoB is complete?

@anticore totally agree. onAFTERtransition sounds like transition is completed already but in a fact it's not.