|
@ -32,6 +32,11 @@ export default class Logic extends EventEmitter { |
|
|
return this.cells.filter((cell) => cell.shape === 'edge'); |
|
|
return this.cells.filter((cell) => cell.shape === 'edge'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_getUnsafeCtx() { |
|
|
|
|
|
// NOTE: don't use in prod
|
|
|
|
|
|
return this._unsafeCtx; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
_runLifecycleEvent(eventName, ctx) { |
|
|
_runLifecycleEvent(eventName, ctx) { |
|
|
if (!LIFECYCLE.has(eventName)) { |
|
|
if (!LIFECYCLE.has(eventName)) { |
|
|
return console.warn(\`Lifecycle \${eventName} is not supported!\`);
|
|
|
return console.warn(\`Lifecycle \${eventName} is not supported!\`);
|
|
@ -108,7 +113,7 @@ export default class Logic extends EventEmitter { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async _execNode(ctx, curNode, lastRet) { |
|
|
async _execNode(ctx, curNode, lastRet, callback) { |
|
|
ctx._transitTo(curNode, lastRet); |
|
|
ctx._transitTo(curNode, lastRet); |
|
|
const fn = nodeFns[curNode.id]; |
|
|
const fn = nodeFns[curNode.id]; |
|
|
const curRet = await fn(ctx); |
|
|
const curRet = await fn(ctx); |
|
@ -118,18 +123,20 @@ export default class Logic extends EventEmitter { |
|
|
const nextNodes = this._getNextNodes(ctx, curNode, curRet); |
|
|
const nextNodes = this._getNextNodes(ctx, curNode, curRet); |
|
|
if (nextNodes.length > 0) { |
|
|
if (nextNodes.length > 0) { |
|
|
nextNodes.forEach(async (node) => { |
|
|
nextNodes.forEach(async (node) => { |
|
|
await this._execNode(ctx, node, lastRet); |
|
|
await this._execNode(ctx, node, lastRet, callback); |
|
|
}); |
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
callback && callback(lastRet); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async invoke(trigger, data) { |
|
|
async invoke(trigger, data, callback) { |
|
|
const curNode = this._getStartNode(trigger); |
|
|
const curNode = this._getStartNode(trigger); |
|
|
if (!curNode) { |
|
|
if (!curNode) { |
|
|
return Promise.reject(new Error(\`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 }); |
|
|
this._unsafeCtx = this._createCtx({ payload: data }); |
|
|
await this._execNode(ctx, curNode); |
|
|
await this._execNode(this._unsafeCtx, curNode, undefined, callback); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
`;
|
|
|
`;
|
|
|