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