From 9ff9f5b8efc61e1438dd5f90d93ee37ba4ae9681 Mon Sep 17 00:00:00 2001
From: Robin Leber <rleber98@gmail.com>
Date: Thu, 27 Jun 2024 16:55:04 +0200
Subject: [PATCH] ADD: pipe-binding: seperate in in and out

---
 src/functions/codeEditor.js  |  2 +-
 src/functions/pipeBinding.js | 84 ++++++++++++++++++++++++------------
 2 files changed, 58 insertions(+), 28 deletions(-)

diff --git a/src/functions/codeEditor.js b/src/functions/codeEditor.js
index ec84150..a350bac 100644
--- a/src/functions/codeEditor.js
+++ b/src/functions/codeEditor.js
@@ -6,7 +6,7 @@ import { showCheck } from "./visualValidation";
 import { handlePipeBinding, getPipesForFilter } from "./pipeBinding";
 
 let editorValues = {
-  doc: "const amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n    if (error0) {\n        throw error0;\n    }\n    connection.createChannel((error1, channel) => {\n        if (error1) {\n            throw error1;\n        }\n\n        // START pipe-binding\n        // END pipe-binding\n\n        // TODO: Code logic here\n\n\n\t});\n});\n",
+  doc: "const amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n    if (error0) {\n        throw error0;\n    }\n    connection.createChannel((error1, channel) => {\n        if (error1) {\n            throw error1;\n        }\n\n        // START pipe-binding\n        // incoming Pipes\n        // outgoing Pipes\n        // END pipe-binding\n\n        // TODO: Code logic here\n\n\n\t});\n});\n",
   extensions: [basicSetup, javascript()],
 };
 
diff --git a/src/functions/pipeBinding.js b/src/functions/pipeBinding.js
index 2fbd594..3d53cca 100644
--- a/src/functions/pipeBinding.js
+++ b/src/functions/pipeBinding.js
@@ -35,7 +35,7 @@ export const getPipesForFilter = (instance) => {
         const spanToChange = document.querySelector(
           `#${connection.pipeId} #PipeName`
         );
-        spanToChange.innerHTML = `"${pipeName}"`;
+        spanToChange.innerHTML = `${pipeName}`;
         showCheck(connection.pipeId);
         instance.repaintEverything();
         defaultCount++;
@@ -50,47 +50,55 @@ export const getPipesForFilter = (instance) => {
 
 export const handlePipeBinding = (pipeMapping, editor) => {
   // clear pipe-binindg code
-  const codeArray = editor.state.doc.toString();
-  const start = "        // START pipe-binding";
+  let codeArray = editor.state.doc.toString();
+  const start = "        // incoming Pipes";
+  const middle = "        // outgoing Pipes";
   const end = "        // END pipe-binding";
 
   const startIndex = codeArray.indexOf(start);
-  const endIndex = codeArray.indexOf(end, startIndex + start.length);
+  const midIndexIn = codeArray.indexOf(middle);
 
-  if (startIndex && endIndex) {
-    const transaction = editor.state.update({
+  if (startIndex && midIndexIn) {
+    const transactionForIn = editor.state.update({
       changes: [
         {
           from: startIndex + start.length,
+          to: midIndexIn,
+          insert: "\n\n",
+        },
+      ],
+    });
+    editor.dispatch(transactionForIn);
+  }
+  codeArray = editor.state.doc.toString();
+  const midIndexOut = codeArray.indexOf(middle);
+  const endIndex = codeArray.indexOf(end);
+
+  if (midIndexOut && endIndex) {
+    const transactionForOut = editor.state.update({
+      changes: [
+        {
+          from: midIndexOut + middle.length,
           to: endIndex,
           insert: "\n",
         },
       ],
     });
-    editor.dispatch(transaction);
+    editor.dispatch(transactionForOut);
   }
 
   // insert pipe-binding-code
-  let lineNumber = 15;
-  pipeMapping.forEach((pipe) => {
-    let line = editor.state.doc.line(lineNumber);
-    let position = line.from;
-    let pipeNameUserGiven = pipe.pipeName;
-    let pipeNameDeklaration = makeValidConstName(pipeNameUserGiven);
-    let insertCode = `\t\tconst ${pipeNameDeklaration} = "${pipeNameUserGiven}";\n\t\tchannel.assert${
-      pipe.pipeType === "Queue" ? "Queue" : "Exchange"
-    }(${pipeNameDeklaration}, ${
-      pipe.pipeType === "Topic" ? `"topic", ` : ""
-    }{\n\t\t\tdurable: false\n\t\t});\n`;
-    let transaction = editor.state.update({
-      changes: {
-        from: position,
-        insert: insertCode,
-      },
-    });
-    editor.dispatch(transaction);
-    lineNumber = lineNumber + 4;
-  });
+  const incomingPipes = pipeMapping.filter(
+    (pipe) => pipe.pipeDirection === "in"
+  );
+  const outgoingPipes = pipeMapping.filter(
+    (pipe) => pipe.pipeDirection === "out"
+  );
+  let lineNumberIn = 16;
+  let lineNumberOut = 18 + incomingPipes.length * 4;
+
+  fillEditorwithCode(editor, incomingPipes, lineNumberIn);
+  fillEditorwithCode(editor, outgoingPipes, lineNumberOut);
 };
 
 const makeValidConstName = (str) => {
@@ -164,3 +172,25 @@ const buildPipesElements = (pipeMapping) => {
     outgoingPipesElement.appendChild(pipeElement);
   });
 };
+
+const fillEditorwithCode = (editor, pipeMapping, lineNumber) => {
+  pipeMapping.forEach((pipe) => {
+    let line = editor.state.doc.line(lineNumber);
+    let position = line.from;
+    let pipeNameUserGiven = pipe.pipeName;
+    let pipeNameDeklaration = makeValidConstName(pipeNameUserGiven);
+    let insertCode = `\t\tconst ${pipeNameDeklaration} = "${pipeNameUserGiven}";\n\t\tchannel.assert${
+      pipe.pipeType === "Queue" ? "Queue" : "Exchange"
+    }(${pipeNameDeklaration}, ${
+      pipe.pipeType === "Topic" ? `"topic", ` : ""
+    }{\n\t\t\tdurable: false\n\t\t});\n`;
+    let transaction = editor.state.update({
+      changes: {
+        from: position,
+        insert: insertCode,
+      },
+    });
+    editor.dispatch(transaction);
+    lineNumber = lineNumber + 4;
+  });
+};
-- 
GitLab