renamed YArray.link -> YArray.quote

This commit is contained in:
Bartosz Sypytkowski 2023-07-07 10:20:10 +02:00
parent 02367b612b
commit 5415565cf0
3 changed files with 18 additions and 12 deletions

View File

@ -204,18 +204,21 @@ export class YArray extends AbstractType {
}
/**
* Returns the weak link to i-th element from a YArray.
* Returns the weak link that allows to refer and observe live changes of contents of an YArray.
* It points at a consecutive range of elements, starting at give `index` and spanning over provided
* length of elements.
*
* @param {number} index The index of the element to return from the YArray
* @param {number} length The number of elements to include in returned weak link reference.
* @return {YWeakLink<T>}
*/
link(index) {
quote(index, length = 1) {
if (this.doc !== null) {
return transact(this.doc, transaction => {
return arrayWeakLink(transaction, this, index)
return arrayWeakLink(transaction, this, index, length)
})
} else {
throw new Error('cannot create a link to an YArray that has not been integrated into YDoc')
throw new Error('cannot quote an YArray that has not been integrated into YDoc')
}
}

View File

@ -197,7 +197,7 @@ export const readYWeakLink = decoder => {
return new YWeakLink(start, end, null, null)
}
const lengthExceeded = error.create('Length exceeded!')
const invalidQuotedRange = error.create('Invalid quoted range length.')
/**
* Returns a {WeakLink} to an YArray element at given index.
@ -208,6 +208,9 @@ const lengthExceeded = error.create('Length exceeded!')
* @return {YWeakLink<any>}
*/
export const arrayWeakLink = (transaction, parent, index, length = 1) => {
if (length <= 0) {
throw invalidQuotedRange
}
let startItem = parent._start
for (;startItem !== null; startItem = startItem.right) {
if (!startItem.deleted && startItem.countable) {
@ -247,7 +250,7 @@ export const arrayWeakLink = (transaction, parent, index, length = 1) => {
}
}
throw lengthExceeded
throw invalidQuotedRange
}
/**

View File

@ -27,7 +27,7 @@ export const testBasicMap = tc => {
export const testBasicArray = tc => {
const { testConnector, array0, array1 } = init(tc, {users:2})
array0.insert(0, [1,2,3])
array0.insert(3, [array0.link(1)])
array0.insert(3, [array0.quote(1)])
t.compare(array0.get(0), 1)
t.compare(array0.get(1), 2)
@ -182,7 +182,7 @@ export const testObserveMapDelete = tc => {
export const testObserveArray = tc => {
const { testConnector, array0, array1 } = init(tc, { users: 2 })
array0.insert(0, ['A','B','C'])
const link0 = /** @type {Y.WeakLink<String>} */ (array0.link(1))
const link0 = /** @type {Y.WeakLink<String>} */ (array0.quote(1))
array0.insert(0, [link0])
/**
* @type {any}
@ -306,7 +306,7 @@ export const testDeepObserveMap = tc => {
const nested = new Y.Map()
array.insert(0, [nested])
const link = array.link(0)
const link = array.quote(0)
map.set('link', link)
// update entry in linked map
@ -468,9 +468,9 @@ export const testDeepObserveRecursive = tc => {
root.insert(1, [m1])
root.insert(2, [m2])
const l0 = root.link(0)
const l1 = root.link(1)
const l2 = root.link(2)
const l0 = root.quote(0)
const l1 = root.quote(1)
const l2 = root.quote(2)
// create cyclic reference between links
m0.set('k1', l1)