loro/loro-js
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
..
.vscode feat: event & wasm 2023-07-29 02:03:51 +08:00
src refactor(ts): make method types better (#315) 2024-04-05 01:43:36 +08:00
tests Cursors (#290) 2024-04-09 16:01:37 +08:00
.gitignore Doc: loro-crdt documents in comments (#165) 2023-11-10 20:30:54 +08:00
.npmignore chore: bump loro-wasm loro-crdt version 2023-11-10 20:41:11 +08:00
CHANGELOG.md chore: bump loro-crdt 2024-04-05 02:25:25 +08:00
LICENSE chore: add license 2023-11-12 23:23:12 +08:00
package.json chore: bump loro-crdt 2024-04-05 02:25:25 +08:00
pnpm-lock.yaml Perf: speed up utf16 and wasm (#90) 2023-04-03 09:29:25 +08:00
README.md chore: bump loro-wasm loro-crdt version 2023-11-10 20:41:11 +08:00
rollup.config.mjs feat: extra pkg loro-crdt to wrap loro-wasm 2023-03-24 10:08:11 +08:00
tsconfig.json refactor: add concrete type for each different container (#313) 2024-04-03 19:24:48 +08:00
vite.config.ts Refactor: use ValueOrContainer for diff event (#202) 2023-12-05 11:57:41 +08:00

loro-crdt

Loro CRDTs is a high-performance CRDT framework.

It makes your app state synchronized, collaborative and maintainable effortlessly.

Learn more at https://loro.dev