Create a new detached LoroMovableList (not attached to any LoroDoc).
The edits on a detached container will not be persisted. To attach the container to the document, please insert it into an attached container.
Readonly
idGet the id of this container.
Readonly
lengthGet the length of list.
Delete all elements in the list.
Get the value at the index. If the value is a container, the corresponding handler will be returned.
Get the attached container associated with this.
Returns an attached Container
that equals to this or created by this, otherwise undefined
.
Get the creator of the list item at the given position.
Get the cursor position at the given pos.
When expressing the position of a cursor, using "index" can be unstable because the cursor's position may change due to other deletions and insertions, requiring updates with each edit. To stably represent a position or range within a list structure, we can utilize the ID of each item/character on List CRDT or Text CRDT for expression.
Loro optimizes State metadata by not storing the IDs of deleted elements. This approach complicates tracking cursors since they rely on these IDs. The solution recalculates position by replaying relevant history to update cursors accurately. To minimize the performance impact of history replay, the system updates cursor info to reference only the IDs of currently present elements, thereby reducing the need for replay.
Optional
side: SideGet the last editor of the list item at the given position.
Get the last mover of the list item at the given position.
Get the shallow value of the movable list.
Unlike toJSON()
which recursively resolves all containers to their values,
getShallowValue()
returns container IDs as strings for any nested containers.
const doc = new LoroDoc();
doc.setPeerId("1");
const list = doc.getMovableList("list");
list.insert(0, 1);
list.insert(1, "two");
const subList = list.insertContainer(2, new LoroList());
subList.insert(0, "sub");
list.getShallowValue(); // [1, "two", "cid:2@1:List"]
list.toJSON(); // [1, "two", ["sub"]]
Whether the container is attached to a document.
If it's detached, the operations on the container will not be persisted.
Check if the container is deleted
"MovableList"
Move the element from from
to to
.
The new position of the element will be to
.
Move the element from from
to to
.
The new position of the element will be to
. This method is optimized to prevent redundant
operations that might occur with a naive remove and insert approach. Specifically, it avoids
creating surplus values in the list, unlike a delete followed by an insert, which can lead to
additional values in cases of concurrent edits. This ensures more efficient and accurate
operations in a MovableList.
Get the parent container.
undefined
.Pop a value from the end of the list.
Set the value at the given position.
It's different from delete
+ insert
that it will replace the value at the position.
For example, if you have a list [1, 2, 3]
, and you call set(1, 100)
, the list will be [1, 100, 3]
.
If concurrently someone call set(1, 200)
, the list will be [1, 200, 3]
or [1, 100, 3]
.
But if you use delete
+ insert
to simulate the set operation, they may create redundant operations
and the final result will be [1, 100, 200, 3]
or [1, 200, 100, 3]
.
Get elements of the list. If the value is a child container, the corresponding
Container
will be returned.
Get elements of the list. If the type of a element is a container, it will be resolved recursively.
The handler of a list container.
Learn more at https://loro.dev/docs/tutorial/list