ifandelse / machina.js

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

Should FSM throw an error when trying handle unknown transition? #153

Closed pasierb closed 5 years ago

pasierb commented 6 years ago

@ifandelse What you think about that? I caught myself couple times debugging this silent fail, that turned out to be transition that was not defined in current state. Would be nice if there was an error thrown is such case. I'm happy to create PR, but wanted to see what you think about it first.

ifandelse commented 5 years ago

There are two options in the existing implementation you could use to find out when an FSM is asked to handle input for which it has no handler:

The reason I'd pick the latter option above is because you may have an FSM that handles a total of say, 7 different inputs, but not all 7 are handled in every state. The intended default behavior is for the FSM to no-op/ignore inputs that aren't valid for the current state. The catch-all handlers provide a way to handle "unknown inputs" explicitly, without breaking the intended defaults. Hope that helps!