loro-crdt 0.14.2 → 0.14.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/deno/.vscode/settings.json +3 -0
- package/deno/deno.json +5 -0
- package/deno/mod.ts +1 -0
- package/deno/test.ts +13 -0
- package/dist/loro.d.ts +40 -2
- package/dist/loro.js +83 -0
- package/dist/loro.js.map +1 -1
- package/dist/loro.mjs +84 -1
- package/dist/loro.mjs.map +1 -1
- package/package.json +2 -2
- package/src/awareness.ts +108 -0
- package/src/index.ts +11 -0
- package/tsconfig.json +6 -1
- package/vite.config.ts +7 -5
- package/dist/src/index.js +0 -106
- package/dist/tests/basic.test.js +0 -415
- package/dist/tests/checkout.test.js +0 -76
- package/dist/tests/event.test.js +0 -345
- package/dist/tests/issue.test.js +0 -59
- package/dist/tests/misc.test.js +0 -245
- package/dist/tests/richtext.test.js +0 -197
- package/dist/tests/type.test.js +0 -22
- package/dist/tests/version.test.js +0 -185
- package/dist/vite.config.js +0 -14
package/CHANGELOG.md
CHANGED
package/deno/deno.json
ADDED
package/deno/mod.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "npm:loro-crdt@0.14.2";
|
package/deno/test.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Loro } from "./mod.ts";
|
|
2
|
+
import { expect } from 'npm:expect'
|
|
3
|
+
|
|
4
|
+
Deno.test("test", () => {
|
|
5
|
+
const doc = new Loro();
|
|
6
|
+
const text = doc.getText("text");
|
|
7
|
+
text.insert(0, "123")
|
|
8
|
+
expect(text.toString()).toEqual("123");
|
|
9
|
+
text.insert(0, "123")
|
|
10
|
+
expect(text.toString()).toEqual("123123");
|
|
11
|
+
const docB = Loro.fromSnapshot(doc.exportSnapshot());
|
|
12
|
+
expect(docB.getText('text').toString()).toEqual("123123");
|
|
13
|
+
})
|
package/dist/loro.d.ts
CHANGED
|
@@ -1,7 +1,38 @@
|
|
|
1
|
-
import { Container, ContainerID, TreeID, OpId, Delta, Value, LoroText, LoroMap, LoroTree, LoroList } from 'loro-wasm';
|
|
1
|
+
import { AwarenessWasm, PeerID as PeerID$1, Container, ContainerID, TreeID, OpId, Delta, Value, LoroText, LoroMap, LoroTree, LoroList } from 'loro-wasm';
|
|
2
2
|
export * from 'loro-wasm';
|
|
3
3
|
export { Loro } from 'loro-wasm';
|
|
4
4
|
|
|
5
|
+
type AwarenessListener = (arg: {
|
|
6
|
+
updated: PeerID$1[];
|
|
7
|
+
added: PeerID$1[];
|
|
8
|
+
removed: PeerID$1[];
|
|
9
|
+
}, origin: "local" | "timeout" | "remote" | string) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Awareness is a structure that allows to track the ephemeral state of the peers.
|
|
12
|
+
*
|
|
13
|
+
* If we don't receive a state update from a peer within the timeout, we will remove their state.
|
|
14
|
+
* The timeout is in milliseconds. This can be used to handle the off-line state of a peer.
|
|
15
|
+
*/
|
|
16
|
+
declare class Awareness<T> {
|
|
17
|
+
inner: AwarenessWasm<T>;
|
|
18
|
+
private peer;
|
|
19
|
+
private timer;
|
|
20
|
+
private timeout;
|
|
21
|
+
private listeners;
|
|
22
|
+
constructor(peer: PeerID$1, timeout?: number);
|
|
23
|
+
apply(bytes: Uint8Array, origin?: string): void;
|
|
24
|
+
setLocalState(state: T): void;
|
|
25
|
+
getLocalState(): T | undefined;
|
|
26
|
+
getAllStates(): Record<PeerID$1, T>;
|
|
27
|
+
encode(peers: PeerID$1[]): Uint8Array;
|
|
28
|
+
encodeAll(): Uint8Array;
|
|
29
|
+
addListener(listener: AwarenessListener): void;
|
|
30
|
+
removeListener(listener: AwarenessListener): void;
|
|
31
|
+
peers(): PeerID$1[];
|
|
32
|
+
destroy(): void;
|
|
33
|
+
private startTimerIfNotEmpty;
|
|
34
|
+
}
|
|
35
|
+
|
|
5
36
|
type Frontiers = OpId[];
|
|
6
37
|
/**
|
|
7
38
|
* Represents a path to identify the exact location of an event's target.
|
|
@@ -339,7 +370,14 @@ declare module "loro-wasm" {
|
|
|
339
370
|
parent(): LoroTreeNode<T> | undefined;
|
|
340
371
|
children(): Array<LoroTreeNode<T>>;
|
|
341
372
|
}
|
|
373
|
+
interface AwarenessWasm<T = unknown> {
|
|
374
|
+
getState(peer: PeerID): T | undefined;
|
|
375
|
+
getTimestamp(peer: PeerID): number | undefined;
|
|
376
|
+
getAllStates(): Record<PeerID, T>;
|
|
377
|
+
setLocalState(value: T): void;
|
|
378
|
+
removeOutdated(): PeerID[];
|
|
379
|
+
}
|
|
342
380
|
}
|
|
343
381
|
type NonNullableType<T> = Exclude<T, null | undefined>;
|
|
344
382
|
|
|
345
|
-
export { Diff, Frontiers, ListDiff, LoroEvent, LoroEventBatch, MapDiff, Path, TextDiff, TreeDiff, TreeDiffItem, getType, isContainer, isContainerId };
|
|
383
|
+
export { Awareness, Diff, Frontiers, ListDiff, LoroEvent, LoroEventBatch, MapDiff, Path, TextDiff, TreeDiff, TreeDiffItem, getType, isContainer, isContainerId };
|
package/dist/loro.js
CHANGED
|
@@ -2,6 +2,88 @@
|
|
|
2
2
|
|
|
3
3
|
var loroWasm = require('loro-wasm');
|
|
4
4
|
|
|
5
|
+
class Awareness {
|
|
6
|
+
inner;
|
|
7
|
+
peer;
|
|
8
|
+
timer;
|
|
9
|
+
timeout;
|
|
10
|
+
listeners = /* @__PURE__ */ new Set();
|
|
11
|
+
constructor(peer, timeout = 3e4) {
|
|
12
|
+
this.inner = new loroWasm.AwarenessWasm(peer, timeout);
|
|
13
|
+
this.peer = peer;
|
|
14
|
+
this.timeout = timeout;
|
|
15
|
+
}
|
|
16
|
+
apply(bytes, origin = "remote") {
|
|
17
|
+
const { updated, added } = this.inner.apply(bytes);
|
|
18
|
+
this.listeners.forEach((listener) => {
|
|
19
|
+
listener({ updated, added, removed: [] }, origin);
|
|
20
|
+
});
|
|
21
|
+
this.startTimerIfNotEmpty();
|
|
22
|
+
}
|
|
23
|
+
setLocalState(state) {
|
|
24
|
+
const wasEmpty = this.inner.getState(this.peer) == null;
|
|
25
|
+
this.inner.setLocalState(state);
|
|
26
|
+
if (wasEmpty) {
|
|
27
|
+
this.listeners.forEach((listener) => {
|
|
28
|
+
listener(
|
|
29
|
+
{ updated: [], added: [this.inner.peer()], removed: [] },
|
|
30
|
+
"local"
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
} else {
|
|
34
|
+
this.listeners.forEach((listener) => {
|
|
35
|
+
listener(
|
|
36
|
+
{ updated: [this.inner.peer()], added: [], removed: [] },
|
|
37
|
+
"local"
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
this.startTimerIfNotEmpty();
|
|
42
|
+
}
|
|
43
|
+
getLocalState() {
|
|
44
|
+
return this.inner.getState(this.peer);
|
|
45
|
+
}
|
|
46
|
+
getAllStates() {
|
|
47
|
+
return this.inner.getAllStates();
|
|
48
|
+
}
|
|
49
|
+
encode(peers) {
|
|
50
|
+
return this.inner.encode(peers);
|
|
51
|
+
}
|
|
52
|
+
encodeAll() {
|
|
53
|
+
return this.inner.encodeAll();
|
|
54
|
+
}
|
|
55
|
+
addListener(listener) {
|
|
56
|
+
this.listeners.add(listener);
|
|
57
|
+
}
|
|
58
|
+
removeListener(listener) {
|
|
59
|
+
this.listeners.delete(listener);
|
|
60
|
+
}
|
|
61
|
+
peers() {
|
|
62
|
+
return this.inner.peers();
|
|
63
|
+
}
|
|
64
|
+
destroy() {
|
|
65
|
+
clearInterval(this.timer);
|
|
66
|
+
this.listeners.clear();
|
|
67
|
+
}
|
|
68
|
+
startTimerIfNotEmpty() {
|
|
69
|
+
if (this.inner.isEmpty() || this.timer != null) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.timer = setInterval(() => {
|
|
73
|
+
const removed = this.inner.removeOutdated();
|
|
74
|
+
if (removed.length > 0) {
|
|
75
|
+
this.listeners.forEach((listener) => {
|
|
76
|
+
listener({ updated: [], added: [], removed }, "timeout");
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (this.inner.isEmpty()) {
|
|
80
|
+
clearInterval(this.timer);
|
|
81
|
+
this.timer = void 0;
|
|
82
|
+
}
|
|
83
|
+
}, this.timeout / 2);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
5
87
|
const CONTAINER_TYPES = ["Map", "Text", "List", "Tree"];
|
|
6
88
|
function isContainerId(s) {
|
|
7
89
|
return s.startsWith("cid:");
|
|
@@ -27,6 +109,7 @@ Object.defineProperty(exports, 'Loro', {
|
|
|
27
109
|
enumerable: true,
|
|
28
110
|
get: function () { return loroWasm.Loro; }
|
|
29
111
|
});
|
|
112
|
+
exports.Awareness = Awareness;
|
|
30
113
|
exports.getType = getType;
|
|
31
114
|
exports.isContainer = isContainer;
|
|
32
115
|
exports.isContainerId = isContainerId;
|
package/dist/loro.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loro.js","sources":["../src/index.ts"],"sourcesContent":["export * from \"loro-wasm\";\nimport {\n Container,\n ContainerID,\n Delta,\n Loro,\n LoroList,\n LoroMap,\n LoroText,\n LoroTree,\n OpId,\n TreeID,\n Value,\n} from \"loro-wasm\";\n\nexport type Frontiers = OpId[];\n\n/**\n * Represents a path to identify the exact location of an event's target.\n * The path is composed of numbers (e.g., indices of a list container) strings\n * (e.g., keys of a map container) and TreeID (the node of a tree container),\n * indicating the absolute position of the event's source within a loro document.\n */\nexport type Path = (number | string | TreeID)[];\n\n/**\n * A batch of events that created by a single `import`/`transaction`/`checkout`.\n *\n * @prop by - How the event is triggered.\n * @prop origin - (Optional) Provides information about the origin of the event.\n * @prop diff - Contains the differential information related to the event.\n * @prop target - Identifies the container ID of the event's target.\n * @prop path - Specifies the absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\nexport interface LoroEventBatch {\n /**\n * How the event is triggered.\n *\n * - `local`: The event is triggered by a local transaction.\n * - `import`: The event is triggered by an import operation.\n * - `checkout`: The event is triggered by a checkout operation.\n */\n by: \"local\" | \"import\" | \"checkout\";\n origin?: string;\n /**\n * The container ID of the current event receiver.\n * It's undefined if the subscriber is on the root document.\n */\n currentTarget?: ContainerID;\n events: LoroEvent[];\n}\n\n/**\n * The concrete event of Loro.\n */\nexport interface LoroEvent {\n /**\n * The container ID of the event's target.\n */\n target: ContainerID;\n diff: Diff;\n /**\n * The absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\n path: Path;\n}\n\nexport type ListDiff = {\n type: \"list\";\n diff: Delta<(Value | Container)[]>[];\n};\n\nexport type TextDiff = {\n type: \"text\";\n diff: Delta<string>[];\n};\n\nexport type MapDiff = {\n type: \"map\";\n updated: Record<string, Value | Container | undefined>;\n};\n\nexport type TreeDiffItem =\n | { target: TreeID; action: \"create\"; parent: TreeID | undefined }\n | { target: TreeID; action: \"delete\" }\n | { target: TreeID; action: \"move\"; parent: TreeID | undefined };\n\nexport type TreeDiff = {\n type: \"tree\";\n diff: TreeDiffItem[];\n};\n\nexport type Diff = ListDiff | TextDiff | MapDiff | TreeDiff;\n\ninterface Listener {\n (event: LoroEventBatch): void;\n}\n\nconst CONTAINER_TYPES = [\"Map\", \"Text\", \"List\", \"Tree\"];\n\nexport function isContainerId(s: string): s is ContainerID {\n return s.startsWith(\"cid:\");\n}\n\nexport { Loro };\n\n/** Whether the value is a container.\n *\n * # Example\n *\n * ```ts\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * const list = doc.getList(\"list\");\n * const text = doc.getText(\"text\");\n * isContainer(map); // true\n * isContainer(list); // true\n * isContainer(text); // true\n * isContainer(123); // false\n * isContainer(\"123\"); // false\n * isContainer({}); // false\n */\nexport function isContainer(value: any): value is Container {\n if (typeof value !== \"object\" || value == null) {\n return false;\n }\n\n const p = Object.getPrototypeOf(value);\n if (p == null || typeof p !== \"object\" || typeof p[\"kind\"] !== \"function\") {\n return false;\n }\n\n return CONTAINER_TYPES.includes(value.kind());\n}\n\n/** Get the type of a value that may be a container.\n *\n * # Example\n *\n * ```ts\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * const list = doc.getList(\"list\");\n * const text = doc.getText(\"text\");\n * getType(map); // \"Map\"\n * getType(list); // \"List\"\n * getType(text); // \"Text\"\n * getType(123); // \"Json\"\n * getType(\"123\"); // \"Json\"\n * getType({}); // \"Json\"\n * ```\n */\nexport function getType<T>(\n value: T,\n): T extends LoroText ? \"Text\"\n : T extends LoroMap<any> ? \"Map\"\n : T extends LoroTree<any> ? \"Tree\"\n : T extends LoroList<any> ? \"List\"\n : \"Json\" {\n if (isContainer(value)) {\n return value.kind() as unknown as any;\n }\n\n return \"Json\" as any;\n}\n\ndeclare module \"loro-wasm\" {\n interface Loro {\n subscribe(listener: Listener): number;\n }\n\n interface Loro<\n T extends Record<string, Container> = Record<string, Container>,\n > {\n /**\n * Get a LoroMap by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * ```\n */\n getMap<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : LoroMap;\n /**\n * Get a LoroList by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * ```\n */\n getList<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : LoroList;\n /**\n * Get a LoroTree by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const tree = doc.getTree(\"tree\");\n * ```\n */\n getTree<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroTree ? T[Key] : LoroTree;\n getText(key: string | ContainerID): LoroText;\n }\n\n interface LoroList<T = unknown> {\n new (): LoroList<T>;\n /**\n * Get elements of the list. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * list.insert(1, \"foo\");\n * list.insert(2, true);\n * list.insertContainer(3, new LoroText());\n * console.log(list.value); // [100, \"foo\", true, LoroText];\n * ```\n */\n toArray(): T[];\n /**\n * Insert a container at the index.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * const text = list.insertContainer(1, new LoroText());\n * text.insert(0, \"Hello\");\n * console.log(list.getDeepValue()); // [100, \"Hello\"];\n * ```\n */\n insertContainer<C extends Container>(\n pos: number,\n child: C,\n ): T extends C ? T : C;\n /**\n * Get the value at the index. If the value is a container, the corresponding handler will be returned.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * console.log(list.get(0)); // 100\n * console.log(list.get(1)); // undefined\n * ```\n */\n get(index: number): T;\n /**\n * Insert a value at index.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * list.insert(1, \"foo\");\n * list.insert(2, true);\n * console.log(list.value); // [100, \"foo\", true];\n * ```\n */\n insert(pos: number, value: Exclude<T, Container>): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n getAttached(): undefined | LoroList<T>;\n }\n\n interface LoroMap<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n new (): LoroMap<T>;\n /**\n * Get the value of the key. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * The object returned is a new js object each time because it need to cross\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const bar = map.get(\"foo\");\n * ```\n */\n getOrCreateContainer<C extends Container>(key: string, child: C): C;\n /**\n * Set the key with a container.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const text = map.setContainer(\"text\", new LoroText());\n * const list = map.setContainer(\"list\", new LoroText());\n * ```\n */\n setContainer<C extends Container, Key extends keyof T>(\n key: Key,\n child: C,\n ): NonNullableType<T[Key]> extends C ? NonNullableType<T[Key]> : C;\n /**\n * Get the value of the key. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * The object/value returned is a new js object/value each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const bar = map.get(\"foo\");\n * ```\n */\n get<Key extends keyof T>(key: Key): T[Key];\n /**\n * Set the key with the value.\n *\n * If the value of the key is exist, the old value will be updated.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * map.set(\"foo\", \"baz\");\n * ```\n */\n set<Key extends keyof T>(key: Key, value: Exclude<T[Key], Container>): void;\n delete(key: string): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroText {\n new (): LoroText;\n insert(pos: number, text: string): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroTree<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n new (): LoroTree<T>;\n createNode(parent: TreeID | undefined): LoroTreeNode<T>;\n move(target: TreeID, parent: TreeID | undefined): void;\n delete(target: TreeID): void;\n has(target: TreeID): boolean;\n getNodeByID(target: TreeID): LoroTreeNode;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroTreeNode<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n /**\n * Get the associated metadata map container of a tree node.\n */\n readonly data: LoroMap<T>;\n createNode(): LoroTreeNode<T>;\n setAsRoot(): void;\n moveTo(parent: LoroTreeNode<T>): void;\n parent(): LoroTreeNode<T> | undefined;\n children(): Array<LoroTreeNode<T>>;\n }\n}\n\ntype NonNullableType<T> = Exclude<T, null | undefined>;\n"],"names":[],"mappings":";;;;AAkGA,MAAM,eAAkB,GAAA,CAAC,KAAO,EAAA,MAAA,EAAQ,QAAQ,MAAM,CAAA,CAAA;AAE/C,SAAS,cAAc,CAA6B,EAAA;AACzD,EAAO,OAAA,CAAA,CAAE,WAAW,MAAM,CAAA,CAAA;AAC5B,CAAA;AAoBO,SAAS,YAAY,KAAgC,EAAA;AAC1D,EAAA,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,KAAA,IAAS,IAAM,EAAA;AAC9C,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAM,MAAA,CAAA,GAAI,MAAO,CAAA,cAAA,CAAe,KAAK,CAAA,CAAA;AACrC,EAAI,IAAA,CAAA,IAAK,QAAQ,OAAO,CAAA,KAAM,YAAY,OAAO,CAAA,CAAE,MAAM,CAAA,KAAM,UAAY,EAAA;AACzE,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAA,OAAO,eAAgB,CAAA,QAAA,CAAS,KAAM,CAAA,IAAA,EAAM,CAAA,CAAA;AAC9C,CAAA;AAmBO,SAAS,QACd,KAKS,EAAA;AACT,EAAI,IAAA,WAAA,CAAY,KAAK,CAAG,EAAA;AACtB,IAAA,OAAO,MAAM,IAAK,EAAA,CAAA;AAAA,GACpB;AAEA,EAAO,OAAA,MAAA,CAAA;AACT;;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"loro.js","sources":["../src/awareness.ts","../src/index.ts"],"sourcesContent":["import { AwarenessWasm, PeerID } from \"loro-wasm\";\n\nexport type AwarenessListener = (\n arg: { updated: PeerID[]; added: PeerID[]; removed: PeerID[] },\n origin: \"local\" | \"timeout\" | \"remote\" | string,\n) => void;\n\n/**\n * Awareness is a structure that allows to track the ephemeral state of the peers.\n *\n * If we don't receive a state update from a peer within the timeout, we will remove their state.\n * The timeout is in milliseconds. This can be used to handle the off-line state of a peer.\n */\nexport class Awareness<T> {\n inner: AwarenessWasm<T>;\n private peer: PeerID;\n private timer: number | undefined;\n private timeout: number;\n private listeners: Set<AwarenessListener> = new Set();\n constructor(peer: PeerID, timeout: number = 30000) {\n this.inner = new AwarenessWasm(peer, timeout);\n this.peer = peer;\n this.timeout = timeout;\n }\n\n apply(bytes: Uint8Array, origin = \"remote\") {\n const { updated, added } = this.inner.apply(bytes);\n this.listeners.forEach((listener) => {\n listener({ updated, added, removed: [] }, origin);\n });\n\n this.startTimerIfNotEmpty();\n }\n\n setLocalState(state: T) {\n const wasEmpty = this.inner.getState(this.peer) == null;\n this.inner.setLocalState(state);\n if (wasEmpty) {\n this.listeners.forEach((listener) => {\n listener(\n { updated: [], added: [this.inner.peer()], removed: [] },\n \"local\",\n );\n });\n } else {\n this.listeners.forEach((listener) => {\n listener(\n { updated: [this.inner.peer()], added: [], removed: [] },\n \"local\",\n );\n });\n }\n\n this.startTimerIfNotEmpty();\n }\n\n getLocalState(): T | undefined {\n return this.inner.getState(this.peer);\n }\n\n getAllStates(): Record<PeerID, T> {\n return this.inner.getAllStates();\n }\n\n encode(peers: PeerID[]): Uint8Array {\n return this.inner.encode(peers);\n }\n\n encodeAll(): Uint8Array {\n return this.inner.encodeAll();\n }\n\n addListener(listener: AwarenessListener) {\n this.listeners.add(listener);\n }\n\n removeListener(listener: AwarenessListener) {\n this.listeners.delete(listener);\n }\n\n peers(): PeerID[] {\n return this.inner.peers();\n }\n\n destroy() {\n clearInterval(this.timer);\n this.listeners.clear();\n }\n\n private startTimerIfNotEmpty() {\n if (this.inner.isEmpty() || this.timer != null) {\n return;\n }\n\n this.timer = setInterval(() => {\n const removed = this.inner.removeOutdated();\n if (removed.length > 0) {\n this.listeners.forEach((listener) => {\n listener({ updated: [], added: [], removed }, \"timeout\");\n });\n }\n if (this.inner.isEmpty()) {\n clearInterval(this.timer);\n this.timer = undefined;\n }\n }, this.timeout / 2) as unknown as number;\n }\n}\n","export * from \"loro-wasm\";\nimport {\n Container,\n ContainerID,\n Delta,\n Loro,\n LoroList,\n LoroMap,\n LoroText,\n LoroTree,\n OpId,\n TreeID,\n Value,\n} from \"loro-wasm\";\nexport { Awareness } from \"./awareness\";\n\nexport type Frontiers = OpId[];\n\n/**\n * Represents a path to identify the exact location of an event's target.\n * The path is composed of numbers (e.g., indices of a list container) strings\n * (e.g., keys of a map container) and TreeID (the node of a tree container),\n * indicating the absolute position of the event's source within a loro document.\n */\nexport type Path = (number | string | TreeID)[];\n\n/**\n * A batch of events that created by a single `import`/`transaction`/`checkout`.\n *\n * @prop by - How the event is triggered.\n * @prop origin - (Optional) Provides information about the origin of the event.\n * @prop diff - Contains the differential information related to the event.\n * @prop target - Identifies the container ID of the event's target.\n * @prop path - Specifies the absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\nexport interface LoroEventBatch {\n /**\n * How the event is triggered.\n *\n * - `local`: The event is triggered by a local transaction.\n * - `import`: The event is triggered by an import operation.\n * - `checkout`: The event is triggered by a checkout operation.\n */\n by: \"local\" | \"import\" | \"checkout\";\n origin?: string;\n /**\n * The container ID of the current event receiver.\n * It's undefined if the subscriber is on the root document.\n */\n currentTarget?: ContainerID;\n events: LoroEvent[];\n}\n\n/**\n * The concrete event of Loro.\n */\nexport interface LoroEvent {\n /**\n * The container ID of the event's target.\n */\n target: ContainerID;\n diff: Diff;\n /**\n * The absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\n path: Path;\n}\n\nexport type ListDiff = {\n type: \"list\";\n diff: Delta<(Value | Container)[]>[];\n};\n\nexport type TextDiff = {\n type: \"text\";\n diff: Delta<string>[];\n};\n\nexport type MapDiff = {\n type: \"map\";\n updated: Record<string, Value | Container | undefined>;\n};\n\nexport type TreeDiffItem =\n | { target: TreeID; action: \"create\"; parent: TreeID | undefined }\n | { target: TreeID; action: \"delete\" }\n | { target: TreeID; action: \"move\"; parent: TreeID | undefined };\n\nexport type TreeDiff = {\n type: \"tree\";\n diff: TreeDiffItem[];\n};\n\nexport type Diff = ListDiff | TextDiff | MapDiff | TreeDiff;\n\ninterface Listener {\n (event: LoroEventBatch): void;\n}\n\nconst CONTAINER_TYPES = [\"Map\", \"Text\", \"List\", \"Tree\"];\n\nexport function isContainerId(s: string): s is ContainerID {\n return s.startsWith(\"cid:\");\n}\n\nexport { Loro };\n\n/** Whether the value is a container.\n *\n * # Example\n *\n * ```ts\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * const list = doc.getList(\"list\");\n * const text = doc.getText(\"text\");\n * isContainer(map); // true\n * isContainer(list); // true\n * isContainer(text); // true\n * isContainer(123); // false\n * isContainer(\"123\"); // false\n * isContainer({}); // false\n */\nexport function isContainer(value: any): value is Container {\n if (typeof value !== \"object\" || value == null) {\n return false;\n }\n\n const p = Object.getPrototypeOf(value);\n if (p == null || typeof p !== \"object\" || typeof p[\"kind\"] !== \"function\") {\n return false;\n }\n\n return CONTAINER_TYPES.includes(value.kind());\n}\n\n/** Get the type of a value that may be a container.\n *\n * # Example\n *\n * ```ts\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * const list = doc.getList(\"list\");\n * const text = doc.getText(\"text\");\n * getType(map); // \"Map\"\n * getType(list); // \"List\"\n * getType(text); // \"Text\"\n * getType(123); // \"Json\"\n * getType(\"123\"); // \"Json\"\n * getType({}); // \"Json\"\n * ```\n */\nexport function getType<T>(\n value: T,\n): T extends LoroText ? \"Text\"\n : T extends LoroMap<any> ? \"Map\"\n : T extends LoroTree<any> ? \"Tree\"\n : T extends LoroList<any> ? \"List\"\n : \"Json\" {\n if (isContainer(value)) {\n return value.kind() as unknown as any;\n }\n\n return \"Json\" as any;\n}\n\ndeclare module \"loro-wasm\" {\n interface Loro {\n subscribe(listener: Listener): number;\n }\n\n interface Loro<\n T extends Record<string, Container> = Record<string, Container>,\n > {\n /**\n * Get a LoroMap by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * ```\n */\n getMap<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : LoroMap;\n /**\n * Get a LoroList by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * ```\n */\n getList<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : LoroList;\n /**\n * Get a LoroTree by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const tree = doc.getTree(\"tree\");\n * ```\n */\n getTree<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroTree ? T[Key] : LoroTree;\n getText(key: string | ContainerID): LoroText;\n }\n\n interface LoroList<T = unknown> {\n new (): LoroList<T>;\n /**\n * Get elements of the list. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * list.insert(1, \"foo\");\n * list.insert(2, true);\n * list.insertContainer(3, new LoroText());\n * console.log(list.value); // [100, \"foo\", true, LoroText];\n * ```\n */\n toArray(): T[];\n /**\n * Insert a container at the index.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * const text = list.insertContainer(1, new LoroText());\n * text.insert(0, \"Hello\");\n * console.log(list.getDeepValue()); // [100, \"Hello\"];\n * ```\n */\n insertContainer<C extends Container>(\n pos: number,\n child: C,\n ): T extends C ? T : C;\n /**\n * Get the value at the index. If the value is a container, the corresponding handler will be returned.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * console.log(list.get(0)); // 100\n * console.log(list.get(1)); // undefined\n * ```\n */\n get(index: number): T;\n /**\n * Insert a value at index.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * list.insert(1, \"foo\");\n * list.insert(2, true);\n * console.log(list.value); // [100, \"foo\", true];\n * ```\n */\n insert(pos: number, value: Exclude<T, Container>): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n getAttached(): undefined | LoroList<T>;\n }\n\n interface LoroMap<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n new (): LoroMap<T>;\n /**\n * Get the value of the key. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * The object returned is a new js object each time because it need to cross\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const bar = map.get(\"foo\");\n * ```\n */\n getOrCreateContainer<C extends Container>(key: string, child: C): C;\n /**\n * Set the key with a container.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const text = map.setContainer(\"text\", new LoroText());\n * const list = map.setContainer(\"list\", new LoroText());\n * ```\n */\n setContainer<C extends Container, Key extends keyof T>(\n key: Key,\n child: C,\n ): NonNullableType<T[Key]> extends C ? NonNullableType<T[Key]> : C;\n /**\n * Get the value of the key. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * The object/value returned is a new js object/value each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const bar = map.get(\"foo\");\n * ```\n */\n get<Key extends keyof T>(key: Key): T[Key];\n /**\n * Set the key with the value.\n *\n * If the value of the key is exist, the old value will be updated.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * map.set(\"foo\", \"baz\");\n * ```\n */\n set<Key extends keyof T>(key: Key, value: Exclude<T[Key], Container>): void;\n delete(key: string): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroText {\n new (): LoroText;\n insert(pos: number, text: string): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroTree<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n new (): LoroTree<T>;\n createNode(parent: TreeID | undefined): LoroTreeNode<T>;\n move(target: TreeID, parent: TreeID | undefined): void;\n delete(target: TreeID): void;\n has(target: TreeID): boolean;\n getNodeByID(target: TreeID): LoroTreeNode;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroTreeNode<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n /**\n * Get the associated metadata map container of a tree node.\n */\n readonly data: LoroMap<T>;\n createNode(): LoroTreeNode<T>;\n setAsRoot(): void;\n moveTo(parent: LoroTreeNode<T>): void;\n parent(): LoroTreeNode<T> | undefined;\n children(): Array<LoroTreeNode<T>>;\n }\n\n interface AwarenessWasm<\n T = unknown,\n > {\n getState(peer: PeerID): T | undefined;\n getTimestamp(peer: PeerID): number | undefined;\n getAllStates(): Record<PeerID, T>;\n setLocalState(value: T): void;\n removeOutdated(): PeerID[];\n }\n}\n\ntype NonNullableType<T> = Exclude<T, null | undefined>;\n"],"names":["AwarenessWasm"],"mappings":";;;;AAaO,M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A;AACrC,EAAI,IAAA,CAAA,IAAK,QAAQ,OAAO,CAAA,KAAM,YAAY,OAAO,CAAA,CAAE,MAAM,CAAA,KAAM,UAAY,EAAA;AACzE,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAA,OAAO,eAAgB,CAAA,QAAA,CAAS,KAAM,CAAA,IAAA,EAAM,CAAA,CAAA;AAC9C,CAAA;AAmBO,SAAS,QACd,KAKS,EAAA;AACT,EAAI,IAAA,WAAA,CAAY,KAAK,CAAG,EAAA;AACtB,IAAA,OAAO,MAAM,IAAK,EAAA,CAAA;AAAA,GACpB;AAEA,EAAO,OAAA,MAAA,CAAA;AACT;;;;;;;;;;;;;;;;;"}
|
package/dist/loro.mjs
CHANGED
|
@@ -1,6 +1,89 @@
|
|
|
1
|
+
import { AwarenessWasm } from 'loro-wasm';
|
|
1
2
|
export * from 'loro-wasm';
|
|
2
3
|
export { Loro } from 'loro-wasm';
|
|
3
4
|
|
|
5
|
+
class Awareness {
|
|
6
|
+
inner;
|
|
7
|
+
peer;
|
|
8
|
+
timer;
|
|
9
|
+
timeout;
|
|
10
|
+
listeners = /* @__PURE__ */ new Set();
|
|
11
|
+
constructor(peer, timeout = 3e4) {
|
|
12
|
+
this.inner = new AwarenessWasm(peer, timeout);
|
|
13
|
+
this.peer = peer;
|
|
14
|
+
this.timeout = timeout;
|
|
15
|
+
}
|
|
16
|
+
apply(bytes, origin = "remote") {
|
|
17
|
+
const { updated, added } = this.inner.apply(bytes);
|
|
18
|
+
this.listeners.forEach((listener) => {
|
|
19
|
+
listener({ updated, added, removed: [] }, origin);
|
|
20
|
+
});
|
|
21
|
+
this.startTimerIfNotEmpty();
|
|
22
|
+
}
|
|
23
|
+
setLocalState(state) {
|
|
24
|
+
const wasEmpty = this.inner.getState(this.peer) == null;
|
|
25
|
+
this.inner.setLocalState(state);
|
|
26
|
+
if (wasEmpty) {
|
|
27
|
+
this.listeners.forEach((listener) => {
|
|
28
|
+
listener(
|
|
29
|
+
{ updated: [], added: [this.inner.peer()], removed: [] },
|
|
30
|
+
"local"
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
} else {
|
|
34
|
+
this.listeners.forEach((listener) => {
|
|
35
|
+
listener(
|
|
36
|
+
{ updated: [this.inner.peer()], added: [], removed: [] },
|
|
37
|
+
"local"
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
this.startTimerIfNotEmpty();
|
|
42
|
+
}
|
|
43
|
+
getLocalState() {
|
|
44
|
+
return this.inner.getState(this.peer);
|
|
45
|
+
}
|
|
46
|
+
getAllStates() {
|
|
47
|
+
return this.inner.getAllStates();
|
|
48
|
+
}
|
|
49
|
+
encode(peers) {
|
|
50
|
+
return this.inner.encode(peers);
|
|
51
|
+
}
|
|
52
|
+
encodeAll() {
|
|
53
|
+
return this.inner.encodeAll();
|
|
54
|
+
}
|
|
55
|
+
addListener(listener) {
|
|
56
|
+
this.listeners.add(listener);
|
|
57
|
+
}
|
|
58
|
+
removeListener(listener) {
|
|
59
|
+
this.listeners.delete(listener);
|
|
60
|
+
}
|
|
61
|
+
peers() {
|
|
62
|
+
return this.inner.peers();
|
|
63
|
+
}
|
|
64
|
+
destroy() {
|
|
65
|
+
clearInterval(this.timer);
|
|
66
|
+
this.listeners.clear();
|
|
67
|
+
}
|
|
68
|
+
startTimerIfNotEmpty() {
|
|
69
|
+
if (this.inner.isEmpty() || this.timer != null) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.timer = setInterval(() => {
|
|
73
|
+
const removed = this.inner.removeOutdated();
|
|
74
|
+
if (removed.length > 0) {
|
|
75
|
+
this.listeners.forEach((listener) => {
|
|
76
|
+
listener({ updated: [], added: [], removed }, "timeout");
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (this.inner.isEmpty()) {
|
|
80
|
+
clearInterval(this.timer);
|
|
81
|
+
this.timer = void 0;
|
|
82
|
+
}
|
|
83
|
+
}, this.timeout / 2);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
4
87
|
const CONTAINER_TYPES = ["Map", "Text", "List", "Tree"];
|
|
5
88
|
function isContainerId(s) {
|
|
6
89
|
return s.startsWith("cid:");
|
|
@@ -22,5 +105,5 @@ function getType(value) {
|
|
|
22
105
|
return "Json";
|
|
23
106
|
}
|
|
24
107
|
|
|
25
|
-
export { getType, isContainer, isContainerId };
|
|
108
|
+
export { Awareness, getType, isContainer, isContainerId };
|
|
26
109
|
//# sourceMappingURL=loro.mjs.map
|
package/dist/loro.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loro.mjs","sources":["../src/index.ts"],"sourcesContent":["export * from \"loro-wasm\";\nimport {\n Container,\n ContainerID,\n Delta,\n Loro,\n LoroList,\n LoroMap,\n LoroText,\n LoroTree,\n OpId,\n TreeID,\n Value,\n} from \"loro-wasm\";\n\nexport type Frontiers = OpId[];\n\n/**\n * Represents a path to identify the exact location of an event's target.\n * The path is composed of numbers (e.g., indices of a list container) strings\n * (e.g., keys of a map container) and TreeID (the node of a tree container),\n * indicating the absolute position of the event's source within a loro document.\n */\nexport type Path = (number | string | TreeID)[];\n\n/**\n * A batch of events that created by a single `import`/`transaction`/`checkout`.\n *\n * @prop by - How the event is triggered.\n * @prop origin - (Optional) Provides information about the origin of the event.\n * @prop diff - Contains the differential information related to the event.\n * @prop target - Identifies the container ID of the event's target.\n * @prop path - Specifies the absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\nexport interface LoroEventBatch {\n /**\n * How the event is triggered.\n *\n * - `local`: The event is triggered by a local transaction.\n * - `import`: The event is triggered by an import operation.\n * - `checkout`: The event is triggered by a checkout operation.\n */\n by: \"local\" | \"import\" | \"checkout\";\n origin?: string;\n /**\n * The container ID of the current event receiver.\n * It's undefined if the subscriber is on the root document.\n */\n currentTarget?: ContainerID;\n events: LoroEvent[];\n}\n\n/**\n * The concrete event of Loro.\n */\nexport interface LoroEvent {\n /**\n * The container ID of the event's target.\n */\n target: ContainerID;\n diff: Diff;\n /**\n * The absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\n path: Path;\n}\n\nexport type ListDiff = {\n type: \"list\";\n diff: Delta<(Value | Container)[]>[];\n};\n\nexport type TextDiff = {\n type: \"text\";\n diff: Delta<string>[];\n};\n\nexport type MapDiff = {\n type: \"map\";\n updated: Record<string, Value | Container | undefined>;\n};\n\nexport type TreeDiffItem =\n | { target: TreeID; action: \"create\"; parent: TreeID | undefined }\n | { target: TreeID; action: \"delete\" }\n | { target: TreeID; action: \"move\"; parent: TreeID | undefined };\n\nexport type TreeDiff = {\n type: \"tree\";\n diff: TreeDiffItem[];\n};\n\nexport type Diff = ListDiff | TextDiff | MapDiff | TreeDiff;\n\ninterface Listener {\n (event: LoroEventBatch): void;\n}\n\nconst CONTAINER_TYPES = [\"Map\", \"Text\", \"List\", \"Tree\"];\n\nexport function isContainerId(s: string): s is ContainerID {\n return s.startsWith(\"cid:\");\n}\n\nexport { Loro };\n\n/** Whether the value is a container.\n *\n * # Example\n *\n * ```ts\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * const list = doc.getList(\"list\");\n * const text = doc.getText(\"text\");\n * isContainer(map); // true\n * isContainer(list); // true\n * isContainer(text); // true\n * isContainer(123); // false\n * isContainer(\"123\"); // false\n * isContainer({}); // false\n */\nexport function isContainer(value: any): value is Container {\n if (typeof value !== \"object\" || value == null) {\n return false;\n }\n\n const p = Object.getPrototypeOf(value);\n if (p == null || typeof p !== \"object\" || typeof p[\"kind\"] !== \"function\") {\n return false;\n }\n\n return CONTAINER_TYPES.includes(value.kind());\n}\n\n/** Get the type of a value that may be a container.\n *\n * # Example\n *\n * ```ts\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * const list = doc.getList(\"list\");\n * const text = doc.getText(\"text\");\n * getType(map); // \"Map\"\n * getType(list); // \"List\"\n * getType(text); // \"Text\"\n * getType(123); // \"Json\"\n * getType(\"123\"); // \"Json\"\n * getType({}); // \"Json\"\n * ```\n */\nexport function getType<T>(\n value: T,\n): T extends LoroText ? \"Text\"\n : T extends LoroMap<any> ? \"Map\"\n : T extends LoroTree<any> ? \"Tree\"\n : T extends LoroList<any> ? \"List\"\n : \"Json\" {\n if (isContainer(value)) {\n return value.kind() as unknown as any;\n }\n\n return \"Json\" as any;\n}\n\ndeclare module \"loro-wasm\" {\n interface Loro {\n subscribe(listener: Listener): number;\n }\n\n interface Loro<\n T extends Record<string, Container> = Record<string, Container>,\n > {\n /**\n * Get a LoroMap by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * ```\n */\n getMap<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : LoroMap;\n /**\n * Get a LoroList by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * ```\n */\n getList<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : LoroList;\n /**\n * Get a LoroTree by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const tree = doc.getTree(\"tree\");\n * ```\n */\n getTree<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroTree ? T[Key] : LoroTree;\n getText(key: string | ContainerID): LoroText;\n }\n\n interface LoroList<T = unknown> {\n new (): LoroList<T>;\n /**\n * Get elements of the list. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * list.insert(1, \"foo\");\n * list.insert(2, true);\n * list.insertContainer(3, new LoroText());\n * console.log(list.value); // [100, \"foo\", true, LoroText];\n * ```\n */\n toArray(): T[];\n /**\n * Insert a container at the index.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * const text = list.insertContainer(1, new LoroText());\n * text.insert(0, \"Hello\");\n * console.log(list.getDeepValue()); // [100, \"Hello\"];\n * ```\n */\n insertContainer<C extends Container>(\n pos: number,\n child: C,\n ): T extends C ? T : C;\n /**\n * Get the value at the index. If the value is a container, the corresponding handler will be returned.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * console.log(list.get(0)); // 100\n * console.log(list.get(1)); // undefined\n * ```\n */\n get(index: number): T;\n /**\n * Insert a value at index.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * list.insert(1, \"foo\");\n * list.insert(2, true);\n * console.log(list.value); // [100, \"foo\", true];\n * ```\n */\n insert(pos: number, value: Exclude<T, Container>): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n getAttached(): undefined | LoroList<T>;\n }\n\n interface LoroMap<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n new (): LoroMap<T>;\n /**\n * Get the value of the key. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * The object returned is a new js object each time because it need to cross\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const bar = map.get(\"foo\");\n * ```\n */\n getOrCreateContainer<C extends Container>(key: string, child: C): C;\n /**\n * Set the key with a container.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const text = map.setContainer(\"text\", new LoroText());\n * const list = map.setContainer(\"list\", new LoroText());\n * ```\n */\n setContainer<C extends Container, Key extends keyof T>(\n key: Key,\n child: C,\n ): NonNullableType<T[Key]> extends C ? NonNullableType<T[Key]> : C;\n /**\n * Get the value of the key. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * The object/value returned is a new js object/value each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const bar = map.get(\"foo\");\n * ```\n */\n get<Key extends keyof T>(key: Key): T[Key];\n /**\n * Set the key with the value.\n *\n * If the value of the key is exist, the old value will be updated.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * map.set(\"foo\", \"baz\");\n * ```\n */\n set<Key extends keyof T>(key: Key, value: Exclude<T[Key], Container>): void;\n delete(key: string): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroText {\n new (): LoroText;\n insert(pos: number, text: string): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroTree<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n new (): LoroTree<T>;\n createNode(parent: TreeID | undefined): LoroTreeNode<T>;\n move(target: TreeID, parent: TreeID | undefined): void;\n delete(target: TreeID): void;\n has(target: TreeID): boolean;\n getNodeByID(target: TreeID): LoroTreeNode;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroTreeNode<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n /**\n * Get the associated metadata map container of a tree node.\n */\n readonly data: LoroMap<T>;\n createNode(): LoroTreeNode<T>;\n setAsRoot(): void;\n moveTo(parent: LoroTreeNode<T>): void;\n parent(): LoroTreeNode<T> | undefined;\n children(): Array<LoroTreeNode<T>>;\n }\n}\n\ntype NonNullableType<T> = Exclude<T, null | undefined>;\n"],"names":[],"mappings":";;;AAkGA,MAAM,eAAkB,GAAA,CAAC,KAAO,EAAA,MAAA,EAAQ,QAAQ,MAAM,CAAA,CAAA;AAE/C,SAAS,cAAc,CAA6B,EAAA;AACzD,EAAO,OAAA,CAAA,CAAE,WAAW,MAAM,CAAA,CAAA;AAC5B,CAAA;AAoBO,SAAS,YAAY,KAAgC,EAAA;AAC1D,EAAA,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,KAAA,IAAS,IAAM,EAAA;AAC9C,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAM,MAAA,CAAA,GAAI,MAAO,CAAA,cAAA,CAAe,KAAK,CAAA,CAAA;AACrC,EAAI,IAAA,CAAA,IAAK,QAAQ,OAAO,CAAA,KAAM,YAAY,OAAO,CAAA,CAAE,MAAM,CAAA,KAAM,UAAY,EAAA;AACzE,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAA,OAAO,eAAgB,CAAA,QAAA,CAAS,KAAM,CAAA,IAAA,EAAM,CAAA,CAAA;AAC9C,CAAA;AAmBO,SAAS,QACd,KAKS,EAAA;AACT,EAAI,IAAA,WAAA,CAAY,KAAK,CAAG,EAAA;AACtB,IAAA,OAAO,MAAM,IAAK,EAAA,CAAA;AAAA,GACpB;AAEA,EAAO,OAAA,MAAA,CAAA;AACT;;;;"}
|
|
1
|
+
{"version":3,"file":"loro.mjs","sources":["../src/awareness.ts","../src/index.ts"],"sourcesContent":["import { AwarenessWasm, PeerID } from \"loro-wasm\";\n\nexport type AwarenessListener = (\n arg: { updated: PeerID[]; added: PeerID[]; removed: PeerID[] },\n origin: \"local\" | \"timeout\" | \"remote\" | string,\n) => void;\n\n/**\n * Awareness is a structure that allows to track the ephemeral state of the peers.\n *\n * If we don't receive a state update from a peer within the timeout, we will remove their state.\n * The timeout is in milliseconds. This can be used to handle the off-line state of a peer.\n */\nexport class Awareness<T> {\n inner: AwarenessWasm<T>;\n private peer: PeerID;\n private timer: number | undefined;\n private timeout: number;\n private listeners: Set<AwarenessListener> = new Set();\n constructor(peer: PeerID, timeout: number = 30000) {\n this.inner = new AwarenessWasm(peer, timeout);\n this.peer = peer;\n this.timeout = timeout;\n }\n\n apply(bytes: Uint8Array, origin = \"remote\") {\n const { updated, added } = this.inner.apply(bytes);\n this.listeners.forEach((listener) => {\n listener({ updated, added, removed: [] }, origin);\n });\n\n this.startTimerIfNotEmpty();\n }\n\n setLocalState(state: T) {\n const wasEmpty = this.inner.getState(this.peer) == null;\n this.inner.setLocalState(state);\n if (wasEmpty) {\n this.listeners.forEach((listener) => {\n listener(\n { updated: [], added: [this.inner.peer()], removed: [] },\n \"local\",\n );\n });\n } else {\n this.listeners.forEach((listener) => {\n listener(\n { updated: [this.inner.peer()], added: [], removed: [] },\n \"local\",\n );\n });\n }\n\n this.startTimerIfNotEmpty();\n }\n\n getLocalState(): T | undefined {\n return this.inner.getState(this.peer);\n }\n\n getAllStates(): Record<PeerID, T> {\n return this.inner.getAllStates();\n }\n\n encode(peers: PeerID[]): Uint8Array {\n return this.inner.encode(peers);\n }\n\n encodeAll(): Uint8Array {\n return this.inner.encodeAll();\n }\n\n addListener(listener: AwarenessListener) {\n this.listeners.add(listener);\n }\n\n removeListener(listener: AwarenessListener) {\n this.listeners.delete(listener);\n }\n\n peers(): PeerID[] {\n return this.inner.peers();\n }\n\n destroy() {\n clearInterval(this.timer);\n this.listeners.clear();\n }\n\n private startTimerIfNotEmpty() {\n if (this.inner.isEmpty() || this.timer != null) {\n return;\n }\n\n this.timer = setInterval(() => {\n const removed = this.inner.removeOutdated();\n if (removed.length > 0) {\n this.listeners.forEach((listener) => {\n listener({ updated: [], added: [], removed }, \"timeout\");\n });\n }\n if (this.inner.isEmpty()) {\n clearInterval(this.timer);\n this.timer = undefined;\n }\n }, this.timeout / 2) as unknown as number;\n }\n}\n","export * from \"loro-wasm\";\nimport {\n Container,\n ContainerID,\n Delta,\n Loro,\n LoroList,\n LoroMap,\n LoroText,\n LoroTree,\n OpId,\n TreeID,\n Value,\n} from \"loro-wasm\";\nexport { Awareness } from \"./awareness\";\n\nexport type Frontiers = OpId[];\n\n/**\n * Represents a path to identify the exact location of an event's target.\n * The path is composed of numbers (e.g., indices of a list container) strings\n * (e.g., keys of a map container) and TreeID (the node of a tree container),\n * indicating the absolute position of the event's source within a loro document.\n */\nexport type Path = (number | string | TreeID)[];\n\n/**\n * A batch of events that created by a single `import`/`transaction`/`checkout`.\n *\n * @prop by - How the event is triggered.\n * @prop origin - (Optional) Provides information about the origin of the event.\n * @prop diff - Contains the differential information related to the event.\n * @prop target - Identifies the container ID of the event's target.\n * @prop path - Specifies the absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\nexport interface LoroEventBatch {\n /**\n * How the event is triggered.\n *\n * - `local`: The event is triggered by a local transaction.\n * - `import`: The event is triggered by an import operation.\n * - `checkout`: The event is triggered by a checkout operation.\n */\n by: \"local\" | \"import\" | \"checkout\";\n origin?: string;\n /**\n * The container ID of the current event receiver.\n * It's undefined if the subscriber is on the root document.\n */\n currentTarget?: ContainerID;\n events: LoroEvent[];\n}\n\n/**\n * The concrete event of Loro.\n */\nexport interface LoroEvent {\n /**\n * The container ID of the event's target.\n */\n target: ContainerID;\n diff: Diff;\n /**\n * The absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\n path: Path;\n}\n\nexport type ListDiff = {\n type: \"list\";\n diff: Delta<(Value | Container)[]>[];\n};\n\nexport type TextDiff = {\n type: \"text\";\n diff: Delta<string>[];\n};\n\nexport type MapDiff = {\n type: \"map\";\n updated: Record<string, Value | Container | undefined>;\n};\n\nexport type TreeDiffItem =\n | { target: TreeID; action: \"create\"; parent: TreeID | undefined }\n | { target: TreeID; action: \"delete\" }\n | { target: TreeID; action: \"move\"; parent: TreeID | undefined };\n\nexport type TreeDiff = {\n type: \"tree\";\n diff: TreeDiffItem[];\n};\n\nexport type Diff = ListDiff | TextDiff | MapDiff | TreeDiff;\n\ninterface Listener {\n (event: LoroEventBatch): void;\n}\n\nconst CONTAINER_TYPES = [\"Map\", \"Text\", \"List\", \"Tree\"];\n\nexport function isContainerId(s: string): s is ContainerID {\n return s.startsWith(\"cid:\");\n}\n\nexport { Loro };\n\n/** Whether the value is a container.\n *\n * # Example\n *\n * ```ts\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * const list = doc.getList(\"list\");\n * const text = doc.getText(\"text\");\n * isContainer(map); // true\n * isContainer(list); // true\n * isContainer(text); // true\n * isContainer(123); // false\n * isContainer(\"123\"); // false\n * isContainer({}); // false\n */\nexport function isContainer(value: any): value is Container {\n if (typeof value !== \"object\" || value == null) {\n return false;\n }\n\n const p = Object.getPrototypeOf(value);\n if (p == null || typeof p !== \"object\" || typeof p[\"kind\"] !== \"function\") {\n return false;\n }\n\n return CONTAINER_TYPES.includes(value.kind());\n}\n\n/** Get the type of a value that may be a container.\n *\n * # Example\n *\n * ```ts\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * const list = doc.getList(\"list\");\n * const text = doc.getText(\"text\");\n * getType(map); // \"Map\"\n * getType(list); // \"List\"\n * getType(text); // \"Text\"\n * getType(123); // \"Json\"\n * getType(\"123\"); // \"Json\"\n * getType({}); // \"Json\"\n * ```\n */\nexport function getType<T>(\n value: T,\n): T extends LoroText ? \"Text\"\n : T extends LoroMap<any> ? \"Map\"\n : T extends LoroTree<any> ? \"Tree\"\n : T extends LoroList<any> ? \"List\"\n : \"Json\" {\n if (isContainer(value)) {\n return value.kind() as unknown as any;\n }\n\n return \"Json\" as any;\n}\n\ndeclare module \"loro-wasm\" {\n interface Loro {\n subscribe(listener: Listener): number;\n }\n\n interface Loro<\n T extends Record<string, Container> = Record<string, Container>,\n > {\n /**\n * Get a LoroMap by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * ```\n */\n getMap<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : LoroMap;\n /**\n * Get a LoroList by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * ```\n */\n getList<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : LoroList;\n /**\n * Get a LoroTree by container id\n *\n * The object returned is a new js object each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const tree = doc.getTree(\"tree\");\n * ```\n */\n getTree<Key extends keyof T>(\n name: Key,\n ): T[Key] extends LoroTree ? T[Key] : LoroTree;\n getText(key: string | ContainerID): LoroText;\n }\n\n interface LoroList<T = unknown> {\n new (): LoroList<T>;\n /**\n * Get elements of the list. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * list.insert(1, \"foo\");\n * list.insert(2, true);\n * list.insertContainer(3, new LoroText());\n * console.log(list.value); // [100, \"foo\", true, LoroText];\n * ```\n */\n toArray(): T[];\n /**\n * Insert a container at the index.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * const text = list.insertContainer(1, new LoroText());\n * text.insert(0, \"Hello\");\n * console.log(list.getDeepValue()); // [100, \"Hello\"];\n * ```\n */\n insertContainer<C extends Container>(\n pos: number,\n child: C,\n ): T extends C ? T : C;\n /**\n * Get the value at the index. If the value is a container, the corresponding handler will be returned.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * console.log(list.get(0)); // 100\n * console.log(list.get(1)); // undefined\n * ```\n */\n get(index: number): T;\n /**\n * Insert a value at index.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const list = doc.getList(\"list\");\n * list.insert(0, 100);\n * list.insert(1, \"foo\");\n * list.insert(2, true);\n * console.log(list.value); // [100, \"foo\", true];\n * ```\n */\n insert(pos: number, value: Exclude<T, Container>): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n getAttached(): undefined | LoroList<T>;\n }\n\n interface LoroMap<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n new (): LoroMap<T>;\n /**\n * Get the value of the key. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * The object returned is a new js object each time because it need to cross\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const bar = map.get(\"foo\");\n * ```\n */\n getOrCreateContainer<C extends Container>(key: string, child: C): C;\n /**\n * Set the key with a container.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const text = map.setContainer(\"text\", new LoroText());\n * const list = map.setContainer(\"list\", new LoroText());\n * ```\n */\n setContainer<C extends Container, Key extends keyof T>(\n key: Key,\n child: C,\n ): NonNullableType<T[Key]> extends C ? NonNullableType<T[Key]> : C;\n /**\n * Get the value of the key. If the value is a child container, the corresponding\n * `Container` will be returned.\n *\n * The object/value returned is a new js object/value each time because it need to cross\n * the WASM boundary.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * const bar = map.get(\"foo\");\n * ```\n */\n get<Key extends keyof T>(key: Key): T[Key];\n /**\n * Set the key with the value.\n *\n * If the value of the key is exist, the old value will be updated.\n *\n * @example\n * ```ts\n * import { Loro } from \"loro-crdt\";\n *\n * const doc = new Loro();\n * const map = doc.getMap(\"map\");\n * map.set(\"foo\", \"bar\");\n * map.set(\"foo\", \"baz\");\n * ```\n */\n set<Key extends keyof T>(key: Key, value: Exclude<T[Key], Container>): void;\n delete(key: string): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroText {\n new (): LoroText;\n insert(pos: number, text: string): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroTree<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n new (): LoroTree<T>;\n createNode(parent: TreeID | undefined): LoroTreeNode<T>;\n move(target: TreeID, parent: TreeID | undefined): void;\n delete(target: TreeID): void;\n has(target: TreeID): boolean;\n getNodeByID(target: TreeID): LoroTreeNode;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroTreeNode<\n T extends Record<string, unknown> = Record<string, unknown>,\n > {\n /**\n * Get the associated metadata map container of a tree node.\n */\n readonly data: LoroMap<T>;\n createNode(): LoroTreeNode<T>;\n setAsRoot(): void;\n moveTo(parent: LoroTreeNode<T>): void;\n parent(): LoroTreeNode<T> | undefined;\n children(): Array<LoroTreeNode<T>>;\n }\n\n interface AwarenessWasm<\n T = unknown,\n > {\n getState(peer: PeerID): T | undefined;\n getTimestamp(peer: PeerID): number | undefined;\n getAllStates(): Record<PeerID, T>;\n setLocalState(value: T): void;\n removeOutdated(): PeerID[];\n }\n}\n\ntype NonNullableType<T> = Exclude<T, null | undefined>;\n"],"names":[],"mappings":";;;;AAaO,MAAM,SAAa,CAAA;AAAA,EACxB,KAAA,CAAA;AAAA,EACQ,IAAA,CAAA;AAAA,EACA,KAAA,CAAA;AAAA,EACA,OAAA,CAAA;AAAA,EACA,SAAA,uBAAwC,GAAI,EAAA,CAAA;AAAA,EACpD,WAAA,CAAY,IAAc,EAAA,OAAA,GAAkB,GAAO,EAAA;AACjD,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A,CAAA;AAC9C,CAAA;AAmBO,SAAS,QACd,KAKS,EAAA;AACT,EAAI,IAAA,WAAA,CAAY,KAAK,CAAG,EAAA;AACtB,IAAA,OAAO,MAAM,IAAK,EAAA,CAAA;AAAA,GACpB;AAEA,EAAO,OAAA,MAAA,CAAA;AACT;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "loro-crdt",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.3",
|
|
4
4
|
"description": "Loro CRDTs is a high-performance CRDT framework that makes your app state synchronized, collaborative and maintainable effortlessly.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"crdt",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"homepage": "https://loro.dev",
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"loro-wasm": "0.14.
|
|
20
|
+
"loro-wasm": "0.14.3"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
package/src/awareness.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { AwarenessWasm, PeerID } from "loro-wasm";
|
|
2
|
+
|
|
3
|
+
export type AwarenessListener = (
|
|
4
|
+
arg: { updated: PeerID[]; added: PeerID[]; removed: PeerID[] },
|
|
5
|
+
origin: "local" | "timeout" | "remote" | string,
|
|
6
|
+
) => void;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Awareness is a structure that allows to track the ephemeral state of the peers.
|
|
10
|
+
*
|
|
11
|
+
* If we don't receive a state update from a peer within the timeout, we will remove their state.
|
|
12
|
+
* The timeout is in milliseconds. This can be used to handle the off-line state of a peer.
|
|
13
|
+
*/
|
|
14
|
+
export class Awareness<T> {
|
|
15
|
+
inner: AwarenessWasm<T>;
|
|
16
|
+
private peer: PeerID;
|
|
17
|
+
private timer: number | undefined;
|
|
18
|
+
private timeout: number;
|
|
19
|
+
private listeners: Set<AwarenessListener> = new Set();
|
|
20
|
+
constructor(peer: PeerID, timeout: number = 30000) {
|
|
21
|
+
this.inner = new AwarenessWasm(peer, timeout);
|
|
22
|
+
this.peer = peer;
|
|
23
|
+
this.timeout = timeout;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
apply(bytes: Uint8Array, origin = "remote") {
|
|
27
|
+
const { updated, added } = this.inner.apply(bytes);
|
|
28
|
+
this.listeners.forEach((listener) => {
|
|
29
|
+
listener({ updated, added, removed: [] }, origin);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
this.startTimerIfNotEmpty();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
setLocalState(state: T) {
|
|
36
|
+
const wasEmpty = this.inner.getState(this.peer) == null;
|
|
37
|
+
this.inner.setLocalState(state);
|
|
38
|
+
if (wasEmpty) {
|
|
39
|
+
this.listeners.forEach((listener) => {
|
|
40
|
+
listener(
|
|
41
|
+
{ updated: [], added: [this.inner.peer()], removed: [] },
|
|
42
|
+
"local",
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
} else {
|
|
46
|
+
this.listeners.forEach((listener) => {
|
|
47
|
+
listener(
|
|
48
|
+
{ updated: [this.inner.peer()], added: [], removed: [] },
|
|
49
|
+
"local",
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this.startTimerIfNotEmpty();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getLocalState(): T | undefined {
|
|
58
|
+
return this.inner.getState(this.peer);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getAllStates(): Record<PeerID, T> {
|
|
62
|
+
return this.inner.getAllStates();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
encode(peers: PeerID[]): Uint8Array {
|
|
66
|
+
return this.inner.encode(peers);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
encodeAll(): Uint8Array {
|
|
70
|
+
return this.inner.encodeAll();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
addListener(listener: AwarenessListener) {
|
|
74
|
+
this.listeners.add(listener);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
removeListener(listener: AwarenessListener) {
|
|
78
|
+
this.listeners.delete(listener);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
peers(): PeerID[] {
|
|
82
|
+
return this.inner.peers();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
destroy() {
|
|
86
|
+
clearInterval(this.timer);
|
|
87
|
+
this.listeners.clear();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private startTimerIfNotEmpty() {
|
|
91
|
+
if (this.inner.isEmpty() || this.timer != null) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this.timer = setInterval(() => {
|
|
96
|
+
const removed = this.inner.removeOutdated();
|
|
97
|
+
if (removed.length > 0) {
|
|
98
|
+
this.listeners.forEach((listener) => {
|
|
99
|
+
listener({ updated: [], added: [], removed }, "timeout");
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (this.inner.isEmpty()) {
|
|
103
|
+
clearInterval(this.timer);
|
|
104
|
+
this.timer = undefined;
|
|
105
|
+
}
|
|
106
|
+
}, this.timeout / 2) as unknown as number;
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
TreeID,
|
|
13
13
|
Value,
|
|
14
14
|
} from "loro-wasm";
|
|
15
|
+
export { Awareness } from "./awareness";
|
|
15
16
|
|
|
16
17
|
export type Frontiers = OpId[];
|
|
17
18
|
|
|
@@ -410,6 +411,16 @@ declare module "loro-wasm" {
|
|
|
410
411
|
parent(): LoroTreeNode<T> | undefined;
|
|
411
412
|
children(): Array<LoroTreeNode<T>>;
|
|
412
413
|
}
|
|
414
|
+
|
|
415
|
+
interface AwarenessWasm<
|
|
416
|
+
T = unknown,
|
|
417
|
+
> {
|
|
418
|
+
getState(peer: PeerID): T | undefined;
|
|
419
|
+
getTimestamp(peer: PeerID): number | undefined;
|
|
420
|
+
getAllStates(): Record<PeerID, T>;
|
|
421
|
+
setLocalState(value: T): void;
|
|
422
|
+
removeOutdated(): PeerID[];
|
|
423
|
+
}
|
|
413
424
|
}
|
|
414
425
|
|
|
415
426
|
type NonNullableType<T> = Exclude<T, null | undefined>;
|
package/tsconfig.json
CHANGED
|
@@ -97,5 +97,10 @@
|
|
|
97
97
|
/* Completeness */
|
|
98
98
|
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
99
99
|
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
100
|
-
}
|
|
100
|
+
},
|
|
101
|
+
"exclude": [
|
|
102
|
+
"node_modules",
|
|
103
|
+
"dist",
|
|
104
|
+
"deno"
|
|
105
|
+
]
|
|
101
106
|
}
|
package/vite.config.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import { configDefaults, defineConfig } from 'vitest/config'
|
|
2
2
|
import wasm from "vite-plugin-wasm";
|
|
3
|
-
import { defineConfig } from "vite";
|
|
4
3
|
|
|
5
4
|
export default defineConfig({
|
|
6
5
|
plugins: [wasm()],
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
test: {
|
|
7
|
+
exclude: [
|
|
8
|
+
...configDefaults.exclude,
|
|
9
|
+
"deno/*"
|
|
10
|
+
]
|
|
11
|
+
},
|
|
10
12
|
});
|