more efficient length computing

This commit is contained in:
Kevin Jahns 2018-11-28 13:20:14 +01:00
parent a2c51c36e9
commit c9ea3a412e
3 changed files with 18 additions and 17 deletions

View File

@ -251,16 +251,22 @@ export class Item {
*/ */
_delete (y, createDelete = true, gcChildren) { _delete (y, createDelete = true, gcChildren) {
if (!this._deleted) { if (!this._deleted) {
const parent = this._parent
const len = this._length
// adjust the length of parent
if (parent.length !== undefined && this._countable) {
parent.length -= len
}
this._deleted = true this._deleted = true
y.ds.mark(this._id, this._length, false) y.ds.mark(this._id, this._length, false)
let del = new Delete() let del = new Delete()
del._targetID = this._id del._targetID = this._id
del._length = this._length del._length = len
if (createDelete) { if (createDelete) {
// broadcast and persists Delete // broadcast and persists Delete
del._integrate(y, true) del._integrate(y, true)
} }
transactionTypeChanged(y, this._parent, this._parentSub) transactionTypeChanged(y, parent, this._parentSub)
y._transaction.deletedStructs.add(this) y._transaction.deletedStructs.add(this)
} }
} }
@ -410,6 +416,10 @@ export class Item {
right._left = this right._left = this
} }
} }
// adjust the length of parent
if (parent.length !== undefined && this._countable) {
parent.length += this._length
}
if (parent._deleted) { if (parent._deleted) {
this._delete(y, false, true) this._delete(y, false, true)
} }

View File

@ -313,9 +313,11 @@ export const compareUsers = (t, users) => {
}) })
for (var i = 0; i < data.length - 1; i++) { for (var i = 0; i < data.length - 1; i++) {
t.group(() => { t.group(() => {
t.compare(userArrayValues[i].length, users[i].get('array').length, 'array length correctly computed')
t.compare(userArrayValues[i], userArrayValues[i + 1], 'array types') t.compare(userArrayValues[i], userArrayValues[i + 1], 'array types')
t.compare(userMapValues[i], userMapValues[i + 1], 'map types') t.compare(userMapValues[i], userMapValues[i + 1], 'map types')
t.compare(userXmlValues[i], userXmlValues[i + 1], 'xml types') t.compare(userXmlValues[i], userXmlValues[i + 1], 'xml types')
t.compare(userTextValues[i].map(a => a.insert).join('').length, users[i].get('text').length, 'text length correctly computed')
t.compare(userTextValues[i], userTextValues[i + 1], 'text types') t.compare(userTextValues[i], userTextValues[i + 1], 'text types')
t.compare(userQuillValues[i], userQuillValues[i + 1], 'quill delta content') t.compare(userQuillValues[i], userQuillValues[i + 1], 'quill delta content')
t.compare(data[i].os, data[i + 1].os, 'os') t.compare(data[i].os, data[i + 1].os, 'os')

View File

@ -72,6 +72,10 @@ export class YArrayEvent extends YEvent {
* A shared Array implementation. * A shared Array implementation.
*/ */
export class YArray extends Type { export class YArray extends Type {
constructor () {
super()
this.length = 0
}
/** /**
* Creates YArray Event and calls observers. * Creates YArray Event and calls observers.
* *
@ -167,21 +171,6 @@ export class YArray extends Type {
} }
} }
/**
* Computes the length of this YArray.
*/
get length () {
let length = 0
let n = this._start
while (n !== null) {
if (!n._deleted && n._countable) {
length += n._length
}
n = n._right
}
return length
}
[Symbol.iterator] () { [Symbol.iterator] () {
return { return {
next: function () { next: function () {