@liveblocks/core 0.19.0-beta0 β†’ 0.19.1-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.
package/dist/index.d.ts CHANGED
@@ -1,58 +1,209 @@
1
1
  /**
2
- * Helper function that can be used to implement exhaustive switch statements
3
- * with TypeScript. Example usage:
4
- *
5
- * type Fruit = "🍎" | "🍌";
6
- *
7
- * switch (fruit) {
8
- * case "🍎":
9
- * case "🍌":
10
- * return doSomething();
2
+ * Represents an indefinitely deep arbitrary JSON data structure. There are
3
+ * four types that make up the Json family:
11
4
  *
12
- * default:
13
- * return assertNever(fruit, "Unknown fruit");
14
- * }
5
+ * - Json any legal JSON value
6
+ * - JsonScalar any legal JSON leaf value (no lists or objects)
7
+ * - JsonArray a JSON value whose outer type is an array
8
+ * - JsonObject a JSON value whose outer type is an object
15
9
  *
16
- * If now the Fruit union is extended (i.e. add "πŸ’"), TypeScript will catch
17
- * this *statically*, rather than at runtime, and force you to handle the
18
- * πŸ’ case.
19
10
  */
20
- declare function assertNever(_value: never, errmsg: string): never;
11
+ declare type Json = JsonScalar | JsonArray | JsonObject;
12
+ declare type JsonScalar = string | number | boolean | null;
13
+ declare type JsonArray = Json[];
14
+ declare type JsonObject = {
15
+ [key: string]: Json | undefined;
16
+ };
17
+ declare function isJsonScalar(data: Json): data is JsonScalar;
18
+ declare function isJsonArray(data: Json): data is JsonArray;
19
+ declare function isJsonObject(data: Json): data is JsonObject;
20
+
21
+ declare enum OpCode {
22
+ INIT = 0,
23
+ SET_PARENT_KEY = 1,
24
+ CREATE_LIST = 2,
25
+ UPDATE_OBJECT = 3,
26
+ CREATE_OBJECT = 4,
27
+ DELETE_CRDT = 5,
28
+ DELETE_OBJECT_KEY = 6,
29
+ CREATE_MAP = 7,
30
+ CREATE_REGISTER = 8
31
+ }
21
32
  /**
22
- * Asserts that a given value is non-nullable. This is similar to TypeScript's
23
- * `!` operator, but will throw an error at runtime (dev-mode only) indicating
24
- * an incorrect assumption.
25
- *
26
- * Instead of:
27
- *
28
- * foo!.bar
29
- *
30
- * Use:
31
- *
32
- * nn(foo).bar
33
- *
33
+ * These operations are the payload for {@link UpdateStorageServerMsg} messages
34
+ * only.
34
35
  */
35
- declare function nn<T>(value: T, errmsg?: string): NonNullable<T>;
36
+ declare type Op = CreateOp | UpdateObjectOp | DeleteCrdtOp | SetParentKeyOp | DeleteObjectKeyOp;
37
+ declare type CreateOp = CreateRootObjectOp | CreateChildOp;
38
+ declare type CreateChildOp = CreateObjectOp | CreateRegisterOp | CreateMapOp | CreateListOp;
39
+ declare type UpdateObjectOp = {
40
+ opId?: string;
41
+ id: string;
42
+ type: OpCode.UPDATE_OBJECT;
43
+ data: Partial<JsonObject>;
44
+ };
45
+ declare type CreateObjectOp = {
46
+ opId?: string;
47
+ id: string;
48
+ intent?: "set";
49
+ deletedId?: string;
50
+ type: OpCode.CREATE_OBJECT;
51
+ parentId: string;
52
+ parentKey: string;
53
+ data: JsonObject;
54
+ };
55
+ declare type CreateRootObjectOp = {
56
+ opId?: string;
57
+ id: string;
58
+ type: OpCode.CREATE_OBJECT;
59
+ data: JsonObject;
60
+ parentId?: never;
61
+ parentKey?: never;
62
+ };
63
+ declare type CreateListOp = {
64
+ opId?: string;
65
+ id: string;
66
+ intent?: "set";
67
+ deletedId?: string;
68
+ type: OpCode.CREATE_LIST;
69
+ parentId: string;
70
+ parentKey: string;
71
+ };
72
+ declare type CreateMapOp = {
73
+ opId?: string;
74
+ id: string;
75
+ intent?: "set";
76
+ deletedId?: string;
77
+ type: OpCode.CREATE_MAP;
78
+ parentId: string;
79
+ parentKey: string;
80
+ };
81
+ declare type CreateRegisterOp = {
82
+ opId?: string;
83
+ id: string;
84
+ intent?: "set";
85
+ deletedId?: string;
86
+ type: OpCode.CREATE_REGISTER;
87
+ parentId: string;
88
+ parentKey: string;
89
+ data: Json;
90
+ };
91
+ declare type DeleteCrdtOp = {
92
+ opId?: string;
93
+ id: string;
94
+ type: OpCode.DELETE_CRDT;
95
+ };
96
+ declare type SetParentKeyOp = {
97
+ opId?: string;
98
+ id: string;
99
+ type: OpCode.SET_PARENT_KEY;
100
+ parentKey: string;
101
+ };
102
+ declare type DeleteObjectKeyOp = {
103
+ opId?: string;
104
+ id: string;
105
+ type: OpCode.DELETE_OBJECT_KEY;
106
+ key: string;
107
+ };
36
108
 
37
- declare type Callback<T> = (event: T) => void;
38
- declare type UnsubscribeCallback = () => void;
39
- declare type Observable<T> = {
40
- subscribe(callback: Callback<T>): UnsubscribeCallback;
41
- subscribeOnce(callback: Callback<T>): UnsubscribeCallback;
109
+ /**
110
+ * Represents an indefinitely deep arbitrary immutable data
111
+ * structure, as returned by the .toImmutable().
112
+ */
113
+ declare type Immutable = Scalar | ImmutableList | ImmutableObject | ImmutableMap;
114
+ declare type Scalar = string | number | boolean | null;
115
+ declare type ImmutableList = readonly Immutable[];
116
+ declare type ImmutableObject = {
117
+ readonly [key: string]: Immutable | undefined;
42
118
  };
119
+ declare type ImmutableMap = ReadonlyMap<string, Immutable>;
43
120
 
44
- declare type ReadonlyArrayWithLegacyMethods<T> = readonly T[] & {
121
+ declare type UpdateDelta = {
122
+ type: "update";
123
+ } | {
124
+ type: "delete";
125
+ };
126
+
127
+ declare type LiveObjectUpdateDelta<O extends {
128
+ [key: string]: unknown;
129
+ }> = {
130
+ [K in keyof O]?: UpdateDelta | undefined;
131
+ };
132
+ /**
133
+ * A LiveObject notification that is sent in-client to any subscribers whenever
134
+ * one or more of the entries inside the LiveObject instance have changed.
135
+ */
136
+ declare type LiveObjectUpdates<TData extends LsonObject> = {
137
+ type: "LiveObject";
138
+ node: LiveObject<TData>;
139
+ updates: LiveObjectUpdateDelta<TData>;
140
+ };
141
+ /**
142
+ * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
143
+ * Keys should be a string, and values should be serializable to JSON.
144
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
145
+ */
146
+ declare class LiveObject<O extends LsonObject> extends AbstractCrdt {
147
+ constructor(obj?: O);
45
148
  /**
46
- * @deprecated Prefer the normal .length property on arrays.
149
+ * Transform the LiveObject into a javascript object
47
150
  */
48
- readonly count: number;
151
+ toObject(): O;
49
152
  /**
50
- * @deprecated Calling .toArray() is no longer needed
153
+ * Adds or updates a property with a specified key and a value.
154
+ * @param key The key of the property to add
155
+ * @param value The value of the property to add
51
156
  */
52
- readonly toArray: () => readonly T[];
53
- };
54
- declare function asArrayWithLegacyMethods<T>(arr: readonly T[]): ReadonlyArrayWithLegacyMethods<T>;
157
+ set<TKey extends keyof O>(key: TKey, value: O[TKey]): void;
158
+ /**
159
+ * Returns a specified property from the LiveObject.
160
+ * @param key The key of the property to get
161
+ */
162
+ get<TKey extends keyof O>(key: TKey): O[TKey];
163
+ /**
164
+ * Deletes a key from the LiveObject
165
+ * @param key The key of the property to delete
166
+ */
167
+ delete(key: keyof O): void;
168
+ /**
169
+ * Adds or updates multiple properties at once with an object.
170
+ * @param patch The object used to overrides properties
171
+ */
172
+ update(patch: Partial<O>): void;
173
+ toImmutable(): ToImmutable<O>;
174
+ }
175
+
176
+ /**
177
+ * Helper type to convert any valid Lson type to the equivalent Json type.
178
+ *
179
+ * Examples:
180
+ *
181
+ * ToImmutable<42> // 42
182
+ * ToImmutable<'hi'> // 'hi'
183
+ * ToImmutable<number> // number
184
+ * ToImmutable<string> // string
185
+ * ToImmutable<string | LiveList<number>> // string | readonly number[]
186
+ * ToImmutable<LiveMap<string, LiveList<number>>>
187
+ * // ReadonlyMap<string, readonly number[]>
188
+ * ToImmutable<LiveObject<{ a: number, b: LiveList<string>, c?: number }>>
189
+ * // { readonly a: null, readonly b: readonly string[], readonly c?: number }
190
+ *
191
+ */
192
+ declare type ToImmutable<L extends Lson | LsonObject> = L extends LiveList<infer I> ? readonly ToImmutable<I>[] : L extends LiveObject<infer O> ? ToImmutable<O> : L extends LiveMap<infer K, infer V> ? ReadonlyMap<K, ToImmutable<V>> : L extends LsonObject ? {
193
+ readonly [K in keyof L]: ToImmutable<Exclude<L[K], undefined>> | (undefined extends L[K] ? undefined : never);
194
+ } : L extends Json ? L : never;
55
195
 
196
+ /**
197
+ * A LiveMap notification that is sent in-client to any subscribers whenever
198
+ * one or more of the values inside the LiveMap instance have changed.
199
+ */
200
+ declare type LiveMapUpdates<TKey extends string, TValue extends Lson> = {
201
+ type: "LiveMap";
202
+ node: LiveMap<TKey, TValue>;
203
+ updates: {
204
+ [key: string]: UpdateDelta;
205
+ };
206
+ };
56
207
  /**
57
208
  * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
58
209
  * Keys should be a string, and values should be serializable to JSON.
@@ -111,150 +262,48 @@ declare class LiveMap<TKey extends string, TValue extends Lson> extends Abstract
111
262
  toImmutable(): ReadonlyMap<TKey, ToImmutable<TValue>>;
112
263
  }
113
264
 
265
+ declare type StorageCallback = (updates: StorageUpdate[]) => void;
114
266
  /**
115
- * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
116
- * Keys should be a string, and values should be serializable to JSON.
117
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
267
+ * The payload of notifications sent (in-client) when LiveStructures change.
268
+ * Messages of this kind are not originating from the network, but are 100%
269
+ * in-client.
118
270
  */
119
- declare class LiveObject<O extends LsonObject> extends AbstractCrdt {
120
- constructor(obj?: O);
121
- /**
122
- * Transform the LiveObject into a javascript object
123
- */
124
- toObject(): O;
271
+ declare type StorageUpdate = LiveMapUpdates<string, Lson> | LiveObjectUpdates<LsonObject> | LiveListUpdates<Lson>;
272
+
273
+ declare abstract class AbstractCrdt {
274
+ get roomId(): string | null;
125
275
  /**
126
- * Adds or updates a property with a specified key and a value.
127
- * @param key The key of the property to add
128
- * @param value The value of the property to add
276
+ * Return an immutable snapshot of this Live node and its children.
129
277
  */
130
- set<TKey extends keyof O>(key: TKey, value: O[TKey]): void;
131
- /**
132
- * Returns a specified property from the LiveObject.
133
- * @param key The key of the property to get
134
- */
135
- get<TKey extends keyof O>(key: TKey): O[TKey];
136
- /**
137
- * Deletes a key from the LiveObject
138
- * @param key The key of the property to delete
139
- */
140
- delete(key: keyof O): void;
141
- /**
142
- * Adds or updates multiple properties at once with an object.
143
- * @param patch The object used to overrides properties
144
- */
145
- update(patch: Partial<O>): void;
146
- toImmutable(): ToImmutable<O>;
147
- }
148
-
149
- /**
150
- * Represents an indefinitely deep arbitrary JSON data structure. There are
151
- * four types that make up the Json family:
152
- *
153
- * - Json any legal JSON value
154
- * - JsonScalar any legal JSON leaf value (no lists or objects)
155
- * - JsonArray a JSON value whose outer type is an array
156
- * - JsonObject a JSON value whose outer type is an object
157
- *
158
- */
159
- declare type Json = JsonScalar | JsonArray | JsonObject;
160
- declare type JsonScalar = string | number | boolean | null;
161
- declare type JsonArray = Json[];
162
- declare type JsonObject = {
163
- [key: string]: Json | undefined;
164
- };
165
- declare function isJsonScalar(data: Json): data is JsonScalar;
166
- declare function isJsonArray(data: Json): data is JsonArray;
167
- declare function isJsonObject(data: Json): data is JsonObject;
168
-
169
- /**
170
- * INTERNAL
171
- */
172
- declare class LiveRegister<TValue extends Json> extends AbstractCrdt {
173
- constructor(data: TValue);
174
- get data(): TValue;
278
+ toImmutable(): Immutable;
175
279
  }
176
280
 
177
- declare type LiveStructure = LiveObject<LsonObject> | LiveList<Lson> | LiveMap<string, Lson>;
178
- /**
179
- * Think of Lson as a sibling of the Json data tree, except that the nested
180
- * data structure can contain a mix of Json values and LiveStructure instances.
181
- */
182
- declare type Lson = Json | LiveStructure;
183
- /**
184
- * LiveNode is the internal tree for managing Live data structures. The key
185
- * difference with Lson is that all the Json values get represented in
186
- * a LiveRegister node.
187
- */
188
- declare type LiveNode = LiveStructure | LiveRegister<Json>;
189
- /**
190
- * A mapping of keys to Lson values. A Lson value is any valid JSON
191
- * value or a Live storage data structure (LiveMap, LiveList, etc.)
192
- */
193
- declare type LsonObject = {
194
- [key: string]: Lson | undefined;
281
+ declare type LiveListUpdateDelta = {
282
+ index: number;
283
+ item: Lson;
284
+ type: "insert";
285
+ } | {
286
+ index: number;
287
+ type: "delete";
288
+ } | {
289
+ index: number;
290
+ previousIndex: number;
291
+ item: Lson;
292
+ type: "move";
293
+ } | {
294
+ index: number;
295
+ item: Lson;
296
+ type: "set";
195
297
  };
196
298
  /**
197
- * Helper type to convert any valid Lson type to the equivalent Json type.
198
- *
199
- * Examples:
200
- *
201
- * ToJson<42> // 42
202
- * ToJson<'hi'> // 'hi'
203
- * ToJson<number> // number
204
- * ToJson<string> // string
205
- * ToJson<string | LiveList<number>> // string | number[]
206
- * ToJson<LiveMap<string, LiveList<number>>>
207
- * // { [key: string]: number[] }
208
- * ToJson<LiveObject<{ a: number, b: LiveList<string>, c?: number }>>
209
- * // { a: null, b: string[], c?: number }
210
- *
211
- */
212
- declare type ToJson<T extends Lson | LsonObject> = T extends Json ? T : T extends LsonObject ? {
213
- [K in keyof T]: ToJson<Exclude<T[K], undefined>> | (undefined extends T[K] ? undefined : never);
214
- } : T extends LiveList<infer I> ? ToJson<I>[] : T extends LiveObject<infer O> ? ToJson<O> : T extends LiveMap<infer KS, infer V> ? {
215
- [K in KS]: ToJson<V>;
216
- } : never;
217
-
218
- /**
219
- * Represents an indefinitely deep arbitrary immutable data
220
- * structure, as returned by the .toImmutable().
299
+ * A LiveList notification that is sent in-client to any subscribers whenever
300
+ * one or more of the items inside the LiveList instance have changed.
221
301
  */
222
-
223
- declare type Immutable = Scalar | ImmutableList | ImmutableObject | ImmutableMap;
224
- declare type Scalar = string | number | boolean | null;
225
- declare type ImmutableList = readonly Immutable[];
226
- declare type ImmutableObject = {
227
- readonly [key: string]: Immutable | undefined;
302
+ declare type LiveListUpdates<TItem extends Lson> = {
303
+ type: "LiveList";
304
+ node: LiveList<TItem>;
305
+ updates: LiveListUpdateDelta[];
228
306
  };
229
- declare type ImmutableMap = ReadonlyMap<string, Immutable>;
230
- /**
231
- * Helper type to convert any valid Lson type to the equivalent Json type.
232
- *
233
- * Examples:
234
- *
235
- * ToImmutable<42> // 42
236
- * ToImmutable<'hi'> // 'hi'
237
- * ToImmutable<number> // number
238
- * ToImmutable<string> // string
239
- * ToImmutable<string | LiveList<number>> // string | readonly number[]
240
- * ToImmutable<LiveMap<string, LiveList<number>>>
241
- * // ReadonlyMap<string, readonly number[]>
242
- * ToImmutable<LiveObject<{ a: number, b: LiveList<string>, c?: number }>>
243
- * // { readonly a: null, readonly b: readonly string[], readonly c?: number }
244
- *
245
- */
246
- declare type ToImmutable<L extends Lson | LsonObject> = L extends LiveList<infer I> ? readonly ToImmutable<I>[] : L extends LiveObject<infer O> ? ToImmutable<O> : L extends LiveMap<infer K, infer V> ? ReadonlyMap<K, ToImmutable<V>> : L extends LsonObject ? {
247
- readonly [K in keyof L]: ToImmutable<Exclude<L[K], undefined>> | (undefined extends L[K] ? undefined : never);
248
- } : L extends Json ? L : never;
249
-
250
- declare abstract class AbstractCrdt {
251
- get roomId(): string | null;
252
- /**
253
- * Return an immutable snapshot of this Live node and its children.
254
- */
255
- toImmutable(): Immutable;
256
- }
257
-
258
307
  /**
259
308
  * The LiveList class represents an ordered collection of items that is synchronized across clients.
260
309
  */
@@ -357,6 +406,55 @@ declare class LiveList<TItem extends Lson> extends AbstractCrdt {
357
406
  toImmutable(): readonly ToImmutable<TItem>[];
358
407
  }
359
408
 
409
+ /**
410
+ * INTERNAL
411
+ */
412
+ declare class LiveRegister<TValue extends Json> extends AbstractCrdt {
413
+ constructor(data: TValue);
414
+ get data(): TValue;
415
+ }
416
+
417
+ declare type LiveStructure = LiveObject<LsonObject> | LiveList<Lson> | LiveMap<string, Lson>;
418
+ /**
419
+ * Think of Lson as a sibling of the Json data tree, except that the nested
420
+ * data structure can contain a mix of Json values and LiveStructure instances.
421
+ */
422
+ declare type Lson = Json | LiveStructure;
423
+ /**
424
+ * LiveNode is the internal tree for managing Live data structures. The key
425
+ * difference with Lson is that all the Json values get represented in
426
+ * a LiveRegister node.
427
+ */
428
+ declare type LiveNode = LiveStructure | LiveRegister<Json>;
429
+ /**
430
+ * A mapping of keys to Lson values. A Lson value is any valid JSON
431
+ * value or a Live storage data structure (LiveMap, LiveList, etc.)
432
+ */
433
+ declare type LsonObject = {
434
+ [key: string]: Lson | undefined;
435
+ };
436
+ /**
437
+ * Helper type to convert any valid Lson type to the equivalent Json type.
438
+ *
439
+ * Examples:
440
+ *
441
+ * ToJson<42> // 42
442
+ * ToJson<'hi'> // 'hi'
443
+ * ToJson<number> // number
444
+ * ToJson<string> // string
445
+ * ToJson<string | LiveList<number>> // string | number[]
446
+ * ToJson<LiveMap<string, LiveList<number>>>
447
+ * // { [key: string]: number[] }
448
+ * ToJson<LiveObject<{ a: number, b: LiveList<string>, c?: number }>>
449
+ * // { a: null, b: string[], c?: number }
450
+ *
451
+ */
452
+ declare type ToJson<T extends Lson | LsonObject> = T extends Json ? T : T extends LsonObject ? {
453
+ [K in keyof T]: ToJson<Exclude<T[K], undefined>> | (undefined extends T[K] ? undefined : never);
454
+ } : T extends LiveList<infer I> ? ToJson<I>[] : T extends LiveObject<infer O> ? ToJson<O> : T extends LiveMap<infer KS, infer V> ? {
455
+ [K in KS]: ToJson<V>;
456
+ } : never;
457
+
360
458
  /**
361
459
  * This type is used by clients to define the metadata for a user.
362
460
  */
@@ -372,93 +470,57 @@ declare type BaseUserMeta = {
372
470
  info?: Json;
373
471
  };
374
472
 
375
- declare enum OpCode {
376
- INIT = 0,
377
- SET_PARENT_KEY = 1,
378
- CREATE_LIST = 2,
379
- UPDATE_OBJECT = 3,
380
- CREATE_OBJECT = 4,
381
- DELETE_CRDT = 5,
382
- DELETE_OBJECT_KEY = 6,
383
- CREATE_MAP = 7,
384
- CREATE_REGISTER = 8
385
- }
473
+ declare type Callback<T> = (event: T) => void;
474
+ declare type UnsubscribeCallback = () => void;
475
+ declare type Observable<T> = {
476
+ subscribe(callback: Callback<T>): UnsubscribeCallback;
477
+ subscribeOnce(callback: Callback<T>): UnsubscribeCallback;
478
+ };
479
+
386
480
  /**
387
- * These operations are the payload for {@link UpdateStorageServerMsg} messages
388
- * only.
481
+ * This helper type is effectively a no-op, but will force TypeScript to
482
+ * "evaluate" any named helper types in its definition. This can sometimes make
483
+ * API signatures clearer in IDEs.
484
+ *
485
+ * For example, in:
486
+ *
487
+ * type Payload<T> = { data: T };
488
+ *
489
+ * let r1: Payload<string>;
490
+ * let r2: Resolve<Payload<string>>;
491
+ *
492
+ * The inferred type of `r1` is going to be `Payload<string>` which shows up in
493
+ * editor hints, and it may be unclear what's inside if you don't know the
494
+ * definition of `Payload`.
495
+ *
496
+ * The inferred type of `r2` is going to be `{ data: string }`, which may be
497
+ * more helpful.
498
+ *
499
+ * This trick comes from:
500
+ * https://effectivetypescript.com/2022/02/25/gentips-4-display/
389
501
  */
390
- declare type Op = CreateOp | UpdateObjectOp | DeleteCrdtOp | SetParentKeyOp | DeleteObjectKeyOp;
391
- declare type CreateOp = CreateRootObjectOp | CreateChildOp;
392
- declare type CreateChildOp = CreateObjectOp | CreateRegisterOp | CreateMapOp | CreateListOp;
393
- declare type UpdateObjectOp = {
394
- opId?: string;
395
- id: string;
396
- type: OpCode.UPDATE_OBJECT;
397
- data: Partial<JsonObject>;
502
+ declare type Resolve<T> = T extends (...args: unknown[]) => unknown ? T : {
503
+ [K in keyof T]: T[K];
398
504
  };
399
- declare type CreateObjectOp = {
400
- opId?: string;
401
- id: string;
402
- intent?: "set";
403
- deletedId?: string;
404
- type: OpCode.CREATE_OBJECT;
405
- parentId: string;
406
- parentKey: string;
407
- data: JsonObject;
505
+
506
+ declare type AppOnlyAuthToken = {
507
+ appId: string;
508
+ roomId?: never;
509
+ scopes: string[];
408
510
  };
409
- declare type CreateRootObjectOp = {
410
- opId?: string;
411
- id: string;
412
- type: OpCode.CREATE_OBJECT;
413
- data: JsonObject;
414
- parentId?: never;
415
- parentKey?: never;
416
- };
417
- declare type CreateListOp = {
418
- opId?: string;
419
- id: string;
420
- intent?: "set";
421
- deletedId?: string;
422
- type: OpCode.CREATE_LIST;
423
- parentId: string;
424
- parentKey: string;
425
- };
426
- declare type CreateMapOp = {
427
- opId?: string;
428
- id: string;
429
- intent?: "set";
430
- deletedId?: string;
431
- type: OpCode.CREATE_MAP;
432
- parentId: string;
433
- parentKey: string;
434
- };
435
- declare type CreateRegisterOp = {
436
- opId?: string;
437
- id: string;
438
- intent?: "set";
439
- deletedId?: string;
440
- type: OpCode.CREATE_REGISTER;
441
- parentId: string;
442
- parentKey: string;
443
- data: Json;
444
- };
445
- declare type DeleteCrdtOp = {
446
- opId?: string;
447
- id: string;
448
- type: OpCode.DELETE_CRDT;
449
- };
450
- declare type SetParentKeyOp = {
451
- opId?: string;
452
- id: string;
453
- type: OpCode.SET_PARENT_KEY;
454
- parentKey: string;
455
- };
456
- declare type DeleteObjectKeyOp = {
457
- opId?: string;
458
- id: string;
459
- type: OpCode.DELETE_OBJECT_KEY;
460
- key: string;
511
+ declare type RoomAuthToken = {
512
+ appId: string;
513
+ roomId: string;
514
+ scopes: string[];
515
+ actor: number;
516
+ maxConnectionsPerRoom?: number;
517
+ id?: string;
518
+ info?: Json;
461
519
  };
520
+ declare type AuthToken = AppOnlyAuthToken | RoomAuthToken;
521
+ declare function isAppOnlyAuthToken(data: JsonObject): data is AppOnlyAuthToken;
522
+ declare function isRoomAuthToken(data: JsonObject): data is RoomAuthToken;
523
+ declare function isAuthToken(data: JsonObject): data is AuthToken;
462
524
 
463
525
  declare enum ClientMsgCode {
464
526
  UPDATE_PRESENCE = 100,
@@ -508,401 +570,67 @@ declare type FetchStorageClientMsg = {
508
570
  type: ClientMsgCode.FETCH_STORAGE;
509
571
  };
510
572
 
511
- declare type IdTuple<T> = [id: string, value: T];
512
- declare enum CrdtType {
513
- OBJECT = 0,
514
- LIST = 1,
515
- MAP = 2,
516
- REGISTER = 3
517
- }
518
- declare type SerializedCrdt = SerializedRootObject | SerializedChild;
519
- declare type SerializedChild = SerializedObject | SerializedList | SerializedMap | SerializedRegister;
520
- declare type SerializedRootObject = {
521
- type: CrdtType.OBJECT;
522
- data: JsonObject;
523
- parentId?: never;
524
- parentKey?: never;
525
- };
526
- declare type SerializedObject = {
527
- type: CrdtType.OBJECT;
528
- parentId: string;
529
- parentKey: string;
530
- data: JsonObject;
531
- };
532
- declare type SerializedList = {
533
- type: CrdtType.LIST;
534
- parentId: string;
535
- parentKey: string;
536
- };
537
- declare type SerializedMap = {
538
- type: CrdtType.MAP;
539
- parentId: string;
540
- parentKey: string;
541
- };
542
- declare type SerializedRegister = {
543
- type: CrdtType.REGISTER;
544
- parentId: string;
545
- parentKey: string;
546
- data: Json;
547
- };
548
- declare function isRootCrdt(crdt: SerializedCrdt): crdt is SerializedRootObject;
549
- declare function isChildCrdt(crdt: SerializedCrdt): crdt is SerializedChild;
550
-
551
- /**
552
- * Lookup table for nodes (= SerializedCrdt values) by their IDs.
553
- */
554
- declare type NodeMap = Map<string, // Node ID
555
- SerializedCrdt>;
556
- /**
557
- * Reverse lookup table for all child nodes (= list of SerializedCrdt values)
558
- * by their parent node's IDs.
559
- */
560
- declare type ParentToChildNodeMap = Map<string, // Parent's node ID
561
- IdTuple<SerializedChild>[]>;
562
-
563
- declare enum ServerMsgCode {
564
- UPDATE_PRESENCE = 100,
565
- USER_JOINED = 101,
566
- USER_LEFT = 102,
567
- BROADCASTED_EVENT = 103,
568
- ROOM_STATE = 104,
569
- INITIAL_STORAGE_STATE = 200,
570
- UPDATE_STORAGE = 201
571
- }
572
- /**
573
- * Messages that can be sent from the server to the client.
574
- */
575
- declare type ServerMsg<TPresence extends JsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg<TUserMeta> | UserLeftServerMsg | BroadcastedEventServerMsg<TRoomEvent> | RoomStateServerMsg<TUserMeta> | InitialDocumentStateServerMsg | UpdateStorageServerMsg;
576
- /**
577
- * Sent by the WebSocket server and broadcasted to all clients to announce that
578
- * a User updated their presence. For example, when a user moves their cursor.
579
- *
580
- * In most cases, the data payload will only include the fields from the
581
- * Presence that have been changed since the last announcement. However, after
582
- * a new user joins a room, a "full presence" will be announced so the newly
583
- * connected user will get each other's user full presence at least once. In
584
- * those cases, the `targetActor` field indicates the newly connected client,
585
- * so all other existing clients can ignore this broadcasted message.
586
- */
587
- declare type UpdatePresenceServerMsg<TPresence extends JsonObject> = {
588
- type: ServerMsgCode.UPDATE_PRESENCE;
589
- /**
590
- * The User whose Presence has changed.
591
- */
592
- actor: number;
593
- /**
594
- * When set, signifies that this is a Full Presenceβ„’ update, not a patch.
595
- *
596
- * The numeric value itself no longer has specific meaning. Historically,
597
- * this field was intended so that clients could ignore these broadcasted
598
- * full presence messages, but it turned out that getting a full presence
599
- * "keyframe" from time to time was useful.
600
- *
601
- * So nowadays, the presence (pun intended) of this `targetActor` field
602
- * is a backward-compatible way of expressing that the `data` contains
603
- * all presence fields, and isn't a partial "patch".
604
- */
605
- targetActor: number;
606
- /**
607
- * The partial or full Presence of a User. If the `targetActor` field is set,
608
- * this will be the full Presence, otherwise it only contain the fields that
609
- * have changed since the last broadcast.
610
- */
611
- data: TPresence;
612
- } | {
613
- type: ServerMsgCode.UPDATE_PRESENCE;
614
- /**
615
- * The User whose Presence has changed.
616
- */
617
- actor: number;
573
+ declare type ReadonlyArrayWithLegacyMethods<T> = readonly T[] & {
618
574
  /**
619
- * Not set for partial presence updates.
575
+ * @deprecated Prefer the normal .length property on arrays.
620
576
  */
621
- targetActor?: undefined;
577
+ readonly count: number;
622
578
  /**
623
- * A partial Presence patch to apply to the User. It will only contain the
624
- * fields that have changed since the last broadcast.
579
+ * @deprecated Calling .toArray() is no longer needed
625
580
  */
626
- data: Partial<TPresence>;
581
+ readonly toArray: () => readonly T[];
627
582
  };
583
+ declare function asArrayWithLegacyMethods<T>(arr: readonly T[]): ReadonlyArrayWithLegacyMethods<T>;
584
+
628
585
  /**
629
- * Sent by the WebSocket server and broadcasted to all clients to announce that
630
- * a new User has joined the Room.
586
+ * Represents a user connected in a room. Treated as immutable.
631
587
  */
632
- declare type UserJoinServerMsg<TUserMeta extends BaseUserMeta> = {
633
- type: ServerMsgCode.USER_JOINED;
634
- actor: number;
588
+ declare type User<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = {
635
589
  /**
636
- * The id of the User that has been set in the authentication endpoint.
637
- * Useful to get additional information about the connected user.
590
+ * The connection id of the user. It is unique and increment at every new connection.
638
591
  */
639
- id: TUserMeta["id"];
592
+ readonly connectionId: number;
640
593
  /**
641
- * Additional user information that has been set in the authentication
642
- * endpoint.
594
+ * The id of the user that has been set in the authentication endpoint.
595
+ * Useful to get additional information about the connected user.
643
596
  */
644
- info: TUserMeta["info"];
597
+ readonly id: TUserMeta["id"];
645
598
  /**
646
- * Permissions that the user has in the Room.
599
+ * Additional user information that has been set in the authentication endpoint.
647
600
  */
648
- scopes: string[];
649
- };
650
- /**
651
- * Sent by the WebSocket server and broadcasted to all clients to announce that
652
- * a new User has left the Room.
653
- */
654
- declare type UserLeftServerMsg = {
655
- type: ServerMsgCode.USER_LEFT;
656
- actor: number;
657
- };
658
- /**
659
- * Sent by the WebSocket server and broadcasted to all clients to announce that
660
- * a User broadcasted an Event to everyone in the Room.
661
- */
662
- declare type BroadcastedEventServerMsg<TRoomEvent extends Json> = {
663
- type: ServerMsgCode.BROADCASTED_EVENT;
601
+ readonly info: TUserMeta["info"];
664
602
  /**
665
- * The User who broadcasted the Event.
603
+ * The user presence.
666
604
  */
667
- actor: number;
605
+ readonly presence: TPresence;
668
606
  /**
669
- * The arbitrary payload of the Event. This can be any JSON value. Clients
670
- * will have to manually verify/decode this event.
607
+ * False if the user can modify the room storage, true otherwise.
671
608
  */
672
- event: TRoomEvent;
609
+ readonly isReadOnly: boolean;
673
610
  };
611
+
674
612
  /**
675
- * Sent by the WebSocket server to a single client in response to the client
676
- * joining the Room, to provide the initial state of the Room. The payload
677
- * includes a list of all other Users that already are in the Room.
613
+ * Represents all the other users connected in the room. Treated as immutable.
678
614
  */
679
- declare type RoomStateServerMsg<TUserMeta extends BaseUserMeta> = {
680
- type: ServerMsgCode.ROOM_STATE;
681
- users: {
682
- [actor: number]: TUserMeta & {
683
- scopes: string[];
684
- };
685
- };
686
- };
687
- /**
688
- * Sent by the WebSocket server to a single client in response to the client
689
- * joining the Room, to provide the initial Storage state of the Room. The
690
- * payload includes the entire Storage document.
691
- */
692
- declare type InitialDocumentStateServerMsg = {
693
- type: ServerMsgCode.INITIAL_STORAGE_STATE;
694
- items: IdTuple<SerializedCrdt>[];
695
- };
696
- /**
697
- * Sent by the WebSocket server and broadcasted to all clients to announce that
698
- * a change occurred in the Storage document.
699
- *
700
- * The payload of this message contains a list of Ops (aka incremental
701
- * mutations to make to the initially loaded document).
702
- */
703
- declare type UpdateStorageServerMsg = {
704
- type: ServerMsgCode.UPDATE_STORAGE;
705
- ops: Op[];
615
+ declare type Others<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = ReadonlyArrayWithLegacyMethods<User<TPresence, TUserMeta>>;
616
+ declare type OthersEvent<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = {
617
+ type: "leave";
618
+ user: User<TPresence, TUserMeta>;
619
+ } | {
620
+ type: "enter";
621
+ user: User<TPresence, TUserMeta>;
622
+ } | {
623
+ type: "update";
624
+ user: User<TPresence, TUserMeta>;
625
+ updates: Partial<TPresence>;
626
+ } | {
627
+ type: "reset";
706
628
  };
707
629
 
708
- /**
709
- * This helper type is effectively a no-op, but will force TypeScript to
710
- * "evaluate" any named helper types in its definition. This can sometimes make
711
- * API signatures clearer in IDEs.
712
- *
713
- * For example, in:
714
- *
715
- * type Payload<T> = { data: T };
716
- *
717
- * let r1: Payload<string>;
718
- * let r2: Resolve<Payload<string>>;
719
- *
720
- * The inferred type of `r1` is going to be `Payload<string>` which shows up in
721
- * editor hints, and it may be unclear what's inside if you don't know the
722
- * definition of `Payload`.
723
- *
724
- * The inferred type of `r2` is going to be `{ data: string }`, which may be
725
- * more helpful.
726
- *
727
- * This trick comes from:
728
- * https://effectivetypescript.com/2022/02/25/gentips-4-display/
729
- */
730
- declare type Resolve<T> = T extends (...args: unknown[]) => unknown ? T : {
731
- [K in keyof T]: T[K];
732
- };
733
630
  declare type CustomEvent<TRoomEvent extends Json> = {
734
631
  connectionId: number;
735
632
  event: TRoomEvent;
736
633
  };
737
- declare type UpdateDelta = {
738
- type: "update";
739
- } | {
740
- type: "delete";
741
- };
742
- /**
743
- * A LiveMap notification that is sent in-client to any subscribers whenever
744
- * one or more of the values inside the LiveMap instance have changed.
745
- */
746
- declare type LiveMapUpdates<TKey extends string, TValue extends Lson> = {
747
- type: "LiveMap";
748
- node: LiveMap<TKey, TValue>;
749
- updates: {
750
- [key: string]: UpdateDelta;
751
- };
752
- };
753
- declare type LiveObjectUpdateDelta<O extends {
754
- [key: string]: unknown;
755
- }> = {
756
- [K in keyof O]?: UpdateDelta | undefined;
757
- };
758
- /**
759
- * A LiveObject notification that is sent in-client to any subscribers whenever
760
- * one or more of the entries inside the LiveObject instance have changed.
761
- */
762
- declare type LiveObjectUpdates<TData extends LsonObject> = {
763
- type: "LiveObject";
764
- node: LiveObject<TData>;
765
- updates: LiveObjectUpdateDelta<TData>;
766
- };
767
- declare type LiveListUpdateDelta = {
768
- index: number;
769
- item: Lson;
770
- type: "insert";
771
- } | {
772
- index: number;
773
- type: "delete";
774
- } | {
775
- index: number;
776
- previousIndex: number;
777
- item: Lson;
778
- type: "move";
779
- } | {
780
- index: number;
781
- item: Lson;
782
- type: "set";
783
- };
784
- /**
785
- * A LiveList notification that is sent in-client to any subscribers whenever
786
- * one or more of the items inside the LiveList instance have changed.
787
- */
788
- declare type LiveListUpdates<TItem extends Lson> = {
789
- type: "LiveList";
790
- node: LiveList<TItem>;
791
- updates: LiveListUpdateDelta[];
792
- };
793
- declare type BroadcastOptions = {
794
- /**
795
- * Whether or not event is queued if the connection is currently closed.
796
- *
797
- * ❗ 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
798
- */
799
- shouldQueueEventIfNotReady: boolean;
800
- };
801
- /**
802
- * The payload of notifications sent (in-client) when LiveStructures change.
803
- * Messages of this kind are not originating from the network, but are 100%
804
- * in-client.
805
- */
806
- declare type StorageUpdate = LiveMapUpdates<string, Lson> | LiveObjectUpdates<LsonObject> | LiveListUpdates<Lson>;
807
- declare type StorageCallback = (updates: StorageUpdate[]) => void;
808
- declare type RoomInitializers<TPresence extends JsonObject, TStorage extends LsonObject> = Resolve<{
809
- /**
810
- * The initial Presence to use and announce when you enter the Room. The
811
- * Presence is available on all users in the Room (me & others).
812
- */
813
- initialPresence: TPresence | ((roomId: string) => TPresence);
814
- /**
815
- * The initial Storage to use when entering a new Room.
816
- */
817
- initialStorage?: TStorage | ((roomId: string) => TStorage);
818
- /**
819
- * Whether or not the room connects to Liveblock servers. Default is true.
820
- *
821
- * Usually set to false when the client is used from the server to not call
822
- * the authentication endpoint or connect via WebSocket.
823
- */
824
- shouldInitiallyConnect?: boolean;
825
- }>;
826
- declare type Client = {
827
- /**
828
- * Gets a room. Returns null if {@link Client.enter} has not been called previously.
829
- *
830
- * @param roomId The id of the room
831
- */
832
- getRoom<TPresence extends JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = never>(roomId: string): Room<TPresence, TStorage, TUserMeta, TRoomEvent> | null;
833
- /**
834
- * Enters a room and returns it.
835
- * @param roomId The id of the room
836
- * @param options Optional. You can provide initializers for the Presence or Storage when entering the Room.
837
- */
838
- enter<TPresence extends JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = never>(roomId: string, options: RoomInitializers<TPresence, TStorage>): Room<TPresence, TStorage, TUserMeta, TRoomEvent>;
839
- /**
840
- * Leaves a room.
841
- * @param roomId The id of the room
842
- */
843
- leave(roomId: string): void;
844
- };
845
- /**
846
- * Represents all the other users connected in the room. Treated as immutable.
847
- */
848
- declare type Others<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = ReadonlyArrayWithLegacyMethods<User<TPresence, TUserMeta>>;
849
- /**
850
- * Represents a user connected in a room. Treated as immutable.
851
- */
852
- declare type User<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = {
853
- /**
854
- * The connection id of the user. It is unique and increment at every new connection.
855
- */
856
- readonly connectionId: number;
857
- /**
858
- * The id of the user that has been set in the authentication endpoint.
859
- * Useful to get additional information about the connected user.
860
- */
861
- readonly id: TUserMeta["id"];
862
- /**
863
- * Additional user information that has been set in the authentication endpoint.
864
- */
865
- readonly info: TUserMeta["info"];
866
- /**
867
- * The user presence.
868
- */
869
- readonly presence: TPresence;
870
- /**
871
- * False if the user can modify the room storage, true otherwise.
872
- */
873
- readonly isReadOnly: boolean;
874
- };
875
- declare type AuthEndpointCallback = (room: string) => Promise<{
876
- token: string;
877
- }>;
878
- declare type AuthEndpoint = string | AuthEndpointCallback;
879
- declare type Polyfills = {
880
- atob?: (data: string) => string;
881
- fetch?: typeof fetch;
882
- WebSocket?: any;
883
- };
884
- /**
885
- * The authentication endpoint that is called to ensure that the current user has access to a room.
886
- * Can be an url or a callback if you need to add additional headers.
887
- */
888
- declare type ClientOptions = {
889
- throttle?: number;
890
- polyfills?: Polyfills;
891
- /**
892
- * Backward-compatible way to set `polyfills.fetch`.
893
- */
894
- fetchPolyfill?: Polyfills["fetch"];
895
- /**
896
- * Backward-compatible way to set `polyfills.WebSocket`.
897
- */
898
- WebSocketPolyfill?: Polyfills["WebSocket"];
899
- } & ({
900
- publicApiKey: string;
901
- authEndpoint?: never;
902
- } | {
903
- publicApiKey?: never;
904
- authEndpoint: AuthEndpoint;
905
- });
906
634
  declare type Connection = {
907
635
  state: "closed";
908
636
  } | {
@@ -925,19 +653,6 @@ declare type Connection = {
925
653
  state: "failed";
926
654
  };
927
655
  declare type ConnectionState = Connection["state"];
928
- declare type OthersEvent<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = {
929
- type: "leave";
930
- user: User<TPresence, TUserMeta>;
931
- } | {
932
- type: "enter";
933
- user: User<TPresence, TUserMeta>;
934
- } | {
935
- type: "update";
936
- user: User<TPresence, TUserMeta>;
937
- updates: Partial<TPresence>;
938
- } | {
939
- type: "reset";
940
- };
941
656
  interface History {
942
657
  /**
943
658
  * Undoes the last operation executed by the current client.
@@ -1015,6 +730,14 @@ interface HistoryEvent {
1015
730
  canUndo: boolean;
1016
731
  canRedo: boolean;
1017
732
  }
733
+ declare type BroadcastOptions = {
734
+ /**
735
+ * Whether or not event is queued if the connection is currently closed.
736
+ *
737
+ * ❗ 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
738
+ */
739
+ shouldQueueEventIfNotReady: boolean;
740
+ };
1018
741
  declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = {
1019
742
  /**
1020
743
  * The id of the room.
@@ -1240,36 +963,74 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
1240
963
  */
1241
964
  batch<T>(fn: () => T): T;
1242
965
  };
1243
- declare enum WebsocketCloseCodes {
1244
- CLOSE_ABNORMAL = 1006,
1245
- INVALID_MESSAGE_FORMAT = 4000,
1246
- NOT_ALLOWED = 4001,
1247
- MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
1248
- MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
1249
- MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
1250
- MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
1251
- CLOSE_WITHOUT_RETRY = 4999
1252
- }
1253
-
1254
- declare type AppOnlyAuthToken = {
1255
- appId: string;
1256
- roomId?: never;
1257
- scopes: string[];
966
+ declare type Polyfills = {
967
+ atob?: (data: string) => string;
968
+ fetch?: typeof fetch;
969
+ WebSocket?: any;
1258
970
  };
1259
- declare type RoomAuthToken = {
1260
- appId: string;
1261
- roomId: string;
1262
- scopes: string[];
1263
- actor: number;
1264
- maxConnectionsPerRoom?: number;
1265
- id?: string;
1266
- info?: Json;
1267
- };
1268
- declare type AuthToken = AppOnlyAuthToken | RoomAuthToken;
1269
- declare function isAppOnlyAuthToken(data: JsonObject): data is AppOnlyAuthToken;
1270
- declare function isRoomAuthToken(data: JsonObject): data is RoomAuthToken;
1271
- declare function isAuthToken(data: JsonObject): data is AuthToken;
971
+ declare type RoomInitializers<TPresence extends JsonObject, TStorage extends LsonObject> = Resolve<{
972
+ /**
973
+ * The initial Presence to use and announce when you enter the Room. The
974
+ * Presence is available on all users in the Room (me & others).
975
+ */
976
+ initialPresence: TPresence | ((roomId: string) => TPresence);
977
+ /**
978
+ * The initial Storage to use when entering a new Room.
979
+ */
980
+ initialStorage?: TStorage | ((roomId: string) => TStorage);
981
+ /**
982
+ * Whether or not the room connects to Liveblock servers. Default is true.
983
+ *
984
+ * Usually set to false when the client is used from the server to not call
985
+ * the authentication endpoint or connect via WebSocket.
986
+ */
987
+ shouldInitiallyConnect?: boolean;
988
+ }>;
1272
989
 
990
+ declare type Client = {
991
+ /**
992
+ * Gets a room. Returns null if {@link Client.enter} has not been called previously.
993
+ *
994
+ * @param roomId The id of the room
995
+ */
996
+ getRoom<TPresence extends JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = never>(roomId: string): Room<TPresence, TStorage, TUserMeta, TRoomEvent> | null;
997
+ /**
998
+ * Enters a room and returns it.
999
+ * @param roomId The id of the room
1000
+ * @param options Optional. You can provide initializers for the Presence or Storage when entering the Room.
1001
+ */
1002
+ enter<TPresence extends JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = never>(roomId: string, options: RoomInitializers<TPresence, TStorage>): Room<TPresence, TStorage, TUserMeta, TRoomEvent>;
1003
+ /**
1004
+ * Leaves a room.
1005
+ * @param roomId The id of the room
1006
+ */
1007
+ leave(roomId: string): void;
1008
+ };
1009
+ declare type AuthEndpoint = string | ((room: string) => Promise<{
1010
+ token: string;
1011
+ }>);
1012
+ /**
1013
+ * The authentication endpoint that is called to ensure that the current user has access to a room.
1014
+ * Can be an url or a callback if you need to add additional headers.
1015
+ */
1016
+ declare type ClientOptions = {
1017
+ throttle?: number;
1018
+ polyfills?: Polyfills;
1019
+ /**
1020
+ * Backward-compatible way to set `polyfills.fetch`.
1021
+ */
1022
+ fetchPolyfill?: Polyfills["fetch"];
1023
+ /**
1024
+ * Backward-compatible way to set `polyfills.WebSocket`.
1025
+ */
1026
+ WebSocketPolyfill?: Polyfills["WebSocket"];
1027
+ } & ({
1028
+ publicApiKey: string;
1029
+ authEndpoint?: never;
1030
+ } | {
1031
+ publicApiKey?: never;
1032
+ authEndpoint: AuthEndpoint;
1033
+ });
1273
1034
  /**
1274
1035
  * Create a client that will be responsible to communicate with liveblocks servers.
1275
1036
  *
@@ -1297,6 +1058,46 @@ declare function isAuthToken(data: JsonObject): data is AuthToken;
1297
1058
  */
1298
1059
  declare function createClient(options: ClientOptions): Client;
1299
1060
 
1061
+ declare function lsonToJson(value: Lson): Json;
1062
+ declare function patchLiveObjectKey<O extends LsonObject, K extends keyof O, V extends Json>(liveObject: LiveObject<O>, key: K, prev?: V, next?: V): void;
1063
+ declare function legacy_patchImmutableObject<S extends JsonObject>(state: S, updates: StorageUpdate[]): S;
1064
+
1065
+ /**
1066
+ * Helper function that can be used to implement exhaustive switch statements
1067
+ * with TypeScript. Example usage:
1068
+ *
1069
+ * type Fruit = "🍎" | "🍌";
1070
+ *
1071
+ * switch (fruit) {
1072
+ * case "🍎":
1073
+ * case "🍌":
1074
+ * return doSomething();
1075
+ *
1076
+ * default:
1077
+ * return assertNever(fruit, "Unknown fruit");
1078
+ * }
1079
+ *
1080
+ * If now the Fruit union is extended (i.e. add "πŸ’"), TypeScript will catch
1081
+ * this *statically*, rather than at runtime, and force you to handle the
1082
+ * πŸ’ case.
1083
+ */
1084
+ declare function assertNever(_value: never, errmsg: string): never;
1085
+ /**
1086
+ * Asserts that a given value is non-nullable. This is similar to TypeScript's
1087
+ * `!` operator, but will throw an error at runtime (dev-mode only) indicating
1088
+ * an incorrect assumption.
1089
+ *
1090
+ * Instead of:
1091
+ *
1092
+ * foo!.bar
1093
+ *
1094
+ * Use:
1095
+ *
1096
+ * nn(foo).bar
1097
+ *
1098
+ */
1099
+ declare function nn<T>(value: T, errmsg?: string): NonNullable<T>;
1100
+
1300
1101
  /**
1301
1102
  * Displays a deprecation warning in the dev console. Only in dev mode, and
1302
1103
  * only once per message/key. In production, this is a no-op.
@@ -1323,9 +1124,11 @@ declare function throwUsageError(message: string): void;
1323
1124
  */
1324
1125
  declare function errorIf(condition: unknown, message: string): void;
1325
1126
 
1326
- declare function lsonToJson(value: Lson): Json;
1327
- declare function patchLiveObjectKey<O extends LsonObject, K extends keyof O, V extends Json>(liveObject: LiveObject<O>, key: K, prev?: V, next?: V): void;
1328
- declare function legacy_patchImmutableObject<S extends JsonObject>(state: S, updates: StorageUpdate[]): S;
1127
+ /**
1128
+ * Freezes the given argument, but only in development builds. In production
1129
+ * builds, this is a no-op for performance reasons.
1130
+ */
1131
+ declare const freeze: typeof Object.freeze;
1329
1132
 
1330
1133
  declare function makePosition(before?: string, after?: string): string;
1331
1134
  declare function comparePosition(posA: string, posB: string): number;
@@ -1341,11 +1144,6 @@ declare function comparePosition(posA: string, posB: string): number;
1341
1144
  */
1342
1145
  declare function shallow(a: unknown, b: unknown): boolean;
1343
1146
 
1344
- /**
1345
- * Freezes the given argument, but only in development builds. In production
1346
- * builds, this is a no-op for performance reasons.
1347
- */
1348
- declare const freeze: typeof Object.freeze;
1349
1147
  declare function isPlainObject(blob: unknown): blob is {
1350
1148
  [key: string]: unknown;
1351
1149
  };
@@ -1359,6 +1157,214 @@ declare function tryParseJson(rawMessage: string): Json | undefined;
1359
1157
  */
1360
1158
  declare function b64decode(b64value: string): string;
1361
1159
 
1160
+ declare type IdTuple<T> = [id: string, value: T];
1161
+ declare enum CrdtType {
1162
+ OBJECT = 0,
1163
+ LIST = 1,
1164
+ MAP = 2,
1165
+ REGISTER = 3
1166
+ }
1167
+ declare type SerializedCrdt = SerializedRootObject | SerializedChild;
1168
+ declare type SerializedChild = SerializedObject | SerializedList | SerializedMap | SerializedRegister;
1169
+ declare type SerializedRootObject = {
1170
+ type: CrdtType.OBJECT;
1171
+ data: JsonObject;
1172
+ parentId?: never;
1173
+ parentKey?: never;
1174
+ };
1175
+ declare type SerializedObject = {
1176
+ type: CrdtType.OBJECT;
1177
+ parentId: string;
1178
+ parentKey: string;
1179
+ data: JsonObject;
1180
+ };
1181
+ declare type SerializedList = {
1182
+ type: CrdtType.LIST;
1183
+ parentId: string;
1184
+ parentKey: string;
1185
+ };
1186
+ declare type SerializedMap = {
1187
+ type: CrdtType.MAP;
1188
+ parentId: string;
1189
+ parentKey: string;
1190
+ };
1191
+ declare type SerializedRegister = {
1192
+ type: CrdtType.REGISTER;
1193
+ parentId: string;
1194
+ parentKey: string;
1195
+ data: Json;
1196
+ };
1197
+ declare function isRootCrdt(crdt: SerializedCrdt): crdt is SerializedRootObject;
1198
+ declare function isChildCrdt(crdt: SerializedCrdt): crdt is SerializedChild;
1199
+
1200
+ declare enum ServerMsgCode {
1201
+ UPDATE_PRESENCE = 100,
1202
+ USER_JOINED = 101,
1203
+ USER_LEFT = 102,
1204
+ BROADCASTED_EVENT = 103,
1205
+ ROOM_STATE = 104,
1206
+ INITIAL_STORAGE_STATE = 200,
1207
+ UPDATE_STORAGE = 201
1208
+ }
1209
+ /**
1210
+ * Messages that can be sent from the server to the client.
1211
+ */
1212
+ declare type ServerMsg<TPresence extends JsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg<TUserMeta> | UserLeftServerMsg | BroadcastedEventServerMsg<TRoomEvent> | RoomStateServerMsg<TUserMeta> | InitialDocumentStateServerMsg | UpdateStorageServerMsg;
1213
+ /**
1214
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
1215
+ * a User updated their presence. For example, when a user moves their cursor.
1216
+ *
1217
+ * In most cases, the data payload will only include the fields from the
1218
+ * Presence that have been changed since the last announcement. However, after
1219
+ * a new user joins a room, a "full presence" will be announced so the newly
1220
+ * connected user will get each other's user full presence at least once. In
1221
+ * those cases, the `targetActor` field indicates the newly connected client,
1222
+ * so all other existing clients can ignore this broadcasted message.
1223
+ */
1224
+ declare type UpdatePresenceServerMsg<TPresence extends JsonObject> = {
1225
+ type: ServerMsgCode.UPDATE_PRESENCE;
1226
+ /**
1227
+ * The User whose Presence has changed.
1228
+ */
1229
+ actor: number;
1230
+ /**
1231
+ * When set, signifies that this is a Full Presenceβ„’ update, not a patch.
1232
+ *
1233
+ * The numeric value itself no longer has specific meaning. Historically,
1234
+ * this field was intended so that clients could ignore these broadcasted
1235
+ * full presence messages, but it turned out that getting a full presence
1236
+ * "keyframe" from time to time was useful.
1237
+ *
1238
+ * So nowadays, the presence (pun intended) of this `targetActor` field
1239
+ * is a backward-compatible way of expressing that the `data` contains
1240
+ * all presence fields, and isn't a partial "patch".
1241
+ */
1242
+ targetActor: number;
1243
+ /**
1244
+ * The partial or full Presence of a User. If the `targetActor` field is set,
1245
+ * this will be the full Presence, otherwise it only contain the fields that
1246
+ * have changed since the last broadcast.
1247
+ */
1248
+ data: TPresence;
1249
+ } | {
1250
+ type: ServerMsgCode.UPDATE_PRESENCE;
1251
+ /**
1252
+ * The User whose Presence has changed.
1253
+ */
1254
+ actor: number;
1255
+ /**
1256
+ * Not set for partial presence updates.
1257
+ */
1258
+ targetActor?: undefined;
1259
+ /**
1260
+ * A partial Presence patch to apply to the User. It will only contain the
1261
+ * fields that have changed since the last broadcast.
1262
+ */
1263
+ data: Partial<TPresence>;
1264
+ };
1265
+ /**
1266
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
1267
+ * a new User has joined the Room.
1268
+ */
1269
+ declare type UserJoinServerMsg<TUserMeta extends BaseUserMeta> = {
1270
+ type: ServerMsgCode.USER_JOINED;
1271
+ actor: number;
1272
+ /**
1273
+ * The id of the User that has been set in the authentication endpoint.
1274
+ * Useful to get additional information about the connected user.
1275
+ */
1276
+ id: TUserMeta["id"];
1277
+ /**
1278
+ * Additional user information that has been set in the authentication
1279
+ * endpoint.
1280
+ */
1281
+ info: TUserMeta["info"];
1282
+ /**
1283
+ * Permissions that the user has in the Room.
1284
+ */
1285
+ scopes: string[];
1286
+ };
1287
+ /**
1288
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
1289
+ * a new User has left the Room.
1290
+ */
1291
+ declare type UserLeftServerMsg = {
1292
+ type: ServerMsgCode.USER_LEFT;
1293
+ actor: number;
1294
+ };
1295
+ /**
1296
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
1297
+ * a User broadcasted an Event to everyone in the Room.
1298
+ */
1299
+ declare type BroadcastedEventServerMsg<TRoomEvent extends Json> = {
1300
+ type: ServerMsgCode.BROADCASTED_EVENT;
1301
+ /**
1302
+ * The User who broadcasted the Event.
1303
+ */
1304
+ actor: number;
1305
+ /**
1306
+ * The arbitrary payload of the Event. This can be any JSON value. Clients
1307
+ * will have to manually verify/decode this event.
1308
+ */
1309
+ event: TRoomEvent;
1310
+ };
1311
+ /**
1312
+ * Sent by the WebSocket server to a single client in response to the client
1313
+ * joining the Room, to provide the initial state of the Room. The payload
1314
+ * includes a list of all other Users that already are in the Room.
1315
+ */
1316
+ declare type RoomStateServerMsg<TUserMeta extends BaseUserMeta> = {
1317
+ type: ServerMsgCode.ROOM_STATE;
1318
+ users: {
1319
+ [actor: number]: TUserMeta & {
1320
+ scopes: string[];
1321
+ };
1322
+ };
1323
+ };
1324
+ /**
1325
+ * Sent by the WebSocket server to a single client in response to the client
1326
+ * joining the Room, to provide the initial Storage state of the Room. The
1327
+ * payload includes the entire Storage document.
1328
+ */
1329
+ declare type InitialDocumentStateServerMsg = {
1330
+ type: ServerMsgCode.INITIAL_STORAGE_STATE;
1331
+ items: IdTuple<SerializedCrdt>[];
1332
+ };
1333
+ /**
1334
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
1335
+ * a change occurred in the Storage document.
1336
+ *
1337
+ * The payload of this message contains a list of Ops (aka incremental
1338
+ * mutations to make to the initially loaded document).
1339
+ */
1340
+ declare type UpdateStorageServerMsg = {
1341
+ type: ServerMsgCode.UPDATE_STORAGE;
1342
+ ops: Op[];
1343
+ };
1344
+
1345
+ /**
1346
+ * Lookup table for nodes (= SerializedCrdt values) by their IDs.
1347
+ */
1348
+ declare type NodeMap = Map<string, // Node ID
1349
+ SerializedCrdt>;
1350
+ /**
1351
+ * Reverse lookup table for all child nodes (= list of SerializedCrdt values)
1352
+ * by their parent node's IDs.
1353
+ */
1354
+ declare type ParentToChildNodeMap = Map<string, // Parent's node ID
1355
+ IdTuple<SerializedChild>[]>;
1356
+
1357
+ declare enum WebsocketCloseCodes {
1358
+ CLOSE_ABNORMAL = 1006,
1359
+ INVALID_MESSAGE_FORMAT = 4000,
1360
+ NOT_ALLOWED = 4001,
1361
+ MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
1362
+ MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
1363
+ MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
1364
+ MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
1365
+ CLOSE_WITHOUT_RETRY = 4999
1366
+ }
1367
+
1362
1368
  /**
1363
1369
  * PRIVATE / INTERNAL APIS
1364
1370
  * -----------------------