From d615d07f745b4c6201ee58474248216b8841a31f Mon Sep 17 00:00:00 2001
From: Maximilian Giller <m.giller@tu-bs.de>
Date: Wed, 27 Jul 2022 23:53:53 +0200
Subject: [PATCH] Prevents creation of duplicate links

---
 src/editor/js/structures/graph/graph.ts | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/editor/js/structures/graph/graph.ts b/src/editor/js/structures/graph/graph.ts
index ebf36c2..b6d42d3 100644
--- a/src/editor/js/structures/graph/graph.ts
+++ b/src/editor/js/structures/graph/graph.ts
@@ -295,6 +295,21 @@ export class Graph extends ManagedData {
         return true;
     }
 
+    getLink(sourceId: number, targetId: number, directionSensitive = true): Link {
+        return this.links.find((l) => {
+            if (l.sourceId === sourceId && l.targetId === targetId) {
+                return true;
+            }
+
+            // Check other direction if allowed
+            if (!directionSensitive && (l.sourceId === targetId && l.targetId === sourceId)) {
+                return true;
+            }
+
+            return false;
+        });
+    }
+
     getNode(id: number): Node {
         return this.getElementWithId(this.nodes, id);
     }
@@ -318,7 +333,7 @@ export class Graph extends ManagedData {
      * @returns True, if successful.
      */
     public addLink(link: Link): boolean {
-        if (this.data.links.includes(link)) {
+        if (this.getLink(link.sourceId, link.targetId, false) !== undefined) {
             return true; // Already exists in graph.
         }
 
-- 
GitLab