|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|