@liveblocks/client 0.16.15 → 0.17.0-test1

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.
Files changed (10) hide show
  1. package/index.d.ts +26 -7
  2. package/index.js +1230 -830
  3. package/index.mjs +1030 -782
  4. package/internal.d.ts +271 -251
  5. package/internal.js +314 -168
  6. package/internal.mjs +265 -130
  7. package/package.json +15 -10
  8. package/shared.d.ts +973 -628
  9. package/shared.js +2568 -1331
  10. package/shared.mjs +1989 -1210
package/shared.d.ts CHANGED
@@ -1,3 +1,251 @@
1
+ declare type ApplyResult =
2
+ | {
3
+ reverse: Op[];
4
+ modified: StorageUpdate;
5
+ }
6
+ | {
7
+ modified: false;
8
+ };
9
+ interface Doc {
10
+ roomId: string;
11
+ generateId: () => string;
12
+ generateOpId: () => string;
13
+ getItem: (id: string) => LiveNode | undefined;
14
+ addItem: (id: string, liveItem: LiveNode) => void;
15
+ deleteItem: (id: string) => void;
16
+ /**
17
+ * Dispatching has three responsibilities:
18
+ * - Sends serialized ops to the WebSocket servers
19
+ * - Add reverse operations to the undo/redo stack
20
+ * - Notify room subscribers with updates (in-client, no networking)
21
+ */
22
+ dispatch: (
23
+ ops: Op[],
24
+ reverseOps: Op[],
25
+ storageUpdates: Map<string, StorageUpdate>
26
+ ) => void;
27
+ }
28
+ declare abstract class AbstractCrdt {
29
+ private __doc?;
30
+ private __id?;
31
+ private _parent;
32
+ get roomId(): string | null;
33
+ }
34
+
35
+ /**
36
+ * The LiveList class represents an ordered collection of items that is synchronized across clients.
37
+ */
38
+ declare class LiveList<TItem extends Lson> extends AbstractCrdt {
39
+ private _items;
40
+ private _implicitlyDeletedItems;
41
+ private _unacknowledgedSets;
42
+ constructor(items?: TItem[]);
43
+ /**
44
+ * Returns the number of elements.
45
+ */
46
+ get length(): number;
47
+ /**
48
+ * Adds one element to the end of the LiveList.
49
+ * @param element The element to add to the end of the LiveList.
50
+ */
51
+ push(element: TItem): void;
52
+ /**
53
+ * Inserts one element at a specified index.
54
+ * @param element The element to insert.
55
+ * @param index The index at which you want to insert the element.
56
+ */
57
+ insert(element: TItem, index: number): void;
58
+ /**
59
+ * Move one element from one index to another.
60
+ * @param index The index of the element to move
61
+ * @param targetIndex The index where the element should be after moving.
62
+ */
63
+ move(index: number, targetIndex: number): void;
64
+ /**
65
+ * Deletes an element at the specified index
66
+ * @param index The index of the element to delete
67
+ */
68
+ delete(index: number): void;
69
+ clear(): void;
70
+ set(index: number, item: TItem): void;
71
+ /**
72
+ * Returns an Array of all the elements in the LiveList.
73
+ */
74
+ toArray(): TItem[];
75
+ /**
76
+ * Tests whether all elements pass the test implemented by the provided function.
77
+ * @param predicate Function to test for each element, taking two arguments (the element and its index).
78
+ * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
79
+ */
80
+ every(predicate: (value: TItem, index: number) => unknown): boolean;
81
+ /**
82
+ * Creates an array with all elements that pass the test implemented by the provided function.
83
+ * @param predicate Function to test each element of the LiveList. Return a value that coerces to true to keep the element, or to false otherwise.
84
+ * @returns An array with the elements that pass the test.
85
+ */
86
+ filter(predicate: (value: TItem, index: number) => unknown): TItem[];
87
+ /**
88
+ * Returns the first element that satisfies the provided testing function.
89
+ * @param predicate Function to execute on each value.
90
+ * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
91
+ */
92
+ find(predicate: (value: TItem, index: number) => unknown): TItem | undefined;
93
+ /**
94
+ * Returns the index of the first element in the LiveList that satisfies the provided testing function.
95
+ * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
96
+ * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
97
+ */
98
+ findIndex(predicate: (value: TItem, index: number) => unknown): number;
99
+ /**
100
+ * Executes a provided function once for each element.
101
+ * @param callbackfn Function to execute on each element.
102
+ */
103
+ forEach(callbackfn: (value: TItem, index: number) => void): void;
104
+ /**
105
+ * Get the element at the specified index.
106
+ * @param index The index on the element to get.
107
+ * @returns The element at the specified index or undefined.
108
+ */
109
+ get(index: number): TItem | undefined;
110
+ /**
111
+ * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
112
+ * @param searchElement Element to locate.
113
+ * @param fromIndex The index to start the search at.
114
+ * @returns The first index of the element in the LiveList; -1 if not found.
115
+ */
116
+ indexOf(searchElement: TItem, fromIndex?: number): number;
117
+ /**
118
+ * Returns the last index at which a given element can be found in the LiveList, or -1 if it is not present. The LiveLsit is searched backwards, starting at fromIndex.
119
+ * @param searchElement Element to locate.
120
+ * @param fromIndex The index at which to start searching backwards.
121
+ * @returns
122
+ */
123
+ lastIndexOf(searchElement: TItem, fromIndex?: number): number;
124
+ /**
125
+ * Creates an array populated with the results of calling a provided function on every element.
126
+ * @param callback Function that is called for every element.
127
+ * @returns An array with each element being the result of the callback function.
128
+ */
129
+ map<U>(callback: (value: TItem, index: number) => U): U[];
130
+ /**
131
+ * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
132
+ * @param predicate Function to test for each element.
133
+ * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
134
+ */
135
+ some(predicate: (value: TItem, index: number) => unknown): boolean;
136
+ [Symbol.iterator](): IterableIterator<TItem>;
137
+ }
138
+
139
+ /**
140
+ * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
141
+ * Keys should be a string, and values should be serializable to JSON.
142
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
143
+ */
144
+ declare class LiveMap<
145
+ TKey extends string,
146
+ TValue extends Lson
147
+ > extends AbstractCrdt {
148
+ private _map;
149
+ constructor(entries?: readonly (readonly [TKey, TValue])[] | undefined);
150
+ /**
151
+ * @deprecated Please call as `new LiveMap()` or `new LiveMap([])` instead.
152
+ */
153
+ constructor(entries: null);
154
+ /**
155
+ * Returns a specified element from the LiveMap.
156
+ * @param key The key of the element to return.
157
+ * @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
158
+ */
159
+ get(key: TKey): TValue | undefined;
160
+ /**
161
+ * Adds or updates an element with a specified key and a value.
162
+ * @param key The key of the element to add. Should be a string.
163
+ * @param value The value of the element to add. Should be serializable to JSON.
164
+ */
165
+ set(key: TKey, value: TValue): void;
166
+ /**
167
+ * Returns the number of elements in the LiveMap.
168
+ */
169
+ get size(): number;
170
+ /**
171
+ * Returns a boolean indicating whether an element with the specified key exists or not.
172
+ * @param key The key of the element to test for presence.
173
+ */
174
+ has(key: TKey): boolean;
175
+ /**
176
+ * Removes the specified element by key.
177
+ * @param key The key of the element to remove.
178
+ * @returns true if an element existed and has been removed, or false if the element does not exist.
179
+ */
180
+ delete(key: TKey): boolean;
181
+ /**
182
+ * Returns a new Iterator object that contains the [key, value] pairs for each element.
183
+ */
184
+ entries(): IterableIterator<[TKey, TValue]>;
185
+ /**
186
+ * Same function object as the initial value of the entries method.
187
+ */
188
+ [Symbol.iterator](): IterableIterator<[TKey, TValue]>;
189
+ /**
190
+ * Returns a new Iterator object that contains the keys for each element.
191
+ */
192
+ keys(): IterableIterator<TKey>;
193
+ /**
194
+ * Returns a new Iterator object that contains the values for each element.
195
+ */
196
+ values(): IterableIterator<TValue>;
197
+ /**
198
+ * Executes a provided function once per each key/value pair in the Map object, in insertion order.
199
+ * @param callback Function to execute for each entry in the map.
200
+ */
201
+ forEach(
202
+ callback: (value: TValue, key: TKey, map: LiveMap<TKey, TValue>) => void
203
+ ): void;
204
+ }
205
+
206
+ /**
207
+ * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
208
+ * Keys should be a string, and values should be serializable to JSON.
209
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
210
+ */
211
+ declare class LiveObject<O extends LsonObject> extends AbstractCrdt {
212
+ private _map;
213
+ private _propToLastUpdate;
214
+ constructor(obj?: O);
215
+ _serialize(
216
+ parentId?: undefined,
217
+ parentKey?: undefined,
218
+ doc?: Doc
219
+ ): CreateOp[];
220
+ private _applyUpdate;
221
+ private _applyDeleteObjectKey;
222
+ /**
223
+ * Transform the LiveObject into a javascript object
224
+ */
225
+ toObject(): O;
226
+ /**
227
+ * Adds or updates a property with a specified key and a value.
228
+ * @param key The key of the property to add
229
+ * @param value The value of the property to add
230
+ */
231
+ set<TKey extends keyof O>(key: TKey, value: O[TKey]): void;
232
+ /**
233
+ * Returns a specified property from the LiveObject.
234
+ * @param key The key of the property to get
235
+ */
236
+ get<TKey extends keyof O>(key: TKey): O[TKey];
237
+ /**
238
+ * Deletes a key from the LiveObject
239
+ * @param key The key of the property to delete
240
+ */
241
+ delete(key: keyof O): void;
242
+ /**
243
+ * Adds or updates multiple properties at once with an object.
244
+ * @param overrides The object used to overrides properties
245
+ */
246
+ update(overrides: Partial<O>): void;
247
+ }
248
+
1
249
  /**
2
250
  * Represents an indefinitely deep arbitrary JSON data structure. There are
3
251
  * four types that make up the Json family:
@@ -12,188 +260,223 @@ declare type Json = JsonScalar | JsonArray | JsonObject;
12
260
  declare type JsonScalar = string | number | boolean | null;
13
261
  declare type JsonArray = Json[];
14
262
  declare type JsonObject = {
15
- [key: string]: Json | undefined;
263
+ [key: string]: Json | undefined;
16
264
  };
17
265
  declare function isJsonScalar(data: Json): data is JsonScalar;
18
266
  declare function isJsonArray(data: Json): data is JsonArray;
19
267
  declare function isJsonObject(data: Json): data is JsonObject;
20
268
 
21
269
  /**
22
- * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
23
- * Keys should be a string, and values should be serializable to JSON.
24
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
270
+ * INTERNAL
25
271
  */
26
- declare class LiveMap<TKey extends string = string, TValue extends Lson = Lson> extends AbstractCrdt {
27
- private _map;
28
- constructor(entries?: readonly (readonly [TKey, TValue])[] | undefined);
29
- /**
30
- * @deprecated Please call as `new LiveMap()` or `new LiveMap([])` instead.
31
- */
32
- constructor(entries: null);
33
- /**
34
- * Returns a specified element from the LiveMap.
35
- * @param key The key of the element to return.
36
- * @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
37
- */
38
- get(key: TKey): TValue | undefined;
39
- /**
40
- * Adds or updates an element with a specified key and a value.
41
- * @param key The key of the element to add. Should be a string.
42
- * @param value The value of the element to add. Should be serializable to JSON.
43
- */
44
- set(key: TKey, value: TValue): void;
45
- /**
46
- * Returns the number of elements in the LiveMap.
47
- */
48
- get size(): number;
49
- /**
50
- * Returns a boolean indicating whether an element with the specified key exists or not.
51
- * @param key The key of the element to test for presence.
52
- */
53
- has(key: TKey): boolean;
54
- /**
55
- * Removes the specified element by key.
56
- * @param key The key of the element to remove.
57
- * @returns true if an element existed and has been removed, or false if the element does not exist.
58
- */
59
- delete(key: TKey): boolean;
60
- /**
61
- * Returns a new Iterator object that contains the [key, value] pairs for each element.
62
- */
63
- entries(): IterableIterator<[TKey, TValue]>;
64
- /**
65
- * Same function object as the initial value of the entries method.
66
- */
67
- [Symbol.iterator](): IterableIterator<[TKey, TValue]>;
68
- /**
69
- * Returns a new Iterator object that contains the keys for each element.
70
- */
71
- keys(): IterableIterator<TKey>;
72
- /**
73
- * Returns a new Iterator object that contains the values for each element.
74
- */
75
- values(): IterableIterator<TValue>;
76
- /**
77
- * Executes a provided function once per each key/value pair in the Map object, in insertion order.
78
- * @param callback Function to execute for each entry in the map.
79
- */
80
- forEach(callback: (value: TValue, key: TKey, map: LiveMap<TKey, TValue>) => void): void;
272
+ declare class LiveRegister<TValue extends Json> extends AbstractCrdt {
273
+ _data: TValue;
274
+ constructor(data: TValue);
275
+ get data(): TValue;
276
+ /**
277
+ * INTERNAL
278
+ */
279
+ static _deserialize(
280
+ [id, item]: IdTuple<SerializedRegister>,
281
+ _parentToChildren: ParentToChildNodeMap,
282
+ doc: Doc
283
+ ): LiveRegister<Json>;
284
+ /**
285
+ * INTERNAL
286
+ */
287
+ _serialize(
288
+ parentId: string,
289
+ parentKey: string,
290
+ doc?: Doc
291
+ ): CreateRegisterOp[];
292
+ /**
293
+ * INTERNAL
294
+ */
295
+ _toSerializedCrdt(): SerializedRegister;
296
+ _attachChild(_op: CreateChildOp): ApplyResult;
297
+ _detachChild(_crdt: LiveNode): ApplyResult;
298
+ _apply(op: Op, isLocal: boolean): ApplyResult;
81
299
  }
82
300
 
301
+ declare type LiveStructure =
302
+ | LiveObject<LsonObject>
303
+ | LiveList<Lson>
304
+ | LiveMap<string, Lson>;
83
305
  /**
84
306
  * Think of Lson as a sibling of the Json data tree, except that the nested
85
307
  * data structure can contain a mix of Json values and LiveStructure instances.
86
308
  */
87
- declare type Lson = Json | LiveObject<LsonObject> | LiveList<Lson> | LiveMap<string, Lson>;
309
+ declare type Lson = Json | LiveStructure;
310
+ /**
311
+ * LiveNode is the internal tree for managing Live data structures. The key
312
+ * difference with Lson is that all the Json values get represented in
313
+ * a LiveRegister node.
314
+ */
315
+ declare type LiveNode = LiveStructure | LiveRegister<Json>;
88
316
  /**
89
317
  * A mapping of keys to Lson values. A Lson value is any valid JSON
90
318
  * value or a Live storage data structure (LiveMap, LiveList, etc.)
91
319
  */
92
320
  declare type LsonObject = {
93
- [key: string]: Lson | undefined;
321
+ [key: string]: Lson | undefined;
94
322
  };
95
323
 
324
+ declare enum OpCode {
325
+ INIT = 0,
326
+ SET_PARENT_KEY = 1,
327
+ CREATE_LIST = 2,
328
+ UPDATE_OBJECT = 3,
329
+ CREATE_OBJECT = 4,
330
+ DELETE_CRDT = 5,
331
+ DELETE_OBJECT_KEY = 6,
332
+ CREATE_MAP = 7,
333
+ CREATE_REGISTER = 8,
334
+ }
96
335
  /**
97
- * The LiveList class represents an ordered collection of items that is synchronized across clients.
336
+ * These operations are the payload for {@link UpdateStorageServerMsg} messages
337
+ * only.
98
338
  */
99
- declare class LiveList<TItem extends Lson = Lson> extends AbstractCrdt {
100
- private _items;
101
- constructor(items?: TItem[]);
102
- /**
103
- * Returns the number of elements.
104
- */
105
- get length(): number;
106
- /**
107
- * Adds one element to the end of the LiveList.
108
- * @param element The element to add to the end of the LiveList.
109
- */
110
- push(element: TItem): void;
111
- /**
112
- * Inserts one element at a specified index.
113
- * @param element The element to insert.
114
- * @param index The index at which you want to insert the element.
115
- */
116
- insert(element: TItem, index: number): void;
117
- /**
118
- * Move one element from one index to another.
119
- * @param index The index of the element to move
120
- * @param targetIndex The index where the element should be after moving.
121
- */
122
- move(index: number, targetIndex: number): void;
123
- /**
124
- * Deletes an element at the specified index
125
- * @param index The index of the element to delete
126
- */
127
- delete(index: number): void;
128
- clear(): void;
129
- set(index: number, item: TItem): void;
130
- /**
131
- * Returns an Array of all the elements in the LiveList.
132
- */
133
- toArray(): TItem[];
134
- /**
135
- * Tests whether all elements pass the test implemented by the provided function.
136
- * @param predicate Function to test for each element, taking two arguments (the element and its index).
137
- * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
138
- */
139
- every(predicate: (value: TItem, index: number) => unknown): boolean;
140
- /**
141
- * Creates an array with all elements that pass the test implemented by the provided function.
142
- * @param predicate Function to test each element of the LiveList. Return a value that coerces to true to keep the element, or to false otherwise.
143
- * @returns An array with the elements that pass the test.
144
- */
145
- filter(predicate: (value: TItem, index: number) => unknown): TItem[];
146
- /**
147
- * Returns the first element that satisfies the provided testing function.
148
- * @param predicate Function to execute on each value.
149
- * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
150
- */
151
- find(predicate: (value: TItem, index: number) => unknown): TItem | undefined;
152
- /**
153
- * Returns the index of the first element in the LiveList that satisfies the provided testing function.
154
- * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
155
- * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
156
- */
157
- findIndex(predicate: (value: TItem, index: number) => unknown): number;
158
- /**
159
- * Executes a provided function once for each element.
160
- * @param callbackfn Function to execute on each element.
161
- */
162
- forEach(callbackfn: (value: TItem, index: number) => void): void;
163
- /**
164
- * Get the element at the specified index.
165
- * @param index The index on the element to get.
166
- * @returns The element at the specified index or undefined.
167
- */
168
- get(index: number): TItem | undefined;
169
- /**
170
- * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
171
- * @param searchElement Element to locate.
172
- * @param fromIndex The index to start the search at.
173
- * @returns The first index of the element in the LiveList; -1 if not found.
174
- */
175
- indexOf(searchElement: TItem, fromIndex?: number): number;
176
- /**
177
- * Returns the last index at which a given element can be found in the LiveList, or -1 if it is not present. The LiveLsit is searched backwards, starting at fromIndex.
178
- * @param searchElement Element to locate.
179
- * @param fromIndex The index at which to start searching backwards.
180
- * @returns
181
- */
182
- lastIndexOf(searchElement: TItem, fromIndex?: number): number;
183
- /**
184
- * Creates an array populated with the results of calling a provided function on every element.
185
- * @param callback Function that is called for every element.
186
- * @returns An array with each element being the result of the callback function.
187
- */
188
- map<U>(callback: (value: TItem, index: number) => U): U[];
189
- /**
190
- * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
191
- * @param predicate Function to test for each element.
192
- * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
193
- */
194
- some(predicate: (value: TItem, index: number) => unknown): boolean;
195
- [Symbol.iterator](): IterableIterator<TItem>;
339
+ declare type Op =
340
+ | CreateOp
341
+ | UpdateObjectOp
342
+ | DeleteCrdtOp
343
+ | SetParentKeyOp
344
+ | DeleteObjectKeyOp;
345
+ declare type CreateOp = CreateRootObjectOp | CreateChildOp;
346
+ declare type CreateChildOp =
347
+ | CreateObjectOp
348
+ | CreateRegisterOp
349
+ | CreateMapOp
350
+ | CreateListOp;
351
+ declare type UpdateObjectOp = {
352
+ opId?: string;
353
+ id: string;
354
+ type: OpCode.UPDATE_OBJECT;
355
+ data: Partial<JsonObject>;
356
+ };
357
+ declare type CreateObjectOp = {
358
+ opId?: string;
359
+ id: string;
360
+ intent?: "set";
361
+ deletedId?: string;
362
+ type: OpCode.CREATE_OBJECT;
363
+ parentId: string;
364
+ parentKey: string;
365
+ data: JsonObject;
366
+ };
367
+ declare type CreateRootObjectOp = {
368
+ opId?: string;
369
+ id: string;
370
+ type: OpCode.CREATE_OBJECT;
371
+ data: JsonObject;
372
+ parentId?: never;
373
+ parentKey?: never;
374
+ };
375
+ declare type CreateListOp = {
376
+ opId?: string;
377
+ id: string;
378
+ intent?: "set";
379
+ deletedId?: string;
380
+ type: OpCode.CREATE_LIST;
381
+ parentId: string;
382
+ parentKey: string;
383
+ };
384
+ declare type CreateMapOp = {
385
+ opId?: string;
386
+ id: string;
387
+ intent?: "set";
388
+ deletedId?: string;
389
+ type: OpCode.CREATE_MAP;
390
+ parentId: string;
391
+ parentKey: string;
392
+ };
393
+ declare type CreateRegisterOp = {
394
+ opId?: string;
395
+ id: string;
396
+ intent?: "set";
397
+ deletedId?: string;
398
+ type: OpCode.CREATE_REGISTER;
399
+ parentId: string;
400
+ parentKey: string;
401
+ data: Json;
402
+ };
403
+ declare type DeleteCrdtOp = {
404
+ opId?: string;
405
+ id: string;
406
+ type: OpCode.DELETE_CRDT;
407
+ };
408
+ declare type SetParentKeyOp = {
409
+ opId?: string;
410
+ id: string;
411
+ type: OpCode.SET_PARENT_KEY;
412
+ parentKey: string;
413
+ };
414
+ declare type DeleteObjectKeyOp = {
415
+ opId?: string;
416
+ id: string;
417
+ type: OpCode.DELETE_OBJECT_KEY;
418
+ key: string;
419
+ };
420
+
421
+ declare type IdTuple<T> = [id: string, value: T];
422
+ declare enum CrdtType {
423
+ OBJECT = 0,
424
+ LIST = 1,
425
+ MAP = 2,
426
+ REGISTER = 3,
196
427
  }
428
+ declare type SerializedCrdt = SerializedRootObject | SerializedChild;
429
+ declare type SerializedChild =
430
+ | SerializedObject
431
+ | SerializedList
432
+ | SerializedMap
433
+ | SerializedRegister;
434
+ declare type SerializedRootObject = {
435
+ type: CrdtType.OBJECT;
436
+ data: JsonObject;
437
+ parentId?: never;
438
+ parentKey?: never;
439
+ };
440
+ declare type SerializedObject = {
441
+ type: CrdtType.OBJECT;
442
+ parentId: string;
443
+ parentKey: string;
444
+ data: JsonObject;
445
+ };
446
+ declare type SerializedList = {
447
+ type: CrdtType.LIST;
448
+ parentId: string;
449
+ parentKey: string;
450
+ };
451
+ declare type SerializedMap = {
452
+ type: CrdtType.MAP;
453
+ parentId: string;
454
+ parentKey: string;
455
+ };
456
+ declare type SerializedRegister = {
457
+ type: CrdtType.REGISTER;
458
+ parentId: string;
459
+ parentKey: string;
460
+ data: Json;
461
+ };
462
+ declare function isRootCrdt(crdt: SerializedCrdt): crdt is SerializedRootObject;
463
+ declare function isChildCrdt(crdt: SerializedCrdt): crdt is SerializedChild;
464
+
465
+ /**
466
+ * Lookup table for nodes (= SerializedCrdt values) by their IDs.
467
+ */
468
+ declare type NodeMap = Map<
469
+ string, // Node ID
470
+ SerializedCrdt
471
+ >;
472
+ /**
473
+ * Reverse lookup table for all child nodes (= list of SerializedCrdt values)
474
+ * by their parent node's IDs.
475
+ */
476
+ declare type ParentToChildNodeMap = Map<
477
+ string, // Parent's node ID
478
+ IdTuple<SerializedChild>[]
479
+ >;
197
480
 
198
481
  /**
199
482
  * This helper type is effectively a no-op, but will force TypeScript to
@@ -217,173 +500,217 @@ declare class LiveList<TItem extends Lson = Lson> extends AbstractCrdt {
217
500
  * This trick comes from:
218
501
  * https://effectivetypescript.com/2022/02/25/gentips-4-display/
219
502
  */
220
- declare type Resolve<T> = T extends Function ? T : {
221
- [K in keyof T]: T[K];
222
- };
223
- declare type MyPresenceCallback<T extends Presence = Presence> = (me: T) => void;
224
- declare type OthersEventCallback<T extends Presence = Presence> = (others: Others<T>, event: OthersEvent<T>) => void;
225
- declare type EventCallback = ({ connectionId, event, }: {
226
- connectionId: number;
227
- event: Json;
503
+ declare type Resolve<T> = T extends (...args: unknown[]) => unknown
504
+ ? T
505
+ : {
506
+ [K in keyof T]: T[K];
507
+ };
508
+ declare type MyPresenceCallback<TPresence extends JsonObject> = (
509
+ me: TPresence
510
+ ) => void;
511
+ declare type OthersEventCallback<TPresence extends JsonObject> = (
512
+ others: Others<TPresence>,
513
+ event: OthersEvent<TPresence>
514
+ ) => void;
515
+ declare type EventCallback = ({
516
+ connectionId,
517
+ event,
518
+ }: {
519
+ connectionId: number;
520
+ event: Json;
228
521
  }) => void;
229
522
  declare type ErrorCallback = (error: Error) => void;
230
523
  declare type ConnectionCallback = (state: ConnectionState) => void;
231
- declare type UpdateDelta = {
232
- type: "update";
233
- } | {
234
- type: "delete";
235
- };
524
+ declare type UpdateDelta =
525
+ | {
526
+ type: "update";
527
+ }
528
+ | {
529
+ type: "delete";
530
+ };
236
531
  /**
237
532
  * A LiveMap notification that is sent in-client to any subscribers whenever
238
533
  * one or more of the values inside the LiveMap instance have changed.
239
534
  */
240
535
  declare type LiveMapUpdates<TKey extends string, TValue extends Lson> = {
241
- type: "LiveMap";
242
- node: LiveMap<TKey, TValue>;
243
- updates: {
244
- [key: string]: UpdateDelta;
245
- };
536
+ type: "LiveMap";
537
+ node: LiveMap<TKey, TValue>;
538
+ updates: {
539
+ [key: string]: UpdateDelta;
540
+ };
246
541
  };
247
- declare type LiveObjectUpdateDelta<O extends {
542
+ declare type LiveObjectUpdateDelta<
543
+ O extends {
248
544
  [key: string]: unknown;
249
- }> = {
250
- [K in keyof O]?: UpdateDelta | undefined;
545
+ }
546
+ > = {
547
+ [K in keyof O]?: UpdateDelta | undefined;
251
548
  };
252
549
  /**
253
550
  * A LiveObject notification that is sent in-client to any subscribers whenever
254
551
  * one or more of the entries inside the LiveObject instance have changed.
255
552
  */
256
553
  declare type LiveObjectUpdates<TData extends LsonObject> = {
257
- type: "LiveObject";
258
- node: LiveObject<TData>;
259
- updates: LiveObjectUpdateDelta<TData>;
260
- };
261
- declare type LiveListUpdateDelta = {
262
- index: number;
263
- item: any;
264
- type: "insert";
265
- } | {
266
- index: number;
267
- type: "delete";
268
- } | {
269
- index: number;
270
- previousIndex: number;
271
- item: any;
272
- type: "move";
273
- } | {
274
- index: number;
275
- item: any;
276
- type: "set";
554
+ type: "LiveObject";
555
+ node: LiveObject<TData>;
556
+ updates: LiveObjectUpdateDelta<TData>;
277
557
  };
558
+ declare type LiveListUpdateDelta =
559
+ | {
560
+ index: number;
561
+ item: Lson;
562
+ type: "insert";
563
+ }
564
+ | {
565
+ index: number;
566
+ type: "delete";
567
+ }
568
+ | {
569
+ index: number;
570
+ previousIndex: number;
571
+ item: Lson;
572
+ type: "move";
573
+ }
574
+ | {
575
+ index: number;
576
+ item: Lson;
577
+ type: "set";
578
+ };
278
579
  /**
279
580
  * A LiveList notification that is sent in-client to any subscribers whenever
280
581
  * one or more of the items inside the LiveList instance have changed.
281
582
  */
282
583
  declare type LiveListUpdates<TItem extends Lson> = {
283
- type: "LiveList";
284
- node: LiveList<TItem>;
285
- updates: LiveListUpdateDelta[];
584
+ type: "LiveList";
585
+ node: LiveList<TItem>;
586
+ updates: LiveListUpdateDelta[];
286
587
  };
287
588
  declare type BroadcastOptions = {
288
- /**
289
- * Whether or not event is queued if the connection is currently closed.
290
- *
291
- * ❗ We are not sure if we want to support this option in the future so it might be deprecated to be replaced by something else
292
- */
293
- shouldQueueEventIfNotReady: boolean;
589
+ /**
590
+ * Whether or not event is queued if the connection is currently closed.
591
+ *
592
+ * ❗ We are not sure if we want to support this option in the future so it might be deprecated to be replaced by something else
593
+ */
594
+ shouldQueueEventIfNotReady: boolean;
294
595
  };
295
596
  /**
296
597
  * The payload of notifications sent (in-client) when LiveStructures change.
297
598
  * Messages of this kind are not originating from the network, but are 100%
298
599
  * in-client.
299
600
  */
300
- declare type StorageUpdate = LiveMapUpdates<string, Lson> | LiveObjectUpdates<LsonObject> | LiveListUpdates<Lson>;
301
- declare type RoomInitializers<TPresence, TStorage> = Resolve<{
302
- /**
303
- * The initial Presence to use and announce when you enter the Room. The
304
- * Presence is available on all users in the Room (me & others).
305
- */
306
- initialPresence?: TPresence | ((roomId: string) => TPresence);
307
- /**
308
- * The initial Storage to use when entering a new Room.
309
- */
310
- initialStorage?: TStorage | ((roomId: string) => TStorage);
311
- /**
312
- * @deprecated Please use `initialPresence` instead. This property is
313
- * scheduled for removal in 0.18.
314
- */
315
- defaultPresence?: () => TPresence;
316
- /**
317
- * @deprecated Please use `initialStorage` instead. This property is
318
- * scheduled for removal in 0.18.
319
- */
320
- defaultStorageRoot?: TStorage;
601
+ declare type StorageUpdate =
602
+ | LiveMapUpdates<string, Lson>
603
+ | LiveObjectUpdates<LsonObject>
604
+ | LiveListUpdates<Lson>;
605
+ declare type RoomInitializers<
606
+ TPresence extends JsonObject,
607
+ TStorage extends LsonObject
608
+ > = Resolve<{
609
+ /**
610
+ * The initial Presence to use and announce when you enter the Room. The
611
+ * Presence is available on all users in the Room (me & others).
612
+ */
613
+ initialPresence?: TPresence | ((roomId: string) => TPresence);
614
+ /**
615
+ * The initial Storage to use when entering a new Room.
616
+ */
617
+ initialStorage?: TStorage | ((roomId: string) => TStorage);
618
+ /**
619
+ * @deprecated Please use `initialPresence` instead. This property is
620
+ * scheduled for removal in 0.18.
621
+ */
622
+ defaultPresence?: () => TPresence;
623
+ /**
624
+ * @deprecated Please use `initialStorage` instead. This property is
625
+ * scheduled for removal in 0.18.
626
+ */
627
+ defaultStorageRoot?: TStorage;
321
628
  }>;
322
629
  declare type Client = {
323
- /**
324
- * Gets a room. Returns null if {@link Client.enter} has not been called previously.
325
- *
326
- * @param roomId The id of the room
327
- */
328
- getRoom(roomId: string): Room | null;
329
- /**
330
- * Enters a room and returns it.
331
- * @param roomId The id of the room
332
- * @param options Optional. You can provide initializers for the Presence or Storage when entering the Room.
333
- */
334
- enter<TStorage extends Record<string, any> = Record<string, any>>(roomId: string, options?: RoomInitializers<Presence, TStorage>): Room;
335
- /**
336
- * Leaves a room.
337
- * @param roomId The id of the room
338
- */
339
- leave(roomId: string): void;
630
+ /**
631
+ * Gets a room. Returns null if {@link Client.enter} has not been called previously.
632
+ *
633
+ * @param roomId The id of the room
634
+ */
635
+ getRoom<TPresence extends JsonObject, TStorage extends LsonObject>(
636
+ roomId: string
637
+ ): Room<TPresence, TStorage> | null;
638
+ /**
639
+ * Enters a room and returns it.
640
+ * @param roomId The id of the room
641
+ * @param options Optional. You can provide initializers for the Presence or Storage when entering the Room.
642
+ */
643
+ enter<TPresence extends JsonObject, TStorage extends LsonObject>(
644
+ roomId: string,
645
+ options?: RoomInitializers<TPresence, TStorage>
646
+ ): Room<TPresence, TStorage>;
647
+ /**
648
+ * Leaves a room.
649
+ * @param roomId The id of the room
650
+ */
651
+ leave(roomId: string): void;
340
652
  };
341
653
  /**
342
654
  * Represents all the other users connected in the room. Treated as immutable.
343
655
  */
344
- interface Others<TPresence extends Presence = Presence> {
345
- /**
346
- * Number of other users in the room.
347
- */
348
- readonly count: number;
349
- /**
350
- * Returns a new Iterator object that contains the users.
351
- */
352
- [Symbol.iterator](): IterableIterator<User<TPresence>>;
353
- /**
354
- * Returns the array of connected users in room.
355
- */
356
- toArray(): User<TPresence>[];
357
- /**
358
- * This function let you map over the connected users in the room.
359
- */
360
- map<U>(callback: (user: User<TPresence>) => U): U[];
656
+ interface Others<TPresence extends JsonObject> {
657
+ /**
658
+ * Number of other users in the room.
659
+ */
660
+ readonly count: number;
661
+ /**
662
+ * Returns a new Iterator object that contains the users.
663
+ */
664
+ [Symbol.iterator](): IterableIterator<User<TPresence>>;
665
+ /**
666
+ * Returns the array of connected users in room.
667
+ */
668
+ toArray(): User<TPresence>[];
669
+ /**
670
+ * This function let you map over the connected users in the room.
671
+ */
672
+ map<U>(callback: (user: User<TPresence>) => U): U[];
361
673
  }
362
674
  /**
363
675
  * Represents a user connected in a room. Treated as immutable.
364
676
  */
365
- declare type User<TPresence extends Presence = Presence> = {
366
- /**
367
- * The connection id of the user. It is unique and increment at every new connection.
368
- */
369
- readonly connectionId: number;
370
- /**
371
- * The id of the user that has been set in the authentication endpoint.
372
- * Useful to get additional information about the connected user.
373
- */
374
- readonly id?: string;
375
- /**
376
- * Additional user information that has been set in the authentication endpoint.
377
- */
378
- readonly info?: any;
379
- /**
380
- * The user presence.
381
- */
382
- readonly presence?: TPresence;
677
+ declare type User<TPresence extends JsonObject> = {
678
+ /**
679
+ * The connection id of the user. It is unique and increment at every new connection.
680
+ */
681
+ readonly connectionId: number;
682
+ /**
683
+ * The id of the user that has been set in the authentication endpoint.
684
+ * Useful to get additional information about the connected user.
685
+ */
686
+ readonly id?: string;
687
+ /**
688
+ * Additional user information that has been set in the authentication endpoint.
689
+ */
690
+ readonly info?: any;
691
+ /**
692
+ * The user presence.
693
+ */
694
+ readonly presence?: TPresence;
383
695
  };
384
- declare type Presence = Record<string, unknown>;
696
+ /**
697
+ * @deprecated Whatever you want to store as presence is app-specific. Please
698
+ * define your own Presence type instead of importing it from
699
+ * `@liveblocks/client`, for example:
700
+ *
701
+ * type Presence = {
702
+ * name: string,
703
+ * cursor: {
704
+ * x: number,
705
+ * y: number,
706
+ * } | null,
707
+ * }
708
+ *
709
+ * As long as it only contains JSON-serializable values, you're good!
710
+ */
711
+ declare type Presence = JsonObject;
385
712
  declare type AuthEndpointCallback = (room: string) => Promise<{
386
- token: string;
713
+ token: string;
387
714
  }>;
388
715
  declare type AuthEndpoint = string | AuthEndpointCallback;
389
716
  /**
@@ -391,396 +718,414 @@ declare type AuthEndpoint = string | AuthEndpointCallback;
391
718
  * Can be an url or a callback if you need to add additional headers.
392
719
  */
393
720
  declare type ClientOptions = {
394
- throttle?: number;
395
- fetchPolyfill?: any;
396
- WebSocketPolyfill?: any;
397
- } & ({
398
- publicApiKey: string;
399
- authEndpoint?: never;
400
- } | {
401
- publicApiKey?: never;
402
- authEndpoint: AuthEndpoint;
403
- });
404
- declare type Connection = {
405
- state: "closed" | "authenticating" | "unavailable" | "failed";
406
- } | {
407
- state: "open" | "connecting";
408
- id: number;
409
- userId?: string;
410
- userInfo?: any;
411
- };
721
+ throttle?: number;
722
+ fetchPolyfill?: typeof fetch;
723
+ WebSocketPolyfill?: any;
724
+ atobPolyfill?: (data: string) => string;
725
+ } & (
726
+ | {
727
+ publicApiKey: string;
728
+ authEndpoint?: never;
729
+ }
730
+ | {
731
+ publicApiKey?: never;
732
+ authEndpoint: AuthEndpoint;
733
+ }
734
+ );
735
+ declare type Connection =
736
+ | {
737
+ state: "closed" | "authenticating" | "unavailable" | "failed";
738
+ }
739
+ | {
740
+ state: "open" | "connecting";
741
+ id: number;
742
+ userId?: string;
743
+ userInfo?: Json;
744
+ };
412
745
  declare type ConnectionState = Connection["state"];
413
- declare type OthersEvent<T extends Presence = Presence> = {
414
- type: "leave";
415
- user: User<T>;
416
- } | {
417
- type: "enter";
418
- user: User<T>;
419
- } | {
420
- type: "update";
421
- user: User<T>;
422
- updates: Partial<T>;
423
- } | {
424
- type: "reset";
425
- };
746
+ declare type OthersEvent<TPresence extends JsonObject> =
747
+ | {
748
+ type: "leave";
749
+ user: User<TPresence>;
750
+ }
751
+ | {
752
+ type: "enter";
753
+ user: User<TPresence>;
754
+ }
755
+ | {
756
+ type: "update";
757
+ user: User<TPresence>;
758
+ updates: Partial<TPresence>;
759
+ }
760
+ | {
761
+ type: "reset";
762
+ };
426
763
  interface History {
764
+ /**
765
+ * Undoes the last operation executed by the current client.
766
+ * It does not impact operations made by other clients.
767
+ *
768
+ * @example
769
+ * room.updatePresence({ selectedId: "xx" }, { addToHistory: true });
770
+ * room.updatePresence({ selectedId: "yy" }, { addToHistory: true });
771
+ * room.history.undo();
772
+ * // room.getPresence() equals { selectedId: "xx" }
773
+ */
774
+ undo: () => void;
775
+ /**
776
+ * Redoes the last operation executed by the current client.
777
+ * It does not impact operations made by other clients.
778
+ *
779
+ * @example
780
+ * room.updatePresence({ selectedId: "xx" }, { addToHistory: true });
781
+ * room.updatePresence({ selectedId: "yy" }, { addToHistory: true });
782
+ * room.history.undo();
783
+ * // room.getPresence() equals { selectedId: "xx" }
784
+ * room.history.redo();
785
+ * // room.getPresence() equals { selectedId: "yy" }
786
+ */
787
+ redo: () => void;
788
+ /**
789
+ * All future modifications made on the Room will be merged together to create a single history item until resume is called.
790
+ *
791
+ * @example
792
+ * room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
793
+ * room.history.pause();
794
+ * room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
795
+ * room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
796
+ * room.history.resume();
797
+ * room.history.undo();
798
+ * // room.getPresence() equals { cursor: { x: 0, y: 0 } }
799
+ */
800
+ pause: () => void;
801
+ /**
802
+ * Resumes history. Modifications made on the Room are not merged into a single history item anymore.
803
+ *
804
+ * @example
805
+ * room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
806
+ * room.history.pause();
807
+ * room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
808
+ * room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
809
+ * room.history.resume();
810
+ * room.history.undo();
811
+ * // room.getPresence() equals { cursor: { x: 0, y: 0 } }
812
+ */
813
+ resume: () => void;
814
+ }
815
+ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject> = {
816
+ /**
817
+ * The id of the room.
818
+ */
819
+ readonly id: string;
820
+ getConnectionState(): ConnectionState;
821
+ subscribe: {
427
822
  /**
428
- * Undoes the last operation executed by the current client.
429
- * It does not impact operations made by other clients.
823
+ * Subscribe to the current user presence updates.
430
824
  *
431
- * @example
432
- * room.updatePresence({ selectedId: "xxx" }, { addToHistory: true });
433
- * room.updatePresence({ selectedId: "yyy" }, { addToHistory: true });
434
- * room.history.undo();
435
- * // room.getPresence() equals { selectedId: "xxx" }
436
- */
437
- undo: () => void;
438
- /**
439
- * Redoes the last operation executed by the current client.
440
- * It does not impact operations made by other clients.
825
+ * @param listener the callback that is called every time the current user presence is updated with {@link Room.updatePresence}.
441
826
  *
442
827
  * @example
443
- * room.updatePresence({ selectedId: "xxx" }, { addToHistory: true });
444
- * room.updatePresence({ selectedId: "yyy" }, { addToHistory: true });
445
- * room.history.undo();
446
- * // room.getPresence() equals { selectedId: "xxx" }
447
- * room.history.redo();
448
- * // room.getPresence() equals { selectedId: "yyy" }
828
+ * room.subscribe("my-presence", (presence) => {
829
+ * // Do something
830
+ * });
449
831
  */
450
- redo: () => void;
832
+ (type: "my-presence", listener: MyPresenceCallback<TPresence>): () => void;
451
833
  /**
452
- * All future modifications made on the Room will be merged together to create a single history item until resume is called.
834
+ * Subscribe to the other users updates.
835
+ *
836
+ * @param listener the callback that is called when a user enters or leaves the room or when a user update its presence.
453
837
  *
454
838
  * @example
455
- * room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
456
- * room.history.pause();
457
- * room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
458
- * room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
459
- * room.history.resume();
460
- * room.history.undo();
461
- * // room.getPresence() equals { cursor: { x: 0, y: 0 } }
839
+ * room.subscribe("others", (others) => {
840
+ * // Do something
841
+ * });
462
842
  */
463
- pause: () => void;
843
+ (type: "others", listener: OthersEventCallback<TPresence>): () => void;
464
844
  /**
465
- * Resumes history. Modifications made on the Room are not merged into a single history item anymore.
845
+ * Subscribe to events broadcasted by {@link Room.broadcastEvent}
846
+ *
847
+ * @param listener the callback that is called when a user calls {@link Room.broadcastEvent}
466
848
  *
467
849
  * @example
468
- * room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
469
- * room.history.pause();
470
- * room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
471
- * room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
472
- * room.history.resume();
473
- * room.history.undo();
474
- * // room.getPresence() equals { cursor: { x: 0, y: 0 } }
475
- */
476
- resume: () => void;
477
- }
478
- declare type Room = {
479
- /**
480
- * The id of the room.
850
+ * room.subscribe("event", ({ event, connectionId }) => {
851
+ * // Do something
852
+ * });
481
853
  */
482
- readonly id: string;
483
- getConnectionState(): ConnectionState;
484
- subscribe: {
485
- /**
486
- * Subscribe to the current user presence updates.
487
- *
488
- * @param listener the callback that is called every time the current user presence is updated with {@link Room.updatePresence}.
489
- *
490
- * @example
491
- * room.subscribe("my-presence", (presence) => {
492
- * // Do something
493
- * });
494
- */
495
- <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): () => void;
496
- /**
497
- * Subscribe to the other users updates.
498
- *
499
- * @param listener the callback that is called when a user enters or leaves the room or when a user update its presence.
500
- *
501
- * @example
502
- * room.subscribe("others", (others) => {
503
- * // Do something
504
- * });
505
- */
506
- <T extends Presence>(type: "others", listener: OthersEventCallback<T>): () => void;
507
- /**
508
- * Subscribe to events broadcasted by {@link Room.broadcastEvent}
509
- *
510
- * @param listener the callback that is called when a user calls {@link Room.broadcastEvent}
511
- *
512
- * @example
513
- * room.subscribe("event", ({ event, connectionId }) => {
514
- * // Do something
515
- * });
516
- */
517
- (type: "event", listener: EventCallback): () => void;
518
- /**
519
- * Subscribe to errors thrown in the room.
520
- */
521
- (type: "error", listener: ErrorCallback): () => void;
522
- /**
523
- * Subscribe to connection state updates.
524
- */
525
- (type: "connection", listener: ConnectionCallback): () => void;
526
- /**
527
- * Subscribes to changes made on a {@link LiveMap}. Returns an unsubscribe function.
528
- * In a future version, we will also expose what exactly changed in the {@link LiveMap}.
529
- *
530
- * @param listener the callback this called when the {@link LiveMap} changes.
531
- *
532
- * @returns Unsubscribe function.
533
- *
534
- * @example
535
- * const liveMap = new LiveMap();
536
- * const unsubscribe = room.subscribe(liveMap, (liveMap) => { });
537
- * unsubscribe();
538
- */
539
- <TKey extends string, TValue extends Lson>(liveMap: LiveMap<TKey, TValue>, listener: (liveMap: LiveMap<TKey, TValue>) => void): () => void;
540
- /**
541
- * Subscribes to changes made on a {@link LiveObject}. Returns an unsubscribe function.
542
- * In a future version, we will also expose what exactly changed in the {@link LiveObject}.
543
- *
544
- * @param listener the callback this called when the {@link LiveObject} changes.
545
- *
546
- * @returns Unsubscribe function.
547
- *
548
- * @example
549
- * const liveObject = new LiveObject();
550
- * const unsubscribe = room.subscribe(liveObject, (liveObject) => { });
551
- * unsubscribe();
552
- */
553
- <TData extends JsonObject>(liveObject: LiveObject<TData>, callback: (liveObject: LiveObject<TData>) => void): () => void;
554
- /**
555
- * Subscribes to changes made on a {@link LiveList}. Returns an unsubscribe function.
556
- * In a future version, we will also expose what exactly changed in the {@link LiveList}.
557
- *
558
- * @param listener the callback this called when the {@link LiveList} changes.
559
- *
560
- * @returns Unsubscribe function.
561
- *
562
- * @example
563
- * const liveList = new LiveList();
564
- * const unsubscribe = room.subscribe(liveList, (liveList) => { });
565
- * unsubscribe();
566
- */
567
- <TItem extends Lson>(liveList: LiveList<TItem>, callback: (liveList: LiveList<TItem>) => void): () => void;
568
- /**
569
- * Subscribes to changes made on a {@link LiveMap} and all the nested data structures. Returns an unsubscribe function.
570
- * In a future version, we will also expose what exactly changed in the {@link LiveMap}.
571
- *
572
- * @param listener the callback this called when the {@link LiveMap} changes.
573
- *
574
- * @returns Unsubscribe function.
575
- *
576
- * @example
577
- * const liveMap = new LiveMap();
578
- * const unsubscribe = room.subscribe(liveMap, (liveMap) => { }, { isDeep: true });
579
- * unsubscribe();
580
- */
581
- <TKey extends string, TValue extends Lson>(liveMap: LiveMap<TKey, TValue>, callback: (updates: LiveMapUpdates<TKey, TValue>[]) => void, options: {
582
- isDeep: true;
583
- }): () => void;
584
- /**
585
- * Subscribes to changes made on a {@link LiveObject} and all the nested data structures. Returns an unsubscribe function.
586
- * In a future version, we will also expose what exactly changed in the {@link LiveObject}.
587
- *
588
- * @param listener the callback this called when the {@link LiveObject} changes.
589
- *
590
- * @returns Unsubscribe function.
591
- *
592
- * @example
593
- * const liveObject = new LiveObject();
594
- * const unsubscribe = room.subscribe(liveObject, (liveObject) => { }, { isDeep: true });
595
- * unsubscribe();
596
- */
597
- <TData extends LsonObject>(liveObject: LiveObject<TData>, callback: (updates: LiveObjectUpdates<TData>[]) => void, options: {
598
- isDeep: true;
599
- }): () => void;
600
- /**
601
- * Subscribes to changes made on a {@link LiveList} and all the nested data structures. Returns an unsubscribe function.
602
- * In a future version, we will also expose what exactly changed in the {@link LiveList}.
603
- *
604
- * @param listener the callback this called when the {@link LiveList} changes.
605
- *
606
- * @returns Unsubscribe function.
607
- *
608
- * @example
609
- * const liveList = new LiveList();
610
- * const unsubscribe = room.subscribe(liveList, (liveList) => { }, { isDeep: true });
611
- * unsubscribe();
612
- */
613
- <TItem extends Lson>(liveList: LiveList<TItem>, callback: (updates: LiveListUpdates<TItem>[]) => void, options: {
614
- isDeep: true;
615
- }): () => void;
616
- };
854
+ (type: "event", listener: EventCallback): () => void;
617
855
  /**
618
- * Room's history contains functions that let you undo and redo operation made on by the current client on the presence and storage.
856
+ * Subscribe to errors thrown in the room.
619
857
  */
620
- history: History;
858
+ (type: "error", listener: ErrorCallback): () => void;
621
859
  /**
622
- * @deprecated use the callback returned by subscribe instead.
623
- * See v0.13 release notes for more information.
624
- * Will be removed in a future version.
860
+ * Subscribe to connection state updates.
625
861
  */
626
- unsubscribe: {
627
- /**
628
- * @deprecated use the callback returned by subscribe instead.
629
- * See v0.13 release notes for more information.
630
- * Will be removed in a future version.
631
- */
632
- <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): void;
633
- /**
634
- * @deprecated use the callback returned by subscribe instead.
635
- * See v0.13 release notes for more information.
636
- * Will be removed in a future version.
637
- */
638
- <T extends Presence>(type: "others", listener: OthersEventCallback<T>): void;
639
- /**
640
- * @deprecated use the callback returned by subscribe instead.
641
- * See v0.13 release notes for more information.
642
- * Will be removed in a future version.
643
- */
644
- (type: "event", listener: EventCallback): void;
645
- /**
646
- * @deprecated use the callback returned by subscribe instead.
647
- * See v0.13 release notes for more information.
648
- * Will be removed in a future version.
649
- */
650
- (type: "error", listener: ErrorCallback): void;
651
- /**
652
- * @deprecated use the callback returned by subscribe instead.
653
- * See v0.13 release notes for more information.
654
- * Will be removed in a future version.
655
- */
656
- (type: "connection", listener: ConnectionCallback): void;
657
- };
862
+ (type: "connection", listener: ConnectionCallback): () => void;
658
863
  /**
659
- * Gets the current user.
660
- * Returns null if not it is not yet connected to the room.
864
+ * Subscribes to changes made on a {@link LiveMap}. Returns an unsubscribe function.
865
+ * In a future version, we will also expose what exactly changed in the {@link LiveMap}.
866
+ *
867
+ * @param listener the callback this called when the {@link LiveMap} changes.
868
+ *
869
+ * @returns Unsubscribe function.
661
870
  *
662
871
  * @example
663
- * const user = room.getSelf();
872
+ * const liveMap = new LiveMap();
873
+ * const unsubscribe = room.subscribe(liveMap, (liveMap) => { });
874
+ * unsubscribe();
664
875
  */
665
- getSelf<TPresence extends Presence = Presence>(): User<TPresence> | null;
876
+ <TKey extends string, TValue extends Lson>(
877
+ liveMap: LiveMap<TKey, TValue>,
878
+ listener: (liveMap: LiveMap<TKey, TValue>) => void
879
+ ): () => void;
666
880
  /**
667
- * Gets the presence of the current user.
881
+ * Subscribes to changes made on a {@link LiveObject}. Returns an unsubscribe function.
882
+ * In a future version, we will also expose what exactly changed in the {@link LiveObject}.
883
+ *
884
+ * @param callback the callback this called when the {@link LiveObject} changes.
885
+ *
886
+ * @returns Unsubscribe function.
668
887
  *
669
888
  * @example
670
- * const presence = room.getPresence();
889
+ * const liveObject = new LiveObject();
890
+ * const unsubscribe = room.subscribe(liveObject, (liveObject) => { });
891
+ * unsubscribe();
671
892
  */
672
- getPresence: <T extends Presence>() => T;
893
+ <TData extends JsonObject>(
894
+ liveObject: LiveObject<TData>,
895
+ callback: (liveObject: LiveObject<TData>) => void
896
+ ): () => void;
673
897
  /**
674
- * Gets all the other users in the room.
898
+ * Subscribes to changes made on a {@link LiveList}. Returns an unsubscribe function.
899
+ * In a future version, we will also expose what exactly changed in the {@link LiveList}.
900
+ *
901
+ * @param callback the callback this called when the {@link LiveList} changes.
902
+ *
903
+ * @returns Unsubscribe function.
675
904
  *
676
905
  * @example
677
- * const others = room.getOthers();
906
+ * const liveList = new LiveList();
907
+ * const unsubscribe = room.subscribe(liveList, (liveList) => { });
908
+ * unsubscribe();
678
909
  */
679
- getOthers: <T extends Presence>() => Others<T>;
910
+ <TItem extends Lson>(
911
+ liveList: LiveList<TItem>,
912
+ callback: (liveList: LiveList<TItem>) => void
913
+ ): () => void;
680
914
  /**
681
- * Updates the presence of the current user. Only pass the properties you want to update. No need to send the full presence.
682
- * @param overrides A partial object that contains the properties you want to update.
683
- * @param overrides Optional object to configure the behavior of updatePresence.
915
+ * Subscribes to changes made on a {@link LiveMap} and all the nested data structures. Returns an unsubscribe function.
916
+ * In a future version, we will also expose what exactly changed in the {@link LiveMap}.
684
917
  *
685
- * @example
686
- * room.updatePresence({ x: 0 });
687
- * room.updatePresence({ y: 0 });
918
+ * @param callback the callback this called when the {@link LiveMap} changes.
688
919
  *
689
- * const presence = room.getPresence();
690
- * // presence is equivalent to { x: 0, y: 0 }
691
- */
692
- updatePresence: <T extends Presence>(overrides: Partial<T>, options?: {
693
- /**
694
- * Whether or not the presence should have an impact on the undo/redo history.
695
- */
696
- addToHistory: boolean;
697
- }) => void;
698
- /**
699
- * Broadcasts an event to other users in the room. Event broadcasted to the room can be listened with {@link Room.subscribe}("event").
700
- * @param {any} event the event to broadcast. Should be serializable to JSON
920
+ * @returns Unsubscribe function.
701
921
  *
702
922
  * @example
703
- * // On client A
704
- * room.broadcastEvent({ type: "EMOJI", emoji: "🔥" });
705
- *
706
- * // On client B
707
- * room.subscribe("event", ({ event }) => {
708
- * if(event.type === "EMOJI") {
709
- * // Do something
710
- * }
711
- * });
923
+ * const liveMap = new LiveMap();
924
+ * const unsubscribe = room.subscribe(liveMap, (liveMap) => { }, { isDeep: true });
925
+ * unsubscribe();
712
926
  */
713
- broadcastEvent: (event: JsonObject, options?: BroadcastOptions) => void;
927
+ <TKey extends string, TValue extends Lson>(
928
+ liveMap: LiveMap<TKey, TValue>,
929
+ callback: (updates: LiveMapUpdates<TKey, TValue>[]) => void,
930
+ options: {
931
+ isDeep: true;
932
+ }
933
+ ): () => void;
714
934
  /**
715
- * Get the room's storage asynchronously.
716
- * The storage's root is a {@link LiveObject}.
935
+ * Subscribes to changes made on a {@link LiveObject} and all the nested data structures. Returns an unsubscribe function.
936
+ * In a future version, we will also expose what exactly changed in the {@link LiveObject}.
937
+ *
938
+ * @param callback the callback this called when the {@link LiveObject} changes.
939
+ *
940
+ * @returns Unsubscribe function.
717
941
  *
718
942
  * @example
719
- * const { root } = await room.getStorage();
943
+ * const liveObject = new LiveObject();
944
+ * const unsubscribe = room.subscribe(liveObject, (liveObject) => { }, { isDeep: true });
945
+ * unsubscribe();
720
946
  */
721
- getStorage: <TStorage extends LsonObject>() => Promise<{
722
- root: LiveObject<TStorage>;
723
- }>;
947
+ <TData extends LsonObject>(
948
+ liveObject: LiveObject<TData>,
949
+ callback: (updates: LiveObjectUpdates<TData>[]) => void,
950
+ options: {
951
+ isDeep: true;
952
+ }
953
+ ): () => void;
724
954
  /**
725
- * Batches modifications made during the given function.
726
- * All the modifications are sent to other clients in a single message.
727
- * All the subscribers are called only after the batch is over.
728
- * All the modifications are merged in a single history item (undo/redo).
955
+ * Subscribes to changes made on a {@link LiveList} and all the nested data structures. Returns an unsubscribe function.
956
+ * In a future version, we will also expose what exactly changed in the {@link LiveList}.
957
+ *
958
+ * @param callback the callback this called when the {@link LiveList} changes.
959
+ *
960
+ * @returns Unsubscribe function.
729
961
  *
730
962
  * @example
731
- * const { root } = await room.getStorage();
732
- * room.batch(() => {
733
- * root.set("x", 0);
734
- * room.updatePresence({ cursor: { x: 100, y: 100 }});
735
- * });
963
+ * const liveList = new LiveList();
964
+ * const unsubscribe = room.subscribe(liveList, (liveList) => { }, { isDeep: true });
965
+ * unsubscribe();
736
966
  */
737
- batch: (fn: () => void) => void;
738
- };
739
-
740
- declare abstract class AbstractCrdt {
741
- private __parent?;
742
- private __doc?;
743
- private __id?;
744
- private __parentKey?;
745
- get roomId(): string | null;
746
- }
747
-
748
- /**
749
- * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
750
- * Keys should be a string, and values should be serializable to JSON.
751
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
752
- */
753
- declare class LiveObject<O extends LsonObject = LsonObject> extends AbstractCrdt {
754
- private _map;
755
- private _propToLastUpdate;
756
- constructor(obj?: O);
757
- private _applyUpdate;
758
- private _applyDeleteObjectKey;
759
- /**
760
- * Transform the LiveObject into a javascript object
761
- */
762
- toObject(): O;
763
- /**
764
- * Adds or updates a property with a specified key and a value.
765
- * @param key The key of the property to add
766
- * @param value The value of the property to add
767
- */
768
- set<TKey extends keyof O>(key: TKey, value: O[TKey]): void;
967
+ <TItem extends Lson>(
968
+ liveList: LiveList<TItem>,
969
+ callback: (updates: LiveListUpdates<TItem>[]) => void,
970
+ options: {
971
+ isDeep: true;
972
+ }
973
+ ): () => void;
974
+ };
975
+ /**
976
+ * Room's history contains functions that let you undo and redo operation made on by the current client on the presence and storage.
977
+ */
978
+ history: History;
979
+ /**
980
+ * Gets the current user.
981
+ * Returns null if not it is not yet connected to the room.
982
+ *
983
+ * @example
984
+ * const user = room.getSelf();
985
+ */
986
+ getSelf(): User<TPresence> | null;
987
+ /**
988
+ * Gets the presence of the current user.
989
+ *
990
+ * @example
991
+ * const presence = room.getPresence();
992
+ */
993
+ getPresence: () => TPresence;
994
+ /**
995
+ * Gets all the other users in the room.
996
+ *
997
+ * @example
998
+ * const others = room.getOthers();
999
+ */
1000
+ getOthers: () => Others<TPresence>;
1001
+ /**
1002
+ * Updates the presence of the current user. Only pass the properties you want to update. No need to send the full presence.
1003
+ * @param overrides A partial object that contains the properties you want to update.
1004
+ * @param overrides Optional object to configure the behavior of updatePresence.
1005
+ *
1006
+ * @example
1007
+ * room.updatePresence({ x: 0 });
1008
+ * room.updatePresence({ y: 0 });
1009
+ *
1010
+ * const presence = room.getPresence();
1011
+ * // presence is equivalent to { x: 0, y: 0 }
1012
+ */
1013
+ updatePresence: (
1014
+ overrides: Partial<TPresence>,
1015
+ options?: {
1016
+ /**
1017
+ * Whether or not the presence should have an impact on the undo/redo history.
1018
+ */
1019
+ addToHistory: boolean;
1020
+ }
1021
+ ) => void;
1022
+ /**
1023
+ * Broadcasts an event to other users in the room. Event broadcasted to the room can be listened with {@link Room.subscribe}("event").
1024
+ * @param {any} event the event to broadcast. Should be serializable to JSON
1025
+ *
1026
+ * @example
1027
+ * // On client A
1028
+ * room.broadcastEvent({ type: "EMOJI", emoji: "🔥" });
1029
+ *
1030
+ * // On client B
1031
+ * room.subscribe("event", ({ event }) => {
1032
+ * if(event.type === "EMOJI") {
1033
+ * // Do something
1034
+ * }
1035
+ * });
1036
+ */
1037
+ broadcastEvent: (event: Json, options?: BroadcastOptions) => void;
1038
+ /**
1039
+ * Get the room's storage asynchronously.
1040
+ * The storage's root is a {@link LiveObject}.
1041
+ *
1042
+ * @example
1043
+ * const { root } = await room.getStorage();
1044
+ */
1045
+ getStorage: <
769
1046
  /**
770
- * Returns a specified property from the LiveObject.
771
- * @param key The key of the property to get
1047
+ * @deprecated This type argument is ignored. If you want to annotate this
1048
+ * type manually, please annotate the Room instance instead.
772
1049
  */
773
- get<TKey extends keyof O>(key: TKey): O[TKey];
774
- /**
775
- * Deletes a key from the LiveObject
776
- * @param key The key of the property to delete
777
- */
778
- delete(key: keyof O): void;
779
- /**
780
- * Adds or updates multiple properties at once with an object.
781
- * @param overrides The object used to overrides properties
782
- */
783
- update(overrides: Partial<O>): void;
1050
+ _ = unknown
1051
+ >() => Promise<{
1052
+ root: LiveObject<TStorage>;
1053
+ }>;
1054
+ /**
1055
+ * Batches modifications made during the given function.
1056
+ * All the modifications are sent to other clients in a single message.
1057
+ * All the subscribers are called only after the batch is over.
1058
+ * All the modifications are merged in a single history item (undo/redo).
1059
+ *
1060
+ * @example
1061
+ * const { root } = await room.getStorage();
1062
+ * room.batch(() => {
1063
+ * root.set("x", 0);
1064
+ * room.updatePresence({ cursor: { x: 100, y: 100 }});
1065
+ * });
1066
+ */
1067
+ batch: (fn: () => void) => void;
1068
+ };
1069
+ declare enum WebsocketCloseCodes {
1070
+ CLOSE_ABNORMAL = 1006,
1071
+ INVALID_MESSAGE_FORMAT = 4000,
1072
+ NOT_ALLOWED = 4001,
1073
+ MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
1074
+ MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
1075
+ MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
1076
+ MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
1077
+ CLOSE_WITHOUT_RETRY = 4999,
784
1078
  }
785
1079
 
786
- export { AbstractCrdt as A, BroadcastOptions as B, ClientOptions as C, History as H, Json as J, LiveObject as L, Others as O, Presence as P, Room as R, StorageUpdate as S, User as U, Client as a, LiveMap as b, LiveList as c, JsonObject as d, Lson as e, LsonObject as f, Resolve as g, RoomInitializers as h, isJsonArray as i, isJsonObject as j, isJsonScalar as k };
1080
+ export {
1081
+ SerializedRootObject as A,
1082
+ BroadcastOptions as B,
1083
+ ClientOptions as C,
1084
+ DeleteCrdtOp as D,
1085
+ SetParentKeyOp as E,
1086
+ UpdateObjectOp as F,
1087
+ CrdtType as G,
1088
+ History as H,
1089
+ IdTuple as I,
1090
+ Json as J,
1091
+ OpCode as K,
1092
+ LiveList as L,
1093
+ isJsonArray as M,
1094
+ NodeMap as N,
1095
+ Others as O,
1096
+ Presence as P,
1097
+ isJsonObject as Q,
1098
+ Room as R,
1099
+ StorageUpdate as S,
1100
+ isJsonScalar as T,
1101
+ User as U,
1102
+ isChildCrdt as V,
1103
+ WebsocketCloseCodes as W,
1104
+ isRootCrdt as X,
1105
+ Client as a,
1106
+ LiveMap as b,
1107
+ LiveObject as c,
1108
+ JsonObject as d,
1109
+ LiveStructure as e,
1110
+ Lson as f,
1111
+ LsonObject as g,
1112
+ Op as h,
1113
+ SerializedCrdt as i,
1114
+ CreateChildOp as j,
1115
+ CreateListOp as k,
1116
+ CreateMapOp as l,
1117
+ CreateObjectOp as m,
1118
+ CreateOp as n,
1119
+ CreateRegisterOp as o,
1120
+ CreateRootObjectOp as p,
1121
+ DeleteObjectKeyOp as q,
1122
+ LiveNode as r,
1123
+ ParentToChildNodeMap as s,
1124
+ Resolve as t,
1125
+ RoomInitializers as u,
1126
+ SerializedChild as v,
1127
+ SerializedList as w,
1128
+ SerializedMap as x,
1129
+ SerializedObject as y,
1130
+ SerializedRegister as z,
1131
+ };