Browse Source

feat: package block handler

Ahmad Kholid 2 years ago
parent
commit
32e1fc2a6a

+ 54 - 31
src/background/workflowEngine/blocksHandler/handlerBlockPackage.js

@@ -1,49 +1,72 @@
-/* eslint-disable */
 export default async function (
   { data, id },
-  { targetHandle, sourceHandle, prevBlockData }
+  { targetHandle: prevTarget, prevBlockData }
 ) {
-  const { 1: targetId } = targetHandle.split('input-');
-  const input = data.inputs.find((input) => input.id === targetId);
+  if (!this.engine.packagesCache[id]) {
+    this.engine.packagesCache[id] = { extracted: false, nodes: {} };
+  }
+
+  const pkgCache = this.engine.packagesCache[id];
+
+  const { 1: targetId } = prevTarget.split('input-');
+  const hasCache = pkgCache.nodes[targetId];
+  if (hasCache)
+    return {
+      data: prevBlockData,
+      nextBlockId: [{ id: hasCache }],
+    };
+
+  const input = data.inputs.find((item) => item.id === targetId);
   if (!input) {
     throw new Error('Input not found');
   }
+  const block = data.data.nodes.find((node) => node.id === input.blockId);
+  pkgCache.nodes[targetId] = block.id;
 
-  let block = null;
+  const connections = {};
 
-  data.data.nodes.find((node) => {
-    if (node.id === input.blockId) {
-      block = node;
-    }
+  if (!pkgCache.extracted) {
+    const outputsMap = new Set();
 
-    this.engine.blocks[node.id] = { ...node };
-  });
+    data.inputs.forEach((item) => {
+      connections[item.id] = [
+        { id: item.blockId, targetId: `${block.id}-input-1` },
+      ];
+    });
+    data.outputs.forEach((output) => {
+      outputsMap.add(output.handleId);
 
-  if (!block) {
-    throw new Error(`Can't find block for this input`);
-  }
+      const connection =
+        this.engine.connectionsMap[`${id}-output-${output.id}`];
+      if (!connection) return;
 
-  console.log(id, data);
-  const connections = {}
+      connections[output.handleId] = [...connection];
+    });
 
-  // const connections = data.data.edges.reduce(
-  //   (acc, { sourceHandle, target, targetHandle }) => {
-  //     // const outputId =
+    data.data.nodes.forEach((node) => {
+      this.engine.blocks[node.id] = { ...node };
+    });
 
-  //     if (!acc[sourceHandle]) acc[sourceHandle] = [];
-  //     acc[sourceHandle].push({ id: target, targetHandle, sourceHandle });
+    if (!block) {
+      throw new Error(`Can't find block for this input`);
+    }
+
+    data.data.edges.forEach(({ sourceHandle, target, targetHandle }) => {
+      if (outputsMap.has(sourceHandle)) return;
+
+      if (!connections[sourceHandle]) connections[sourceHandle] = [];
+      connections[sourceHandle].push({
+        id: target,
+        targetHandle,
+        sourceHandle,
+      });
+    });
+
+    pkgCache.extracted = true;
+  }
 
-  //     return acc;
-  //   },
-  //   {
-  //     [targetId]: [
-  //       { id: block.id, sourceHandle, targetId: `${block.id}-input-1` },
-  //     ],
-  //   }
-  // );
+  Object.assign(this.engine.connectionsMap, connections);
 
-  // Object.assign(this.engine.connectionsMap, connections);
-  // console.log('MAP:\t', this.engine.connectionsMap);
   return {
     data: prevBlockData,
     nextBlockId: [{ id: block.id }],

+ 9 - 0
src/newtab/pages/workflows/[id].vue

@@ -895,6 +895,15 @@ function onNodesChange(changes) {
       state.dataChanged = true;
       nodeChanges.removed.push(id);
     } else if (type === 'add') {
+      if (isPackage) {
+        const excludeBlocks = ['block-package', 'trigger'];
+        if (excludeBlocks.includes(item.label)) {
+          editor.value.removeNodes([item]);
+        }
+
+        return;
+      }
+
       nodeChanges.added.push(item);
     }
   });