Browse Source

feat: support excuting node's code concurrently

master
smallstonesk 4 years ago
parent
commit
5edd203bd0
  1. 37
      packages/cli/src/cmd/dev/template/logic.js

37
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'); return this.cells.filter(cell => cell.shape === 'edge');
} }
_createCtx() {
const ctx = new Context();
ctx.emit = this.emit.bind(this);
return ctx;
}
_getStartNode(trigger) { _getStartNode(trigger) {
for(const cell of this.startNodes) { for(const cell of this.startNodes) {
if(cell.data.trigger === trigger) { 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) { for(const edge of this.edges) {
let isMatched = edge.source.cell === curNode.id; 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 // 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; isMatched = isMatched && edge.source.port === matchedPort;
} }
if(isMatched) { if(isMatched) {
const nextNodeId = edge.target.cell; // NOTE: not each edge both has source and target
return this.nodes.find(item => item.id === nextNodeId); const nextNode = this.nodes.find(item => item.id === edge.target.cell);
nextNode && nodes.push(nextNode);
} }
} }
return nodes;
} }
_createCtx() { async _execNode(ctx, curNode, lastRet) {
const ctx = new Context(); ctx._transitTo(curNode, lastRet);
ctx.emit = this.emit.bind(this); const fn = nodeFns[curNode.id];
return ctx; 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) { async invoke(trigger, data) {
@ -74,14 +89,8 @@ export default class Logic extends EventEmitter{
if(!curNode) { if(!curNode) {
return Promise.reject(`Invoke failed! No logic-start named ${trigger} found!`); return Promise.reject(`Invoke failed! No logic-start named ${trigger} found!`);
} }
let lastRet;
const ctx = this._createCtx(); const ctx = this._createCtx();
ctx._reset({payload: data}); ctx._reset({payload: data});
while(curNode) { await this._execNode(ctx, curNode);
ctx._transitTo(curNode, lastRet);
const fn = nodeFns[curNode.id];
lastRet = await fn(ctx);
curNode = this._getNextNode(ctx, curNode, lastRet);
}
} }
} }

Loading…
Cancel
Save