diff --git a/structs/Item.js b/structs/Item.js index 7ea72def..0f1cd9cf 100644 --- a/structs/Item.js +++ b/structs/Item.js @@ -251,16 +251,22 @@ export class Item { */ _delete (y, createDelete = true, gcChildren) { 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 y.ds.mark(this._id, this._length, false) let del = new Delete() del._targetID = this._id - del._length = this._length + del._length = len if (createDelete) { // broadcast and persists Delete del._integrate(y, true) } - transactionTypeChanged(y, this._parent, this._parentSub) + transactionTypeChanged(y, parent, this._parentSub) y._transaction.deletedStructs.add(this) } } @@ -410,6 +416,10 @@ export class Item { right._left = this } } + // adjust the length of parent + if (parent.length !== undefined && this._countable) { + parent.length += this._length + } if (parent._deleted) { this._delete(y, false, true) } diff --git a/tests/helper.js b/tests/helper.js index 6e3d35ed..1a864c11 100644 --- a/tests/helper.js +++ b/tests/helper.js @@ -313,9 +313,11 @@ export const compareUsers = (t, users) => { }) for (var i = 0; i < data.length - 1; i++) { 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(userMapValues[i], userMapValues[i + 1], 'map 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(userQuillValues[i], userQuillValues[i + 1], 'quill delta content') t.compare(data[i].os, data[i + 1].os, 'os') diff --git a/types/YArray.js b/types/YArray.js index 9aa45a89..925bf72f 100644 --- a/types/YArray.js +++ b/types/YArray.js @@ -72,6 +72,10 @@ export class YArrayEvent extends YEvent { * A shared Array implementation. */ export class YArray extends Type { + constructor () { + super() + this.length = 0 + } /** * 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] () { return { next: function () {