loro-crdt 0.13.1 → 0.14.0

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 CHANGED
@@ -1,5 +1,40 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.14.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Improved API
8
+
9
+ ### 🚀 Features
10
+
11
+ - Access value/container by path (#308)
12
+ - Decode import blob meta (#307)
13
+
14
+ ### 🐛 Bug Fixes
15
+
16
+ - Decode iter return result by updating columnar to 0.3.4 (#309)
17
+
18
+ ### 🚜 Refactor
19
+
20
+ - Replace "local" and "fromCheckout" in event with "triggeredBy" (#312)
21
+ - Add concrete type for each different container (#313)
22
+ - _(ts)_ Make types better (#315)
23
+
24
+ ### 📚 Documentation
25
+
26
+ - Refine wasm docs (#304)
27
+ - Clarify that peer id should be convertible to a u64 (#306)
28
+
29
+ ### ⚙️ Miscellaneous Tasks
30
+
31
+ - Add coverage report cli (#311)
32
+
33
+ ### Patch Changes
34
+
35
+ - Updated dependencies
36
+ - loro-wasm@0.14.0
37
+
3
38
  ## 0.13.1
4
39
 
5
40
  ### Patch Changes
package/dist/loro.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ContainerID, Container, Value, TreeID, OpId, Delta, LoroText, LoroMap, LoroTree, LoroList } from 'loro-wasm';
1
+ import { 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
 
@@ -13,18 +13,21 @@ type Path = (number | string | TreeID)[];
13
13
  /**
14
14
  * A batch of events that created by a single `import`/`transaction`/`checkout`.
15
15
  *
16
- * @prop local - Indicates whether the event is local.
16
+ * @prop by - How the event is triggered.
17
17
  * @prop origin - (Optional) Provides information about the origin of the event.
18
18
  * @prop diff - Contains the differential information related to the event.
19
19
  * @prop target - Identifies the container ID of the event's target.
20
20
  * @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.
21
21
  */
22
22
  interface LoroEventBatch {
23
- local: boolean;
24
23
  /**
25
- * If true, this event was triggered by a checkout.
24
+ * How the event is triggered.
25
+ *
26
+ * - `local`: The event is triggered by a local transaction.
27
+ * - `import`: The event is triggered by an import operation.
28
+ * - `checkout`: The event is triggered by a checkout operation.
26
29
  */
27
- fromCheckout: boolean;
30
+ by: "local" | "import" | "checkout";
28
31
  origin?: string;
29
32
  /**
30
33
  * The container ID of the current event receiver.
@@ -120,33 +123,193 @@ declare module "loro-wasm" {
120
123
  interface Loro {
121
124
  subscribe(listener: Listener): number;
122
125
  }
123
- interface Loro<T extends Record<string, any> = Record<string, any>> {
124
- getTypedMap<Key extends keyof T & string>(name: Key): T[Key] extends LoroMap ? T[Key] : never;
125
- getTypedList<Key extends keyof T & string>(name: Key): T[Key] extends LoroList ? T[Key] : never;
126
- getMap(key: string | ContainerID): LoroMap<T[string]>;
127
- getList(key: string | ContainerID): LoroList<T[string]>;
128
- getTree(key: string | ContainerID): LoroTree<T[string]>;
126
+ interface Loro<T extends Record<string, Container> = Record<string, Container>> {
127
+ /**
128
+ * Get a LoroMap by container id
129
+ *
130
+ * The object returned is a new js object each time because it need to cross
131
+ * the WASM boundary.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * import { Loro } from "loro-crdt";
136
+ *
137
+ * const doc = new Loro();
138
+ * const map = doc.getMap("map");
139
+ * ```
140
+ */
141
+ getMap<Key extends keyof T>(name: Key): T[Key] extends LoroMap ? T[Key] : LoroMap;
142
+ /**
143
+ * Get a LoroList by container id
144
+ *
145
+ * The object returned is a new js object each time because it need to cross
146
+ * the WASM boundary.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * import { Loro } from "loro-crdt";
151
+ *
152
+ * const doc = new Loro();
153
+ * const list = doc.getList("list");
154
+ * ```
155
+ */
156
+ getList<Key extends keyof T>(name: Key): T[Key] extends LoroList ? T[Key] : LoroList;
157
+ /**
158
+ * Get a LoroTree by container id
159
+ *
160
+ * The object returned is a new js object each time because it need to cross
161
+ * the WASM boundary.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * import { Loro } from "loro-crdt";
166
+ *
167
+ * const doc = new Loro();
168
+ * const tree = doc.getTree("tree");
169
+ * ```
170
+ */
171
+ getTree<Key extends keyof T>(name: Key): T[Key] extends LoroTree ? T[Key] : LoroTree;
129
172
  getText(key: string | ContainerID): LoroText;
130
173
  }
131
- interface LoroList<T extends any[] = any[]> {
174
+ interface LoroList<T = unknown> {
132
175
  new (): LoroList<T>;
133
- insertContainer<C extends Container>(pos: number, child: C): C;
134
- get(index: number): undefined | Value | Container;
135
- getTyped<Key extends keyof T & number>(loro: Loro, index: Key): T[Key];
136
- insertTyped<Key extends keyof T & number>(pos: Key, value: T[Key]): void;
137
- insert(pos: number, value: Value): void;
176
+ /**
177
+ * Get elements of the list. If the value is a child container, the corresponding
178
+ * `Container` will be returned.
179
+ *
180
+ * @example
181
+ * ```ts
182
+ * import { Loro } from "loro-crdt";
183
+ *
184
+ * const doc = new Loro();
185
+ * const list = doc.getList("list");
186
+ * list.insert(0, 100);
187
+ * list.insert(1, "foo");
188
+ * list.insert(2, true);
189
+ * list.insertContainer(3, new LoroText());
190
+ * console.log(list.value); // [100, "foo", true, LoroText];
191
+ * ```
192
+ */
193
+ toArray(): T[];
194
+ /**
195
+ * Insert a container at the index.
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * import { Loro } from "loro-crdt";
200
+ *
201
+ * const doc = new Loro();
202
+ * const list = doc.getList("list");
203
+ * list.insert(0, 100);
204
+ * const text = list.insertContainer(1, new LoroText());
205
+ * text.insert(0, "Hello");
206
+ * console.log(list.getDeepValue()); // [100, "Hello"];
207
+ * ```
208
+ */
209
+ insertContainer<C extends Container>(pos: number, child: C): T extends C ? T : C;
210
+ /**
211
+ * Get the value at the index. If the value is a container, the corresponding handler will be returned.
212
+ *
213
+ * @example
214
+ * ```ts
215
+ * import { Loro } from "loro-crdt";
216
+ *
217
+ * const doc = new Loro();
218
+ * const list = doc.getList("list");
219
+ * list.insert(0, 100);
220
+ * console.log(list.get(0)); // 100
221
+ * console.log(list.get(1)); // undefined
222
+ * ```
223
+ */
224
+ get(index: number): T;
225
+ /**
226
+ * Insert a value at index.
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * import { Loro } from "loro-crdt";
231
+ *
232
+ * const doc = new Loro();
233
+ * const list = doc.getList("list");
234
+ * list.insert(0, 100);
235
+ * list.insert(1, "foo");
236
+ * list.insert(2, true);
237
+ * console.log(list.value); // [100, "foo", true];
238
+ * ```
239
+ */
240
+ insert(pos: number, value: Exclude<T, Container>): void;
138
241
  delete(pos: number, len: number): void;
139
242
  subscribe(txn: Loro, listener: Listener): number;
140
243
  getAttached(): undefined | LoroList<T>;
141
244
  }
142
- interface LoroMap<T extends Record<string, any> = Record<string, any>> {
245
+ interface LoroMap<T extends Record<string, unknown> = Record<string, unknown>> {
143
246
  new (): LoroMap<T>;
247
+ /**
248
+ * Get the value of the key. If the value is a child container, the corresponding
249
+ * `Container` will be returned.
250
+ *
251
+ * The object returned is a new js object each time because it need to cross
252
+ *
253
+ * @example
254
+ * ```ts
255
+ * import { Loro } from "loro-crdt";
256
+ *
257
+ * const doc = new Loro();
258
+ * const map = doc.getMap("map");
259
+ * map.set("foo", "bar");
260
+ * const bar = map.get("foo");
261
+ * ```
262
+ */
144
263
  getOrCreateContainer<C extends Container>(key: string, child: C): C;
145
- setContainer<C extends Container>(key: string, child: C): C;
146
- get(key: string): undefined | Value | Container;
147
- getTyped<Key extends keyof T & string>(txn: Loro, key: Key): T[Key];
148
- set(key: string, value: Value): void;
149
- setTyped<Key extends keyof T & string>(key: Key, value: T[Key]): void;
264
+ /**
265
+ * Set the key with a container.
266
+ *
267
+ * @example
268
+ * ```ts
269
+ * import { Loro } from "loro-crdt";
270
+ *
271
+ * const doc = new Loro();
272
+ * const map = doc.getMap("map");
273
+ * map.set("foo", "bar");
274
+ * const text = map.setContainer("text", new LoroText());
275
+ * const list = map.setContainer("list", new LoroText());
276
+ * ```
277
+ */
278
+ setContainer<C extends Container, Key extends keyof T>(key: Key, child: C): NonNullableType<T[Key]> extends C ? NonNullableType<T[Key]> : C;
279
+ /**
280
+ * Get the value of the key. If the value is a child container, the corresponding
281
+ * `Container` will be returned.
282
+ *
283
+ * The object/value returned is a new js object/value each time because it need to cross
284
+ * the WASM boundary.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * import { Loro } from "loro-crdt";
289
+ *
290
+ * const doc = new Loro();
291
+ * const map = doc.getMap("map");
292
+ * map.set("foo", "bar");
293
+ * const bar = map.get("foo");
294
+ * ```
295
+ */
296
+ get<Key extends keyof T>(key: Key): T[Key];
297
+ /**
298
+ * Set the key with the value.
299
+ *
300
+ * If the value of the key is exist, the old value will be updated.
301
+ *
302
+ * @example
303
+ * ```ts
304
+ * import { Loro } from "loro-crdt";
305
+ *
306
+ * const doc = new Loro();
307
+ * const map = doc.getMap("map");
308
+ * map.set("foo", "bar");
309
+ * map.set("foo", "baz");
310
+ * ```
311
+ */
312
+ set<Key extends keyof T>(key: Key, value: Exclude<T[Key], Container>): void;
150
313
  delete(key: string): void;
151
314
  subscribe(txn: Loro, listener: Listener): number;
152
315
  }
@@ -156,7 +319,7 @@ declare module "loro-wasm" {
156
319
  delete(pos: number, len: number): void;
157
320
  subscribe(txn: Loro, listener: Listener): number;
158
321
  }
159
- interface LoroTree<T extends Record<string, any> = Record<string, any>> {
322
+ interface LoroTree<T extends Record<string, unknown> = Record<string, unknown>> {
160
323
  new (): LoroTree<T>;
161
324
  createNode(parent: TreeID | undefined): LoroTreeNode<T>;
162
325
  move(target: TreeID, parent: TreeID | undefined): void;
@@ -165,14 +328,18 @@ declare module "loro-wasm" {
165
328
  getNodeByID(target: TreeID): LoroTreeNode;
166
329
  subscribe(txn: Loro, listener: Listener): number;
167
330
  }
168
- interface LoroTreeNode<T extends Record<string, any> = Record<string, any>> {
331
+ interface LoroTreeNode<T extends Record<string, unknown> = Record<string, unknown>> {
332
+ /**
333
+ * Get the associated metadata map container of a tree node.
334
+ */
169
335
  readonly data: LoroMap<T>;
170
336
  createNode(): LoroTreeNode<T>;
171
337
  setAsRoot(): void;
172
338
  moveTo(parent: LoroTreeNode<T>): void;
173
- parent(): LoroTreeNode | undefined;
339
+ parent(): LoroTreeNode<T> | undefined;
174
340
  children(): Array<LoroTreeNode<T>>;
175
341
  }
176
342
  }
343
+ type NonNullableType<T> = Exclude<T, null | undefined>;
177
344
 
178
345
  export { Diff, Frontiers, ListDiff, LoroEvent, LoroEventBatch, MapDiff, Path, TextDiff, TreeDiff, TreeDiffItem, getType, isContainer, isContainerId };
package/dist/loro.js CHANGED
@@ -2,34 +2,6 @@
2
2
 
3
3
  var loroWasm = require('loro-wasm');
4
4
 
5
- loroWasm.Loro.prototype.getTypedMap = function(...args) {
6
- return this.getMap(...args);
7
- };
8
- loroWasm.Loro.prototype.getTypedList = function(...args) {
9
- return this.getList(...args);
10
- };
11
- loroWasm.LoroList.prototype.getTyped = function(loro, index) {
12
- const value = this.get(index);
13
- if (typeof value === "string" && isContainerId(value)) {
14
- return loro.getContainerById(value);
15
- } else {
16
- return value;
17
- }
18
- };
19
- loroWasm.LoroList.prototype.insertTyped = function(...args) {
20
- return this.insert(...args);
21
- };
22
- loroWasm.LoroMap.prototype.getTyped = function(loro, key) {
23
- const value = this.get(key);
24
- if (typeof value === "string" && isContainerId(value)) {
25
- return loro.getContainerById(value);
26
- } else {
27
- return value;
28
- }
29
- };
30
- loroWasm.LoroMap.prototype.setTyped = function(...args) {
31
- return this.set(...args);
32
- };
33
5
  const CONTAINER_TYPES = ["Map", "Text", "List", "Tree"];
34
6
  function isContainerId(s) {
35
7
  return s.startsWith("cid:");
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 LoroTreeNode,\n OpId,\n TreeID,\n Value,\n} from \"loro-wasm\";\n\nLoro.prototype.getTypedMap = function (...args) {\n return this.getMap(...args);\n};\nLoro.prototype.getTypedList = function (...args) {\n return this.getList(...args);\n};\nLoroList.prototype.getTyped = function (loro, index) {\n const value = this.get(index);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroList.prototype.insertTyped = function (...args) {\n return this.insert(...args);\n};\nLoroMap.prototype.getTyped = function (loro, key) {\n const value = this.get(key);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroMap.prototype.setTyped = function (...args) {\n return this.set(...args);\n};\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 local - Indicates whether the event is local.\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 local: boolean;\n /**\n * If true, this event was triggered by a checkout.\n */\n fromCheckout: boolean;\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<T extends Record<string, any> = Record<string, any>> {\n getTypedMap<Key extends keyof T & string>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : never;\n getTypedList<Key extends keyof T & string>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : never;\n getMap(key: string | ContainerID): LoroMap<T[string]>;\n getList(key: string | ContainerID): LoroList<T[string]>;\n getTree(key: string | ContainerID): LoroTree<T[string]>;\n getText(key: string | ContainerID): LoroText;\n }\n\n interface LoroList<\n T extends any[] = any[],\n > {\n new (): LoroList<T>;\n insertContainer<C extends Container>(\n pos: number,\n child: C,\n ): C;\n get(index: number): undefined | Value | Container;\n getTyped<Key extends keyof T & number>(loro: Loro, index: Key): T[Key];\n insertTyped<Key extends keyof T & number>(pos: Key, value: T[Key]): void;\n insert(pos: number, value: Value): 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, any> = Record<string, any>,\n > {\n new (): LoroMap<T>;\n getOrCreateContainer<C extends Container>(\n key: string,\n child: C,\n ): C;\n setContainer<C extends Container>(\n key: string,\n child: C,\n ): C;\n get(key: string): undefined | Value | Container;\n getTyped<Key extends keyof T & string>(txn: Loro, key: Key): T[Key];\n set(key: string, value: Value): void;\n setTyped<Key extends keyof T & string>(key: Key, value: T[Key]): 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, any> = Record<string, any>,\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, any> = Record<string, any>,\n > {\n readonly data: LoroMap<T>;\n createNode(): LoroTreeNode<T>;\n setAsRoot(): void;\n moveTo(parent: LoroTreeNode<T>): void;\n parent(): LoroTreeNode | undefined;\n children(): Array<LoroTreeNode<T>>;\n }\n}\n"],"names":["Loro","LoroList","LoroMap"],"mappings":";;;;AAgBAA,aAAK,CAAA,SAAA,CAAU,WAAc,GAAA,SAAA,GAAa,IAAM,EAAA;AAC9C,EAAO,OAAA,IAAA,CAAK,MAAO,CAAA,GAAG,IAAI,CAAA,CAAA;AAC5B,CAAA,CAAA;AACAA,aAAK,CAAA,SAAA,CAAU,YAAe,GAAA,SAAA,GAAa,IAAM,EAAA;AAC/C,EAAO,OAAA,IAAA,CAAK,OAAQ,CAAA,GAAG,IAAI,CAAA,CAAA;AAC7B,CAAA,CAAA;AACAC,iBAAA,CAAS,SAAU,CAAA,QAAA,GAAW,SAAU,IAAA,EAAM,KAAO,EAAA;AACnD,EAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,GAAA,CAAI,KAAK,CAAA,CAAA;AAC5B,EAAA,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,aAAA,CAAc,KAAK,CAAG,EAAA;AACrD,IAAO,OAAA,IAAA,CAAK,iBAAiB,KAAK,CAAA,CAAA;AAAA,GAC7B,MAAA;AACL,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF,CAAA,CAAA;AACAA,iBAAS,CAAA,SAAA,CAAU,WAAc,GAAA,SAAA,GAAa,IAAM,EAAA;AAClD,EAAO,OAAA,IAAA,CAAK,MAAO,CAAA,GAAG,IAAI,CAAA,CAAA;AAC5B,CAAA,CAAA;AACAC,gBAAA,CAAQ,SAAU,CAAA,QAAA,GAAW,SAAU,IAAA,EAAM,GAAK,EAAA;AAChD,EAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,GAAA,CAAI,GAAG,CAAA,CAAA;AAC1B,EAAA,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,aAAA,CAAc,KAAK,CAAG,EAAA;AACrD,IAAO,OAAA,IAAA,CAAK,iBAAiB,KAAK,CAAA,CAAA;AAAA,GAC7B,MAAA;AACL,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF,CAAA,CAAA;AACAA,gBAAQ,CAAA,SAAA,CAAU,QAAW,GAAA,SAAA,GAAa,IAAM,EAAA;AAC9C,EAAO,OAAA,IAAA,CAAK,GAAI,CAAA,GAAG,IAAI,CAAA,CAAA;AACzB,CAAA,CAAA;AAkFA,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/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;;;;;;;;;;;;;;;;"}
package/dist/loro.mjs CHANGED
@@ -1,35 +1,6 @@
1
- import { Loro, LoroList, LoroMap } from 'loro-wasm';
2
1
  export * from 'loro-wasm';
3
2
  export { Loro } from 'loro-wasm';
4
3
 
5
- Loro.prototype.getTypedMap = function(...args) {
6
- return this.getMap(...args);
7
- };
8
- Loro.prototype.getTypedList = function(...args) {
9
- return this.getList(...args);
10
- };
11
- LoroList.prototype.getTyped = function(loro, index) {
12
- const value = this.get(index);
13
- if (typeof value === "string" && isContainerId(value)) {
14
- return loro.getContainerById(value);
15
- } else {
16
- return value;
17
- }
18
- };
19
- LoroList.prototype.insertTyped = function(...args) {
20
- return this.insert(...args);
21
- };
22
- LoroMap.prototype.getTyped = function(loro, key) {
23
- const value = this.get(key);
24
- if (typeof value === "string" && isContainerId(value)) {
25
- return loro.getContainerById(value);
26
- } else {
27
- return value;
28
- }
29
- };
30
- LoroMap.prototype.setTyped = function(...args) {
31
- return this.set(...args);
32
- };
33
4
  const CONTAINER_TYPES = ["Map", "Text", "List", "Tree"];
34
5
  function isContainerId(s) {
35
6
  return s.startsWith("cid:");
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 LoroTreeNode,\n OpId,\n TreeID,\n Value,\n} from \"loro-wasm\";\n\nLoro.prototype.getTypedMap = function (...args) {\n return this.getMap(...args);\n};\nLoro.prototype.getTypedList = function (...args) {\n return this.getList(...args);\n};\nLoroList.prototype.getTyped = function (loro, index) {\n const value = this.get(index);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroList.prototype.insertTyped = function (...args) {\n return this.insert(...args);\n};\nLoroMap.prototype.getTyped = function (loro, key) {\n const value = this.get(key);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroMap.prototype.setTyped = function (...args) {\n return this.set(...args);\n};\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 local - Indicates whether the event is local.\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 local: boolean;\n /**\n * If true, this event was triggered by a checkout.\n */\n fromCheckout: boolean;\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<T extends Record<string, any> = Record<string, any>> {\n getTypedMap<Key extends keyof T & string>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : never;\n getTypedList<Key extends keyof T & string>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : never;\n getMap(key: string | ContainerID): LoroMap<T[string]>;\n getList(key: string | ContainerID): LoroList<T[string]>;\n getTree(key: string | ContainerID): LoroTree<T[string]>;\n getText(key: string | ContainerID): LoroText;\n }\n\n interface LoroList<\n T extends any[] = any[],\n > {\n new (): LoroList<T>;\n insertContainer<C extends Container>(\n pos: number,\n child: C,\n ): C;\n get(index: number): undefined | Value | Container;\n getTyped<Key extends keyof T & number>(loro: Loro, index: Key): T[Key];\n insertTyped<Key extends keyof T & number>(pos: Key, value: T[Key]): void;\n insert(pos: number, value: Value): 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, any> = Record<string, any>,\n > {\n new (): LoroMap<T>;\n getOrCreateContainer<C extends Container>(\n key: string,\n child: C,\n ): C;\n setContainer<C extends Container>(\n key: string,\n child: C,\n ): C;\n get(key: string): undefined | Value | Container;\n getTyped<Key extends keyof T & string>(txn: Loro, key: Key): T[Key];\n set(key: string, value: Value): void;\n setTyped<Key extends keyof T & string>(key: Key, value: T[Key]): 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, any> = Record<string, any>,\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, any> = Record<string, any>,\n > {\n readonly data: LoroMap<T>;\n createNode(): LoroTreeNode<T>;\n setAsRoot(): void;\n moveTo(parent: LoroTreeNode<T>): void;\n parent(): LoroTreeNode | undefined;\n children(): Array<LoroTreeNode<T>>;\n }\n}\n"],"names":[],"mappings":";;;;AAgBA,IAAK,CAAA,SAAA,CAAU,WAAc,GAAA,SAAA,GAAa,IAAM,EAAA;AAC9C,EAAO,OAAA,IAAA,CAAK,MAAO,CAAA,GAAG,IAAI,CAAA,CAAA;AAC5B,CAAA,CAAA;AACA,IAAK,CAAA,SAAA,CAAU,YAAe,GAAA,SAAA,GAAa,IAAM,EAAA;AAC/C,EAAO,OAAA,IAAA,CAAK,OAAQ,CAAA,GAAG,IAAI,CAAA,CAAA;AAC7B,CAAA,CAAA;AACA,QAAA,CAAS,SAAU,CAAA,QAAA,GAAW,SAAU,IAAA,EAAM,KAAO,EAAA;AACnD,EAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,GAAA,CAAI,KAAK,CAAA,CAAA;AAC5B,EAAA,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,aAAA,CAAc,KAAK,CAAG,EAAA;AACrD,IAAO,OAAA,IAAA,CAAK,iBAAiB,KAAK,CAAA,CAAA;AAAA,GAC7B,MAAA;AACL,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF,CAAA,CAAA;AACA,QAAS,CAAA,SAAA,CAAU,WAAc,GAAA,SAAA,GAAa,IAAM,EAAA;AAClD,EAAO,OAAA,IAAA,CAAK,MAAO,CAAA,GAAG,IAAI,CAAA,CAAA;AAC5B,CAAA,CAAA;AACA,OAAA,CAAQ,SAAU,CAAA,QAAA,GAAW,SAAU,IAAA,EAAM,GAAK,EAAA;AAChD,EAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,GAAA,CAAI,GAAG,CAAA,CAAA;AAC1B,EAAA,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,aAAA,CAAc,KAAK,CAAG,EAAA;AACrD,IAAO,OAAA,IAAA,CAAK,iBAAiB,KAAK,CAAA,CAAA;AAAA,GAC7B,MAAA;AACL,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF,CAAA,CAAA;AACA,OAAQ,CAAA,SAAA,CAAU,QAAW,GAAA,SAAA,GAAa,IAAM,EAAA;AAC9C,EAAO,OAAA,IAAA,CAAK,GAAI,CAAA,GAAG,IAAI,CAAA,CAAA;AACzB,CAAA,CAAA;AAkFA,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/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;;;;"}
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.getType = exports.isContainer = exports.Loro = exports.isContainerId = void 0;
18
+ __exportStar(require("loro-wasm"), exports);
19
+ const loro_wasm_1 = require("loro-wasm");
20
+ Object.defineProperty(exports, "Loro", { enumerable: true, get: function () { return loro_wasm_1.Loro; } });
21
+ loro_wasm_1.Loro.prototype.getTypedMap = function (...args) {
22
+ return this.getMap(...args);
23
+ };
24
+ loro_wasm_1.Loro.prototype.getTypedList = function (...args) {
25
+ return this.getList(...args);
26
+ };
27
+ loro_wasm_1.LoroList.prototype.getTyped = function (loro, index) {
28
+ const value = this.get(index);
29
+ if (typeof value === "string" && isContainerId(value)) {
30
+ return loro.getContainerById(value);
31
+ }
32
+ else {
33
+ return value;
34
+ }
35
+ };
36
+ loro_wasm_1.LoroList.prototype.insertTyped = function (...args) {
37
+ return this.insert(...args);
38
+ };
39
+ loro_wasm_1.LoroMap.prototype.getTyped = function (loro, key) {
40
+ const value = this.get(key);
41
+ if (typeof value === "string" && isContainerId(value)) {
42
+ return loro.getContainerById(value);
43
+ }
44
+ else {
45
+ return value;
46
+ }
47
+ };
48
+ loro_wasm_1.LoroMap.prototype.setTyped = function (...args) {
49
+ return this.set(...args);
50
+ };
51
+ const CONTAINER_TYPES = ["Map", "Text", "List", "Tree"];
52
+ function isContainerId(s) {
53
+ return s.startsWith("cid:");
54
+ }
55
+ exports.isContainerId = isContainerId;
56
+ /** Whether the value is a container.
57
+ *
58
+ * # Example
59
+ *
60
+ * ```ts
61
+ * const doc = new Loro();
62
+ * const map = doc.getMap("map");
63
+ * const list = doc.getList("list");
64
+ * const text = doc.getText("text");
65
+ * isContainer(map); // true
66
+ * isContainer(list); // true
67
+ * isContainer(text); // true
68
+ * isContainer(123); // false
69
+ * isContainer("123"); // false
70
+ * isContainer({}); // false
71
+ */
72
+ function isContainer(value) {
73
+ if (typeof value !== "object" || value == null) {
74
+ return false;
75
+ }
76
+ const p = Object.getPrototypeOf(value);
77
+ if (p == null || typeof p !== "object" || typeof p["kind"] !== "function") {
78
+ return false;
79
+ }
80
+ return CONTAINER_TYPES.includes(value.kind());
81
+ }
82
+ exports.isContainer = isContainer;
83
+ /** Get the type of a value that may be a container.
84
+ *
85
+ * # Example
86
+ *
87
+ * ```ts
88
+ * const doc = new Loro();
89
+ * const map = doc.getMap("map");
90
+ * const list = doc.getList("list");
91
+ * const text = doc.getText("text");
92
+ * getType(map); // "Map"
93
+ * getType(list); // "List"
94
+ * getType(text); // "Text"
95
+ * getType(123); // "Json"
96
+ * getType("123"); // "Json"
97
+ * getType({}); // "Json"
98
+ * ```
99
+ */
100
+ function getType(value) {
101
+ if (isContainer(value)) {
102
+ return value.kind();
103
+ }
104
+ return "Json";
105
+ }
106
+ exports.getType = getType;