loro/crates/loro-internal
Zixuan Chen 81ae2abacc
Cursors (#290)
This PR introduces support for retrieving and querying cursors.

## Motivation

Using "index" to denote cursor positions can be unstable, as positions may shift with document edits. To reliably represent a position or range within a document, it is more effective to leverage the unique ID of each item/character in a List CRDT or Text CRDT.

## Updating Cursors

Loro optimizes State metadata by not storing the IDs of deleted elements. This approach, while efficient, complicates tracking cursor positions since they rely on these IDs for precise locations within the document. The solution recalculates position by replaying relevant history to update stable positions 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.

Each position has a "Side" information, indicating the actual cursor position is on the left, right, or directly in the center of the target ID.

Note: In JavaScript, the offset returned when querying a Stable Position is based on the UTF-16 index.

# Example

```ts
const loro = new Loro();
const list = loro.getList("list");
list.insert(0, "a");
const pos0 = list.getStablePos(0);
list.insert(1, "b");
{
  const ans = loro.queryStablePos(pos0!);
  expect(ans.offset).toEqual(0);
  expect(ans.side).toEqual(0);
  expect(ans.update).toBeUndefined();
}
list.insert(0, "c");
{
  const ans = loro.queryStablePos(pos0!);
  expect(ans.offset).toEqual(1);
  expect(ans.side).toEqual(0);
  expect(ans.update).toBeUndefined();
}
list.delete(1, 1);
{
  const ans = loro.queryStablePos(pos0!);
  expect(ans.offset).toEqual(1);
  expect(ans.side).toEqual(-1);
  expect(ans.update).toBeDefined();
}
```
2024-04-09 16:01:37 +08:00
..
benches refactor!: Add prelim support, making creating sub container easier (#300) 2024-03-30 11:38:24 +08:00
examples refactor!: Add prelim support, making creating sub container easier (#300) 2024-03-30 11:38:24 +08:00
fuzz feat: decode import blob meta (#307) 2024-04-01 17:25:43 +08:00
proptest-regressions/dag refactor: move loro-core to loro-internal 2023-01-16 20:08:43 +08:00
scripts refactor: replace "local" and "fromCheckout" in event with "triggeredBy" (#312) 2024-04-03 17:56:01 +08:00
src Cursors (#290) 2024-04-09 16:01:37 +08:00
tests refactor: replace "local" and "fromCheckout" in event with "triggeredBy" (#312) 2024-04-03 17:56:01 +08:00
.gitignore refactor: move loro-core to loro-internal 2023-01-16 20:08:43 +08:00
Cargo.toml fix: decode iter return result by updating columnar to 0.3.4 (#309) 2024-04-01 17:29:07 +08:00
deno.json feat: import without state 2023-08-05 23:29:11 +08:00
deno.lock feat: stabilizing encoding (#219) 2024-01-02 17:03:24 +08:00
Encoding.md feat: stabilizing encoding (#219) 2024-01-02 17:03:24 +08:00
README.md Feat-rust-api (#193) 2023-11-28 16:22:43 +08:00

loro-internal

This is the internal implementation of Loro. Its interface subject to change. You should not use it as dependency directly. Consider use loro crate instead.