ifandelse / machina.js

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

Handle an event in child and in parent? #136

Closed colegleason closed 6 years ago

colegleason commented 7 years ago

I have a hierarchical FSM where I would like the parent to contain almost all of the logic for handling a certain event, but would like the child to do some cleanup code before the parent handles the event. How can I have the event be handled by the child and then the parent, sort of the equivalent of calling "super" in a overriden function?

ifandelse commented 6 years ago

@colegleason It's not currently possible to handle the same event in a child and parent - as the intent of a hierarchical FSM is to delegate the handling to a child FSM if the child has a handler. You could separate the behavior into two inputs for the FSM to handle - have the child handle the first/original input, do the cleanup you need and then tell the FSM to handle the second input (for which the child does not have a handler, but the parent does) and it will bubble up to be handled by the parent. This may not be applicable, but since you mentioned cleanup, there is a _reset event (see the bottom of this page) in hierarchical FSMs that is intended to provide a cleanup hook when moving into a state with a child FSM.