从一种特定状态转换到另一种特定状态时执行操作

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

本文关键字:状态 另一种 执行 操作 转换 一种      更新时间:2023-09-26

在Machina.js的特定状态之间转换时,有没有办法执行动作?

例如,假设我有状态"A,B,C"。

我想写一个这样的函数:

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

这与Akka的FSM实施精神相同。这可能吗?

谢谢!

通了。人们只需要关注transition事件。请参阅此处的 API 文档。

var fsm = new machina.Fsm({     
            initialState: 'A',      
            states: {
                "A":    {},
                "B" :   {},
                "C" :   {}
            }
        });

fsm.on('transition', function(transition){      
            console.log("[" + transition.fromState + "] -(" + transition.action  + ")-> [" + transition.toState + "]");
        });

你能在 A 的 onExit 中设置一些东西,你在 C 的 onEnter 中检查吗?