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

158 views Asked by At

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!

2

There are 2 answers

1
Andriy Drozdyuk On BEST ANSWER

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 + "]");
        });
0
Pieter21 On

Can you set something in the onExit of A, that you inspect in the onEnter of C?