Browse Source

fix: context output is not correct in CodeRun component

master
smallstonesk 4 years ago
parent
commit
10a0165f4e
  1. 7
      packages/compile-code/src/template/context.ts
  2. 4
      packages/compile-code/src/template/logic.ts
  3. 33
      packages/compile-code/src/template/runOnline.ts

7
packages/compile-code/src/template/context.ts

@ -6,7 +6,7 @@ export default `export default class Context {
_init(opts = {}) {
const { payload = {} } = opts;
this.curNode = null;
this.context = Object.create(null);
this.context = {};
this.payload = Object.freeze({ ...payload });
}
@ -15,11 +15,6 @@ export default `export default class Context {
this.lastRet = lastRet;
}
prepare(opts = {}) {
this.payload = opts.payload;
this.context = {};
}
getConfig() {
return this.curNode.data.configData;
}

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

@ -2,7 +2,7 @@ export default `import nodeFns from './nodeFns';
import Context from './context';
import EventEmitter from 'eventemitter3';
const LIFECYCLE = new Set(['ctxCreated']);
const LIFECYCLE = new Set(['ctxCreated', 'enterNode', 'leaveNode']);
const SHAPES = {
START: 'imove-start',
BRANCH: 'imove-branch',
@ -116,7 +116,9 @@ export default class Logic extends EventEmitter {
async _execNode(ctx, curNode, lastRet, callback) {
ctx._transitTo(curNode, lastRet);
const fn = nodeFns[curNode.id];
this._runLifecycleEvent('enterNode', ctx);
const curRet = await fn(ctx);
this._runLifecycleEvent('leaveNode', ctx);
if (curNode.shape !== SHAPES.BRANCH) {
lastRet = curRet;
}

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

@ -28,27 +28,32 @@ const makeCode = (mockNode: any, mockInput: any) => `
const mockPlugin = () => {
const mockNode = ${JSON.stringify(mockNode)};
const mockInput = ${JSON.stringify(mockInput)};
const mockKeys = [
['config', 'getConfig'],
const toMockTargets = [
['pipe', 'getPipe'],
['config', 'getConfig'],
['payload', 'getPayload'],
['context', 'getContext']
['context', 'getContext'],
];
return {
ctxCreated(ctx) {
if(mockNode && mockInput) {
mockKeys.forEach(([inputType, method]) => {
const _method = ctx[method];
ctx[method] = () => {
if(ctx.curNode.id === mockNode.id) {
return mockInput[inputType];
} else {
return _method();
}
}
enterNode(ctx) {
// hijack
if(ctx.curNode.id === mockNode.id) {
toMockTargets.forEach(item => {
const [type, method] = item;
item[2] = ctx[method];
ctx[method] = () => mockInput[type];
});
}
},
leaveNode(ctx) {
// restore
if(ctx.curNode.id === mockNode.id) {
toMockTargets.forEach(item => {
const [type, method, originMethod] = item;
ctx[method] = originMethod;
});
}
}
};
};

Loading…
Cancel
Save