@liveblocks/client 0.18.3 → 0.18.4

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.
@@ -1,869 +0,0 @@
1
- declare type Callback<T> = (event: T) => void;
2
- declare type UnsubscribeCallback = () => void;
3
- declare type Observable<T> = {
4
- subscribe(callback: Callback<T>): UnsubscribeCallback;
5
- subscribeOnce(callback: Callback<T>): UnsubscribeCallback;
6
- };
7
-
8
- declare type ReadonlyArrayWithLegacyMethods<T> = readonly T[] & {
9
- /**
10
- * @deprecated Prefer the normal .length property on arrays.
11
- */
12
- readonly count: number;
13
- /**
14
- * @deprecated Calling .toArray() is no longer needed
15
- */
16
- readonly toArray: () => readonly T[];
17
- };
18
- declare function asArrayWithLegacyMethods<T>(arr: readonly T[]): ReadonlyArrayWithLegacyMethods<T>;
19
-
20
- /**
21
- * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
22
- * Keys should be a string, and values should be serializable to JSON.
23
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
24
- */
25
- declare class LiveMap<TKey extends string, TValue extends Lson> extends AbstractCrdt {
26
- constructor(entries?: readonly (readonly [TKey, TValue])[] | undefined);
27
- /**
28
- * Returns a specified element from the LiveMap.
29
- * @param key The key of the element to return.
30
- * @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
31
- */
32
- get(key: TKey): TValue | undefined;
33
- /**
34
- * Adds or updates an element with a specified key and a value.
35
- * @param key The key of the element to add. Should be a string.
36
- * @param value The value of the element to add. Should be serializable to JSON.
37
- */
38
- set(key: TKey, value: TValue): void;
39
- /**
40
- * Returns the number of elements in the LiveMap.
41
- */
42
- get size(): number;
43
- /**
44
- * Returns a boolean indicating whether an element with the specified key exists or not.
45
- * @param key The key of the element to test for presence.
46
- */
47
- has(key: TKey): boolean;
48
- /**
49
- * Removes the specified element by key.
50
- * @param key The key of the element to remove.
51
- * @returns true if an element existed and has been removed, or false if the element does not exist.
52
- */
53
- delete(key: TKey): boolean;
54
- /**
55
- * Returns a new Iterator object that contains the [key, value] pairs for each element.
56
- */
57
- entries(): IterableIterator<[TKey, TValue]>;
58
- /**
59
- * Same function object as the initial value of the entries method.
60
- */
61
- [Symbol.iterator](): IterableIterator<[TKey, TValue]>;
62
- /**
63
- * Returns a new Iterator object that contains the keys for each element.
64
- */
65
- keys(): IterableIterator<TKey>;
66
- /**
67
- * Returns a new Iterator object that contains the values for each element.
68
- */
69
- values(): IterableIterator<TValue>;
70
- /**
71
- * Executes a provided function once per each key/value pair in the Map object, in insertion order.
72
- * @param callback Function to execute for each entry in the map.
73
- */
74
- forEach(callback: (value: TValue, key: TKey, map: LiveMap<TKey, TValue>) => void): void;
75
- toImmutable(): ReadonlyMap<TKey, ToImmutable<TValue>>;
76
- }
77
-
78
- /**
79
- * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
80
- * Keys should be a string, and values should be serializable to JSON.
81
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
82
- */
83
- declare class LiveObject<O extends LsonObject> extends AbstractCrdt {
84
- constructor(obj?: O);
85
- /**
86
- * Transform the LiveObject into a javascript object
87
- */
88
- toObject(): O;
89
- /**
90
- * Adds or updates a property with a specified key and a value.
91
- * @param key The key of the property to add
92
- * @param value The value of the property to add
93
- */
94
- set<TKey extends keyof O>(key: TKey, value: O[TKey]): void;
95
- /**
96
- * Returns a specified property from the LiveObject.
97
- * @param key The key of the property to get
98
- */
99
- get<TKey extends keyof O>(key: TKey): O[TKey];
100
- /**
101
- * Deletes a key from the LiveObject
102
- * @param key The key of the property to delete
103
- */
104
- delete(key: keyof O): void;
105
- /**
106
- * Adds or updates multiple properties at once with an object.
107
- * @param patch The object used to overrides properties
108
- */
109
- update(patch: Partial<O>): void;
110
- toImmutable(): ToImmutable<O>;
111
- }
112
-
113
- /**
114
- * Represents an indefinitely deep arbitrary JSON data structure. There are
115
- * four types that make up the Json family:
116
- *
117
- * - Json any legal JSON value
118
- * - JsonScalar any legal JSON leaf value (no lists or objects)
119
- * - JsonArray a JSON value whose outer type is an array
120
- * - JsonObject a JSON value whose outer type is an object
121
- *
122
- */
123
- declare type Json = JsonScalar | JsonArray | JsonObject;
124
- declare type JsonScalar = string | number | boolean | null;
125
- declare type JsonArray = Json[];
126
- declare type JsonObject = {
127
- [key: string]: Json | undefined;
128
- };
129
- declare function isJsonScalar(data: Json): data is JsonScalar;
130
- declare function isJsonArray(data: Json): data is JsonArray;
131
- declare function isJsonObject(data: Json): data is JsonObject;
132
-
133
- /**
134
- * INTERNAL
135
- */
136
- declare class LiveRegister<TValue extends Json> extends AbstractCrdt {
137
- constructor(data: TValue);
138
- get data(): TValue;
139
- }
140
-
141
- declare type LiveStructure = LiveObject<LsonObject> | LiveList<Lson> | LiveMap<string, Lson>;
142
- /**
143
- * Think of Lson as a sibling of the Json data tree, except that the nested
144
- * data structure can contain a mix of Json values and LiveStructure instances.
145
- */
146
- declare type Lson = Json | LiveStructure;
147
- /**
148
- * LiveNode is the internal tree for managing Live data structures. The key
149
- * difference with Lson is that all the Json values get represented in
150
- * a LiveRegister node.
151
- */
152
- declare type LiveNode = LiveStructure | LiveRegister<Json>;
153
- /**
154
- * A mapping of keys to Lson values. A Lson value is any valid JSON
155
- * value or a Live storage data structure (LiveMap, LiveList, etc.)
156
- */
157
- declare type LsonObject = {
158
- [key: string]: Lson | undefined;
159
- };
160
- /**
161
- * Helper type to convert any valid Lson type to the equivalent Json type.
162
- *
163
- * Examples:
164
- *
165
- * ToJson<42> // 42
166
- * ToJson<'hi'> // 'hi'
167
- * ToJson<number> // number
168
- * ToJson<string> // string
169
- * ToJson<string | LiveList<number>> // string | number[]
170
- * ToJson<LiveMap<string, LiveList<number>>>
171
- * // { [key: string]: number[] }
172
- * ToJson<LiveObject<{ a: number, b: LiveList<string>, c?: number }>>
173
- * // { a: null, b: string[], c?: number }
174
- *
175
- */
176
- declare type ToJson<T extends Lson | LsonObject> = T extends Json ? T : T extends LsonObject ? {
177
- [K in keyof T]: ToJson<Exclude<T[K], undefined>> | (undefined extends T[K] ? undefined : never);
178
- } : T extends LiveList<infer I> ? ToJson<I>[] : T extends LiveObject<infer O> ? ToJson<O> : T extends LiveMap<infer KS, infer V> ? {
179
- [K in KS]: ToJson<V>;
180
- } : never;
181
-
182
- /**
183
- * Represents an indefinitely deep arbitrary immutable data
184
- * structure, as returned by the .toImmutable().
185
- */
186
-
187
- declare type Immutable = Scalar | ImmutableList | ImmutableObject | ImmutableMap;
188
- declare type Scalar = string | number | boolean | null;
189
- declare type ImmutableList = readonly Immutable[];
190
- declare type ImmutableObject = {
191
- readonly [key: string]: Immutable | undefined;
192
- };
193
- declare type ImmutableMap = ReadonlyMap<string, Immutable>;
194
- /**
195
- * Helper type to convert any valid Lson type to the equivalent Json type.
196
- *
197
- * Examples:
198
- *
199
- * ToImmutable<42> // 42
200
- * ToImmutable<'hi'> // 'hi'
201
- * ToImmutable<number> // number
202
- * ToImmutable<string> // string
203
- * ToImmutable<string | LiveList<number>> // string | readonly number[]
204
- * ToImmutable<LiveMap<string, LiveList<number>>>
205
- * // { readonly [key: string]: readonly number[] }
206
- * ToImmutable<LiveObject<{ a: number, b: LiveList<string>, c?: number }>>
207
- * // { readonly a: null, readonly b: readonly string[], readonly c?: number }
208
- *
209
- */
210
- 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 ? {
211
- readonly [K in keyof L]: ToImmutable<Exclude<L[K], undefined>> | (undefined extends L[K] ? undefined : never);
212
- } : L extends Json ? L : never;
213
-
214
- declare abstract class AbstractCrdt {
215
- get roomId(): string | null;
216
- /**
217
- * Return an immutable snapshot of this Live node and its children.
218
- */
219
- toImmutable(): Immutable;
220
- }
221
-
222
- /**
223
- * The LiveList class represents an ordered collection of items that is synchronized across clients.
224
- */
225
- declare class LiveList<TItem extends Lson> extends AbstractCrdt {
226
- constructor(items?: TItem[]);
227
- /**
228
- * Returns the number of elements.
229
- */
230
- get length(): number;
231
- /**
232
- * Adds one element to the end of the LiveList.
233
- * @param element The element to add to the end of the LiveList.
234
- */
235
- push(element: TItem): void;
236
- /**
237
- * Inserts one element at a specified index.
238
- * @param element The element to insert.
239
- * @param index The index at which you want to insert the element.
240
- */
241
- insert(element: TItem, index: number): void;
242
- /**
243
- * Move one element from one index to another.
244
- * @param index The index of the element to move
245
- * @param targetIndex The index where the element should be after moving.
246
- */
247
- move(index: number, targetIndex: number): void;
248
- /**
249
- * Deletes an element at the specified index
250
- * @param index The index of the element to delete
251
- */
252
- delete(index: number): void;
253
- clear(): void;
254
- set(index: number, item: TItem): void;
255
- /**
256
- * Returns an Array of all the elements in the LiveList.
257
- */
258
- toArray(): TItem[];
259
- /**
260
- * Tests whether all elements pass the test implemented by the provided function.
261
- * @param predicate Function to test for each element, taking two arguments (the element and its index).
262
- * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
263
- */
264
- every(predicate: (value: TItem, index: number) => unknown): boolean;
265
- /**
266
- * Creates an array with all elements that pass the test implemented by the provided function.
267
- * @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.
268
- * @returns An array with the elements that pass the test.
269
- */
270
- filter(predicate: (value: TItem, index: number) => unknown): TItem[];
271
- /**
272
- * Returns the first element that satisfies the provided testing function.
273
- * @param predicate Function to execute on each value.
274
- * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
275
- */
276
- find(predicate: (value: TItem, index: number) => unknown): TItem | undefined;
277
- /**
278
- * Returns the index of the first element in the LiveList that satisfies the provided testing function.
279
- * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
280
- * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
281
- */
282
- findIndex(predicate: (value: TItem, index: number) => unknown): number;
283
- /**
284
- * Executes a provided function once for each element.
285
- * @param callbackfn Function to execute on each element.
286
- */
287
- forEach(callbackfn: (value: TItem, index: number) => void): void;
288
- /**
289
- * Get the element at the specified index.
290
- * @param index The index on the element to get.
291
- * @returns The element at the specified index or undefined.
292
- */
293
- get(index: number): TItem | undefined;
294
- /**
295
- * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
296
- * @param searchElement Element to locate.
297
- * @param fromIndex The index to start the search at.
298
- * @returns The first index of the element in the LiveList; -1 if not found.
299
- */
300
- indexOf(searchElement: TItem, fromIndex?: number): number;
301
- /**
302
- * 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.
303
- * @param searchElement Element to locate.
304
- * @param fromIndex The index at which to start searching backwards.
305
- * @returns
306
- */
307
- lastIndexOf(searchElement: TItem, fromIndex?: number): number;
308
- /**
309
- * Creates an array populated with the results of calling a provided function on every element.
310
- * @param callback Function that is called for every element.
311
- * @returns An array with each element being the result of the callback function.
312
- */
313
- map<U>(callback: (value: TItem, index: number) => U): U[];
314
- /**
315
- * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
316
- * @param predicate Function to test for each element.
317
- * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
318
- */
319
- some(predicate: (value: TItem, index: number) => unknown): boolean;
320
- [Symbol.iterator](): IterableIterator<TItem>;
321
- toImmutable(): readonly ToImmutable<TItem>[];
322
- }
323
-
324
- declare type BaseUserMeta = {
325
- /**
326
- * The id of the user that has been set in the authentication endpoint.
327
- * Useful to get additional information about the connected user.
328
- */
329
- id?: string;
330
- /**
331
- * Additional user information that has been set in the authentication endpoint.
332
- */
333
- info?: Json;
334
- };
335
-
336
- /**
337
- * This helper type is effectively a no-op, but will force TypeScript to
338
- * "evaluate" any named helper types in its definition. This can sometimes make
339
- * API signatures clearer in IDEs.
340
- *
341
- * For example, in:
342
- *
343
- * type Payload<T> = { data: T };
344
- *
345
- * let r1: Payload<string>;
346
- * let r2: Resolve<Payload<string>>;
347
- *
348
- * The inferred type of `r1` is going to be `Payload<string>` which shows up in
349
- * editor hints, and it may be unclear what's inside if you don't know the
350
- * definition of `Payload`.
351
- *
352
- * The inferred type of `r2` is going to be `{ data: string }`, which may be
353
- * more helpful.
354
- *
355
- * This trick comes from:
356
- * https://effectivetypescript.com/2022/02/25/gentips-4-display/
357
- */
358
- declare type Resolve<T> = T extends (...args: unknown[]) => unknown ? T : {
359
- [K in keyof T]: T[K];
360
- };
361
- declare type CustomEvent<TRoomEvent extends Json> = {
362
- connectionId: number;
363
- event: TRoomEvent;
364
- };
365
- declare type UpdateDelta = {
366
- type: "update";
367
- } | {
368
- type: "delete";
369
- };
370
- /**
371
- * A LiveMap notification that is sent in-client to any subscribers whenever
372
- * one or more of the values inside the LiveMap instance have changed.
373
- */
374
- declare type LiveMapUpdates<TKey extends string, TValue extends Lson> = {
375
- type: "LiveMap";
376
- node: LiveMap<TKey, TValue>;
377
- updates: {
378
- [key: string]: UpdateDelta;
379
- };
380
- };
381
- declare type LiveObjectUpdateDelta<O extends {
382
- [key: string]: unknown;
383
- }> = {
384
- [K in keyof O]?: UpdateDelta | undefined;
385
- };
386
- /**
387
- * A LiveObject notification that is sent in-client to any subscribers whenever
388
- * one or more of the entries inside the LiveObject instance have changed.
389
- */
390
- declare type LiveObjectUpdates<TData extends LsonObject> = {
391
- type: "LiveObject";
392
- node: LiveObject<TData>;
393
- updates: LiveObjectUpdateDelta<TData>;
394
- };
395
- declare type LiveListUpdateDelta = {
396
- index: number;
397
- item: Lson;
398
- type: "insert";
399
- } | {
400
- index: number;
401
- type: "delete";
402
- } | {
403
- index: number;
404
- previousIndex: number;
405
- item: Lson;
406
- type: "move";
407
- } | {
408
- index: number;
409
- item: Lson;
410
- type: "set";
411
- };
412
- /**
413
- * A LiveList notification that is sent in-client to any subscribers whenever
414
- * one or more of the items inside the LiveList instance have changed.
415
- */
416
- declare type LiveListUpdates<TItem extends Lson> = {
417
- type: "LiveList";
418
- node: LiveList<TItem>;
419
- updates: LiveListUpdateDelta[];
420
- };
421
- declare type BroadcastOptions = {
422
- /**
423
- * Whether or not event is queued if the connection is currently closed.
424
- *
425
- * ❗ 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
426
- */
427
- shouldQueueEventIfNotReady: boolean;
428
- };
429
- /**
430
- * The payload of notifications sent (in-client) when LiveStructures change.
431
- * Messages of this kind are not originating from the network, but are 100%
432
- * in-client.
433
- */
434
- declare type StorageUpdate = LiveMapUpdates<string, Lson> | LiveObjectUpdates<LsonObject> | LiveListUpdates<Lson>;
435
- declare type StorageCallback = (updates: StorageUpdate[]) => void;
436
- declare type RoomInitializers<TPresence extends JsonObject, TStorage extends LsonObject> = Resolve<{
437
- /**
438
- * The initial Presence to use and announce when you enter the Room. The
439
- * Presence is available on all users in the Room (me & others).
440
- */
441
- initialPresence: TPresence | ((roomId: string) => TPresence);
442
- /**
443
- * The initial Storage to use when entering a new Room.
444
- */
445
- initialStorage?: TStorage | ((roomId: string) => TStorage);
446
- }>;
447
- declare type Client = {
448
- /**
449
- * Gets a room. Returns null if {@link Client.enter} has not been called previously.
450
- *
451
- * @param roomId The id of the room
452
- */
453
- getRoom<TPresence extends JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = never>(roomId: string): Room<TPresence, TStorage, TUserMeta, TRoomEvent> | null;
454
- /**
455
- * Enters a room and returns it.
456
- * @param roomId The id of the room
457
- * @param options Optional. You can provide initializers for the Presence or Storage when entering the Room.
458
- */
459
- 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>;
460
- /**
461
- * Leaves a room.
462
- * @param roomId The id of the room
463
- */
464
- leave(roomId: string): void;
465
- };
466
- /**
467
- * Represents all the other users connected in the room. Treated as immutable.
468
- */
469
- declare type Others<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = ReadonlyArrayWithLegacyMethods<User<TPresence, TUserMeta>>;
470
- /**
471
- * Represents a user connected in a room. Treated as immutable.
472
- */
473
- declare type User<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = {
474
- /**
475
- * The connection id of the user. It is unique and increment at every new connection.
476
- */
477
- readonly connectionId: number;
478
- /**
479
- * The id of the user that has been set in the authentication endpoint.
480
- * Useful to get additional information about the connected user.
481
- */
482
- readonly id: TUserMeta["id"];
483
- /**
484
- * Additional user information that has been set in the authentication endpoint.
485
- */
486
- readonly info: TUserMeta["info"];
487
- /**
488
- * The user presence.
489
- */
490
- readonly presence: TPresence;
491
- };
492
- declare type AuthEndpointCallback = (room: string) => Promise<{
493
- token: string;
494
- }>;
495
- declare type AuthEndpoint = string | AuthEndpointCallback;
496
- declare type Polyfills = {
497
- atob?: (data: string) => string;
498
- fetch?: typeof fetch;
499
- WebSocket?: any;
500
- };
501
- /**
502
- * The authentication endpoint that is called to ensure that the current user has access to a room.
503
- * Can be an url or a callback if you need to add additional headers.
504
- */
505
- declare type ClientOptions = {
506
- throttle?: number;
507
- polyfills?: Polyfills;
508
- /**
509
- * Backward-compatible way to set `polyfills.fetch`.
510
- */
511
- fetchPolyfill?: Polyfills["fetch"];
512
- /**
513
- * Backward-compatible way to set `polyfills.WebSocket`.
514
- */
515
- WebSocketPolyfill?: Polyfills["WebSocket"];
516
- } & ({
517
- publicApiKey: string;
518
- authEndpoint?: never;
519
- } | {
520
- publicApiKey?: never;
521
- authEndpoint: AuthEndpoint;
522
- });
523
- declare type Connection = {
524
- state: "closed";
525
- } | {
526
- state: "authenticating";
527
- } | {
528
- state: "connecting";
529
- id: number;
530
- userId?: string;
531
- userInfo?: Json;
532
- } | {
533
- state: "open";
534
- id: number;
535
- userId?: string;
536
- userInfo?: Json;
537
- } | {
538
- state: "unavailable";
539
- } | {
540
- state: "failed";
541
- };
542
- declare type ConnectionState = Connection["state"];
543
- declare type OthersEvent<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = {
544
- type: "leave";
545
- user: User<TPresence, TUserMeta>;
546
- } | {
547
- type: "enter";
548
- user: User<TPresence, TUserMeta>;
549
- } | {
550
- type: "update";
551
- user: User<TPresence, TUserMeta>;
552
- updates: Partial<TPresence>;
553
- } | {
554
- type: "reset";
555
- };
556
- interface History {
557
- /**
558
- * Undoes the last operation executed by the current client.
559
- * It does not impact operations made by other clients.
560
- *
561
- * @example
562
- * room.updatePresence({ selectedId: "xx" }, { addToHistory: true });
563
- * room.updatePresence({ selectedId: "yy" }, { addToHistory: true });
564
- * room.history.undo();
565
- * // room.getPresence() equals { selectedId: "xx" }
566
- */
567
- undo: () => void;
568
- /**
569
- * Redoes the last operation executed by the current client.
570
- * It does not impact operations made by other clients.
571
- *
572
- * @example
573
- * room.updatePresence({ selectedId: "xx" }, { addToHistory: true });
574
- * room.updatePresence({ selectedId: "yy" }, { addToHistory: true });
575
- * room.history.undo();
576
- * // room.getPresence() equals { selectedId: "xx" }
577
- * room.history.redo();
578
- * // room.getPresence() equals { selectedId: "yy" }
579
- */
580
- redo: () => void;
581
- /**
582
- * Returns whether there are any operations to undo.
583
- *
584
- * @example
585
- * room.updatePresence({ selectedId: "xx" }, { addToHistory: true });
586
- * // room.history.canUndo() is true
587
- * room.history.undo();
588
- * // room.history.canUndo() is false
589
- */
590
- canUndo: () => boolean;
591
- /**
592
- * Returns whether there are any operations to redo.
593
- *
594
- * @example
595
- * room.updatePresence({ selectedId: "xx" }, { addToHistory: true });
596
- * room.history.undo();
597
- * // room.history.canRedo() is true
598
- * room.history.redo();
599
- * // room.history.canRedo() is false
600
- */
601
- canRedo: () => boolean;
602
- /**
603
- * All future modifications made on the Room will be merged together to create a single history item until resume is called.
604
- *
605
- * @example
606
- * room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
607
- * room.history.pause();
608
- * room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
609
- * room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
610
- * room.history.resume();
611
- * room.history.undo();
612
- * // room.getPresence() equals { cursor: { x: 0, y: 0 } }
613
- */
614
- pause: () => void;
615
- /**
616
- * Resumes history. Modifications made on the Room are not merged into a single history item anymore.
617
- *
618
- * @example
619
- * room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
620
- * room.history.pause();
621
- * room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
622
- * room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
623
- * room.history.resume();
624
- * room.history.undo();
625
- * // room.getPresence() equals { cursor: { x: 0, y: 0 } }
626
- */
627
- resume: () => void;
628
- }
629
- interface HistoryEvent {
630
- canUndo: boolean;
631
- canRedo: boolean;
632
- }
633
- declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = {
634
- /**
635
- * The id of the room.
636
- */
637
- readonly id: string;
638
- /**
639
- * A client is considered "self aware" if it knows its own
640
- * metadata and connection ID (from the auth server).
641
- */
642
- isSelfAware(): boolean;
643
- getConnectionState(): ConnectionState;
644
- readonly subscribe: {
645
- /**
646
- * Subscribe to the current user presence updates.
647
- *
648
- * @param listener the callback that is called every time the current user presence is updated with {@link Room.updatePresence}.
649
- *
650
- * @returns Unsubscribe function.
651
- *
652
- * @example
653
- * room.subscribe("my-presence", (presence) => {
654
- * // Do something
655
- * });
656
- */
657
- (type: "my-presence", listener: Callback<TPresence>): () => void;
658
- /**
659
- * Subscribe to the other users updates.
660
- *
661
- * @param listener the callback that is called when a user enters or leaves the room or when a user update its presence.
662
- *
663
- * @returns Unsubscribe function.
664
- *
665
- * @example
666
- * room.subscribe("others", (others) => {
667
- * // Do something
668
- * });
669
- *
670
- */
671
- (type: "others", listener: (others: Others<TPresence, TUserMeta>, event: OthersEvent<TPresence, TUserMeta>) => void): () => void;
672
- /**
673
- * Subscribe to events broadcasted by {@link Room.broadcastEvent}
674
- *
675
- * @param listener the callback that is called when a user calls {@link Room.broadcastEvent}
676
- *
677
- * @returns Unsubscribe function.
678
- *
679
- * @example
680
- * room.subscribe("event", ({ event, connectionId }) => {
681
- * // Do something
682
- * });
683
- *
684
- */
685
- (type: "event", listener: Callback<CustomEvent<TRoomEvent>>): () => void;
686
- /**
687
- * Subscribe to errors thrown in the room.
688
- *
689
- * @returns Unsubscribe function.
690
- *
691
- */
692
- (type: "error", listener: ErrorCallback): () => void;
693
- /**
694
- * Subscribe to connection state updates.
695
- *
696
- * @returns Unsubscribe function.
697
- *
698
- */
699
- (type: "connection", listener: Callback<ConnectionState>): () => void;
700
- /**
701
- * Subscribes to changes made on a Live structure. Returns an unsubscribe function.
702
- * In a future version, we will also expose what exactly changed in the Live structure.
703
- *
704
- * @param callback The callback this called when the Live structure changes.
705
- *
706
- * @returns Unsubscribe function.
707
- *
708
- * @example
709
- * const liveMap = new LiveMap(); // Could also be LiveList or LiveObject
710
- * const unsubscribe = room.subscribe(liveMap, (liveMap) => { });
711
- * unsubscribe();
712
- */
713
- <L extends LiveStructure>(liveStructure: L, callback: (node: L) => void): () => void;
714
- /**
715
- * Subscribes to changes made on a Live structure and all the nested data
716
- * structures. Returns an unsubscribe function. In a future version, we
717
- * will also expose what exactly changed in the Live structure.
718
- *
719
- * @param callback The callback this called when the Live structure, or any
720
- * of its nested values, changes.
721
- *
722
- * @returns Unsubscribe function.
723
- *
724
- * @example
725
- * const liveMap = new LiveMap(); // Could also be LiveList or LiveObject
726
- * const unsubscribe = room.subscribe(liveMap, (updates) => { }, { isDeep: true });
727
- * unsubscribe();
728
- */
729
- <L extends LiveStructure>(liveStructure: L, callback: StorageCallback, options: {
730
- isDeep: true;
731
- }): () => void;
732
- /**
733
- * Subscribe to the current user's history changes.
734
- *
735
- * @returns Unsubscribe function.
736
- *
737
- * @example
738
- * room.subscribe("history", ({ canUndo, canRedo }) => {
739
- * // Do something
740
- * });
741
- *
742
- */
743
- (type: "history", listener: Callback<HistoryEvent>): () => void;
744
- };
745
- /**
746
- * Room's history contains functions that let you undo and redo operation made on by the current client on the presence and storage.
747
- */
748
- readonly history: History;
749
- /**
750
- * Gets the current user.
751
- * Returns null if not it is not yet connected to the room.
752
- *
753
- * @example
754
- * const user = room.getSelf();
755
- */
756
- getSelf(): User<TPresence, TUserMeta> | null;
757
- /**
758
- * Gets the presence of the current user.
759
- *
760
- * @example
761
- * const presence = room.getPresence();
762
- */
763
- getPresence(): TPresence;
764
- /**
765
- * Gets all the other users in the room.
766
- *
767
- * @example
768
- * const others = room.getOthers();
769
- */
770
- getOthers(): Others<TPresence, TUserMeta>;
771
- /**
772
- * Updates the presence of the current user. Only pass the properties you want to update. No need to send the full presence.
773
- * @param patch A partial object that contains the properties you want to update.
774
- * @param options Optional object to configure the behavior of updatePresence.
775
- *
776
- * @example
777
- * room.updatePresence({ x: 0 });
778
- * room.updatePresence({ y: 0 });
779
- *
780
- * const presence = room.getPresence();
781
- * // presence is equivalent to { x: 0, y: 0 }
782
- */
783
- updatePresence(patch: Partial<TPresence>, options?: {
784
- /**
785
- * Whether or not the presence should have an impact on the undo/redo history.
786
- */
787
- addToHistory: boolean;
788
- }): void;
789
- /**
790
- * Broadcasts an event to other users in the room. Event broadcasted to the room can be listened with {@link Room.subscribe}("event").
791
- * @param {any} event the event to broadcast. Should be serializable to JSON
792
- *
793
- * @example
794
- * // On client A
795
- * room.broadcastEvent({ type: "EMOJI", emoji: "🔥" });
796
- *
797
- * // On client B
798
- * room.subscribe("event", ({ event }) => {
799
- * if(event.type === "EMOJI") {
800
- * // Do something
801
- * }
802
- * });
803
- */
804
- broadcastEvent(event: TRoomEvent, options?: BroadcastOptions): void;
805
- /**
806
- * Get the room's storage asynchronously.
807
- * The storage's root is a {@link LiveObject}.
808
- *
809
- * @example
810
- * const { root } = await room.getStorage();
811
- */
812
- getStorage(): Promise<{
813
- root: LiveObject<TStorage>;
814
- }>;
815
- /**
816
- * Get the room's storage synchronously.
817
- * The storage's root is a {@link LiveObject}.
818
- *
819
- * @example
820
- * const root = room.getStorageSnapshot();
821
- */
822
- getStorageSnapshot(): LiveObject<TStorage> | null;
823
- readonly events: {
824
- customEvent: Observable<{
825
- connectionId: number;
826
- event: TRoomEvent;
827
- }>;
828
- me: Observable<TPresence>;
829
- others: Observable<{
830
- others: Others<TPresence, TUserMeta>;
831
- event: OthersEvent<TPresence, TUserMeta>;
832
- }>;
833
- error: Observable<Error>;
834
- connection: Observable<ConnectionState>;
835
- storage: Observable<StorageUpdate[]>;
836
- history: Observable<HistoryEvent>;
837
- /**
838
- * Subscribe to the storage loaded event. Will fire at most once during the
839
- * lifetime of a Room.
840
- */
841
- storageDidLoad: Observable<void>;
842
- };
843
- /**
844
- * Batches modifications made during the given function.
845
- * All the modifications are sent to other clients in a single message.
846
- * All the subscribers are called only after the batch is over.
847
- * All the modifications are merged in a single history item (undo/redo).
848
- *
849
- * @example
850
- * const { root } = await room.getStorage();
851
- * room.batch(() => {
852
- * root.set("x", 0);
853
- * room.updatePresence({ cursor: { x: 100, y: 100 }});
854
- * });
855
- */
856
- batch<T>(fn: () => T): T;
857
- };
858
- declare enum WebsocketCloseCodes {
859
- CLOSE_ABNORMAL = 1006,
860
- INVALID_MESSAGE_FORMAT = 4000,
861
- NOT_ALLOWED = 4001,
862
- MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
863
- MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
864
- MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
865
- MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
866
- CLOSE_WITHOUT_RETRY = 4999
867
- }
868
-
869
- export { BaseUserMeta as B, ClientOptions as C, History as H, Immutable as I, Json as J, LiveList as L, Others as O, Room as R, StorageUpdate as S, ToJson as T, User as U, WebsocketCloseCodes as W, Client as a, LiveMap as b, LiveObject as c, BroadcastOptions as d, JsonObject as e, LiveStructure as f, Lson as g, LsonObject as h, asArrayWithLegacyMethods as i, LiveNode as j, Resolve as k, RoomInitializers as l, ToImmutable as m, isJsonArray as n, isJsonObject as o, isJsonScalar as p };