Browse Source

fix(compile-code): run imove-branch node port condition only once while getNextNodes

master
Hans Chan 4 years ago
parent
commit
27d894720e
  1. 4
      example/src/pages/index.tsx
  2. 25
      packages/compile-code/src/template/logic.ts

4
example/src/pages/index.tsx

@ -7,8 +7,8 @@ const onSave = (data: { nodes: any; edges: any }): void => {
function Arrange(): JSX.Element { function Arrange(): JSX.Element {
return ( return (
<div style={{height: '100vh'}}> <div style={{ height: '100vh' }}>
<IMove onSave={onSave}/> <IMove onSave={onSave} />
</div> </div>
); );
} }

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

@ -63,23 +63,30 @@ export default class Logic extends EventEmitter {
_getNextNodes(ctx, curNode, curRet) { _getNextNodes(ctx, curNode, curRet) {
const nodes = []; const nodes = [];
for (const edge of this.edges) {
let isMatched = edge.source.cell === curNode.id; // NOTE: if it is a imove-branch node, find out which port match the curRet condition
// NOTE: if it is a imove-branch node, each port's condition should be tested whether it is matched const isCurNodeShapeBranch = curNode.shape === SHAPES.BRANCH;
if (curNode.shape === SHAPES.BRANCH) { let curNodeMatchedPort = '';
let matchedPort = ''; if (isCurNodeShapeBranch) {
const { ports } = curNode.data; const { ports } = curNode.data;
for (const key in ports) { for (const key in ports) {
const { condition } = ports[key]; const { condition } = ports[key];
// eslint-disable-next-line no-new-func
const ret = new Function('ctx', 'return ' + condition)(ctx); const ret = new Function('ctx', 'return ' + condition)(ctx);
if (ret === Boolean(curRet)) { if (ret === Boolean(curRet)) {
matchedPort = key; curNodeMatchedPort = key;
break; break; // for (const key in ports)
} }
} }
isMatched = isMatched && edge.source.port === matchedPort;
} }
if (isMatched) {
// NOTE: find out next node via edges which source is curNode
for (const edge of this.edges) {
// edge's source is curNode
const isMatchedSource = edge.source.cell === curNode.id;
// if it is a imove-branch node, edge.source.port match curRet condition
const isMatchedPort = !isCurNodeShapeBranch || edge.source.port === curNodeMatchedPort;
if (isMatchedSource && isMatchedPort) {
// NOTE: not each edge both has source and target // 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); nextNode && nodes.push(nextNode);

Loading…
Cancel
Save