all YArray.tests type fixes

This commit is contained in:
Kevin Jahns
2019-04-03 02:30:44 +02:00
parent e23582b1cd
commit 415de1cc4c
17 changed files with 383 additions and 180 deletions

View File

@@ -17,6 +17,7 @@ import { AbstractRef, AbstractStruct } from './AbstractStruct.js' // eslint-disa
import * as error from 'lib0/error.js'
import { replaceStruct, addStruct } from '../utils/StructStore.js'
import { addToDeleteSet } from '../utils/DeleteSet.js'
import { ItemDeleted } from './ItemDeleted.js'
/**
* Split leftItem into two items
@@ -408,9 +409,14 @@ export class AbstractItem extends AbstractStruct {
/**
* @param {Y} y
* @return {GC|ItemDeleted}
*/
gc (y) {
replaceStruct(y.store, this, new GC(this.id, this.length))
const r = this.parent._item !== null && this.parent._item.deleted
? new GC(this.id, this.length)
: new ItemDeleted(this.id, this.left, this.right, this.parent, this.parentSub, this.length)
replaceStruct(y.store, this, r)
return r
}
/**

View File

@@ -17,6 +17,16 @@ export class AbstractStruct {
*/
this.id = id
}
/**
* Merge this struct with the item to the right.
* This method is already assuming that `this.id.clock + this.length === this.id.clock`.
* Also this method does *not* remove right from StructStore!
* @param {AbstractStruct} right
* @return {boolean} wether this merged with right
*/
mergeWith (right) {
return false
}
/**
* @type {number}
*/

View File

@@ -3,6 +3,7 @@
*/
import { AbstractRef, AbstractStruct } from './AbstractStruct.js'
import { ID, readID, createID, writeID } from '../utils/ID.js' // eslint-disable-line
import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
import * as decoding from 'lib0/decoding.js'
import * as encoding from 'lib0/encoding.js'
@@ -26,6 +27,15 @@ export class GC extends AbstractStruct {
return true
}
/**
* @param {AbstractStruct} right
* @return {boolean}
*/
mergeWith (right) {
this.length += right.length
return true
}
/**
* @param {encoding.Encoder} encoder
* @param {number} offset

View File

@@ -37,6 +37,17 @@ export class ItemDeleted extends AbstractItem {
copy (id, left, right, parent, parentSub) {
return new ItemDeleted(id, left, right, parent, parentSub, this.length)
}
/**
* @param {ItemDeleted} right
* @return {boolean}
*/
mergeWith (right) {
if (right.origin === this && this.right === right) {
this.length += right.length
return true
}
return false
}
/**
* @param {encoding.Encoder} encoder
* @param {number} offset

View File

@@ -25,6 +25,9 @@ export class ItemJSON extends AbstractItem {
*/
constructor (id, left, right, parent, parentSub, content) {
super(id, left, right, parent, parentSub)
/**
* @type {Array<any>}
*/
this.content = content
}
/**
@@ -56,6 +59,17 @@ export class ItemJSON extends AbstractItem {
right.content = this.content.splice(diff)
return right
}
/**
* @param {ItemJSON} right
* @return {boolean}
*/
mergeWith (right) {
if (right.origin === this && this.right === right) {
this.content = this.content.concat(right.content)
return true
}
return false
}
/**
* @param {encoding.Encoder} encoder
* @param {number} offset
@@ -63,8 +77,8 @@ export class ItemJSON extends AbstractItem {
write (encoder, offset) {
super.write(encoder, offset, structJSONRefNumber)
const len = this.content.length
encoding.writeVarUint(encoder, len)
for (let i = 0; i < len; i++) {
encoding.writeVarUint(encoder, len - offset)
for (let i = offset; i < len; i++) {
const c = this.content[i]
encoding.writeVarString(encoder, c === undefined ? 'undefined' : JSON.stringify(c))
}

View File

@@ -60,13 +60,24 @@ export class ItemString extends AbstractItem {
this.string = this.string.slice(0, diff)
return right
}
/**
* @param {ItemString} right
* @return {boolean}
*/
mergeWith (right) {
if (right.origin === this && this.right === right) {
this.string += right.string
return true
}
return false
}
/**
* @param {encoding.Encoder} encoder
* @param {number} offset
*/
write (encoder, offset) {
super.write(encoder, offset, structStringRefNumber)
encoding.writeVarString(encoder, this.string)
encoding.writeVarString(encoder, offset === 0 ? this.string : this.string.slice(offset))
}
}

View File

@@ -18,6 +18,8 @@ import { readYXmlHook } from '../types/YXmlHook.js'
import { readYXmlText } from '../types/YXmlText.js'
import { getItemCleanEnd, getItemCleanStart, getItemType } from '../utils/StructStore.js'
import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
import { GC } from './GC.js' // eslint-disable-line
import { ItemDeleted } from './ItemDeleted.js' // eslint-disable-line
/**
* @param {Y} y
@@ -130,10 +132,11 @@ export class ItemType extends AbstractItem {
/**
* @param {Y} y
* @return {ItemDeleted|GC}
*/
gc (y) {
this.gcChildren(y)
super.gc(y)
return super.gc(y)
}
}