@liveblocks/client 0.15.0-alpha.3 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +5 -10
  2. package/lib/esm/index.js +2991 -5
  3. package/lib/esm/index.mjs +2991 -0
  4. package/lib/esm/internal.js +149 -0
  5. package/lib/esm/internal.mjs +149 -0
  6. package/lib/{esm/types.d.ts → index.d.ts} +253 -61
  7. package/lib/index.js +4237 -0
  8. package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
  9. package/lib/internal.js +193 -0
  10. package/package.json +32 -10
  11. package/lib/cjs/AbstractCrdt.d.ts +0 -68
  12. package/lib/cjs/AbstractCrdt.js +0 -95
  13. package/lib/cjs/LiveList.d.ts +0 -144
  14. package/lib/cjs/LiveList.js +0 -530
  15. package/lib/cjs/LiveMap.d.ts +0 -91
  16. package/lib/cjs/LiveMap.js +0 -325
  17. package/lib/cjs/LiveObject.d.ts +0 -80
  18. package/lib/cjs/LiveObject.js +0 -485
  19. package/lib/cjs/LiveRegister.d.ts +0 -29
  20. package/lib/cjs/LiveRegister.js +0 -88
  21. package/lib/cjs/client.d.ts +0 -27
  22. package/lib/cjs/client.js +0 -123
  23. package/lib/cjs/immutable.d.ts +0 -9
  24. package/lib/cjs/immutable.js +0 -299
  25. package/lib/cjs/index.d.ts +0 -6
  26. package/lib/cjs/index.js +0 -18
  27. package/lib/cjs/live.d.ts +0 -181
  28. package/lib/cjs/live.js +0 -49
  29. package/lib/cjs/position.d.ts +0 -6
  30. package/lib/cjs/position.js +0 -113
  31. package/lib/cjs/room.d.ts +0 -159
  32. package/lib/cjs/room.js +0 -1129
  33. package/lib/cjs/types.d.ts +0 -502
  34. package/lib/cjs/types.js +0 -2
  35. package/lib/cjs/utils.d.ts +0 -15
  36. package/lib/cjs/utils.js +0 -225
  37. package/lib/esm/AbstractCrdt.d.ts +0 -68
  38. package/lib/esm/AbstractCrdt.js +0 -91
  39. package/lib/esm/LiveList.d.ts +0 -144
  40. package/lib/esm/LiveList.js +0 -526
  41. package/lib/esm/LiveMap.d.ts +0 -91
  42. package/lib/esm/LiveMap.js +0 -321
  43. package/lib/esm/LiveObject.d.ts +0 -80
  44. package/lib/esm/LiveObject.js +0 -481
  45. package/lib/esm/LiveRegister.d.ts +0 -29
  46. package/lib/esm/LiveRegister.js +0 -84
  47. package/lib/esm/client.d.ts +0 -27
  48. package/lib/esm/client.js +0 -119
  49. package/lib/esm/immutable.d.ts +0 -9
  50. package/lib/esm/immutable.js +0 -290
  51. package/lib/esm/index.d.ts +0 -6
  52. package/lib/esm/live.js +0 -46
  53. package/lib/esm/position.d.ts +0 -6
  54. package/lib/esm/position.js +0 -106
  55. package/lib/esm/room.d.ts +0 -159
  56. package/lib/esm/room.js +0 -1123
  57. package/lib/esm/types.js +0 -1
  58. package/lib/esm/utils.d.ts +0 -15
  59. package/lib/esm/utils.js +0 -213
@@ -1,502 +0,0 @@
1
- import { AbstractCrdt } from "./AbstractCrdt";
2
- import type { LiveList } from "./LiveList";
3
- import type { LiveMap } from "./LiveMap";
4
- import type { LiveObject } from "./LiveObject";
5
- export declare type MyPresenceCallback<T extends Presence = Presence> = (me: T) => void;
6
- export declare type OthersEventCallback<T extends Presence = Presence> = (others: Others<T>, event: OthersEvent<T>) => void;
7
- export declare type EventCallback = ({ connectionId, event, }: {
8
- connectionId: number;
9
- event: any;
10
- }) => void;
11
- export declare type ErrorCallback = (error: Error) => void;
12
- export declare type ConnectionCallback = (state: ConnectionState) => void;
13
- export declare type RoomEventCallbackMap = {
14
- "my-presence": MyPresenceCallback;
15
- others: OthersEventCallback;
16
- event: EventCallback;
17
- error: ErrorCallback;
18
- connection: ConnectionCallback;
19
- };
20
- export declare type UpdateDelta = {
21
- type: "update";
22
- } | {
23
- type: "delete";
24
- };
25
- export declare type LiveMapUpdates<TKey extends string = string, TValue = any> = {
26
- type: "LiveMap";
27
- node: LiveMap<TKey, TValue>;
28
- updates: Record<TKey, UpdateDelta>;
29
- };
30
- export declare type LiveObjectUpdateDelta<T> = Partial<{
31
- [Property in keyof T]: UpdateDelta;
32
- }>;
33
- export declare type LiveObjectUpdates<TData = any> = {
34
- type: "LiveObject";
35
- node: LiveObject<TData>;
36
- updates: LiveObjectUpdateDelta<TData>;
37
- };
38
- export declare type LiveListUpdateDelta = {
39
- index: number;
40
- item: AbstractCrdt;
41
- type: "insert";
42
- } | {
43
- index: number;
44
- type: "delete";
45
- } | {
46
- index: number;
47
- previousIndex: number;
48
- item: AbstractCrdt;
49
- type: "move";
50
- };
51
- export declare type LiveListUpdates<TItem = any> = {
52
- type: "LiveList";
53
- node: LiveList<TItem>;
54
- updates: LiveListUpdateDelta[];
55
- };
56
- export declare type BroadcastOptions = {
57
- /**
58
- * Whether or not event is queued if the connection is currently closed.
59
- *
60
- * ❗ 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
61
- */
62
- shouldQueueEventIfNotReady: boolean;
63
- };
64
- export declare type StorageUpdate = LiveMapUpdates | LiveObjectUpdates | LiveListUpdates;
65
- export declare type StorageCallback = (updates: StorageUpdate[]) => void;
66
- export declare type Client = {
67
- /**
68
- * Gets a room. Returns null if {@link Client.enter} has not been called previously.
69
- *
70
- * @param roomId The id of the room
71
- */
72
- getRoom(roomId: string): Room | null;
73
- /**
74
- * Enters a room and returns it.
75
- * @param roomId The id of the room
76
- * @param defaultPresence Optional. Should be serializable to JSON. If omitted, an empty object will be used.
77
- */
78
- enter<TStorageRoot extends Record<string, any> = Record<string, any>>(roomId: string, options?: {
79
- defaultPresence?: Presence;
80
- defaultStorageRoot?: TStorageRoot;
81
- }): Room;
82
- /**
83
- * Leaves a room.
84
- * @param roomId The id of the room
85
- */
86
- leave(roomId: string): void;
87
- };
88
- export declare type AuthenticationToken = {
89
- actor: number;
90
- id?: string;
91
- info?: any;
92
- };
93
- /**
94
- * Represents all the other users connected in the room. Treated as immutable.
95
- */
96
- export interface Others<TPresence extends Presence = Presence> {
97
- /**
98
- * Number of other users in the room.
99
- */
100
- readonly count: number;
101
- /**
102
- * Returns a new Iterator object that contains the users.
103
- */
104
- [Symbol.iterator](): IterableIterator<User<TPresence>>;
105
- /**
106
- * Returns the array of connected users in room.
107
- */
108
- toArray(): User<TPresence>[];
109
- /**
110
- * This function let you map over the connected users in the room.
111
- */
112
- map<U>(callback: (user: User<TPresence>) => U): U[];
113
- }
114
- /**
115
- * Represents a user connected in a room. Treated as immutable.
116
- */
117
- export declare type User<TPresence extends Presence = Presence> = {
118
- /**
119
- * The connection id of the user. It is unique and increment at every new connection.
120
- */
121
- readonly connectionId: number;
122
- /**
123
- * The id of the user that has been set in the authentication endpoint.
124
- * Useful to get additional information about the connected user.
125
- */
126
- readonly id?: string;
127
- /**
128
- * Additional user information that has been set in the authentication endpoint.
129
- */
130
- readonly info?: any;
131
- /**
132
- * The user presence.
133
- */
134
- readonly presence?: TPresence;
135
- };
136
- export declare type Presence = {
137
- [key: string]: any;
138
- };
139
- declare type AuthEndpointCallback = (room: string) => Promise<{
140
- token: string;
141
- }>;
142
- export declare type AuthEndpoint = string | AuthEndpointCallback;
143
- /**
144
- * The authentication endpoint that is called to ensure that the current user has access to a room.
145
- * Can be an url or a callback if you need to add additional headers.
146
- */
147
- export declare type ClientOptions = {
148
- throttle?: number;
149
- fetchPolyfill?: any;
150
- WebSocketPolyfill?: any;
151
- } & ({
152
- publicApiKey: string;
153
- authEndpoint?: never;
154
- } | {
155
- publicApiKey?: never;
156
- authEndpoint: AuthEndpoint;
157
- });
158
- export declare type AuthorizeResponse = {
159
- token: string;
160
- };
161
- export declare type Authentication = {
162
- type: "public";
163
- publicApiKey: string;
164
- url: string;
165
- } | {
166
- type: "private";
167
- url: string;
168
- } | {
169
- type: "custom";
170
- callback: (room: string) => Promise<AuthorizeResponse>;
171
- };
172
- declare type ConnectionState = "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting";
173
- export declare type Connection = {
174
- state: "closed" | "authenticating" | "unavailable" | "failed";
175
- } | {
176
- state: "open" | "connecting";
177
- id: number;
178
- userId?: string;
179
- userInfo?: any;
180
- };
181
- export declare type OthersEvent<T extends Presence = Presence> = {
182
- type: "leave";
183
- user: User<T>;
184
- } | {
185
- type: "enter";
186
- user: User<T>;
187
- } | {
188
- type: "update";
189
- user: User<T>;
190
- updates: Partial<T>;
191
- } | {
192
- type: "reset";
193
- };
194
- export declare type Room = {
195
- getConnectionState(): ConnectionState;
196
- subscribe: {
197
- /**
198
- * Subscribe to the current user presence updates.
199
- *
200
- * @param listener the callback that is called every time the current user presence is updated with {@link Room.updatePresence}.
201
- *
202
- * @example
203
- * room.subscribe("my-presence", (presence) => {
204
- * // Do something
205
- * });
206
- */
207
- <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): () => void;
208
- /**
209
- * Subscribe to the other users updates.
210
- *
211
- * @param listener the callback that is called when a user enters or leaves the room or when a user update its presence.
212
- *
213
- * @example
214
- * room.subscribe("others", (others) => {
215
- * // Do something
216
- * });
217
- */
218
- <T extends Presence>(type: "others", listener: OthersEventCallback<T>): () => void;
219
- /**
220
- * Subscribe to events broadcasted by {@link Room.broadcastEvent}
221
- *
222
- * @param listener the callback that is called when a user calls {@link Room.broadcastEvent}
223
- *
224
- * @example
225
- * room.subscribe("event", ({ event, connectionId }) => {
226
- * // Do something
227
- * });
228
- */
229
- (type: "event", listener: EventCallback): () => void;
230
- /**
231
- * Subscribe to errors thrown in the room.
232
- */
233
- (type: "error", listener: ErrorCallback): () => void;
234
- /**
235
- * Subscribe to connection state updates.
236
- */
237
- (type: "connection", listener: ConnectionCallback): () => void;
238
- /**
239
- * Subscribes to changes made on a {@link LiveMap}. Returns an unsubscribe function.
240
- * In a future version, we will also expose what exactly changed in the {@link LiveMap}.
241
- *
242
- * @param listener the callback this called when the {@link LiveMap} changes.
243
- *
244
- * @returns Unsubscribe function.
245
- *
246
- * @example
247
- * const liveMap = new LiveMap();
248
- * const unsubscribe = room.subscribe(liveMap, (liveMap) => { });
249
- * unsubscribe();
250
- */
251
- <TKey extends string, TValue>(liveMap: LiveMap<TKey, TValue>, listener: (liveMap: LiveMap<TKey, TValue>) => void): () => void;
252
- /**
253
- * Subscribes to changes made on a {@link LiveObject}. Returns an unsubscribe function.
254
- * In a future version, we will also expose what exactly changed in the {@link LiveObject}.
255
- *
256
- * @param listener the callback this called when the {@link LiveObject} changes.
257
- *
258
- * @returns Unsubscribe function.
259
- *
260
- * @example
261
- * const liveObject = new LiveObject();
262
- * const unsubscribe = room.subscribe(liveObject, (liveObject) => { });
263
- * unsubscribe();
264
- */
265
- <TData>(liveObject: LiveObject<TData>, callback: (liveObject: LiveObject<TData>) => void): () => void;
266
- /**
267
- * Subscribes to changes made on a {@link LiveList}. Returns an unsubscribe function.
268
- * In a future version, we will also expose what exactly changed in the {@link LiveList}.
269
- *
270
- * @param listener the callback this called when the {@link LiveList} changes.
271
- *
272
- * @returns Unsubscribe function.
273
- *
274
- * @example
275
- * const liveList = new LiveList();
276
- * const unsubscribe = room.subscribe(liveList, (liveList) => { });
277
- * unsubscribe();
278
- */
279
- <TItem>(liveList: LiveList<TItem>, callback: (liveList: LiveList<TItem>) => void): () => void;
280
- /**
281
- * Subscribes to changes made on a {@link LiveMap} and all the nested data structures. Returns an unsubscribe function.
282
- * In a future version, we will also expose what exactly changed in the {@link LiveMap}.
283
- *
284
- * @param listener the callback this called when the {@link LiveMap} changes.
285
- *
286
- * @returns Unsubscribe function.
287
- *
288
- * @example
289
- * const liveMap = new LiveMap();
290
- * const unsubscribe = room.subscribe(liveMap, (liveMap) => { }, { isDeep: true });
291
- * unsubscribe();
292
- */
293
- <TKey extends string, TValue>(liveMap: LiveMap<TKey, TValue>, callback: (updates: StorageUpdate[]) => void, options: {
294
- isDeep: true;
295
- }): () => void;
296
- /**
297
- * Subscribes to changes made on a {@link LiveObject} and all the nested data structures. Returns an unsubscribe function.
298
- * In a future version, we will also expose what exactly changed in the {@link LiveObject}.
299
- *
300
- * @param listener the callback this called when the {@link LiveObject} changes.
301
- *
302
- * @returns Unsubscribe function.
303
- *
304
- * @example
305
- * const liveObject = new LiveObject();
306
- * const unsubscribe = room.subscribe(liveObject, (liveObject) => { }, { isDeep: true });
307
- * unsubscribe();
308
- */
309
- <TData>(liveObject: LiveObject<TData>, callback: (updates: StorageUpdate[]) => void, options: {
310
- isDeep: true;
311
- }): () => void;
312
- /**
313
- * Subscribes to changes made on a {@link LiveList} and all the nested data structures. Returns an unsubscribe function.
314
- * In a future version, we will also expose what exactly changed in the {@link LiveList}.
315
- *
316
- * @param listener the callback this called when the {@link LiveList} changes.
317
- *
318
- * @returns Unsubscribe function.
319
- *
320
- * @example
321
- * const liveList = new LiveList();
322
- * const unsubscribe = room.subscribe(liveList, (liveList) => { }, { isDeep: true });
323
- * unsubscribe();
324
- */
325
- <TItem>(liveList: LiveList<TItem>, callback: (updates: StorageUpdate[]) => void, options: {
326
- isDeep: true;
327
- }): () => void;
328
- };
329
- /**
330
- * Room's history contains functions that let you undo and redo operation made on by the current client on the presence and storage.
331
- */
332
- history: {
333
- /**
334
- * Undoes the last operation executed by the current client.
335
- * It does not impact operations made by other clients.
336
- *
337
- * @example
338
- * room.updatePresence({ selectedId: "xxx" }, { addToHistory: true });
339
- * room.updatePresence({ selectedId: "yyy" }, { addToHistory: true });
340
- * room.history.undo();
341
- * // room.getPresence() equals { selectedId: "xxx" }
342
- */
343
- undo: () => void;
344
- /**
345
- * Redoes the last operation executed by the current client.
346
- * It does not impact operations made by other clients.
347
- *
348
- * @example
349
- * room.updatePresence({ selectedId: "xxx" }, { addToHistory: true });
350
- * room.updatePresence({ selectedId: "yyy" }, { addToHistory: true });
351
- * room.history.undo();
352
- * // room.getPresence() equals { selectedId: "xxx" }
353
- * room.history.redo();
354
- * // room.getPresence() equals { selectedId: "yyy" }
355
- */
356
- redo: () => void;
357
- /**
358
- * All future modifications made on the Room will be merged together to create a single history item until resume is called.
359
- *
360
- * @example
361
- * room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
362
- * room.history.pause();
363
- * room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
364
- * room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
365
- * room.history.resume();
366
- * room.history.undo();
367
- * // room.getPresence() equals { cursor: { x: 0, y: 0 } }
368
- */
369
- pause: () => void;
370
- /**
371
- * Resumes history. Modifications made on the Room are not merged into a single history item anymore.
372
- *
373
- * @example
374
- * room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
375
- * room.history.pause();
376
- * room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
377
- * room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
378
- * room.history.resume();
379
- * room.history.undo();
380
- * // room.getPresence() equals { cursor: { x: 0, y: 0 } }
381
- */
382
- resume: () => void;
383
- };
384
- /**
385
- * @deprecated use the callback returned by subscribe instead.
386
- * See v0.13 release notes for more information.
387
- * Will be removed in a future version.
388
- */
389
- unsubscribe: {
390
- /**
391
- * @deprecated use the callback returned by subscribe instead.
392
- * See v0.13 release notes for more information.
393
- * Will be removed in a future version.
394
- */
395
- <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): void;
396
- /**
397
- * @deprecated use the callback returned by subscribe instead.
398
- * See v0.13 release notes for more information.
399
- * Will be removed in a future version.
400
- */
401
- <T extends Presence>(type: "others", listener: OthersEventCallback<T>): void;
402
- /**
403
- * @deprecated use the callback returned by subscribe instead.
404
- * See v0.13 release notes for more information.
405
- * Will be removed in a future version.
406
- */
407
- (type: "event", listener: EventCallback): void;
408
- /**
409
- * @deprecated use the callback returned by subscribe instead.
410
- * See v0.13 release notes for more information.
411
- * Will be removed in a future version.
412
- */
413
- (type: "error", listener: ErrorCallback): void;
414
- /**
415
- * @deprecated use the callback returned by subscribe instead.
416
- * See v0.13 release notes for more information.
417
- * Will be removed in a future version.
418
- */
419
- (type: "connection", listener: ConnectionCallback): void;
420
- };
421
- /**
422
- * Gets the current user.
423
- * Returns null if not it is not yet connected to the room.
424
- *
425
- * @example
426
- * const user = room.getSelf();
427
- */
428
- getSelf<TPresence extends Presence = Presence>(): User<TPresence> | null;
429
- /**
430
- * Gets the presence of the current user.
431
- *
432
- * @example
433
- * const presence = room.getPresence();
434
- */
435
- getPresence: <T extends Presence>() => T;
436
- /**
437
- * Gets all the other users in the room.
438
- *
439
- * @example
440
- * const others = room.getOthers();
441
- */
442
- getOthers: <T extends Presence>() => Others<T>;
443
- /**
444
- * Updates the presence of the current user. Only pass the properties you want to update. No need to send the full presence.
445
- * @param overrides A partial object that contains the properties you want to update.
446
- * @param overrides Optional object to configure the behavior of updatePresence.
447
- *
448
- * @example
449
- * room.updatePresence({ x: 0 });
450
- * room.updatePresence({ y: 0 });
451
- *
452
- * const presence = room.getPresence();
453
- * // presence is equivalent to { x: 0, y: 0 }
454
- */
455
- updatePresence: <T extends Presence>(overrides: Partial<T>, options?: {
456
- /**
457
- * Whether or not the presence should have an impact on the undo/redo history.
458
- */
459
- addToHistory: boolean;
460
- }) => void;
461
- /**
462
- * Broadcasts an event to other users in the room. Event broadcasted to the room can be listened with {@link Room.subscribe}("event").
463
- * @param {any} event the event to broadcast. Should be serializable to JSON
464
- *
465
- * @example
466
- * // On client A
467
- * room.broadcastEvent({ type: "EMOJI", emoji: "🔥" });
468
- *
469
- * // On client B
470
- * room.subscribe("event", ({ event }) => {
471
- * if(event.type === "EMOJI") {
472
- * // Do something
473
- * }
474
- * });
475
- */
476
- broadcastEvent: (event: any, options?: BroadcastOptions) => void;
477
- /**
478
- * Get the room's storage asynchronously.
479
- * The storage's root is a {@link LiveObject}.
480
- *
481
- * @example
482
- * const { root } = await room.getStorage();
483
- */
484
- getStorage: <TRoot>() => Promise<{
485
- root: LiveObject<TRoot>;
486
- }>;
487
- /**
488
- * Batches modifications made during the given function.
489
- * All the modifications are sent to other clients in a single message.
490
- * All the subscribers are called only after the batch is over.
491
- * All the modifications are merged in a single history item (undo/redo).
492
- *
493
- * @example
494
- * const { root } = await room.getStorage();
495
- * room.batch(() => {
496
- * root.set("x", 0);
497
- * room.updatePresence({ cursor: { x: 100, y: 100 }});
498
- * });
499
- */
500
- batch: (fn: () => void) => void;
501
- };
502
- export {};
package/lib/cjs/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,15 +0,0 @@
1
- import { AbstractCrdt, Doc } from "./AbstractCrdt";
2
- import { SerializedCrdtWithId, Op, SerializedCrdt } from "./live";
3
- import { StorageUpdate } from "./types";
4
- export declare function remove<T>(array: T[], item: T): void;
5
- export declare function isSameNodeOrChildOf(node: AbstractCrdt, parent: AbstractCrdt): boolean;
6
- export declare function deserialize(entry: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): AbstractCrdt;
7
- export declare function isCrdt(obj: any): obj is AbstractCrdt;
8
- export declare function selfOrRegisterValue(obj: AbstractCrdt): any;
9
- export declare function selfOrRegister(obj: any): AbstractCrdt;
10
- export declare function getTreesDiffOperations(currentItems: Map<string, SerializedCrdt>, newItems: Map<string, SerializedCrdt>): Op[];
11
- export declare function mergeStorageUpdates(first: StorageUpdate | undefined, second: StorageUpdate): StorageUpdate;
12
- export declare function findNonSerializableValue(value: any, path?: string): {
13
- path: string;
14
- value: any;
15
- } | false;