@liveblocks/client 0.12.2 → 0.13.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.
- package/README.md +34 -6
- package/lib/cjs/AbstractCrdt.d.ts +61 -0
- package/lib/cjs/AbstractCrdt.js +98 -0
- package/lib/cjs/LiveList.d.ts +133 -0
- package/lib/cjs/LiveList.js +374 -0
- package/lib/cjs/LiveMap.d.ts +83 -0
- package/lib/cjs/LiveMap.js +272 -0
- package/lib/cjs/LiveObject.d.ts +66 -0
- package/lib/cjs/LiveObject.js +368 -0
- package/lib/cjs/LiveRegister.d.ts +21 -0
- package/lib/cjs/LiveRegister.js +69 -0
- package/lib/cjs/index.d.ts +3 -1
- package/lib/cjs/index.js +7 -5
- package/lib/cjs/room.d.ts +50 -9
- package/lib/cjs/room.js +476 -85
- package/lib/cjs/types.d.ts +220 -40
- package/lib/cjs/utils.d.ts +7 -0
- package/lib/cjs/utils.js +64 -1
- package/lib/esm/AbstractCrdt.d.ts +61 -0
- package/lib/esm/AbstractCrdt.js +94 -0
- package/lib/esm/LiveList.d.ts +133 -0
- package/lib/esm/LiveList.js +370 -0
- package/lib/esm/LiveMap.d.ts +83 -0
- package/lib/esm/LiveMap.js +268 -0
- package/lib/esm/LiveObject.d.ts +66 -0
- package/lib/esm/LiveObject.js +364 -0
- package/lib/esm/LiveRegister.d.ts +21 -0
- package/lib/esm/LiveRegister.js +65 -0
- package/lib/esm/index.d.ts +3 -1
- package/lib/esm/index.js +3 -1
- package/lib/esm/room.d.ts +50 -9
- package/lib/esm/room.js +478 -84
- package/lib/esm/types.d.ts +220 -40
- package/lib/esm/utils.d.ts +7 -0
- package/lib/esm/utils.js +58 -0
- package/package.json +3 -3
- package/lib/cjs/doc.d.ts +0 -347
- package/lib/cjs/doc.js +0 -1349
- package/lib/cjs/storage.d.ts +0 -21
- package/lib/cjs/storage.js +0 -68
- package/lib/esm/doc.d.ts +0 -347
- package/lib/esm/doc.js +0 -1342
- package/lib/esm/storage.d.ts +0 -21
- package/lib/esm/storage.js +0 -65
package/lib/cjs/types.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { LiveList } from "./LiveList";
|
|
2
|
+
import type { LiveMap } from "./LiveMap";
|
|
3
|
+
import type { LiveObject } from "./LiveObject";
|
|
2
4
|
export declare type MyPresenceCallback<T extends Presence = Presence> = (me: T) => void;
|
|
3
5
|
export declare type OthersEventCallback<T extends Presence = Presence> = (others: Others<T>, event: OthersEvent<T>) => void;
|
|
4
6
|
export declare type EventCallback = ({ connectionId, event, }: {
|
|
@@ -14,17 +16,31 @@ export declare type RoomEventCallbackMap = {
|
|
|
14
16
|
error: ErrorCallback;
|
|
15
17
|
connection: ConnectionCallback;
|
|
16
18
|
};
|
|
19
|
+
export declare type LiveMapUpdates<TKey extends string = string, TValue = any> = {
|
|
20
|
+
type: "LiveMap";
|
|
21
|
+
node: LiveMap<TKey, TValue>;
|
|
22
|
+
};
|
|
23
|
+
export declare type LiveObjectUpdates<TData = any> = {
|
|
24
|
+
type: "LiveObject";
|
|
25
|
+
node: LiveObject<TData>;
|
|
26
|
+
};
|
|
27
|
+
export declare type LiveListUpdates<TItem = any> = {
|
|
28
|
+
type: "LiveList";
|
|
29
|
+
node: LiveList<TItem>;
|
|
30
|
+
};
|
|
31
|
+
export declare type StorageUpdate = LiveMapUpdates | LiveObjectUpdates | LiveListUpdates;
|
|
32
|
+
export declare type StorageCallback = (updates: StorageUpdate[]) => void;
|
|
17
33
|
export declare type Client = {
|
|
18
34
|
/**
|
|
19
35
|
* Gets a room. Returns null if {@link Client.enter} has not been called previously.
|
|
20
36
|
*
|
|
21
|
-
* @param roomId
|
|
37
|
+
* @param roomId The id of the room
|
|
22
38
|
*/
|
|
23
39
|
getRoom(roomId: string): Room | null;
|
|
24
40
|
/**
|
|
25
41
|
* Enters a room and returns it.
|
|
26
|
-
* @param roomId
|
|
27
|
-
* @param defaultPresence
|
|
42
|
+
* @param roomId The id of the room
|
|
43
|
+
* @param defaultPresence Optional. Should be serializable to JSON. If omitted, an empty object will be used.
|
|
28
44
|
*/
|
|
29
45
|
enter<TStorageRoot extends Record<string, any> = Record<string, any>>(roomId: string, options?: {
|
|
30
46
|
defaultPresence?: Presence;
|
|
@@ -32,7 +48,7 @@ export declare type Client = {
|
|
|
32
48
|
}): Room;
|
|
33
49
|
/**
|
|
34
50
|
* Leaves a room.
|
|
35
|
-
* @param roomId
|
|
51
|
+
* @param roomId The id of the room
|
|
36
52
|
*/
|
|
37
53
|
leave(roomId: string): void;
|
|
38
54
|
};
|
|
@@ -131,85 +147,224 @@ export declare type Room = {
|
|
|
131
147
|
/**
|
|
132
148
|
* Subscribe to the current user presence updates.
|
|
133
149
|
*
|
|
134
|
-
* @param listener
|
|
150
|
+
* @param listener the callback that is called every time the current user presence is updated with {@link Room.updatePresence}.
|
|
135
151
|
*
|
|
136
152
|
* @example
|
|
137
153
|
* room.subscribe("my-presence", (presence) => {
|
|
138
154
|
* // Do something
|
|
139
155
|
* });
|
|
140
156
|
*/
|
|
141
|
-
<T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): void;
|
|
157
|
+
<T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): () => void;
|
|
142
158
|
/**
|
|
143
159
|
* Subscribe to the other users updates.
|
|
144
160
|
*
|
|
145
|
-
* @param listener
|
|
161
|
+
* @param listener the callback that is called when a user enters or leaves the room or when a user update its presence.
|
|
146
162
|
*
|
|
147
163
|
* @example
|
|
148
164
|
* room.subscribe("others", (others) => {
|
|
149
165
|
* // Do something
|
|
150
166
|
* });
|
|
151
167
|
*/
|
|
152
|
-
<T extends Presence>(type: "others", listener: OthersEventCallback<T>): void;
|
|
168
|
+
<T extends Presence>(type: "others", listener: OthersEventCallback<T>): () => void;
|
|
153
169
|
/**
|
|
154
170
|
* Subscribe to events broadcasted by {@link Room.broadcastEvent}
|
|
155
171
|
*
|
|
156
|
-
* @param listener
|
|
172
|
+
* @param listener the callback that is called when a user calls {@link Room.broadcastEvent}
|
|
157
173
|
*
|
|
158
174
|
* @example
|
|
159
175
|
* room.subscribe("event", ({ event, connectionId }) => {
|
|
160
176
|
* // Do something
|
|
161
177
|
* });
|
|
162
178
|
*/
|
|
163
|
-
(type: "event", listener: EventCallback): void;
|
|
179
|
+
(type: "event", listener: EventCallback): () => void;
|
|
164
180
|
/**
|
|
165
181
|
* Subscribe to errors thrown in the room.
|
|
166
182
|
*/
|
|
167
|
-
(type: "error", listener: ErrorCallback): void;
|
|
183
|
+
(type: "error", listener: ErrorCallback): () => void;
|
|
168
184
|
/**
|
|
169
185
|
* Subscribe to connection state updates.
|
|
170
186
|
*/
|
|
171
|
-
(type: "connection", listener: ConnectionCallback): void;
|
|
172
|
-
};
|
|
173
|
-
unsubscribe: {
|
|
187
|
+
(type: "connection", listener: ConnectionCallback): () => void;
|
|
174
188
|
/**
|
|
175
|
-
*
|
|
189
|
+
* Subscribes to changes made on a {@link LiveMap}. Returns an unsubscribe function.
|
|
190
|
+
* In a future version, we will also expose what exactly changed in the {@link LiveMap}.
|
|
176
191
|
*
|
|
177
|
-
* @param listener
|
|
192
|
+
* @param listener the callback this called when the {@link LiveMap} changes.
|
|
193
|
+
*
|
|
194
|
+
* @returns Unsubscribe function.
|
|
178
195
|
*
|
|
179
196
|
* @example
|
|
180
|
-
* const
|
|
181
|
-
* room.subscribe(
|
|
182
|
-
*
|
|
197
|
+
* const liveMap = new LiveMap();
|
|
198
|
+
* const unsubscribe = room.subscribe(liveMap, (liveMap) => { });
|
|
199
|
+
* unsubscribe();
|
|
183
200
|
*/
|
|
184
|
-
<
|
|
201
|
+
<TKey extends string, TValue>(liveMap: LiveMap<TKey, TValue>, listener: (liveMap: LiveMap<TKey, TValue>) => void): () => void;
|
|
185
202
|
/**
|
|
186
|
-
*
|
|
203
|
+
* Subscribes to changes made on a {@link LiveObject}. Returns an unsubscribe function.
|
|
204
|
+
* In a future version, we will also expose what exactly changed in the {@link LiveObject}.
|
|
205
|
+
*
|
|
206
|
+
* @param listener the callback this called when the {@link LiveObject} changes.
|
|
187
207
|
*
|
|
188
|
-
* @
|
|
208
|
+
* @returns Unsubscribe function.
|
|
189
209
|
*
|
|
190
210
|
* @example
|
|
191
|
-
* const
|
|
192
|
-
* room.subscribe(
|
|
193
|
-
*
|
|
211
|
+
* const liveObject = new LiveObject();
|
|
212
|
+
* const unsubscribe = room.subscribe(liveObject, (liveObject) => { });
|
|
213
|
+
* unsubscribe();
|
|
194
214
|
*/
|
|
195
|
-
<
|
|
215
|
+
<TData>(liveObject: LiveObject<TData>, callback: (liveObject: LiveObject<TData>) => void): () => void;
|
|
216
|
+
/**
|
|
217
|
+
* Subscribes to changes made on a {@link LiveList}. Returns an unsubscribe function.
|
|
218
|
+
* In a future version, we will also expose what exactly changed in the {@link LiveList}.
|
|
219
|
+
*
|
|
220
|
+
* @param listener the callback this called when the {@link LiveList} changes.
|
|
221
|
+
*
|
|
222
|
+
* @returns Unsubscribe function.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* const liveList = new LiveList();
|
|
226
|
+
* const unsubscribe = room.subscribe(liveList, (liveList) => { });
|
|
227
|
+
* unsubscribe();
|
|
228
|
+
*/
|
|
229
|
+
<TItem>(liveList: LiveList<TItem>, callback: (liveList: LiveList<TItem>) => void): () => void;
|
|
230
|
+
/**
|
|
231
|
+
* Subscribes to changes made on a {@link LiveMap} and all the nested data structures. Returns an unsubscribe function.
|
|
232
|
+
* In a future version, we will also expose what exactly changed in the {@link LiveMap}.
|
|
233
|
+
*
|
|
234
|
+
* @param listener the callback this called when the {@link LiveMap} changes.
|
|
235
|
+
*
|
|
236
|
+
* @returns Unsubscribe function.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* const liveMap = new LiveMap();
|
|
240
|
+
* const unsubscribe = room.subscribe(liveMap, (liveMap) => { }, { isDeep: true });
|
|
241
|
+
* unsubscribe();
|
|
242
|
+
*/
|
|
243
|
+
<TKey extends string, TValue>(liveMap: LiveMap<TKey, TValue>, callback: (updates: StorageUpdate[]) => void, options: {
|
|
244
|
+
isDeep: true;
|
|
245
|
+
}): () => void;
|
|
196
246
|
/**
|
|
197
|
-
*
|
|
247
|
+
* Subscribes to changes made on a {@link LiveObject} and all the nested data structures. Returns an unsubscribe function.
|
|
248
|
+
* In a future version, we will also expose what exactly changed in the {@link LiveObject}.
|
|
249
|
+
*
|
|
250
|
+
* @param listener the callback this called when the {@link LiveObject} changes.
|
|
198
251
|
*
|
|
199
|
-
* @
|
|
252
|
+
* @returns Unsubscribe function.
|
|
200
253
|
*
|
|
201
254
|
* @example
|
|
202
|
-
* const
|
|
203
|
-
* room.subscribe(
|
|
204
|
-
*
|
|
255
|
+
* const liveObject = new LiveObject();
|
|
256
|
+
* const unsubscribe = room.subscribe(liveObject, (liveObject) => { }, { isDeep: true });
|
|
257
|
+
* unsubscribe();
|
|
258
|
+
*/
|
|
259
|
+
<TData>(liveObject: LiveObject<TData>, callback: (updates: StorageUpdate[]) => void, options: {
|
|
260
|
+
isDeep: true;
|
|
261
|
+
}): () => void;
|
|
262
|
+
/**
|
|
263
|
+
* Subscribes to changes made on a {@link LiveList} and all the nested data structures. Returns an unsubscribe function.
|
|
264
|
+
* In a future version, we will also expose what exactly changed in the {@link LiveList}.
|
|
265
|
+
*
|
|
266
|
+
* @param listener the callback this called when the {@link LiveList} changes.
|
|
267
|
+
*
|
|
268
|
+
* @returns Unsubscribe function.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* const liveList = new LiveList();
|
|
272
|
+
* const unsubscribe = room.subscribe(liveList, (liveList) => { }, { isDeep: true });
|
|
273
|
+
* unsubscribe();
|
|
274
|
+
*/
|
|
275
|
+
<TItem>(liveList: LiveList<TItem>, callback: (updates: StorageUpdate[]) => void, options: {
|
|
276
|
+
isDeep: true;
|
|
277
|
+
}): () => void;
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* Room's history contains functions that let you undo and redo operation made on by the current client on the presence and storage.
|
|
281
|
+
*/
|
|
282
|
+
history: {
|
|
283
|
+
/**
|
|
284
|
+
* Undoes the last operation executed by the current client.
|
|
285
|
+
* It does not impact operations made by other clients.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* room.updatePresence({ selectedId: "xxx" }, { addToHistory: true });
|
|
289
|
+
* room.updatePresence({ selectedId: "yyy" }, { addToHistory: true });
|
|
290
|
+
* room.history.undo();
|
|
291
|
+
* // room.getPresence() equals { selectedId: "xxx" }
|
|
292
|
+
*/
|
|
293
|
+
undo: () => void;
|
|
294
|
+
/**
|
|
295
|
+
* Redoes the last operation executed by the current client.
|
|
296
|
+
* It does not impact operations made by other clients.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* room.updatePresence({ selectedId: "xxx" }, { addToHistory: true });
|
|
300
|
+
* room.updatePresence({ selectedId: "yyy" }, { addToHistory: true });
|
|
301
|
+
* room.history.undo();
|
|
302
|
+
* // room.getPresence() equals { selectedId: "xxx" }
|
|
303
|
+
* room.history.redo();
|
|
304
|
+
* // room.getPresence() equals { selectedId: "yyy" }
|
|
305
|
+
*/
|
|
306
|
+
redo: () => void;
|
|
307
|
+
/**
|
|
308
|
+
* All future modifications made on the Room will be merged together to create a single history item until resume is called.
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
|
|
312
|
+
* room.history.pause();
|
|
313
|
+
* room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
|
|
314
|
+
* room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
|
|
315
|
+
* room.history.resume();
|
|
316
|
+
* room.history.undo();
|
|
317
|
+
* // room.getPresence() equals { cursor: { x: 0, y: 0 } }
|
|
318
|
+
*/
|
|
319
|
+
pause: () => void;
|
|
320
|
+
/**
|
|
321
|
+
* Resumes history. Modifications made on the Room are not merged into a single history item anymore.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* room.updatePresence({ cursor: { x: 0, y: 0 } }, { addToHistory: true });
|
|
325
|
+
* room.history.pause();
|
|
326
|
+
* room.updatePresence({ cursor: { x: 1, y: 1 } }, { addToHistory: true });
|
|
327
|
+
* room.updatePresence({ cursor: { x: 2, y: 2 } }, { addToHistory: true });
|
|
328
|
+
* room.history.resume();
|
|
329
|
+
* room.history.undo();
|
|
330
|
+
* // room.getPresence() equals { cursor: { x: 0, y: 0 } }
|
|
331
|
+
*/
|
|
332
|
+
resume: () => void;
|
|
333
|
+
};
|
|
334
|
+
/**
|
|
335
|
+
* @deprecated use the callback returned by subscribe instead.
|
|
336
|
+
* See v0.13 release notes for more information.
|
|
337
|
+
* Will be removed in a future version.
|
|
338
|
+
*/
|
|
339
|
+
unsubscribe: {
|
|
340
|
+
/**
|
|
341
|
+
* @deprecated use the callback returned by subscribe instead.
|
|
342
|
+
* See v0.13 release notes for more information.
|
|
343
|
+
* Will be removed in a future version.
|
|
344
|
+
*/
|
|
345
|
+
<T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): void;
|
|
346
|
+
/**
|
|
347
|
+
* @deprecated use the callback returned by subscribe instead.
|
|
348
|
+
* See v0.13 release notes for more information.
|
|
349
|
+
* Will be removed in a future version.
|
|
350
|
+
*/
|
|
351
|
+
<T extends Presence>(type: "others", listener: OthersEventCallback<T>): void;
|
|
352
|
+
/**
|
|
353
|
+
* @deprecated use the callback returned by subscribe instead.
|
|
354
|
+
* See v0.13 release notes for more information.
|
|
355
|
+
* Will be removed in a future version.
|
|
205
356
|
*/
|
|
206
357
|
(type: "event", listener: EventCallback): void;
|
|
207
358
|
/**
|
|
208
|
-
*
|
|
359
|
+
* @deprecated use the callback returned by subscribe instead.
|
|
360
|
+
* See v0.13 release notes for more information.
|
|
361
|
+
* Will be removed in a future version.
|
|
209
362
|
*/
|
|
210
363
|
(type: "error", listener: ErrorCallback): void;
|
|
211
364
|
/**
|
|
212
|
-
*
|
|
365
|
+
* @deprecated use the callback returned by subscribe instead.
|
|
366
|
+
* See v0.13 release notes for more information.
|
|
367
|
+
* Will be removed in a future version.
|
|
213
368
|
*/
|
|
214
369
|
(type: "connection", listener: ConnectionCallback): void;
|
|
215
370
|
};
|
|
@@ -237,7 +392,8 @@ export declare type Room = {
|
|
|
237
392
|
getOthers: <T extends Presence>() => Others<T>;
|
|
238
393
|
/**
|
|
239
394
|
* Updates the presence of the current user. Only pass the properties you want to update. No need to send the full presence.
|
|
240
|
-
* @param
|
|
395
|
+
* @param overrides A partial object that contains the properties you want to update.
|
|
396
|
+
* @param overrides Optional object to configure the behavior of updatePresence.
|
|
241
397
|
*
|
|
242
398
|
* @example
|
|
243
399
|
* room.updatePresence({ x: 0 });
|
|
@@ -246,10 +402,15 @@ export declare type Room = {
|
|
|
246
402
|
* const presence = room.getPresence();
|
|
247
403
|
* // presence is equivalent to { x: 0, y: 0 }
|
|
248
404
|
*/
|
|
249
|
-
updatePresence: <T extends Presence>(overrides: Partial<T
|
|
405
|
+
updatePresence: <T extends Presence>(overrides: Partial<T>, options?: {
|
|
406
|
+
/**
|
|
407
|
+
* Whether or not the presence should have an impact on the undo/redo history.
|
|
408
|
+
*/
|
|
409
|
+
addToHistory: boolean;
|
|
410
|
+
}) => void;
|
|
250
411
|
/**
|
|
251
|
-
*
|
|
252
|
-
* @param {any} event
|
|
412
|
+
* Broadcasts an event to other users in the room. Event broadcasted to the room can be listened with {@link Room.subscribe}("event").
|
|
413
|
+
* @param {any} event the event to broadcast. Should be serializable to JSON
|
|
253
414
|
*
|
|
254
415
|
* @example
|
|
255
416
|
* // On client A
|
|
@@ -263,10 +424,29 @@ export declare type Room = {
|
|
|
263
424
|
* });
|
|
264
425
|
*/
|
|
265
426
|
broadcastEvent: (event: any) => void;
|
|
427
|
+
/**
|
|
428
|
+
* Get the room's storage asynchronously.
|
|
429
|
+
* The storage's root is a {@link LiveObject}.
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* const { root } = await room.getStorage();
|
|
433
|
+
*/
|
|
266
434
|
getStorage: <TRoot>() => Promise<{
|
|
267
435
|
root: LiveObject<TRoot>;
|
|
268
436
|
}>;
|
|
269
|
-
|
|
270
|
-
|
|
437
|
+
/**
|
|
438
|
+
* Batches modifications made during the given function.
|
|
439
|
+
* All the modifications are sent to other clients in a single message.
|
|
440
|
+
* All the subscribers are called only after the batch is over.
|
|
441
|
+
* All the modifications are merged in a single history item (undo/redo).
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* const { root } = await room.getStorage();
|
|
445
|
+
* room.batch(() => {
|
|
446
|
+
* root.set("x", 0);
|
|
447
|
+
* room.updatePresence({ cursor: { x: 100, y: 100 }});
|
|
448
|
+
* });
|
|
449
|
+
*/
|
|
450
|
+
batch: (fn: () => void) => void;
|
|
271
451
|
};
|
|
272
452
|
export {};
|
package/lib/cjs/utils.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
+
import { AbstractCrdt, Doc } from "./AbstractCrdt";
|
|
2
|
+
import { SerializedCrdtWithId } from "./live";
|
|
1
3
|
export declare function remove<T>(array: T[], item: T): void;
|
|
4
|
+
export declare function isSameNodeOrChildOf(node: AbstractCrdt, parent: AbstractCrdt): boolean;
|
|
5
|
+
export declare function deserialize(entry: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): AbstractCrdt;
|
|
6
|
+
export declare function isCrdt(obj: any): obj is AbstractCrdt;
|
|
7
|
+
export declare function selfOrRegisterValue(obj: AbstractCrdt): any;
|
|
8
|
+
export declare function selfOrRegister(obj: any): AbstractCrdt;
|
package/lib/cjs/utils.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.remove = void 0;
|
|
3
|
+
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");
|
|
4
9
|
function remove(array, item) {
|
|
5
10
|
for (let i = 0; i < array.length; i++) {
|
|
6
11
|
if (array[i] === item) {
|
|
@@ -10,3 +15,61 @@ function remove(array, item) {
|
|
|
10
15
|
}
|
|
11
16
|
}
|
|
12
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;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Op } from "./live";
|
|
2
|
+
export declare type ApplyResult = {
|
|
3
|
+
reverse: Op[];
|
|
4
|
+
modified: AbstractCrdt;
|
|
5
|
+
} | {
|
|
6
|
+
modified: false;
|
|
7
|
+
};
|
|
8
|
+
export interface Doc {
|
|
9
|
+
generateId: () => string;
|
|
10
|
+
generateOpId: () => string;
|
|
11
|
+
addItem: (id: string, item: AbstractCrdt) => void;
|
|
12
|
+
deleteItem: (id: string) => void;
|
|
13
|
+
dispatch: (ops: Op[], reverseOps: Op[], modified: AbstractCrdt[]) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare abstract class AbstractCrdt {
|
|
16
|
+
#private;
|
|
17
|
+
/**
|
|
18
|
+
* INTERNAL
|
|
19
|
+
*/
|
|
20
|
+
protected get _doc(): Doc | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* INTERNAL
|
|
23
|
+
*/
|
|
24
|
+
get _id(): string | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* INTERNAL
|
|
27
|
+
*/
|
|
28
|
+
get _parent(): AbstractCrdt | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* INTERNAL
|
|
31
|
+
*/
|
|
32
|
+
get _parentKey(): string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* INTERNAL
|
|
35
|
+
*/
|
|
36
|
+
_apply(op: Op): ApplyResult;
|
|
37
|
+
/**
|
|
38
|
+
* INTERNAL
|
|
39
|
+
*/
|
|
40
|
+
_setParentLink(parent: AbstractCrdt, key: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* INTERNAL
|
|
43
|
+
*/
|
|
44
|
+
_attach(id: string, doc: Doc): void;
|
|
45
|
+
/**
|
|
46
|
+
* INTERNAL
|
|
47
|
+
*/
|
|
48
|
+
abstract _attachChild(id: string, key: string, crdt: AbstractCrdt): ApplyResult;
|
|
49
|
+
/**
|
|
50
|
+
* INTERNAL
|
|
51
|
+
*/
|
|
52
|
+
_detach(): void;
|
|
53
|
+
/**
|
|
54
|
+
* INTERNAL
|
|
55
|
+
*/
|
|
56
|
+
abstract _detachChild(crdt: AbstractCrdt): void;
|
|
57
|
+
/**
|
|
58
|
+
* INTERNAL
|
|
59
|
+
*/
|
|
60
|
+
abstract _serialize(parentId: string, parentKey: string): Op[];
|
|
61
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
+
};
|
|
12
|
+
var _AbstractCrdt_parent, _AbstractCrdt_doc, _AbstractCrdt_id, _AbstractCrdt_parentKey;
|
|
13
|
+
import { OpType } from "./live";
|
|
14
|
+
export class AbstractCrdt {
|
|
15
|
+
constructor() {
|
|
16
|
+
_AbstractCrdt_parent.set(this, void 0);
|
|
17
|
+
_AbstractCrdt_doc.set(this, void 0);
|
|
18
|
+
_AbstractCrdt_id.set(this, void 0);
|
|
19
|
+
_AbstractCrdt_parentKey.set(this, void 0);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* INTERNAL
|
|
23
|
+
*/
|
|
24
|
+
get _doc() {
|
|
25
|
+
return __classPrivateFieldGet(this, _AbstractCrdt_doc, "f");
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* INTERNAL
|
|
29
|
+
*/
|
|
30
|
+
get _id() {
|
|
31
|
+
return __classPrivateFieldGet(this, _AbstractCrdt_id, "f");
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* INTERNAL
|
|
35
|
+
*/
|
|
36
|
+
get _parent() {
|
|
37
|
+
return __classPrivateFieldGet(this, _AbstractCrdt_parent, "f");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* INTERNAL
|
|
41
|
+
*/
|
|
42
|
+
get _parentKey() {
|
|
43
|
+
return __classPrivateFieldGet(this, _AbstractCrdt_parentKey, "f");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* INTERNAL
|
|
47
|
+
*/
|
|
48
|
+
_apply(op) {
|
|
49
|
+
switch (op.type) {
|
|
50
|
+
case OpType.DeleteCrdt: {
|
|
51
|
+
if (this._parent != null && this._parentKey != null) {
|
|
52
|
+
const parent = this._parent;
|
|
53
|
+
const reverse = this._serialize(this._parent._id, this._parentKey);
|
|
54
|
+
this._parent._detachChild(this);
|
|
55
|
+
return { modified: parent, reverse };
|
|
56
|
+
}
|
|
57
|
+
return { modified: false };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return { modified: false };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* INTERNAL
|
|
64
|
+
*/
|
|
65
|
+
_setParentLink(parent, key) {
|
|
66
|
+
if (__classPrivateFieldGet(this, _AbstractCrdt_parent, "f") != null && __classPrivateFieldGet(this, _AbstractCrdt_parent, "f") !== parent) {
|
|
67
|
+
throw new Error("Cannot attach parent if it already exist");
|
|
68
|
+
}
|
|
69
|
+
__classPrivateFieldSet(this, _AbstractCrdt_parentKey, key, "f");
|
|
70
|
+
__classPrivateFieldSet(this, _AbstractCrdt_parent, parent, "f");
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* INTERNAL
|
|
74
|
+
*/
|
|
75
|
+
_attach(id, doc) {
|
|
76
|
+
if (__classPrivateFieldGet(this, _AbstractCrdt_id, "f") || __classPrivateFieldGet(this, _AbstractCrdt_doc, "f")) {
|
|
77
|
+
throw new Error("Cannot attach if CRDT is already attached");
|
|
78
|
+
}
|
|
79
|
+
doc.addItem(id, this);
|
|
80
|
+
__classPrivateFieldSet(this, _AbstractCrdt_id, id, "f");
|
|
81
|
+
__classPrivateFieldSet(this, _AbstractCrdt_doc, doc, "f");
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* INTERNAL
|
|
85
|
+
*/
|
|
86
|
+
_detach() {
|
|
87
|
+
if (__classPrivateFieldGet(this, _AbstractCrdt_doc, "f") && __classPrivateFieldGet(this, _AbstractCrdt_id, "f")) {
|
|
88
|
+
__classPrivateFieldGet(this, _AbstractCrdt_doc, "f").deleteItem(__classPrivateFieldGet(this, _AbstractCrdt_id, "f"));
|
|
89
|
+
}
|
|
90
|
+
__classPrivateFieldSet(this, _AbstractCrdt_parent, undefined, "f");
|
|
91
|
+
__classPrivateFieldSet(this, _AbstractCrdt_doc, undefined, "f");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
_AbstractCrdt_parent = new WeakMap(), _AbstractCrdt_doc = new WeakMap(), _AbstractCrdt_id = new WeakMap(), _AbstractCrdt_parentKey = new WeakMap();
|