From e1df1a7a123ea24a43564c5f62efb1ad76153cad Mon Sep 17 00:00:00 2001
From: Kevin Jahns <kevin.jahns@rwth-aachen.de>
Date: Wed, 13 Apr 2016 16:59:54 +0200
Subject: [PATCH] fixed one more problem with ds & fixed ace example &
 insertions are combined even when created in different execution tasks

---
 src/Database.js    | 29 ++++++++++++++++++++++++++++-
 src/Transaction.js |  4 ++--
 2 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/src/Database.js b/src/Database.js
index 67d787dc..b9998b4e 100644
--- a/src/Database.js
+++ b/src/Database.js
@@ -300,7 +300,12 @@ module.exports = function (Y /* :any */) {
         for (var sid in ls) {
           var l = ls[sid]
           var id = JSON.parse(sid)
-          var op = yield* this.getInsertion(id)
+          var op
+          if (typeof id[1] === 'string') {
+            op = yield* this.getOperation(id)
+          } else {
+            op = yield* this.getInsertion(id)
+          }
           if (op == null) {
             store.listenersById[sid] = l
           } else {
@@ -338,6 +343,28 @@ module.exports = function (Y /* :any */) {
             yield* Y.Struct[op.struct].execute.call(this, op)
             yield* this.addOperation(op)
             yield* this.store.operationAdded(this, op)
+
+            // if insertion, try to combine with left
+            if (op.left != null &&
+                op.content != null &&
+                op.left[0] === op.id[0] &&
+                Y.utils.compareIds(op.left, op.origin)
+            ) {
+              var left = yield* this.getInsertion(op.left)
+              if (left.content != null &&
+                  left.id[1] + left.content.length === op.id[1] &&
+                  left.originOf.length === 1 &&
+                  !left.gc && !left.deleted &&
+                  !op.gc && !op.deleted
+              ) {
+                // combine!
+                left.originOf = op.originOf
+                left.content = left.content.concat(op.content)
+                left.right = op.right
+                yield* this.os.delete(op.id)
+                yield* this.setOperation(left)
+              }
+            }
           }
         }
       }
diff --git a/src/Transaction.js b/src/Transaction.js
index 85de9f95..b1fe0444 100644
--- a/src/Transaction.js
+++ b/src/Transaction.js
@@ -378,12 +378,12 @@ module.exports = function (Y/* :any */) {
             break
           } else {
             // we can extend n with next
-            if (diff >= next.len) {
+            if (diff > next.len) {
               // n is even longer than next
               // get next.next, and try to extend it
               var _next = yield* this.ds.findNext(next.id)
               yield* this.ds.delete(next.id)
-              if (_next == null || n.id[0] !== next.id[0]) {
+              if (_next == null || n.id[0] !== _next.id[0]) {
                 break
               } else {
                 next = _next
-- 
GitLab