array.insert throws length-exceeded event - fixes #314

This commit is contained in:
Kevin Jahns 2021-06-24 16:50:25 +02:00
parent 89c5541ee6
commit 311dd50f1b
4 changed files with 51 additions and 3 deletions

View File

@ -679,6 +679,8 @@ export const typeListInsertGenericsAfter = (transaction, parent, referenceItem,
packJsonContent() packJsonContent()
} }
const lengthExceeded = error.create('Length exceeded!')
/** /**
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType<any>} parent * @param {AbstractType<any>} parent
@ -689,6 +691,9 @@ export const typeListInsertGenericsAfter = (transaction, parent, referenceItem,
* @function * @function
*/ */
export const typeListInsertGenerics = (transaction, parent, index, content) => { export const typeListInsertGenerics = (transaction, parent, index, content) => {
if (index > parent._length) {
throw lengthExceeded
}
if (index === 0) { if (index === 0) {
if (parent._searchMarker) { if (parent._searchMarker) {
updateMarkerChanges(parent._searchMarker, index, content.length) updateMarkerChanges(parent._searchMarker, index, content.length)
@ -766,7 +771,7 @@ export const typeListDelete = (transaction, parent, index, length) => {
n = n.right n = n.right
} }
if (length > 0) { if (length > 0) {
throw error.create('array length exceeded') throw lengthExceeded
} }
if (parent._searchMarker) { if (parent._searchMarker) {
updateMarkerChanges(parent._searchMarker, startIndex, -startLength + length /* in case we remove the above exception */) updateMarkerChanges(parent._searchMarker, startIndex, -startLength + length /* in case we remove the above exception */)

View File

@ -61,6 +61,49 @@ export const testLengthIssue = tc => {
t.assert(arr.length === arr.toArray().length) t.assert(arr.length === arr.toArray().length)
} }
/**
* Debugging yjs#314
*
* @param {t.TestCase} tc
*/
export const testLengthIssue2 = tc => {
const doc = new Y.Doc()
const next = doc.getArray()
doc.transact(() => {
next.insert(0, ['group2'])
})
doc.transact(() => {
next.insert(1, ['rectangle3'])
})
doc.transact(() => {
next.delete(0)
next.insert(0, ['rectangle3'])
})
next.delete(1)
doc.transact(() => {
next.insert(1, ['ellipse4'])
})
doc.transact(() => {
next.insert(2, ['ellipse3'])
})
doc.transact(() => {
next.insert(3, ['ellipse2'])
})
doc.transact(() => {
doc.transact(() => {
t.fails(() => {
next.insert(5, ['rectangle2'])
})
next.insert(4, ['rectangle2'])
})
doc.transact(() => {
// this should not throw an error message
next.delete(4)
})
})
console.log(next.toArray())
}
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */