use new ListPosition abstraction in Y.Array .slice and .get
This commit is contained in:
parent
56ab251e79
commit
a723c32557
@ -445,8 +445,8 @@ export class ListPosition {
|
|||||||
while (item && !this.reachedEnd && (len > 0 || (len === 0 && (!item.countable || item.deleted)))) {
|
while (item && !this.reachedEnd && (len > 0 || (len === 0 && (!item.countable || item.deleted)))) {
|
||||||
if (item.countable && !item.deleted && item.moved === this.currMove) {
|
if (item.countable && !item.deleted && item.moved === this.currMove) {
|
||||||
len -= item.length
|
len -= item.length
|
||||||
if (len <= 0) {
|
if (len < 0) {
|
||||||
this.rel = -len
|
this.rel = item.length + len
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else if (item.content.constructor === ContentMove) {
|
} else if (item.content.constructor === ContentMove) {
|
||||||
@ -473,6 +473,10 @@ export class ListPosition {
|
|||||||
}
|
}
|
||||||
this.index -= len
|
this.index -= len
|
||||||
this.item = item
|
this.item = item
|
||||||
|
if (len > 0) {
|
||||||
|
throw lengthExceeded
|
||||||
|
}
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -484,7 +488,7 @@ export class ListPosition {
|
|||||||
while (this.item && this.item.countable && !this.reachedEnd && len > 0) {
|
while (this.item && this.item.countable && !this.reachedEnd && len > 0) {
|
||||||
if (!this.item.deleted) {
|
if (!this.item.deleted) {
|
||||||
const content = this.item.content.getContent()
|
const content = this.item.content.getContent()
|
||||||
const slicedContent = content.length <= len || this.rel > 0 ? content : content.slice(this.rel, len)
|
const slicedContent = content.length <= len && this.rel === 0 ? content : content.slice(this.rel, this.rel + len)
|
||||||
len -= slicedContent.length
|
len -= slicedContent.length
|
||||||
result.push(...slicedContent)
|
result.push(...slicedContent)
|
||||||
if (content.length !== slicedContent.length) {
|
if (content.length !== slicedContent.length) {
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
import {
|
import {
|
||||||
YEvent,
|
YEvent,
|
||||||
AbstractType,
|
AbstractType,
|
||||||
typeListGet,
|
|
||||||
typeListForEach,
|
typeListForEach,
|
||||||
typeListCreateIterator,
|
typeListCreateIterator,
|
||||||
typeListInsertGenerics,
|
typeListInsertGenerics,
|
||||||
@ -17,7 +16,6 @@ import {
|
|||||||
ListPosition,
|
ListPosition,
|
||||||
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item // eslint-disable-line
|
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item // eslint-disable-line
|
||||||
} from '../internals.js'
|
} from '../internals.js'
|
||||||
import { typeListSlice } from './AbstractType.js'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event that describes the changes on a YArray
|
* Event that describes the changes on a YArray
|
||||||
@ -179,7 +177,9 @@ export class YArray extends AbstractType {
|
|||||||
* @return {T}
|
* @return {T}
|
||||||
*/
|
*/
|
||||||
get (index) {
|
get (index) {
|
||||||
return typeListGet(this, index)
|
return transact(/** @type {Doc} */ (this.doc), tr =>
|
||||||
|
new ListPosition(this, tr).forward(index).slice(1)[0]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -201,7 +201,9 @@ export class YArray extends AbstractType {
|
|||||||
* @return {Array<T>}
|
* @return {Array<T>}
|
||||||
*/
|
*/
|
||||||
slice (start = 0, end = this.length) {
|
slice (start = 0, end = this.length) {
|
||||||
return typeListSlice(this, start, end)
|
return transact(/** @type {Doc} */ (this.doc), tr =>
|
||||||
|
new ListPosition(this, tr).forward(start).slice(end < 0 ? this.length + end - start : end - start)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -456,6 +456,8 @@ const getUniqueNumber = () => _uniqueNumber++
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Array<function(Doc,prng.PRNG,any):void>}
|
* @type {Array<function(Doc,prng.PRNG,any):void>}
|
||||||
|
*
|
||||||
|
* @todo to replace content to a separate data structure so we know that insert & returns work as expected!!!
|
||||||
*/
|
*/
|
||||||
const arrayTransactions = [
|
const arrayTransactions = [
|
||||||
function insert (user, gen) {
|
function insert (user, gen) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user