refactor read/write of structs

This commit is contained in:
Kevin Jahns
2019-04-07 23:08:08 +02:00
parent 90b3fa9dd9
commit 7a111de186
19 changed files with 418 additions and 304 deletions

View File

@@ -7,14 +7,12 @@
import {
AbstractItem,
AbstractItemRef,
getItemCleanEnd,
getItemCleanStart,
getItemType,
computeItemParams,
changeItemRefOffset,
GC,
splitItem,
addToDeleteSet,
StructStore, Transaction, ID, AbstractType // eslint-disable-line
Y, StructStore, Transaction, ID, AbstractType // eslint-disable-line
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
@@ -112,41 +110,29 @@ export class ItemDeletedRef extends AbstractItemRef {
return this.len
}
/**
* @param {Transaction} transaction
* @param {Y} y
* @param {StructStore} store
* @param {number} offset
* @return {ItemDeleted|GC}
*/
toStruct (transaction, offset) {
const y = transaction.y
const store = y.store
toStruct (y, store, offset) {
if (offset > 0) {
changeItemRefOffset(this, offset)
this.len = this.len - offset
}
let parent
if (this.parent !== null) {
const parentItem = getItemType(store, this.parent)
switch (parentItem.constructor) {
case ItemDeleted:
case GC:
return new GC(this.id, 1)
}
parent = parentItem.type
} else {
// @ts-ignore
parent = y.get(this.parentYKey)
}
return new ItemDeleted(
this.id,
this.left === null ? null : getItemCleanEnd(store, this.left),
this.left,
this.right === null ? null : getItemCleanStart(store, this.right),
this.right,
parent,
this.parentSub,
this.len
)
const { left, right, parent, parentSub } = computeItemParams(y, store, this.left, this.right, this.parent, this.parentSub, this.parentYKey)
return parent === null
? new GC(this.id, this.length)
: new ItemDeleted(
this.id,
left,
this.left,
right,
this.right,
parent,
parentSub,
this.len
)
}
}