fix #297 (length not updated) by updating search markers properly

This commit is contained in:
Kevin Jahns 2021-05-25 21:17:01 +02:00
parent dc9717ecd0
commit 6886881b76
2 changed files with 42 additions and 0 deletions

View File

@ -566,6 +566,19 @@ export class Item extends AbstractStruct {
this.content.constructor === right.content.constructor &&
this.content.mergeWith(right.content)
) {
const searchMarker = /** @type {AbstractType<any>} */ (this.parent)._searchMarker
if (searchMarker) {
searchMarker.forEach(marker => {
if (marker.p === right) {
// right is going to be "forgotten" so we need to update the marker
marker.p = this
// adjust marker index
if (!this.deleted) {
marker.index -= this.length
}
}
})
}
if (right.keep) {
this.keep = true
}

View File

@ -32,6 +32,35 @@ export const testSlice = tc => {
t.compareArrays(arr.slice(0, 2), [0, 1])
}
/**
* Debugging yjs#297 - a critical bug connected to the search-marker approach
*
* @param {t.TestCase} tc
*/
export const testLengthIssue = tc => {
const doc1 = new Y.Doc()
const arr = doc1.getArray('array')
arr.push([0, 1, 2, 3])
arr.delete(0)
arr.insert(0, [0])
t.assert(arr.length === arr.toArray().length)
doc1.transact(() => {
arr.delete(1)
t.assert(arr.length === arr.toArray().length)
arr.insert(1, [1])
t.assert(arr.length === arr.toArray().length)
arr.delete(2)
t.assert(arr.length === arr.toArray().length)
arr.insert(2, [2])
t.assert(arr.length === arr.toArray().length)
})
t.assert(arr.length === arr.toArray().length)
arr.delete(1)
t.assert(arr.length === arr.toArray().length)
arr.insert(1, [1])
t.assert(arr.length === arr.toArray().length)
}
/**
* @param {t.TestCase} tc
*/