AbstractItem.mergeWith helper outsourced into separate function

This commit is contained in:
Kevin Jahns
2019-04-24 18:10:33 +02:00
parent 45237571b7
commit 1d0f9faa91
8 changed files with 62 additions and 57 deletions

View File

@@ -27,6 +27,22 @@ import * as maplib from 'lib0/map.js'
import * as set from 'lib0/set.js'
import * as binary from 'lib0/binary.js'
/**
* @param {AbstractItem} left
* @param {AbstractItem} right
* @return {boolean} If true, right is removed from the linked list and should be discarded
*/
export const mergeItemWith = (left, right) => {
if (compareIDs(right.origin, left.lastId) && left.right === right && compareIDs(left.rightOrigin, right.rightOrigin)) {
left.right = right.right
if (left.right !== null) {
left.right.left = left
}
return true
}
return false
}
/**
* Split leftItem into two items
* @param {Transaction} transaction
@@ -376,22 +392,6 @@ export class AbstractItem extends AbstractStruct {
throw new Error('unimplemented')
}
/**
* @param {AbstractItem} right
* @return {boolean}
*
* @private
*/
mergeWith (right) {
if (compareIDs(right.origin, this.lastId) && this.right === right && compareIDs(this.rightOrigin, right.rightOrigin)) {
this.right = right.right
if (this.right !== null) {
this.right.left = this
}
return true
}
return false
}
/**
* Mark this Item as deleted.
*

View File

@@ -7,6 +7,7 @@ import {
GC,
splitItem,
addToDeleteSet,
mergeItemWith,
Y, StructStore, Transaction, ID, AbstractType // eslint-disable-line
} from '../internals.js'
@@ -78,7 +79,7 @@ export class ItemDeleted extends AbstractItem {
* @return {boolean}
*/
mergeWith (right) {
if (super.mergeWith(right)) {
if (mergeItemWith(this, right)) {
this._len += right._len
return true
}

View File

@@ -6,6 +6,7 @@ import {
splitItem,
changeItemRefOffset,
GC,
mergeItemWith,
Transaction, StructStore, Y, ID, AbstractType // eslint-disable-line
} from '../internals.js'
@@ -74,7 +75,7 @@ export class ItemJSON extends AbstractItem {
* @return {boolean}
*/
mergeWith (right) {
if (super.mergeWith(right)) {
if (mergeItemWith(this, right)) {
this.content = this.content.concat(right.content)
return true
}

View File

@@ -6,6 +6,7 @@ import {
splitItem,
changeItemRefOffset,
GC,
mergeItemWith,
Transaction, StructStore, Y, ID, AbstractType // eslint-disable-line
} from '../internals.js'
@@ -73,7 +74,7 @@ export class ItemString extends AbstractItem {
* @return {boolean}
*/
mergeWith (right) {
if (super.mergeWith(right)) {
if (mergeItemWith(this, right)) {
this.string += right.string
return true
}