js中常常有各种回调函数,有时候这些回掉函数构成一个复杂的系统,难以阅读和维护。 为了解决这个问题,flow把这些回调函数通过一棵树的形式组织起来,让整体的流程一目了然。
npm install flow-tree
- 顺序节点
- 循环节点
- 选择节点
var fun1 = function(next){
setTimeout(function(){
next();
// or
//next(FNode.FNodeResultType.Next);
,1000}
}
var fun1 = function(next){
var id = setTimeout(function(){
id = null;
next();
// or
//next(FNode.FNodeResultType.Next);
,1000}
arguments.callee.clear_handler = function(){
if(id!=null)
clearInterval(id)
id = null;
}
}
[
fun1,
fun2,
fun3,
tree1,
tree2,
tree3,
]
子节点返回FNode.FNodeResultType.Break退出, next(FNode.FNodeResultType.Break);
[
"loop",
fun1,
fun2,
fun3,
tree1,
tree2
]
fun0计算switch的值,通过next设置。next({type:FNode.FNodeResultType.Next,value:"value1"})
{
"switch": fun0,
"case_value1":tree1,
"case_value2":fun2,
}
NPC.prototype.build = function() {
var tree = [
"loop",
this.idle,
{
"switch": this.waitOrder,//sit stand
"case_sit": [
this.gotoSeat,
this.sitDown,
[
"loop",
this.lookLeft,
this.idle,
this.lookRight,
this.idle,
this.checkTired,
],
],
"case_stand": [
this.standUp,
this.idle,
this.keke,
],
},
];
this.runNode = FNode.build(tree,this);
}
//...
var npc = new NPC();
npc.build() //建立树
npc.runNode.run(); //执行树