|
|
@ -2,17 +2,14 @@ import nodeFns from './nodeFns'; |
|
|
|
import Context from './context'; |
|
|
|
import EventEmitter from 'eventemitter3'; |
|
|
|
|
|
|
|
const LIFECYCLE = [ |
|
|
|
'ctxCreated' |
|
|
|
]; |
|
|
|
const LIFECYCLE = new Set(['ctxCreated']); |
|
|
|
const SHAPES = { |
|
|
|
START: 'imove-start', |
|
|
|
BRANCH: 'imove-branch', |
|
|
|
BEHAVIOR: 'imove-behavior' |
|
|
|
} |
|
|
|
BEHAVIOR: 'imove-behavior', |
|
|
|
}; |
|
|
|
|
|
|
|
export default class Logic extends EventEmitter { |
|
|
|
|
|
|
|
constructor(opts = {}) { |
|
|
|
super(); |
|
|
|
this.dsl = opts.dsl; |
|
|
@ -24,23 +21,23 @@ export default class Logic extends EventEmitter{ |
|
|
|
} |
|
|
|
|
|
|
|
get nodes() { |
|
|
|
return this.cells.filter(cell => cell.shape !== 'edge'); |
|
|
|
return this.cells.filter((cell) => cell.shape !== 'edge'); |
|
|
|
} |
|
|
|
|
|
|
|
get startNodes() { |
|
|
|
return this.cells.filter(cell => cell.shape === SHAPES.START); |
|
|
|
return this.cells.filter((cell) => cell.shape === SHAPES.START); |
|
|
|
} |
|
|
|
|
|
|
|
get edges() { |
|
|
|
return this.cells.filter(cell => cell.shape === 'edge'); |
|
|
|
return this.cells.filter((cell) => cell.shape === 'edge'); |
|
|
|
} |
|
|
|
|
|
|
|
_runLifecycleEvent(eventName, ctx) { |
|
|
|
if(LIFECYCLE.indexOf(eventName) === -1) { |
|
|
|
if (!LIFECYCLE.has(eventName)) { |
|
|
|
return console.warn(`Lifecycle ${eventName} is not supported!`); |
|
|
|
} |
|
|
|
if (this.lifeCycleEvents[eventName]) { |
|
|
|
this.lifeCycleEvents[eventName].forEach(fn => fn(ctx)); |
|
|
|
this.lifeCycleEvents[eventName].forEach((fn) => fn(ctx)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -69,7 +66,10 @@ export default class Logic extends EventEmitter{ |
|
|
|
const { ports } = curNode.data; |
|
|
|
for (const key in ports) { |
|
|
|
const { condition } = ports[key]; |
|
|
|
const ret = new Function('ctx', 'return ' + condition)(ctx); |
|
|
|
const ret = ((ctx) => { |
|
|
|
return condition; |
|
|
|
})(ctx); |
|
|
|
// const ret = new Function('ctx', 'return ' + condition)(ctx);
|
|
|
|
if (ret === Boolean(curRet)) { |
|
|
|
matchedPort = key; |
|
|
|
break; |
|
|
@ -79,7 +79,7 @@ export default class Logic extends EventEmitter{ |
|
|
|
} |
|
|
|
if (isMatched) { |
|
|
|
// NOTE: not each edge both has source and target
|
|
|
|
const nextNode = this.nodes.find(item => item.id === edge.target.cell); |
|
|
|
const nextNode = this.nodes.find((item) => item.id === edge.target.cell); |
|
|
|
nextNode && nodes.push(nextNode); |
|
|
|
} |
|
|
|
} |
|
|
@ -96,11 +96,11 @@ export default class Logic extends EventEmitter{ |
|
|
|
console.error('imove plugin must return an object.'); |
|
|
|
return; |
|
|
|
} |
|
|
|
for (let eventName in plugin) { |
|
|
|
if(!plugin.hasOwnProperty(eventName)) { |
|
|
|
for (const eventName in plugin) { |
|
|
|
if (!Object.prototype.hasOwnProperty.call(plugin, eventName)) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
if(LIFECYCLE.indexOf(eventName) === -1) { |
|
|
|
if (!LIFECYCLE.has(eventName)) { |
|
|
|
console.warn(`Lifecycle ${eventName} is not supported in imove.`); |
|
|
|
continue; |
|
|
|
} |
|
|
@ -120,16 +120,16 @@ export default class Logic extends EventEmitter{ |
|
|
|
} |
|
|
|
const nextNodes = this._getNextNodes(ctx, curNode, curRet); |
|
|
|
if (nextNodes.length > 0) { |
|
|
|
nextNodes.forEach(async node => { |
|
|
|
nextNodes.forEach(async (node) => { |
|
|
|
await this._execNode(ctx, node, lastRet); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async invoke(trigger, data) { |
|
|
|
let curNode = this._getStartNode(trigger); |
|
|
|
const curNode = this._getStartNode(trigger); |
|
|
|
if (!curNode) { |
|
|
|
return Promise.reject(`Invoke failed! No logic-start named ${trigger} found!`); |
|
|
|
return Promise.reject(new Error(`Invoke failed! No logic-start named ${trigger} found!`)); |
|
|
|
} |
|
|
|
const ctx = this._createCtx({ payload: data }); |
|
|
|
await this._execNode(ctx, curNode); |
|
|
|