From d7a11ccf4d0cc9a543d71296e9c36225e32b554d Mon Sep 17 00:00:00 2001
From: Kevin Jahns <kevin.jahns@pm.me>
Date: Thu, 18 Jun 2020 00:31:25 +0200
Subject: [PATCH] fix gc regression issue & add another breaking condition for
 the integration algorithm

---
 src/structs/Item.js | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/structs/Item.js b/src/structs/Item.js
index bedbfeda..95acec56 100644
--- a/src/structs/Item.js
+++ b/src/structs/Item.js
@@ -355,6 +355,9 @@ export class Item extends AbstractStruct {
       this.right = getItemCleanStart(transaction, this.rightOrigin)
       this.rightOrigin = this.right.id
     }
+    if ((this.left && this.left.constructor === GC) || (this.right && this.right.constructor === GC)) {
+      this.parent = null
+    }
     // only set parent if this shouldn't be garbage collected
     if (!this.parent) {
       if (this.left && this.left.constructor === Item) {
@@ -432,10 +435,14 @@ export class Item extends AbstractStruct {
             if (o.id.client < this.id.client) {
               left = o
               conflictingItems.clear()
-            }
-          } else if (o.origin !== null && itemsBeforeOrigin.has(getItem(transaction.doc.store, o.origin))) {
+            } else if (compareIDs(this.rightOrigin, o.rightOrigin)) {
+              // this and o are conflicting and point to the same integration points. The id decides which item comes first.
+              // Since this is to the left of o, we can break here
+              break
+            } // else, o might be integrated before an item that this conflicts with. If so, we will find it in the next iterations
+          } else if (o.origin !== null && itemsBeforeOrigin.has(getItem(transaction.doc.store, o.origin))) { // use getItem instead of getItemCleanEnd because we don't want / need to split items.
             // case 2
-            if (o.origin === null || !conflictingItems.has(getItem(transaction.doc.store, o.origin))) {
+            if (!conflictingItems.has(getItem(transaction.doc.store, o.origin))) {
               left = o
               conflictingItems.clear()
             }