/* * 流程控制器 * 作者:caoke * */ class Step{ //初始化 constructor(stepArr,callback){ this.stepArr=stepArr; this.curIndex=0; this.hasRunTimes=Array(stepArr.length).fill(0); this.callback=callback; } callback(){ this.next() } // 运行当前流程 run(){ const step=this.stepArr[this.curIndex] if(step){ this.hasRunTimes[this.curIndex]++ this.callback.apply(this,[step,this.hasRunTimes[this.curIndex]]) } } // 跳转到某个流程 go(step){ this.curIndex=this.stepArr.indexOf(step) this.run() } // 进入下一个流程 next(){ this.curIndex++ this.run() } } // demo var control=new Step([‘step1‘,‘step2‘,‘step3‘,‘step4‘,‘step5‘],function (step,hasRunTime) { console.log(step,hasRunTime) switch (step){ case ‘step5‘: if(hasRunTime<2){ this.go(‘step3‘) } default: this.next() } }) control.run() // "C:\Program Files\JetBrains\WebStorm 2018.1.2\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" D:\17zuoye\go\test.js // step1 1 // step2 1 // step3 1 // step4 1 // step5 1 // step3 2 // step4 2 // step5 2 // // Process finished with exit code 0
原文地址:https://www.cnblogs.com/caoke/p/9892138.html
时间: 2024-11-13 08:18:58