From 5edd203bd033826fe22c7b0e1f3b2f1a960785e7 Mon Sep 17 00:00:00 2001 From: smallstonesk <575913857@qq.com> Date: Wed, 2 Sep 2020 10:13:49 +0800 Subject: [PATCH] feat: support excuting node's code concurrently --- packages/cli/src/cmd/dev/template/logic.js | 37 ++++++++++++++-------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/packages/cli/src/cmd/dev/template/logic.js b/packages/cli/src/cmd/dev/template/logic.js index b1969e7..426a0ae 100644 --- a/packages/cli/src/cmd/dev/template/logic.js +++ b/packages/cli/src/cmd/dev/template/logic.js @@ -31,6 +31,12 @@ export default class Logic extends EventEmitter{ return this.cells.filter(cell => cell.shape === 'edge'); } + _createCtx() { + const ctx = new Context(); + ctx.emit = this.emit.bind(this); + return ctx; + } + _getStartNode(trigger) { for(const cell of this.startNodes) { if(cell.data.trigger === trigger) { @@ -39,7 +45,8 @@ export default class Logic extends EventEmitter{ } } - _getNextNode(ctx, curNode, lastRet) { + _getNextNodes(ctx, curNode, lastRet) { + const nodes = []; for(const edge of this.edges) { let isMatched = edge.source.cell === curNode.id; // NOTE: if it is a imove-branch node, each port's condition should be tested whether it is matched @@ -57,16 +64,24 @@ export default class Logic extends EventEmitter{ isMatched = isMatched && edge.source.port === matchedPort; } if(isMatched) { - const nextNodeId = edge.target.cell; - return this.nodes.find(item => item.id === nextNodeId); + // NOTE: not each edge both has source and target + const nextNode = this.nodes.find(item => item.id === edge.target.cell); + nextNode && nodes.push(nextNode); } } + return nodes; } - _createCtx() { - const ctx = new Context(); - ctx.emit = this.emit.bind(this); - return ctx; + async _execNode(ctx, curNode, lastRet) { + ctx._transitTo(curNode, lastRet); + const fn = nodeFns[curNode.id]; + lastRet = await fn(ctx); + const nextNodes = this._getNextNodes(ctx, curNode, lastRet); + if(nextNodes.length > 0) { + nextNodes.forEach(async node => { + await this._execNode(ctx, node, lastRet); + }); + } } async invoke(trigger, data) { @@ -74,14 +89,8 @@ export default class Logic extends EventEmitter{ if(!curNode) { return Promise.reject(`Invoke failed! No logic-start named ${trigger} found!`); } - let lastRet; const ctx = this._createCtx(); ctx._reset({payload: data}); - while(curNode) { - ctx._transitTo(curNode, lastRet); - const fn = nodeFns[curNode.id]; - lastRet = await fn(ctx); - curNode = this._getNextNode(ctx, curNode, lastRet); - } + await this._execNode(ctx, curNode); } }