ifandelse / machina.js

js ex machina - finite state machines in JavaScript
http://machina-js.org/
Other
1.93k stars 147 forks source link

Remove unnecessary child FSM creation by only instantiating upon transitions #155

Open joepie91 opened 6 years ago

joepie91 commented 6 years ago

This fixes the second issue that I mentioned in #154; it ensures that new child FSMs are only instantiated when transitioning to a new child-containing state, not when eg. handleing a command that's received by a child.

As before, I've created a bugcase to demonstrate it, including an example of the erroneous results: https://git.cryto.net/joepie91/machina-factory-bugcase/src/child-switch

Of note:

joepie91 commented 6 years ago

As an aside: this issue originally caused a strange bug in my application where I'd get inconsistent states; a child FSM would transition from state A to state B, but then when handleing a command it would suddenly be in state A again, despite no transition event from B -> A having ever been emitted on the parent FSM (and this wasn't a possible transition in the first place). Presumably there were difference instances of the child FSM, where one of them was in state A and the other was in state B.

This may or may not be indicative of a bug elsewhere. I'm unsure how reproducible the issue is, and I haven't had the time to try it. This PR plus my other one make the bug go away, however.

ifandelse commented 6 years ago

Thanks @joepie91. I'm working on catching up on PRs this week (finally have some time off!).