implement performant push in arrays
This commit is contained in:
parent
3aebb8db83
commit
20e1234af2
@ -735,6 +735,29 @@ export const typeListInsertGenerics = (transaction, parent, index, content) => {
|
|||||||
return typeListInsertGenericsAfter(transaction, parent, n, content)
|
return typeListInsertGenericsAfter(transaction, parent, n, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pushing content is special as we generally want to push after the last item. So we don't have to update
|
||||||
|
* the serach marker.
|
||||||
|
*
|
||||||
|
* @param {Transaction} transaction
|
||||||
|
* @param {AbstractType<any>} parent
|
||||||
|
* @param {Array<Object<string,any>|Array<any>|number|null|string|Uint8Array>} content
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @function
|
||||||
|
*/
|
||||||
|
export const typeListPushGenerics = (transaction, parent, content) => {
|
||||||
|
// Use the marker with the highest index and iterate to the right.
|
||||||
|
const marker = (parent._searchMarker || []).reduce((maxMarker, currMarker) => currMarker.index > maxMarker.index ? currMarker : maxMarker, { index: 0, p: parent._start })
|
||||||
|
let n = marker.p
|
||||||
|
if (n) {
|
||||||
|
while (n.right) {
|
||||||
|
n = n.right
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return typeListInsertGenericsAfter(transaction, parent, n, content)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Transaction} transaction
|
* @param {Transaction} transaction
|
||||||
* @param {AbstractType<any>} parent
|
* @param {AbstractType<any>} parent
|
||||||
|
@ -10,6 +10,7 @@ import {
|
|||||||
typeListForEach,
|
typeListForEach,
|
||||||
typeListCreateIterator,
|
typeListCreateIterator,
|
||||||
typeListInsertGenerics,
|
typeListInsertGenerics,
|
||||||
|
typeListPushGenerics,
|
||||||
typeListDelete,
|
typeListDelete,
|
||||||
typeListMap,
|
typeListMap,
|
||||||
YArrayRefID,
|
YArrayRefID,
|
||||||
@ -142,9 +143,17 @@ export class YArray extends AbstractType {
|
|||||||
* Appends content to this YArray.
|
* Appends content to this YArray.
|
||||||
*
|
*
|
||||||
* @param {Array<T>} content Array of content to append.
|
* @param {Array<T>} content Array of content to append.
|
||||||
|
*
|
||||||
|
* @todo Use the following implementation in all types.
|
||||||
*/
|
*/
|
||||||
push (content) {
|
push (content) {
|
||||||
this.insert(this.length, content)
|
if (this.doc !== null) {
|
||||||
|
transact(this.doc, transaction => {
|
||||||
|
typeListPushGenerics(transaction, this, content)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
/** @type {Array<any>} */ (this._prelimContent).push(...content)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user