diff --git a/src/types/YArray.js b/src/types/YArray.js index 4ea94568..e2d91614 100644 --- a/src/types/YArray.js +++ b/src/types/YArray.js @@ -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} */ - 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') } } diff --git a/src/types/YWeakLink.js b/src/types/YWeakLink.js index 7fb637ea..9df1d734 100644 --- a/src/types/YWeakLink.js +++ b/src/types/YWeakLink.js @@ -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} */ 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 } /** diff --git a/tests/y-weak-links.tests.js b/tests/y-weak-links.tests.js index 679a8886..1b5a2176 100644 --- a/tests/y-weak-links.tests.js +++ b/tests/y-weak-links.tests.js @@ -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} */ (array0.link(1)) + const link0 = /** @type {Y.WeakLink} */ (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)