loro-crdt 1.0.7 → 1.0.8-alpha.1

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/dist/loro.d.ts DELETED
@@ -1,595 +0,0 @@
1
- import { Value, AwarenessWasm, PeerID as PeerID$1, Container, ContainerID, TreeID, LoroDoc, OpId, Delta, LoroText, LoroMap, LoroTree, LoroList, LoroCounter, ContainerType } from 'loro-wasm';
2
- export * from 'loro-wasm';
3
-
4
- type AwarenessListener = (arg: {
5
- updated: PeerID$1[];
6
- added: PeerID$1[];
7
- removed: PeerID$1[];
8
- }, origin: "local" | "timeout" | "remote" | string) => void;
9
- /**
10
- * Awareness is a structure that allows to track the ephemeral state of the peers.
11
- *
12
- * If we don't receive a state update from a peer within the timeout, we will remove their state.
13
- * The timeout is in milliseconds. This can be used to handle the off-line state of a peer.
14
- */
15
- declare class Awareness<T extends Value = Value> {
16
- inner: AwarenessWasm<T>;
17
- private peer;
18
- private timer;
19
- private timeout;
20
- private listeners;
21
- constructor(peer: PeerID$1, timeout?: number);
22
- apply(bytes: Uint8Array, origin?: string): void;
23
- setLocalState(state: T): void;
24
- getLocalState(): T | undefined;
25
- getAllStates(): Record<PeerID$1, T>;
26
- encode(peers: PeerID$1[]): Uint8Array;
27
- encodeAll(): Uint8Array;
28
- addListener(listener: AwarenessListener): void;
29
- removeListener(listener: AwarenessListener): void;
30
- peers(): PeerID$1[];
31
- destroy(): void;
32
- private startTimerIfNotEmpty;
33
- }
34
-
35
- /**
36
- * @deprecated Please use LoroDoc
37
- */
38
- declare class Loro extends LoroDoc {
39
- }
40
-
41
- type Frontiers = OpId[];
42
- /**
43
- * Represents a path to identify the exact location of an event's target.
44
- * The path is composed of numbers (e.g., indices of a list container) strings
45
- * (e.g., keys of a map container) and TreeID (the node of a tree container),
46
- * indicating the absolute position of the event's source within a loro document.
47
- */
48
- type Path = (number | string | TreeID)[];
49
- /**
50
- * A batch of events that created by a single `import`/`transaction`/`checkout`.
51
- *
52
- * @prop by - How the event is triggered.
53
- * @prop origin - (Optional) Provides information about the origin of the event.
54
- * @prop diff - Contains the differential information related to the event.
55
- * @prop target - Identifies the container ID of the event's target.
56
- * @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.
57
- */
58
- interface LoroEventBatch {
59
- /**
60
- * How the event is triggered.
61
- *
62
- * - `local`: The event is triggered by a local transaction.
63
- * - `import`: The event is triggered by an import operation.
64
- * - `checkout`: The event is triggered by a checkout operation.
65
- */
66
- by: "local" | "import" | "checkout";
67
- origin?: string;
68
- /**
69
- * The container ID of the current event receiver.
70
- * It's undefined if the subscriber is on the root document.
71
- */
72
- currentTarget?: ContainerID;
73
- events: LoroEvent[];
74
- }
75
- /**
76
- * The concrete event of Loro.
77
- */
78
- interface LoroEvent {
79
- /**
80
- * The container ID of the event's target.
81
- */
82
- target: ContainerID;
83
- diff: Diff;
84
- /**
85
- * The absolute path of the event's emitter, which can be an index of a list container or a key of a map container.
86
- */
87
- path: Path;
88
- }
89
- type ListDiff = {
90
- type: "list";
91
- diff: Delta<(Value | Container)[]>[];
92
- };
93
- type TextDiff = {
94
- type: "text";
95
- diff: Delta<string>[];
96
- };
97
- type MapDiff = {
98
- type: "map";
99
- updated: Record<string, Value | Container | undefined>;
100
- };
101
- type TreeDiffItem = {
102
- target: TreeID;
103
- action: "create";
104
- parent: TreeID | undefined;
105
- index: number;
106
- fractionalIndex: string;
107
- } | {
108
- target: TreeID;
109
- action: "delete";
110
- oldParent: TreeID | undefined;
111
- oldIndex: number;
112
- } | {
113
- target: TreeID;
114
- action: "move";
115
- parent: TreeID | undefined;
116
- index: number;
117
- fractionalIndex: string;
118
- oldParent: TreeID | undefined;
119
- oldIndex: number;
120
- };
121
- type TreeDiff = {
122
- type: "tree";
123
- diff: TreeDiffItem[];
124
- };
125
- type CounterDiff = {
126
- type: "counter";
127
- increment: number;
128
- };
129
- type Diff = ListDiff | TextDiff | MapDiff | TreeDiff | CounterDiff;
130
- interface Listener {
131
- (event: LoroEventBatch): void;
132
- }
133
- declare function isContainerId(s: string): s is ContainerID;
134
- /** Whether the value is a container.
135
- *
136
- * # Example
137
- *
138
- * ```ts
139
- * const doc = new LoroDoc();
140
- * const map = doc.getMap("map");
141
- * const list = doc.getList("list");
142
- * const text = doc.getText("text");
143
- * isContainer(map); // true
144
- * isContainer(list); // true
145
- * isContainer(text); // true
146
- * isContainer(123); // false
147
- * isContainer("123"); // false
148
- * isContainer({}); // false
149
- */
150
- declare function isContainer(value: any): value is Container;
151
- /** Get the type of a value that may be a container.
152
- *
153
- * # Example
154
- *
155
- * ```ts
156
- * const doc = new LoroDoc();
157
- * const map = doc.getMap("map");
158
- * const list = doc.getList("list");
159
- * const text = doc.getText("text");
160
- * getType(map); // "Map"
161
- * getType(list); // "List"
162
- * getType(text); // "Text"
163
- * getType(123); // "Json"
164
- * getType("123"); // "Json"
165
- * getType({}); // "Json"
166
- * ```
167
- */
168
- declare function getType<T>(value: T): T extends LoroText ? "Text" : T extends LoroMap<any> ? "Map" : T extends LoroTree<any> ? "Tree" : T extends LoroList<any> ? "List" : T extends LoroCounter ? "Counter" : "Json";
169
- type Subscription = () => void;
170
- declare module "loro-wasm" {
171
- interface LoroDoc {
172
- subscribe(listener: Listener): Subscription;
173
- }
174
- interface UndoManager {
175
- /**
176
- * Set the callback function that is called when an undo/redo step is pushed.
177
- * The function can return a meta data value that will be attached to the given stack item.
178
- *
179
- * @param listener - The callback function.
180
- */
181
- setOnPush(listener?: UndoConfig["onPush"]): void;
182
- /**
183
- * Set the callback function that is called when an undo/redo step is popped.
184
- * The function will have a meta data value that was attached to the given stack item when `onPush` was called.
185
- *
186
- * @param listener - The callback function.
187
- */
188
- setOnPop(listener?: UndoConfig["onPop"]): void;
189
- }
190
- interface LoroDoc<T extends Record<string, Container> = Record<string, Container>> {
191
- /**
192
- * Get a LoroMap by container id
193
- *
194
- * The object returned is a new js object each time because it need to cross
195
- * the WASM boundary.
196
- *
197
- * @example
198
- * ```ts
199
- * import { LoroDoc } from "loro-crdt";
200
- *
201
- * const doc = new LoroDoc();
202
- * const map = doc.getMap("map");
203
- * ```
204
- */
205
- getMap<Key extends keyof T | ContainerID>(name: Key): T[Key] extends LoroMap ? T[Key] : LoroMap;
206
- /**
207
- * Get a LoroList by container id
208
- *
209
- * The object returned is a new js object each time because it need to cross
210
- * the WASM boundary.
211
- *
212
- * @example
213
- * ```ts
214
- * import { LoroDoc } from "loro-crdt";
215
- *
216
- * const doc = new LoroDoc();
217
- * const list = doc.getList("list");
218
- * ```
219
- */
220
- getList<Key extends keyof T | ContainerID>(name: Key): T[Key] extends LoroList ? T[Key] : LoroList;
221
- /**
222
- * Get a LoroMovableList by container id
223
- *
224
- * The object returned is a new js object each time because it need to cross
225
- * the WASM boundary.
226
- *
227
- * @example
228
- * ```ts
229
- * import { LoroDoc } from "loro-crdt";
230
- *
231
- * const doc = new LoroDoc();
232
- * const list = doc.getList("list");
233
- * ```
234
- */
235
- getMovableList<Key extends keyof T | ContainerID>(name: Key): T[Key] extends LoroMovableList ? T[Key] : LoroMovableList;
236
- /**
237
- * Get a LoroTree by container id
238
- *
239
- * The object returned is a new js object each time because it need to cross
240
- * the WASM boundary.
241
- *
242
- * @example
243
- * ```ts
244
- * import { LoroDoc } from "loro-crdt";
245
- *
246
- * const doc = new LoroDoc();
247
- * const tree = doc.getTree("tree");
248
- * ```
249
- */
250
- getTree<Key extends keyof T | ContainerID>(name: Key): T[Key] extends LoroTree ? T[Key] : LoroTree;
251
- getText(key: string | ContainerID): LoroText;
252
- }
253
- interface LoroList<T = unknown> {
254
- new (): LoroList<T>;
255
- /**
256
- * Get elements of the list. If the value is a child container, the corresponding
257
- * `Container` will be returned.
258
- *
259
- * @example
260
- * ```ts
261
- * import { LoroDoc } from "loro-crdt";
262
- *
263
- * const doc = new LoroDoc();
264
- * const list = doc.getList("list");
265
- * list.insert(0, 100);
266
- * list.insert(1, "foo");
267
- * list.insert(2, true);
268
- * list.insertContainer(3, new LoroText());
269
- * console.log(list.value); // [100, "foo", true, LoroText];
270
- * ```
271
- */
272
- toArray(): T[];
273
- /**
274
- * Insert a container at the index.
275
- *
276
- * @example
277
- * ```ts
278
- * import { LoroDoc, LoroText } from "loro-crdt";
279
- *
280
- * const doc = new LoroDoc();
281
- * const list = doc.getList("list");
282
- * list.insert(0, 100);
283
- * const text = list.insertContainer(1, new LoroText());
284
- * text.insert(0, "Hello");
285
- * console.log(list.toJSON()); // [100, "Hello"];
286
- * ```
287
- */
288
- insertContainer<C extends Container>(pos: number, child: C): T extends C ? T : C;
289
- /**
290
- * Get the value at the index. If the value is a container, the corresponding handler will be returned.
291
- *
292
- * @example
293
- * ```ts
294
- * import { LoroDoc } from "loro-crdt";
295
- *
296
- * const doc = new LoroDoc();
297
- * const list = doc.getList("list");
298
- * list.insert(0, 100);
299
- * console.log(list.get(0)); // 100
300
- * console.log(list.get(1)); // undefined
301
- * ```
302
- */
303
- get(index: number): T;
304
- /**
305
- * Insert a value at index.
306
- *
307
- * @example
308
- * ```ts
309
- * import { LoroDoc } from "loro-crdt";
310
- *
311
- * const doc = new LoroDoc();
312
- * const list = doc.getList("list");
313
- * list.insert(0, 100);
314
- * list.insert(1, "foo");
315
- * list.insert(2, true);
316
- * console.log(list.value); // [100, "foo", true];
317
- * ```
318
- */
319
- insert<V extends T>(pos: number, value: Exclude<V, Container>): void;
320
- delete(pos: number, len: number): void;
321
- push<V extends T>(value: Exclude<V, Container>): void;
322
- subscribe(listener: Listener): Subscription;
323
- getAttached(): undefined | LoroList<T>;
324
- }
325
- interface LoroMovableList<T = unknown> {
326
- new (): LoroMovableList<T>;
327
- /**
328
- * Get elements of the list. If the value is a child container, the corresponding
329
- * `Container` will be returned.
330
- *
331
- * @example
332
- * ```ts
333
- * import { LoroDoc, LoroText } from "loro-crdt";
334
- *
335
- * const doc = new LoroDoc();
336
- * const list = doc.getMovableList("list");
337
- * list.insert(0, 100);
338
- * list.insert(1, "foo");
339
- * list.insert(2, true);
340
- * list.insertContainer(3, new LoroText());
341
- * console.log(list.value); // [100, "foo", true, LoroText];
342
- * ```
343
- */
344
- toArray(): T[];
345
- /**
346
- * Insert a container at the index.
347
- *
348
- * @example
349
- * ```ts
350
- * import { LoroDoc } from "loro-crdt";
351
- *
352
- * const doc = new LoroDoc();
353
- * const list = doc.getMovableList("list");
354
- * list.insert(0, 100);
355
- * const text = list.insertContainer(1, new LoroText());
356
- * text.insert(0, "Hello");
357
- * console.log(list.toJSON()); // [100, "Hello"];
358
- * ```
359
- */
360
- insertContainer<C extends Container>(pos: number, child: C): T extends C ? T : C;
361
- /**
362
- * Get the value at the index. If the value is a container, the corresponding handler will be returned.
363
- *
364
- * @example
365
- * ```ts
366
- * import { LoroDoc } from "loro-crdt";
367
- *
368
- * const doc = new LoroDoc();
369
- * const list = doc.getMovableList("list");
370
- * list.insert(0, 100);
371
- * console.log(list.get(0)); // 100
372
- * console.log(list.get(1)); // undefined
373
- * ```
374
- */
375
- get(index: number): T;
376
- /**
377
- * Insert a value at index.
378
- *
379
- * @example
380
- * ```ts
381
- * import { LoroDoc } from "loro-crdt";
382
- *
383
- * const doc = new LoroDoc();
384
- * const list = doc.getMovableList("list");
385
- * list.insert(0, 100);
386
- * list.insert(1, "foo");
387
- * list.insert(2, true);
388
- * console.log(list.value); // [100, "foo", true];
389
- * ```
390
- */
391
- insert<V extends T>(pos: number, value: Exclude<V, Container>): void;
392
- delete(pos: number, len: number): void;
393
- push<V extends T>(value: Exclude<V, Container>): void;
394
- subscribe(listener: Listener): Subscription;
395
- getAttached(): undefined | LoroMovableList<T>;
396
- /**
397
- * Set the value at the given position.
398
- *
399
- * It's different from `delete` + `insert` that it will replace the value at the position.
400
- *
401
- * For example, if you have a list `[1, 2, 3]`, and you call `set(1, 100)`, the list will be `[1, 100, 3]`.
402
- * If concurrently someone call `set(1, 200)`, the list will be `[1, 200, 3]` or `[1, 100, 3]`.
403
- *
404
- * But if you use `delete` + `insert` to simulate the set operation, they may create redundant operations
405
- * and the final result will be `[1, 100, 200, 3]` or `[1, 200, 100, 3]`.
406
- *
407
- * @example
408
- * ```ts
409
- * import { LoroDoc } from "loro-crdt";
410
- *
411
- * const doc = new LoroDoc();
412
- * const list = doc.getList("list");
413
- * list.insert(0, 100);
414
- * list.insert(1, "foo");
415
- * list.insert(2, true);
416
- * list.set(1, "bar");
417
- * console.log(list.value); // [100, "bar", true];
418
- * ```
419
- */
420
- set<V extends T>(pos: number, value: Exclude<V, Container>): void;
421
- /**
422
- * Set a container at the index.
423
- *
424
- * @example
425
- * ```ts
426
- * import { LoroDoc } from "loro-crdt";
427
- *
428
- * const doc = new LoroDoc();
429
- * const list = doc.getMovableList("list");
430
- * list.insert(0, 100);
431
- * const text = list.setContainer(0, new LoroText());
432
- * text.insert(0, "Hello");
433
- * console.log(list.toJSON()); // ["Hello"];
434
- * ```
435
- */
436
- setContainer<C extends Container>(pos: number, child: C): T extends C ? T : C;
437
- }
438
- interface LoroMap<T extends Record<string, unknown> = Record<string, unknown>> {
439
- new (): LoroMap<T>;
440
- /**
441
- * Get the value of the key. If the value is a child container, the corresponding
442
- * `Container` will be returned.
443
- *
444
- * The object returned is a new js object each time because it need to cross
445
- *
446
- * @example
447
- * ```ts
448
- * import { LoroDoc } from "loro-crdt";
449
- *
450
- * const doc = new LoroDoc();
451
- * const map = doc.getMap("map");
452
- * map.set("foo", "bar");
453
- * const bar = map.get("foo");
454
- * ```
455
- */
456
- getOrCreateContainer<C extends Container>(key: string, child: C): C;
457
- /**
458
- * Set the key with a container.
459
- *
460
- * @example
461
- * ```ts
462
- * import { LoroDoc } from "loro-crdt";
463
- *
464
- * const doc = new LoroDoc();
465
- * const map = doc.getMap("map");
466
- * map.set("foo", "bar");
467
- * const text = map.setContainer("text", new LoroText());
468
- * const list = map.setContainer("list", new LoroText());
469
- * ```
470
- */
471
- setContainer<C extends Container, Key extends keyof T>(key: Key, child: C): NonNullableType<T[Key]> extends C ? NonNullableType<T[Key]> : C;
472
- /**
473
- * Get the value of the key. If the value is a child container, the corresponding
474
- * `Container` will be returned.
475
- *
476
- * The object/value returned is a new js object/value each time because it need to cross
477
- * the WASM boundary.
478
- *
479
- * @example
480
- * ```ts
481
- * import { LoroDoc } from "loro-crdt";
482
- *
483
- * const doc = new LoroDoc();
484
- * const map = doc.getMap("map");
485
- * map.set("foo", "bar");
486
- * const bar = map.get("foo");
487
- * ```
488
- */
489
- get<Key extends keyof T>(key: Key): T[Key];
490
- /**
491
- * Set the key with the value.
492
- *
493
- * If the value of the key is exist, the old value will be updated.
494
- *
495
- * @example
496
- * ```ts
497
- * import { LoroDoc } from "loro-crdt";
498
- *
499
- * const doc = new LoroDoc();
500
- * const map = doc.getMap("map");
501
- * map.set("foo", "bar");
502
- * map.set("foo", "baz");
503
- * ```
504
- */
505
- set<Key extends keyof T, V extends T[Key]>(key: Key, value: Exclude<V, Container>): void;
506
- delete(key: string): void;
507
- subscribe(listener: Listener): Subscription;
508
- }
509
- interface LoroText {
510
- new (): LoroText;
511
- insert(pos: number, text: string): void;
512
- delete(pos: number, len: number): void;
513
- subscribe(listener: Listener): Subscription;
514
- }
515
- interface LoroTree<T extends Record<string, unknown> = Record<string, unknown>> {
516
- new (): LoroTree<T>;
517
- /**
518
- * Create a new tree node as the child of parent and return a `LoroTreeNode` instance.
519
- * If the parent is undefined, the tree node will be a root node.
520
- *
521
- * If the index is not provided, the new node will be appended to the end.
522
- *
523
- * @example
524
- * ```ts
525
- * import { LoroDoc } from "loro-crdt";
526
- *
527
- * const doc = new LoroDoc();
528
- * const tree = doc.getTree("tree");
529
- * const root = tree.createNode();
530
- * const node = tree.createNode(undefined, 0);
531
- *
532
- * // undefined
533
- * // / \
534
- * // node root
535
- * ```
536
- */
537
- createNode(parent?: TreeID, index?: number): LoroTreeNode<T>;
538
- move(target: TreeID, parent?: TreeID, index?: number): void;
539
- delete(target: TreeID): void;
540
- has(target: TreeID): boolean;
541
- /**
542
- * Get LoroTreeNode by the TreeID.
543
- */
544
- getNodeByID(target: TreeID): LoroTreeNode<T>;
545
- subscribe(listener: Listener): Subscription;
546
- }
547
- interface LoroTreeNode<T extends Record<string, unknown> = Record<string, unknown>> {
548
- /**
549
- * Get the associated metadata map container of a tree node.
550
- */
551
- readonly data: LoroMap<T>;
552
- /**
553
- * Create a new node as the child of the current node and
554
- * return an instance of `LoroTreeNode`.
555
- *
556
- * If the index is not provided, the new node will be appended to the end.
557
- *
558
- * @example
559
- * ```typescript
560
- * import { LoroDoc } from "loro-crdt";
561
- *
562
- * let doc = new LoroDoc();
563
- * let tree = doc.getTree("tree");
564
- * let root = tree.createNode();
565
- * let node = root.createNode();
566
- * let node2 = root.createNode(0);
567
- * // root
568
- * // / \
569
- * // node2 node
570
- * ```
571
- */
572
- createNode(index?: number): LoroTreeNode<T>;
573
- move(parent?: LoroTreeNode<T>, index?: number): void;
574
- parent(): LoroTreeNode<T> | undefined;
575
- /**
576
- * Get the children of this node.
577
- *
578
- * The objects returned are new js objects each time because they need to cross
579
- * the WASM boundary.
580
- */
581
- children(): Array<LoroTreeNode<T>> | undefined;
582
- }
583
- interface AwarenessWasm<T extends Value = Value> {
584
- getState(peer: PeerID): T | undefined;
585
- getTimestamp(peer: PeerID): number | undefined;
586
- getAllStates(): Record<PeerID, T>;
587
- setLocalState(value: T): void;
588
- removeOutdated(): PeerID[];
589
- }
590
- }
591
- type NonNullableType<T> = Exclude<T, null | undefined>;
592
- declare function newContainerID(id: OpId, type: ContainerType): ContainerID;
593
- declare function newRootContainerID(name: string, type: ContainerType): ContainerID;
594
-
595
- export { Awareness, CounterDiff, Diff, Frontiers, ListDiff, Loro, LoroEvent, LoroEventBatch, MapDiff, Path, Subscription, TextDiff, TreeDiff, TreeDiffItem, getType, isContainer, isContainerId, newContainerID, newRootContainerID };