Search code examples
javascriptfsmmachina.js

Execute an action when transition from one specific state to another?


Is there a way to execute an action when transitioning between specific states in Machina.js?

For example, say I have states "A, B, C".

I want to write a function like:

when("A", "C", function(){ console.log("Caught transition from A to C! Yay!"); }

This is in the same spirit as Akka's FSM implementation. Is this possible?

Thank you!


Solution

  • Figured it out. One just has to watch for transition event. See API doc here.

    var fsm = new machina.Fsm({     
                initialState: 'A',      
                states: {
                    "A":    {},
                    "B" :   {},
                    "C" :   {}
                }
            });
    
    
    fsm.on('transition', function(transition){      
                console.log("[" + transition.fromState + "] -(" + transition.action  + ")-> [" + transition.toState + "]");
            });