一个任务处理工具
// import vueCompiler from '@vue/compiler-dom'; // // import fs from 'fs'; // const str=fs.readFileSync('App.vue').toString(); // // const rootNode=vueCompiler.parse(str) // console.log(rootNode) const _={ isInclude:function (item1,item2){ if(Object.prototype.toString.call(item1)==='[object Object]'&&Object.prototype.toString.call(item2)==='[object Object]'){ let isOk=true for(let k in item2){ if(!this.isInclude(item1[k],item2[k])){ isOk=false break } } return isOk }else{ return item1===item2 } } } const matchConfigs=[ { async match(node){ return true }, task:async function (node){ console.log('fitMatch',node) } },{ match:{}, task:function (node){ console.log('fitM',node) } } ] const rootNode={ type:1 } const taskMan={ //将tree转成数组任务 init(taskArr,matchConfigs){ this.matchConfigs=matchConfigs this.taskArr=[rootNode] for(let i=0;i<taskArr.length;i++){ this.run(this.taskArr[i]) } }, async run(node){ const matchConfigs=this.matchConfigs for(let i=0;i<matchConfigs.length;i++){ const item=matchConfigs[i] let isMath=false if(Object.prototype.toString.call(item.match)==='[object Object]'){ if(_.isInclude(node,item.match)){ isMath=true } }else if(Object.prototype.toString.call(item.match)==='[object Function]'){ isMath=item.match(node) }else if(Object.prototype.toString.call(item.match)==='[object AsyncFunction]'){ isMath=await item.match(node) } if(isMath){ await this.fitMatch(node,item) break } } }, async fitMatch(node,item){ if(Object.prototype.toString.call(item.task)==='[object Function]'){ item.task(node) }else if(Object.prototype.toString.call(item.task)==='[object AsyncFunction]'){ await item.task(node) } } } taskMan.init([rootNode],matchConfigs)
标签:node,item1,const,处理,Object,matchConfigs,item2,任务,工具 From: https://www.cnblogs.com/caoke/p/18560712