Browse Source

feat: add callback for logic.invoke

master
smallstonesk 4 years ago
parent
commit
d781c99b51
  1. 17
      packages/compile-code/src/template/logic.ts
  2. 6
      packages/compile-code/src/template/runOnline.ts

17
packages/compile-code/src/template/logic.ts

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

6
packages/compile-code/src/template/runOnline.ts

@ -26,6 +26,10 @@ export default `
// instantiation and invoke // instantiation and invoke
const logic = new Logic({ dsl }); const logic = new Logic({ dsl });
logic.invoke('$TRIGGER$'); logic.invoke('$TRIGGER$', {}, (pipe) => {
const ctx = logic._getUnsafeCtx();
const context = ctx.getContext();
window.dispatchEvent(new CustomEvent('iMoveOnlineExecEnds', {detail: {pipe, context}}));
});
})(); })();
`; `;

Loading…
Cancel
Save