@liveblocks/client 0.15.0-alpha.2 → 0.15.0

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