after refactor - some tests are working again

This commit is contained in:
Kevin Jahns
2019-04-05 00:37:09 +02:00
parent 30bf3742c9
commit e56899a02c
23 changed files with 234 additions and 152 deletions

View File

@@ -11,6 +11,7 @@ import {
addStruct,
addToDeleteSet,
ItemDeleted,
findRootTypeKey,
ID, AbstractType, Y, Transaction // eslint-disable-line
} from '../internals.js'
@@ -229,7 +230,7 @@ export class AbstractItem extends AbstractStruct {
maplib.setIfUndefined(transaction.changed, parent, set.create).add(parentSub)
}
// @ts-ignore
if (parent._item.deleted || (left !== null && parentSub !== null)) {
if ((parent._item !== null && parent._item.deleted) || (left !== null && parentSub !== null)) {
// delete if parent is deleted or if this is not the current attribute value of parent
this.delete(transaction)
} else if (parentSub !== null && left === null && right !== null) {
@@ -443,16 +444,12 @@ export class AbstractItem extends AbstractStruct {
const info = (encodingRef & binary.BITS5) |
((this.origin === null) ? 0 : binary.BIT8) | // origin is defined
((this.rightOrigin === null) ? 0 : binary.BIT7) | // right origin is defined
((this.parentSub !== null) ? 0 : binary.BIT6) // parentSub is non-null
((this.parentSub === null) ? 0 : binary.BIT6) // parentSub is non-null
encoding.writeUint8(encoder, info)
if (offset === 0) {
writeID(encoder, this.id)
if (this.origin !== null) {
if (this.origin !== null) {
if (offset === 0) {
writeID(encoder, this.origin.lastId)
}
} else {
writeID(encoder, createID(this.id.client, this.id.clock + offset))
if (this.origin !== null) {
} else {
writeID(encoder, createID(this.id.client, this.id.clock + offset - 1))
}
}
@@ -465,17 +462,7 @@ export class AbstractItem extends AbstractStruct {
// parent type on y._map
// find the correct key
// @ts-ignore we know that y exists
const map = parent._y.share
let ykey = null
for (const [key, type] of map) {
if (type === parent) {
ykey = key
break
}
}
if (ykey === null) {
throw error.unexpectedCase()
}
const ykey = findRootTypeKey(this.parent)
encoding.writeVarUint(encoder, 1) // write parentYKey
encoding.writeVarString(encoder, ykey)
} else {
@@ -509,7 +496,7 @@ export class AbstractItemRef extends AbstractRef {
*/
this.right = (info & binary.BIT7) === binary.BIT7 ? readID(decoder) : null
const canCopyParentInfo = (info & (binary.BIT7 | binary.BIT8)) === 0
const hasParentYKey = decoding.readVarUint(decoder) === 1
const hasParentYKey = canCopyParentInfo ? decoding.readVarUint(decoder) === 1 : false
/**
* If parent = null and neither left nor right are defined, then we know that `parent` is child of `y`
* and we read the next string as parentYKey.
@@ -541,16 +528,4 @@ export class AbstractItemRef extends AbstractRef {
missing.push(this.parent)
}
}
/**
* @param {Transaction} transaction
* @return {Array<ID|null>}
*/
getMissing (transaction) {
return [
createID(this.id.client, this.id.clock - 1),
this.left,
this.right,
this.parent
]
}
}

View File

@@ -18,6 +18,7 @@ export class AbstractStruct {
* @readonly
*/
this.id = id
this.deleted = false
}
/**
* Merge this struct with the item to the right.
@@ -35,12 +36,6 @@ export class AbstractStruct {
get length () {
throw error.methodUnimplemented()
}
/**
* @type {boolean}
*/
get deleted () {
throw error.methodUnimplemented()
}
/**
* @param {encoding.Encoder} encoder The encoder to write data to.
* @param {number} offset
@@ -87,4 +82,10 @@ export class AbstractRef {
toStruct (transaction) {
throw error.methodUnimplemented()
}
/**
* @type {number}
*/
get length () {
return 1
}
}

View File

@@ -25,11 +25,12 @@ export class GC extends AbstractStruct {
/**
* @type {number}
*/
this.length = length
this._len = length
this.deleted = true
}
get deleted () {
return true
get length () {
return this._len
}
/**
@@ -37,7 +38,7 @@ export class GC extends AbstractStruct {
* @return {boolean}
*/
mergeWith (right) {
this.length += right.length
this._len += right.length
return true
}
@@ -52,7 +53,7 @@ export class GC extends AbstractStruct {
} else {
writeID(encoder, createID(this.id.client, this.id.clock + offset))
}
encoding.writeVarUint(encoder, this.length)
encoding.writeVarUint(encoder, this._len)
}
}
@@ -71,7 +72,10 @@ export class GCRef extends AbstractRef {
/**
* @type {number}
*/
this.length = decoding.readVarUint(decoder)
this._len = decoding.readVarUint(decoder)
}
get length () {
return this._len
}
missing () {
return [
@@ -84,7 +88,7 @@ export class GCRef extends AbstractRef {
toStruct () {
return new GC(
this.id,
this.length
this._len
)
}
}

View File

@@ -29,7 +29,11 @@ export class ItemDeleted extends AbstractItem {
*/
constructor (id, left, right, parent, parentSub, length) {
super(id, left, right, parent, parentSub)
this.length = length
this._len = length
this.deleted = true
}
get length () {
return this._len
}
/**
* @param {ID} id
@@ -47,7 +51,7 @@ export class ItemDeleted extends AbstractItem {
*/
mergeWith (right) {
if (right.origin === this && this.right === right) {
this.length += right.length
this._len += right.length
return true
}
return false
@@ -73,7 +77,10 @@ export class ItemDeletedRef extends AbstractItemRef {
/**
* @type {number}
*/
this.length = decoding.readVarUint(decoder)
this.len = decoding.readVarUint(decoder)
}
get length () {
return this.len
}
/**
* @param {Transaction} transaction
@@ -89,7 +96,7 @@ export class ItemDeletedRef extends AbstractItemRef {
// @ts-ignore
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent).type,
this.parentSub,
this.length
this.len
)
}
}

View File

@@ -74,7 +74,7 @@ export class ItemEmbedRef extends AbstractItemRef {
this.left === null ? null : getItemCleanEnd(store, transaction, this.left),
this.right === null ? null : getItemCleanStart(store, transaction, this.right),
// @ts-ignore
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent),
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent).type,
this.parentSub,
this.embed
)

View File

@@ -81,7 +81,7 @@ export class ItemFormatRef extends AbstractItemRef {
this.left === null ? null : getItemCleanEnd(store, transaction, this.left),
this.right === null ? null : getItemCleanStart(store, transaction, this.right),
// @ts-ignore
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent),
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent).type,
this.parentSub,
this.key,
this.value

View File

@@ -108,6 +108,9 @@ export class ItemJSONRef extends AbstractItemRef {
}
this.content = cs
}
get length () {
return this.content.length
}
/**
* @param {Transaction} transaction
* @return {ItemJSON}
@@ -120,7 +123,7 @@ export class ItemJSONRef extends AbstractItemRef {
this.left === null ? null : getItemCleanEnd(store, transaction, this.left),
this.right === null ? null : getItemCleanStart(store, transaction, this.right),
// @ts-ignore
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent),
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent).type,
this.parentSub,
this.content
)

View File

@@ -97,6 +97,9 @@ export class ItemStringRef extends AbstractItemRef {
*/
this.string = decoding.readVarString(decoder)
}
get length () {
return this.string.length
}
/**
* @param {Transaction} transaction
* @return {ItemString}
@@ -109,7 +112,7 @@ export class ItemStringRef extends AbstractItemRef {
this.left === null ? null : getItemCleanEnd(store, transaction, this.left),
this.right === null ? null : getItemCleanStart(store, transaction, this.right),
// @ts-ignore
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent),
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent).type,
this.parentSub,
this.string
)

View File

@@ -49,6 +49,14 @@ export const typeRefs = [
readYXmlText
]
export const YArrayRefID = 0
export const YMapRefID = 1
export const YTextRefID = 2
export const YXmlElementRefID = 3
export const YXmlFragmentRefID = 4
export const YXmlHookRefID = 5
export const YXmlTextRefID = 6
export class ItemType extends AbstractItem {
/**
* @param {ID} id
@@ -62,6 +70,7 @@ export class ItemType extends AbstractItem {
super(id, left, right, parent, parentSub)
this.type = type
}
getContent () {
return [this.type]
}
@@ -80,7 +89,8 @@ export class ItemType extends AbstractItem {
* @param {Transaction} transaction
*/
integrate (transaction) {
this.type._integrate(transaction, this)
this.type._integrate(transaction.y, this)
super.integrate(transaction)
}
/**
* @param {encoding.Encoder} encoder
@@ -168,7 +178,7 @@ export class ItemTypeRef extends AbstractItemRef {
this.left === null ? null : getItemCleanEnd(store, transaction, this.left),
this.right === null ? null : getItemCleanStart(store, transaction, this.right),
// @ts-ignore
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent),
this.parent === null ? y.get(this.parentYKey) : getItemType(store, this.parent).type,
this.parentSub,
this.type
)