@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.
Files changed (44) hide show
  1. package/README.md +34 -6
  2. package/lib/cjs/AbstractCrdt.d.ts +61 -0
  3. package/lib/cjs/AbstractCrdt.js +98 -0
  4. package/lib/cjs/LiveList.d.ts +133 -0
  5. package/lib/cjs/LiveList.js +374 -0
  6. package/lib/cjs/LiveMap.d.ts +83 -0
  7. package/lib/cjs/LiveMap.js +272 -0
  8. package/lib/cjs/LiveObject.d.ts +66 -0
  9. package/lib/cjs/LiveObject.js +368 -0
  10. package/lib/cjs/LiveRegister.d.ts +21 -0
  11. package/lib/cjs/LiveRegister.js +69 -0
  12. package/lib/cjs/index.d.ts +3 -1
  13. package/lib/cjs/index.js +7 -5
  14. package/lib/cjs/room.d.ts +50 -9
  15. package/lib/cjs/room.js +476 -85
  16. package/lib/cjs/types.d.ts +220 -40
  17. package/lib/cjs/utils.d.ts +7 -0
  18. package/lib/cjs/utils.js +64 -1
  19. package/lib/esm/AbstractCrdt.d.ts +61 -0
  20. package/lib/esm/AbstractCrdt.js +94 -0
  21. package/lib/esm/LiveList.d.ts +133 -0
  22. package/lib/esm/LiveList.js +370 -0
  23. package/lib/esm/LiveMap.d.ts +83 -0
  24. package/lib/esm/LiveMap.js +268 -0
  25. package/lib/esm/LiveObject.d.ts +66 -0
  26. package/lib/esm/LiveObject.js +364 -0
  27. package/lib/esm/LiveRegister.d.ts +21 -0
  28. package/lib/esm/LiveRegister.js +65 -0
  29. package/lib/esm/index.d.ts +3 -1
  30. package/lib/esm/index.js +3 -1
  31. package/lib/esm/room.d.ts +50 -9
  32. package/lib/esm/room.js +478 -84
  33. package/lib/esm/types.d.ts +220 -40
  34. package/lib/esm/utils.d.ts +7 -0
  35. package/lib/esm/utils.js +58 -0
  36. package/package.json +3 -3
  37. package/lib/cjs/doc.d.ts +0 -347
  38. package/lib/cjs/doc.js +0 -1349
  39. package/lib/cjs/storage.d.ts +0 -21
  40. package/lib/cjs/storage.js +0 -68
  41. package/lib/esm/doc.d.ts +0 -347
  42. package/lib/esm/doc.js +0 -1342
  43. package/lib/esm/storage.d.ts +0 -21
  44. package/lib/esm/storage.js +0 -65
package/lib/esm/doc.js DELETED
@@ -1,1342 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- 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");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- 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");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _Doc_instances, _Doc_clock, _Doc_opClock, _Doc_items, _Doc_root, _Doc_actor, _Doc_dispatch, _Doc_undoStack, _Doc_redoStack, _Doc_applyOp, _AbstractCrdt_instances, _AbstractCrdt_listeners, _AbstractCrdt_deepListeners, _AbstractCrdt_parent, _AbstractCrdt_doc, _AbstractCrdt_id, _AbstractCrdt_parentKey, _AbstractCrdt_applySetParentKey, _AbstractCrdt_applyRegister, _AbstractCrdt_applyCreateObject, _AbstractCrdt_applyCreateMap, _AbstractCrdt_applyCreateList, _LiveObject_instances, _LiveObject_map, _LiveObject_propToLastUpdate, _LiveObject_applyDeleteObjectKey, _LiveMap_map, _LiveRegister_data, _LiveList_items, _LiveListIterator_innerIterator;
13
- import { remove } from "./utils";
14
- import { CrdtType, OpType, } from "./live";
15
- import { compare, makePosition } from "./position";
16
- function noOp() { }
17
- const MAX_UNDO_STACK = 50;
18
- export class Doc {
19
- constructor(root, actor = 0, dispatch = noOp) {
20
- _Doc_instances.add(this);
21
- _Doc_clock.set(this, 0);
22
- _Doc_opClock.set(this, 0);
23
- _Doc_items.set(this, new Map());
24
- _Doc_root.set(this, void 0);
25
- _Doc_actor.set(this, void 0);
26
- _Doc_dispatch.set(this, void 0);
27
- _Doc_undoStack.set(this, []);
28
- _Doc_redoStack.set(this, []);
29
- __classPrivateFieldSet(this, _Doc_root, root, "f");
30
- __classPrivateFieldSet(this, _Doc_actor, actor, "f");
31
- __classPrivateFieldSet(this, _Doc_dispatch, dispatch, "f");
32
- }
33
- static from(root, actor = 0, dispatch = noOp) {
34
- const rootRecord = new LiveObject(root);
35
- const storage = new Doc(rootRecord, actor, dispatch);
36
- rootRecord._attach(storage.generateId(), storage);
37
- storage.dispatch(rootRecord._serialize());
38
- return storage;
39
- }
40
- static load(items, actor, dispatch = noOp) {
41
- if (items.length === 0) {
42
- throw new Error("Internal error: cannot load storage without items");
43
- }
44
- const parentToChildren = new Map();
45
- let root = null;
46
- for (const tuple of items) {
47
- const parentId = tuple[1].parentId;
48
- if (parentId == null) {
49
- root = tuple;
50
- }
51
- else {
52
- const children = parentToChildren.get(parentId);
53
- if (children != null) {
54
- children.push(tuple);
55
- }
56
- else {
57
- parentToChildren.set(parentId, [tuple]);
58
- }
59
- }
60
- }
61
- if (root == null) {
62
- throw new Error("Root can't be null");
63
- }
64
- const doc = new Doc(null, actor, dispatch);
65
- __classPrivateFieldSet(doc, _Doc_root, LiveObject._deserialize(root, parentToChildren, doc), "f");
66
- return doc;
67
- }
68
- dispatch(ops) {
69
- __classPrivateFieldSet(this, _Doc_redoStack, [], "f");
70
- __classPrivateFieldGet(this, _Doc_dispatch, "f").call(this, ops);
71
- }
72
- addItem(id, item) {
73
- __classPrivateFieldGet(this, _Doc_items, "f").set(id, item);
74
- }
75
- deleteItem(id) {
76
- __classPrivateFieldGet(this, _Doc_items, "f").delete(id);
77
- }
78
- getItem(id) {
79
- return __classPrivateFieldGet(this, _Doc_items, "f").get(id);
80
- }
81
- apply(ops) {
82
- const reverse = [];
83
- for (const op of ops) {
84
- reverse.push(...__classPrivateFieldGet(this, _Doc_instances, "m", _Doc_applyOp).call(this, op));
85
- }
86
- return reverse;
87
- }
88
- get root() {
89
- return __classPrivateFieldGet(this, _Doc_root, "f");
90
- }
91
- addToUndoStack(ops) {
92
- if (__classPrivateFieldGet(this, _Doc_undoStack, "f").length >= MAX_UNDO_STACK) {
93
- __classPrivateFieldGet(this, _Doc_undoStack, "f").shift();
94
- }
95
- __classPrivateFieldGet(this, _Doc_undoStack, "f").push(ops);
96
- }
97
- undo() {
98
- const ops = __classPrivateFieldGet(this, _Doc_undoStack, "f").pop();
99
- if (ops == null) {
100
- return;
101
- }
102
- __classPrivateFieldGet(this, _Doc_redoStack, "f").push(this.apply(ops));
103
- __classPrivateFieldGet(this, _Doc_dispatch, "f").call(this, ops);
104
- }
105
- redo() {
106
- const ops = __classPrivateFieldGet(this, _Doc_redoStack, "f").pop();
107
- if (ops == null) {
108
- return;
109
- }
110
- __classPrivateFieldGet(this, _Doc_undoStack, "f").push(this.apply(ops));
111
- __classPrivateFieldGet(this, _Doc_dispatch, "f").call(this, ops);
112
- }
113
- count() {
114
- return __classPrivateFieldGet(this, _Doc_items, "f").size;
115
- }
116
- generateId() {
117
- var _a, _b;
118
- return `${__classPrivateFieldGet(this, _Doc_actor, "f")}:${__classPrivateFieldSet(this, _Doc_clock, (_b = __classPrivateFieldGet(this, _Doc_clock, "f"), _a = _b++, _b), "f"), _a}`;
119
- }
120
- generateOpId() {
121
- var _a, _b;
122
- return `${__classPrivateFieldGet(this, _Doc_actor, "f")}:${__classPrivateFieldSet(this, _Doc_opClock, (_b = __classPrivateFieldGet(this, _Doc_opClock, "f"), _a = _b++, _b), "f"), _a}`;
123
- }
124
- }
125
- _Doc_clock = new WeakMap(), _Doc_opClock = new WeakMap(), _Doc_items = new WeakMap(), _Doc_root = new WeakMap(), _Doc_actor = new WeakMap(), _Doc_dispatch = new WeakMap(), _Doc_undoStack = new WeakMap(), _Doc_redoStack = new WeakMap(), _Doc_instances = new WeakSet(), _Doc_applyOp = function _Doc_applyOp(op) {
126
- switch (op.type) {
127
- case OpType.DeleteObjectKey:
128
- case OpType.UpdateObject:
129
- case OpType.DeleteCrdt:
130
- case OpType.SetParentKey: {
131
- const item = __classPrivateFieldGet(this, _Doc_items, "f").get(op.id);
132
- if (item == null) {
133
- return [];
134
- }
135
- return item._apply(op);
136
- break;
137
- }
138
- case OpType.CreateList:
139
- case OpType.CreateObject:
140
- case OpType.CreateMap:
141
- case OpType.CreateRegister: {
142
- const parent = __classPrivateFieldGet(this, _Doc_items, "f").get(op.parentId);
143
- if (parent == null) {
144
- return [];
145
- }
146
- return parent._apply(op);
147
- break;
148
- }
149
- }
150
- return [];
151
- };
152
- class AbstractCrdt {
153
- constructor() {
154
- _AbstractCrdt_instances.add(this);
155
- _AbstractCrdt_listeners.set(this, []);
156
- _AbstractCrdt_deepListeners.set(this, []);
157
- _AbstractCrdt_parent.set(this, void 0);
158
- _AbstractCrdt_doc.set(this, void 0);
159
- _AbstractCrdt_id.set(this, void 0);
160
- _AbstractCrdt_parentKey.set(this, void 0);
161
- }
162
- /**
163
- * INTERNAL
164
- */
165
- get _doc() {
166
- return __classPrivateFieldGet(this, _AbstractCrdt_doc, "f");
167
- }
168
- /**
169
- * INTERNAL
170
- */
171
- get _id() {
172
- return __classPrivateFieldGet(this, _AbstractCrdt_id, "f");
173
- }
174
- /**
175
- * INTERNAL
176
- */
177
- get _parent() {
178
- return __classPrivateFieldGet(this, _AbstractCrdt_parent, "f");
179
- }
180
- /**
181
- * INTERNAL
182
- */
183
- get _parentKey() {
184
- return __classPrivateFieldGet(this, _AbstractCrdt_parentKey, "f");
185
- }
186
- _apply(op) {
187
- switch (op.type) {
188
- case OpType.DeleteCrdt: {
189
- if (this._parent != null && this._parentKey != null) {
190
- const reverse = this._serialize(this._parent._id, this._parentKey);
191
- this._parent._detachChild(this);
192
- return reverse;
193
- }
194
- return [];
195
- }
196
- case OpType.CreateObject: {
197
- return __classPrivateFieldGet(this, _AbstractCrdt_instances, "m", _AbstractCrdt_applyCreateObject).call(this, op);
198
- }
199
- case OpType.CreateMap: {
200
- return __classPrivateFieldGet(this, _AbstractCrdt_instances, "m", _AbstractCrdt_applyCreateMap).call(this, op);
201
- }
202
- case OpType.CreateRegister: {
203
- return __classPrivateFieldGet(this, _AbstractCrdt_instances, "m", _AbstractCrdt_applyRegister).call(this, op);
204
- }
205
- case OpType.CreateList: {
206
- return __classPrivateFieldGet(this, _AbstractCrdt_instances, "m", _AbstractCrdt_applyCreateList).call(this, op);
207
- }
208
- case OpType.SetParentKey: {
209
- return __classPrivateFieldGet(this, _AbstractCrdt_instances, "m", _AbstractCrdt_applySetParentKey).call(this, op);
210
- }
211
- }
212
- return [];
213
- }
214
- /**
215
- * INTERNAL
216
- */
217
- _setParentLink(parent, key) {
218
- if (__classPrivateFieldGet(this, _AbstractCrdt_parent, "f") != null && __classPrivateFieldGet(this, _AbstractCrdt_parent, "f") !== parent) {
219
- throw new Error("Cannot attach parent if it already exist");
220
- }
221
- __classPrivateFieldSet(this, _AbstractCrdt_parentKey, key, "f");
222
- __classPrivateFieldSet(this, _AbstractCrdt_parent, parent, "f");
223
- }
224
- /**
225
- * INTERNAL
226
- */
227
- _attach(id, doc) {
228
- if (__classPrivateFieldGet(this, _AbstractCrdt_id, "f") || __classPrivateFieldGet(this, _AbstractCrdt_doc, "f")) {
229
- throw new Error("Cannot attach if CRDT is already attached");
230
- }
231
- doc.addItem(id, this);
232
- __classPrivateFieldSet(this, _AbstractCrdt_id, id, "f");
233
- __classPrivateFieldSet(this, _AbstractCrdt_doc, doc, "f");
234
- }
235
- /**
236
- * INTERNAL
237
- */
238
- _detach() {
239
- if (__classPrivateFieldGet(this, _AbstractCrdt_doc, "f") && __classPrivateFieldGet(this, _AbstractCrdt_id, "f")) {
240
- __classPrivateFieldGet(this, _AbstractCrdt_doc, "f").deleteItem(__classPrivateFieldGet(this, _AbstractCrdt_id, "f"));
241
- }
242
- __classPrivateFieldSet(this, _AbstractCrdt_parent, undefined, "f");
243
- __classPrivateFieldSet(this, _AbstractCrdt_doc, undefined, "f");
244
- }
245
- /**
246
- * Subscribes to updates.
247
- */
248
- subscribe(listener) {
249
- __classPrivateFieldGet(this, _AbstractCrdt_listeners, "f").push(listener);
250
- }
251
- /**
252
- * Subscribes to updates and children updates.
253
- */
254
- subscribeDeep(listener) {
255
- __classPrivateFieldGet(this, _AbstractCrdt_deepListeners, "f").push(listener);
256
- }
257
- /**
258
- * Unsubscribes to updates.
259
- */
260
- unsubscribe(listener) {
261
- remove(__classPrivateFieldGet(this, _AbstractCrdt_listeners, "f"), listener);
262
- }
263
- /**
264
- * Unsubscribes to updates and children updates.
265
- */
266
- unsubscribeDeep(listener) {
267
- remove(__classPrivateFieldGet(this, _AbstractCrdt_deepListeners, "f"), listener);
268
- }
269
- /**
270
- * INTERNAL
271
- */
272
- _notify(onlyDeep = false) {
273
- if (onlyDeep === false) {
274
- for (const listener of __classPrivateFieldGet(this, _AbstractCrdt_listeners, "f")) {
275
- listener();
276
- }
277
- }
278
- for (const listener of __classPrivateFieldGet(this, _AbstractCrdt_deepListeners, "f")) {
279
- listener();
280
- }
281
- if (this._parent) {
282
- this._parent._notify(true);
283
- }
284
- }
285
- }
286
- _AbstractCrdt_listeners = new WeakMap(), _AbstractCrdt_deepListeners = new WeakMap(), _AbstractCrdt_parent = new WeakMap(), _AbstractCrdt_doc = new WeakMap(), _AbstractCrdt_id = new WeakMap(), _AbstractCrdt_parentKey = new WeakMap(), _AbstractCrdt_instances = new WeakSet(), _AbstractCrdt_applySetParentKey = function _AbstractCrdt_applySetParentKey(op) {
287
- if (this._parent == null) {
288
- return [];
289
- }
290
- if (this._parent instanceof LiveList) {
291
- const previousKey = this._parentKey;
292
- this._parent._setChildKey(op.parentKey, this);
293
- return [
294
- { type: OpType.SetParentKey, id: this._id, parentKey: previousKey },
295
- ];
296
- }
297
- return [];
298
- }, _AbstractCrdt_applyRegister = function _AbstractCrdt_applyRegister(op) {
299
- if (this._doc == null) {
300
- throw new Error("Internal: doc should exist");
301
- }
302
- if (this._doc.getItem(op.id) != null) {
303
- return [];
304
- }
305
- return this._attachChild(op.id, op.parentKey, new LiveRegister(op.data));
306
- }, _AbstractCrdt_applyCreateObject = function _AbstractCrdt_applyCreateObject(op) {
307
- if (this._doc == null) {
308
- throw new Error("Internal: doc should exist");
309
- }
310
- if (this._doc.getItem(op.id) != null) {
311
- return [];
312
- }
313
- return this._attachChild(op.id, op.parentKey, new LiveObject(op.data));
314
- }, _AbstractCrdt_applyCreateMap = function _AbstractCrdt_applyCreateMap(op) {
315
- if (this._doc == null) {
316
- throw new Error("Internal: doc should exist");
317
- }
318
- if (this._doc.getItem(op.id) != null) {
319
- return [];
320
- }
321
- return this._attachChild(op.id, op.parentKey, new LiveMap());
322
- }, _AbstractCrdt_applyCreateList = function _AbstractCrdt_applyCreateList(op) {
323
- if (this._doc == null) {
324
- throw new Error("Internal: doc should exist");
325
- }
326
- if (this._doc.getItem(op.id) != null) {
327
- return [];
328
- }
329
- return this._attachChild(op.id, op.parentKey, new LiveList());
330
- };
331
- /**
332
- * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
333
- * Keys should be a string, and values should be serializable to JSON.
334
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
335
- */
336
- export class LiveObject extends AbstractCrdt {
337
- constructor(object = {}) {
338
- super();
339
- _LiveObject_instances.add(this);
340
- _LiveObject_map.set(this, void 0);
341
- _LiveObject_propToLastUpdate.set(this, new Map());
342
- for (const key in object) {
343
- const value = object[key];
344
- if (value instanceof AbstractCrdt) {
345
- value._setParentLink(this, key);
346
- }
347
- }
348
- __classPrivateFieldSet(this, _LiveObject_map, new Map(Object.entries(object)), "f");
349
- }
350
- /**
351
- * INTERNAL
352
- */
353
- _serialize(parentId, parentKey) {
354
- if (this._id == null) {
355
- throw new Error("Cannot serialize item is not attached");
356
- }
357
- const ops = [];
358
- const op = {
359
- id: this._id,
360
- type: OpType.CreateObject,
361
- parentId,
362
- parentKey,
363
- data: {},
364
- };
365
- ops.push(op);
366
- for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
367
- if (value instanceof AbstractCrdt) {
368
- ops.push(...value._serialize(this._id, key));
369
- }
370
- else {
371
- op.data[key] = value;
372
- }
373
- }
374
- return ops;
375
- }
376
- /**
377
- * INTERNAL
378
- */
379
- static _deserialize([id, item], parentToChildren, doc) {
380
- if (item.type !== CrdtType.Object) {
381
- throw new Error(`Tried to deserialize a record but item type is "${item.type}"`);
382
- }
383
- const object = new LiveObject(item.data);
384
- object._attach(id, doc);
385
- const children = parentToChildren.get(id);
386
- if (children == null) {
387
- return object;
388
- }
389
- for (const entry of children) {
390
- const crdt = entry[1];
391
- if (crdt.parentKey == null) {
392
- throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
393
- }
394
- const child = deserialize(entry, parentToChildren, doc);
395
- child._setParentLink(object, crdt.parentKey);
396
- __classPrivateFieldGet(object, _LiveObject_map, "f").set(crdt.parentKey, child);
397
- }
398
- return object;
399
- }
400
- /**
401
- * INTERNAL
402
- */
403
- _attach(id, doc) {
404
- super._attach(id, doc);
405
- for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
406
- if (value instanceof AbstractCrdt) {
407
- value._attach(doc.generateId(), doc);
408
- }
409
- }
410
- }
411
- /**
412
- * INTERNAL
413
- */
414
- _attachChild(id, key, child) {
415
- if (this._doc == null) {
416
- throw new Error("Can't attach child if doc is not present");
417
- }
418
- const previousValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
419
- let result;
420
- if (isCrdt(previousValue)) {
421
- result = previousValue._serialize(this._id, key);
422
- previousValue._detach();
423
- }
424
- else if (previousValue === undefined) {
425
- result = [
426
- { type: OpType.DeleteObjectKey, id: this._id, key: key },
427
- ];
428
- }
429
- else {
430
- result = [
431
- {
432
- type: OpType.UpdateObject,
433
- id: this._id,
434
- data: { [key]: previousValue },
435
- },
436
- ];
437
- }
438
- __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, child);
439
- child._setParentLink(this, key);
440
- child._attach(id, this._doc);
441
- this._notify();
442
- return result;
443
- }
444
- /**
445
- * INTERNAL
446
- */
447
- _detachChild(child) {
448
- for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
449
- if (value === child) {
450
- __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
451
- }
452
- }
453
- if (child) {
454
- child._detach();
455
- }
456
- this._notify();
457
- }
458
- /**
459
- * INTERNAL
460
- */
461
- _detach() {
462
- super._detach();
463
- for (const value of __classPrivateFieldGet(this, _LiveObject_map, "f").values()) {
464
- if (isCrdt(value)) {
465
- value._detach();
466
- }
467
- }
468
- }
469
- /**
470
- * INTERNAL
471
- */
472
- _apply(op) {
473
- var _a;
474
- if (op.type === OpType.UpdateObject) {
475
- const reverse = [];
476
- const reverseUpdate = {
477
- type: OpType.UpdateObject,
478
- id: this._id,
479
- data: {},
480
- };
481
- reverse.push(reverseUpdate);
482
- for (const key in op.data) {
483
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
484
- if (oldValue !== undefined) {
485
- reverseUpdate.data[key] = oldValue;
486
- }
487
- else if (oldValue === undefined) {
488
- reverse.push({ type: OpType.DeleteObjectKey, id: this._id, key });
489
- }
490
- }
491
- for (const key in op.data) {
492
- if (op.opId == null) {
493
- op.opId = (_a = this._doc) === null || _a === void 0 ? void 0 : _a.generateOpId();
494
- }
495
- const lastOpId = __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key);
496
- if (lastOpId === op.opId) {
497
- __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").delete(key);
498
- continue;
499
- }
500
- else if (lastOpId != null) {
501
- continue;
502
- }
503
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
504
- if (isCrdt(oldValue)) {
505
- oldValue._detach();
506
- }
507
- const value = op.data[key];
508
- __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, value);
509
- }
510
- this._notify();
511
- return reverse;
512
- }
513
- else if (op.type === OpType.DeleteObjectKey) {
514
- return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyDeleteObjectKey).call(this, op);
515
- }
516
- return super._apply(op);
517
- }
518
- /**
519
- * Transform the LiveObject into a javascript object
520
- */
521
- toObject() {
522
- return Object.fromEntries(__classPrivateFieldGet(this, _LiveObject_map, "f"));
523
- }
524
- /**
525
- * Adds or updates a property with a specified key and a value.
526
- * @param key The key of the property to add
527
- * @param value The value of the property to add
528
- */
529
- set(key, value) {
530
- // TODO: Find out why typescript complains
531
- this.update({ [key]: value });
532
- }
533
- /**
534
- * Returns a specified property from the LiveObject.
535
- * @param key The key of the property to get
536
- */
537
- get(key) {
538
- return __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
539
- }
540
- /**
541
- * Adds or updates multiple properties at once with an object.
542
- * @param overrides The object used to overrides properties
543
- */
544
- update(overrides) {
545
- if (this._doc == null || this._id == null) {
546
- for (const key in overrides) {
547
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
548
- if (oldValue instanceof AbstractCrdt) {
549
- oldValue._detach();
550
- }
551
- const newValue = overrides[key];
552
- if (newValue instanceof AbstractCrdt) {
553
- newValue._setParentLink(this, key);
554
- }
555
- __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
556
- }
557
- this._notify();
558
- return;
559
- }
560
- const ops = [];
561
- const reverseOps = [];
562
- const opId = this._doc.generateOpId();
563
- const updateOp = {
564
- opId,
565
- id: this._id,
566
- type: OpType.UpdateObject,
567
- data: {},
568
- };
569
- ops.push(updateOp);
570
- const reverseUpdateOp = {
571
- id: this._id,
572
- type: OpType.UpdateObject,
573
- data: {},
574
- };
575
- reverseOps.push(reverseUpdateOp);
576
- for (const key in overrides) {
577
- __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, opId);
578
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
579
- if (oldValue instanceof AbstractCrdt) {
580
- reverseOps.push(...oldValue._serialize(this._id, key));
581
- oldValue._detach();
582
- }
583
- else if (oldValue === undefined) {
584
- reverseOps.push({ type: OpType.DeleteObjectKey, id: this._id, key });
585
- }
586
- else {
587
- reverseUpdateOp.data[key] = oldValue;
588
- }
589
- const newValue = overrides[key];
590
- if (newValue instanceof AbstractCrdt) {
591
- newValue._setParentLink(this, key);
592
- newValue._attach(this._doc.generateId(), this._doc);
593
- ops.push(...newValue._serialize(this._id, key));
594
- }
595
- else {
596
- updateOp.data[key] = newValue;
597
- }
598
- __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
599
- }
600
- this._doc.addToUndoStack(reverseOps);
601
- this._doc.dispatch(ops);
602
- this._notify();
603
- }
604
- }
605
- _LiveObject_map = new WeakMap(), _LiveObject_propToLastUpdate = new WeakMap(), _LiveObject_instances = new WeakSet(), _LiveObject_applyDeleteObjectKey = function _LiveObject_applyDeleteObjectKey(op) {
606
- const key = op.key;
607
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
608
- let result = [];
609
- if (isCrdt(oldValue)) {
610
- result = oldValue._serialize(this._id, op.key);
611
- oldValue._detach();
612
- }
613
- else if (oldValue !== undefined) {
614
- result = [
615
- {
616
- type: OpType.UpdateObject,
617
- id: this._id,
618
- data: { [key]: oldValue },
619
- },
620
- ];
621
- }
622
- __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
623
- this._notify();
624
- return result;
625
- };
626
- /**
627
- * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
628
- * Keys should be a string, and values should be serializable to JSON.
629
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
630
- */
631
- export class LiveMap extends AbstractCrdt {
632
- constructor(entries) {
633
- super();
634
- _LiveMap_map.set(this, void 0);
635
- if (entries) {
636
- const mappedEntries = [];
637
- for (const entry of entries) {
638
- const value = selfOrRegister(entry[1]);
639
- value._setParentLink(this, entry[0]);
640
- mappedEntries.push([entry[0], value]);
641
- }
642
- __classPrivateFieldSet(this, _LiveMap_map, new Map(mappedEntries), "f");
643
- }
644
- else {
645
- __classPrivateFieldSet(this, _LiveMap_map, new Map(), "f");
646
- }
647
- }
648
- /**
649
- * INTERNAL
650
- */
651
- _serialize(parentId, parentKey) {
652
- if (this._id == null) {
653
- throw new Error("Cannot serialize item is not attached");
654
- }
655
- if (parentId == null || parentKey == null) {
656
- throw new Error("Cannot serialize map if parentId or parentKey is undefined");
657
- }
658
- const ops = [];
659
- const op = {
660
- id: this._id,
661
- type: OpType.CreateMap,
662
- parentId,
663
- parentKey,
664
- };
665
- ops.push(op);
666
- for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
667
- ops.push(...value._serialize(this._id, key));
668
- }
669
- return ops;
670
- }
671
- /**
672
- * INTERNAL
673
- */
674
- static _deserialize([id, item], parentToChildren, doc) {
675
- if (item.type !== CrdtType.Map) {
676
- throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
677
- }
678
- const map = new LiveMap();
679
- map._attach(id, doc);
680
- const children = parentToChildren.get(id);
681
- if (children == null) {
682
- return map;
683
- }
684
- for (const entry of children) {
685
- const crdt = entry[1];
686
- if (crdt.parentKey == null) {
687
- throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
688
- }
689
- const child = deserialize(entry, parentToChildren, doc);
690
- child._setParentLink(map, crdt.parentKey);
691
- __classPrivateFieldGet(map, _LiveMap_map, "f").set(crdt.parentKey, child);
692
- }
693
- return map;
694
- }
695
- /**
696
- * INTERNAL
697
- */
698
- _attach(id, doc) {
699
- super._attach(id, doc);
700
- for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
701
- if (isCrdt(value)) {
702
- value._attach(doc.generateId(), doc);
703
- }
704
- }
705
- }
706
- /**
707
- * INTERNAL
708
- */
709
- _attachChild(id, key, child) {
710
- if (this._doc == null) {
711
- throw new Error("Can't attach child if doc is not present");
712
- }
713
- const previousValue = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
714
- let result;
715
- if (previousValue) {
716
- result = previousValue._serialize(this._id, key);
717
- previousValue._detach();
718
- }
719
- else {
720
- result = [{ type: OpType.DeleteCrdt, id }];
721
- }
722
- child._setParentLink(this, key);
723
- child._attach(id, this._doc);
724
- __classPrivateFieldGet(this, _LiveMap_map, "f").set(key, child);
725
- this._notify();
726
- return result;
727
- }
728
- /**
729
- * INTERNAL
730
- */
731
- _detach() {
732
- super._detach();
733
- for (const item of __classPrivateFieldGet(this, _LiveMap_map, "f").values()) {
734
- item._detach();
735
- }
736
- }
737
- /**
738
- * INTERNAL
739
- */
740
- _detachChild(child) {
741
- for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
742
- if (value === child) {
743
- __classPrivateFieldGet(this, _LiveMap_map, "f").delete(key);
744
- }
745
- }
746
- child._detach();
747
- this._notify();
748
- }
749
- /**
750
- * Returns a specified element from the LiveMap.
751
- * @param key The key of the element to return.
752
- * @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
753
- */
754
- get(key) {
755
- const value = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
756
- if (value == undefined) {
757
- return undefined;
758
- }
759
- return selfOrRegisterValue(value);
760
- }
761
- /**
762
- * Adds or updates an element with a specified key and a value.
763
- * @param key The key of the element to add. Should be a string.
764
- * @param value The value of the element to add. Should be serializable to JSON.
765
- */
766
- set(key, value) {
767
- const oldValue = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
768
- if (oldValue) {
769
- oldValue._detach();
770
- }
771
- const item = selfOrRegister(value);
772
- item._setParentLink(this, key);
773
- __classPrivateFieldGet(this, _LiveMap_map, "f").set(key, item);
774
- if (this._doc && this._id) {
775
- const id = this._doc.generateId();
776
- item._attach(id, this._doc);
777
- this._doc.addToUndoStack(oldValue
778
- ? oldValue._serialize(this._id, key)
779
- : [{ type: OpType.DeleteCrdt, id }]);
780
- this._doc.dispatch(item._serialize(this._id, key));
781
- }
782
- this._notify();
783
- }
784
- /**
785
- * Returns the number of elements in the LiveMap.
786
- */
787
- get size() {
788
- return __classPrivateFieldGet(this, _LiveMap_map, "f").size;
789
- }
790
- /**
791
- * Returns a boolean indicating whether an element with the specified key exists or not.
792
- * @param key The key of the element to test for presence.
793
- */
794
- has(key) {
795
- return __classPrivateFieldGet(this, _LiveMap_map, "f").has(key);
796
- }
797
- /**
798
- * Removes the specified element by key.
799
- * @param key The key of the element to remove.
800
- * @returns true if an element existed and has been removed, or false if the element does not exist.
801
- */
802
- delete(key) {
803
- const item = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
804
- if (item == null) {
805
- return false;
806
- }
807
- item._detach();
808
- if (this._doc && item._id) {
809
- this._doc.addToUndoStack(item._serialize(this._id, key));
810
- this._doc.dispatch([{ type: OpType.DeleteCrdt, id: item._id }]);
811
- }
812
- __classPrivateFieldGet(this, _LiveMap_map, "f").delete(key);
813
- this._notify();
814
- return true;
815
- }
816
- /**
817
- * Returns a new Iterator object that contains the [key, value] pairs for each element.
818
- */
819
- entries() {
820
- const innerIterator = __classPrivateFieldGet(this, _LiveMap_map, "f").entries();
821
- return {
822
- [Symbol.iterator]: function () {
823
- return this;
824
- },
825
- next() {
826
- const iteratorValue = innerIterator.next();
827
- if (iteratorValue.done) {
828
- return {
829
- done: true,
830
- value: undefined,
831
- };
832
- }
833
- const entry = iteratorValue.value;
834
- return {
835
- value: [entry[0], selfOrRegisterValue(iteratorValue.value[1])],
836
- };
837
- },
838
- };
839
- }
840
- /**
841
- * Same function object as the initial value of the entries method.
842
- */
843
- [(_LiveMap_map = new WeakMap(), Symbol.iterator)]() {
844
- return this.entries();
845
- }
846
- /**
847
- * Returns a new Iterator object that contains the keys for each element.
848
- */
849
- keys() {
850
- return __classPrivateFieldGet(this, _LiveMap_map, "f").keys();
851
- }
852
- /**
853
- * Returns a new Iterator object that contains the values for each element.
854
- */
855
- values() {
856
- const innerIterator = __classPrivateFieldGet(this, _LiveMap_map, "f").values();
857
- return {
858
- [Symbol.iterator]: function () {
859
- return this;
860
- },
861
- next() {
862
- const iteratorValue = innerIterator.next();
863
- if (iteratorValue.done) {
864
- return {
865
- done: true,
866
- value: undefined,
867
- };
868
- }
869
- return {
870
- value: selfOrRegisterValue(iteratorValue.value),
871
- };
872
- },
873
- };
874
- }
875
- /**
876
- * Executes a provided function once per each key/value pair in the Map object, in insertion order.
877
- * @param callback Function to execute for each entry in the map.
878
- */
879
- forEach(callback) {
880
- for (const entry of this) {
881
- callback(entry[1], entry[0], this);
882
- }
883
- }
884
- }
885
- /**
886
- * INTERNAL
887
- */
888
- class LiveRegister extends AbstractCrdt {
889
- constructor(data) {
890
- super();
891
- _LiveRegister_data.set(this, void 0);
892
- __classPrivateFieldSet(this, _LiveRegister_data, data, "f");
893
- }
894
- get data() {
895
- return __classPrivateFieldGet(this, _LiveRegister_data, "f");
896
- }
897
- /**
898
- * INTERNAL
899
- */
900
- static _deserialize([id, item], parentToChildren, doc) {
901
- if (item.type !== CrdtType.Register) {
902
- throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
903
- }
904
- const register = new LiveRegister(item.data);
905
- register._attach(id, doc);
906
- return register;
907
- }
908
- /**
909
- * INTERNAL
910
- */
911
- _serialize(parentId, parentKey) {
912
- if (this._id == null || parentId == null || parentKey == null) {
913
- throw new Error("Cannot serialize register if parentId or parentKey is undefined");
914
- }
915
- return [
916
- {
917
- type: OpType.CreateRegister,
918
- id: this._id,
919
- parentId,
920
- parentKey,
921
- data: this.data,
922
- },
923
- ];
924
- }
925
- _attachChild(id, key, crdt) {
926
- throw new Error("Method not implemented.");
927
- }
928
- _detachChild(crdt) {
929
- throw new Error("Method not implemented.");
930
- }
931
- _apply(op) {
932
- return super._apply(op);
933
- }
934
- }
935
- _LiveRegister_data = new WeakMap();
936
- /**
937
- * The LiveList class represents an ordered collection of items that is synchorinized across clients.
938
- */
939
- export class LiveList extends AbstractCrdt {
940
- constructor(items = []) {
941
- super();
942
- // TODO: Naive array at first, find a better data structure. Maybe an Order statistics tree?
943
- _LiveList_items.set(this, []);
944
- let position = undefined;
945
- for (let i = 0; i < items.length; i++) {
946
- const newPosition = makePosition(position);
947
- const item = selfOrRegister(items[i]);
948
- __classPrivateFieldGet(this, _LiveList_items, "f").push([item, newPosition]);
949
- position = newPosition;
950
- }
951
- }
952
- /**
953
- * INTERNAL
954
- */
955
- static _deserialize([id, item], parentToChildren, doc) {
956
- const list = new LiveList([]);
957
- list._attach(id, doc);
958
- const children = parentToChildren.get(id);
959
- if (children == null) {
960
- return list;
961
- }
962
- for (const entry of children) {
963
- const child = deserialize(entry, parentToChildren, doc);
964
- child._setParentLink(list, entry[1].parentKey);
965
- __classPrivateFieldGet(list, _LiveList_items, "f").push([child, entry[1].parentKey]);
966
- __classPrivateFieldGet(list, _LiveList_items, "f").sort((itemA, itemB) => compare(itemA[1], itemB[1]));
967
- }
968
- return list;
969
- }
970
- /**
971
- * INTERNAL
972
- */
973
- _serialize(parentId, parentKey) {
974
- if (this._id == null) {
975
- throw new Error("Cannot serialize item is not attached");
976
- }
977
- if (parentId == null || parentKey == null) {
978
- throw new Error("Cannot serialize list if parentId or parentKey is undefined");
979
- }
980
- const ops = [];
981
- const op = {
982
- id: this._id,
983
- type: OpType.CreateList,
984
- parentId,
985
- parentKey,
986
- };
987
- ops.push(op);
988
- for (const [value, key] of __classPrivateFieldGet(this, _LiveList_items, "f")) {
989
- ops.push(...value._serialize(this._id, key));
990
- }
991
- return ops;
992
- }
993
- /**
994
- * INTERNAL
995
- */
996
- _attach(id, doc) {
997
- super._attach(id, doc);
998
- for (const [item, position] of __classPrivateFieldGet(this, _LiveList_items, "f")) {
999
- item._attach(doc.generateId(), doc);
1000
- }
1001
- }
1002
- /**
1003
- * INTERNAL
1004
- */
1005
- _detach() {
1006
- super._detach();
1007
- for (const [value] of __classPrivateFieldGet(this, _LiveList_items, "f")) {
1008
- value._detach();
1009
- }
1010
- }
1011
- /**
1012
- * INTERNAL
1013
- */
1014
- _attachChild(id, key, child) {
1015
- var _a;
1016
- if (this._doc == null) {
1017
- throw new Error("Can't attach child if doc is not present");
1018
- }
1019
- child._attach(id, this._doc);
1020
- child._setParentLink(this, key);
1021
- const index = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[1] === key);
1022
- // Assign a temporary position until we get the fix from the backend
1023
- if (index !== -1) {
1024
- __classPrivateFieldGet(this, _LiveList_items, "f")[index][1] = makePosition(key, (_a = __classPrivateFieldGet(this, _LiveList_items, "f")[index + 1]) === null || _a === void 0 ? void 0 : _a[1]);
1025
- }
1026
- __classPrivateFieldGet(this, _LiveList_items, "f").push([child, key]);
1027
- __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => compare(itemA[1], itemB[1]));
1028
- this._notify();
1029
- return [{ type: OpType.DeleteCrdt, id }];
1030
- }
1031
- /**
1032
- * INTERNAL
1033
- */
1034
- _detachChild(child) {
1035
- const indexToDelete = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((item) => item[0] === child);
1036
- __classPrivateFieldGet(this, _LiveList_items, "f").splice(indexToDelete, 1);
1037
- if (child) {
1038
- child._detach();
1039
- }
1040
- this._notify();
1041
- }
1042
- /**
1043
- * INTERNAL
1044
- */
1045
- _setChildKey(key, child) {
1046
- var _a;
1047
- child._setParentLink(this, key);
1048
- const index = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[1] === key);
1049
- // Assign a temporary position until we get the fix from the backend
1050
- if (index !== -1) {
1051
- __classPrivateFieldGet(this, _LiveList_items, "f")[index][1] = makePosition(key, (_a = __classPrivateFieldGet(this, _LiveList_items, "f")[index + 1]) === null || _a === void 0 ? void 0 : _a[1]);
1052
- }
1053
- const item = __classPrivateFieldGet(this, _LiveList_items, "f").find((item) => item[0] === child);
1054
- if (item) {
1055
- item[1] = key;
1056
- }
1057
- __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => compare(itemA[1], itemB[1]));
1058
- this._notify();
1059
- }
1060
- /**
1061
- * INTERNAL
1062
- */
1063
- _apply(op) {
1064
- return super._apply(op);
1065
- }
1066
- /**
1067
- * Returns the number of elements.
1068
- */
1069
- get length() {
1070
- return __classPrivateFieldGet(this, _LiveList_items, "f").length;
1071
- }
1072
- /**
1073
- * Adds one element to the end of the LiveList.
1074
- * @param element The element to add to the end of the LiveList.
1075
- */
1076
- push(element) {
1077
- return this.insert(element, this.length);
1078
- }
1079
- /**
1080
- * Inserts one element at a specified index.
1081
- * @param element The element to insert.
1082
- * @param index The index at which you want to insert the element.
1083
- */
1084
- insert(element, index) {
1085
- if (index < 0 || index > __classPrivateFieldGet(this, _LiveList_items, "f").length) {
1086
- throw new Error(`Cannot delete list item at index "${index}". index should be between 0 and ${__classPrivateFieldGet(this, _LiveList_items, "f").length}`);
1087
- }
1088
- let before = __classPrivateFieldGet(this, _LiveList_items, "f")[index - 1] ? __classPrivateFieldGet(this, _LiveList_items, "f")[index - 1][1] : undefined;
1089
- let after = __classPrivateFieldGet(this, _LiveList_items, "f")[index] ? __classPrivateFieldGet(this, _LiveList_items, "f")[index][1] : undefined;
1090
- const position = makePosition(before, after);
1091
- const value = selfOrRegister(element);
1092
- value._setParentLink(this, position);
1093
- __classPrivateFieldGet(this, _LiveList_items, "f").push([value, position]);
1094
- __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => compare(itemA[1], itemB[1]));
1095
- this._notify();
1096
- if (this._doc && this._id) {
1097
- const id = this._doc.generateId();
1098
- value._attach(id, this._doc);
1099
- this._doc.addToUndoStack([{ type: OpType.DeleteCrdt, id }]);
1100
- this._doc.dispatch(value._serialize(this._id, position));
1101
- }
1102
- }
1103
- /**
1104
- * Move one element from one index to another.
1105
- * @param index The index of the element to move
1106
- * @param targetIndex The index where the element should be after moving.
1107
- */
1108
- move(index, targetIndex) {
1109
- if (targetIndex < 0) {
1110
- throw new Error("targetIndex cannot be less than 0");
1111
- }
1112
- if (targetIndex >= __classPrivateFieldGet(this, _LiveList_items, "f").length) {
1113
- throw new Error("targetIndex cannot be greater or equal than the list length");
1114
- }
1115
- if (index < 0) {
1116
- throw new Error("index cannot be less than 0");
1117
- }
1118
- if (index >= __classPrivateFieldGet(this, _LiveList_items, "f").length) {
1119
- throw new Error("index cannot be greater or equal than the list length");
1120
- }
1121
- let beforePosition = null;
1122
- let afterPosition = null;
1123
- if (index < targetIndex) {
1124
- afterPosition =
1125
- targetIndex === __classPrivateFieldGet(this, _LiveList_items, "f").length - 1
1126
- ? undefined
1127
- : __classPrivateFieldGet(this, _LiveList_items, "f")[targetIndex + 1][1];
1128
- beforePosition = __classPrivateFieldGet(this, _LiveList_items, "f")[targetIndex][1];
1129
- }
1130
- else {
1131
- afterPosition = __classPrivateFieldGet(this, _LiveList_items, "f")[targetIndex][1];
1132
- beforePosition =
1133
- targetIndex === 0 ? undefined : __classPrivateFieldGet(this, _LiveList_items, "f")[targetIndex - 1][1];
1134
- }
1135
- const position = makePosition(beforePosition, afterPosition);
1136
- const item = __classPrivateFieldGet(this, _LiveList_items, "f")[index];
1137
- const previousPosition = item[1];
1138
- item[1] = position;
1139
- item[0]._setParentLink(this, position);
1140
- __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => compare(itemA[1], itemB[1]));
1141
- this._notify();
1142
- if (this._doc && this._id) {
1143
- this._doc.addToUndoStack([
1144
- {
1145
- type: OpType.SetParentKey,
1146
- id: item[0]._id,
1147
- parentKey: previousPosition,
1148
- },
1149
- ]);
1150
- this._doc.dispatch([
1151
- {
1152
- type: OpType.SetParentKey,
1153
- id: item[0]._id,
1154
- parentKey: position,
1155
- },
1156
- ]);
1157
- }
1158
- }
1159
- /**
1160
- * Deletes an element at the specified index
1161
- * @param index The index of the element to delete
1162
- */
1163
- delete(index) {
1164
- if (index < 0 || index >= __classPrivateFieldGet(this, _LiveList_items, "f").length) {
1165
- throw new Error(`Cannot delete list item at index "${index}". index should be between 0 and ${__classPrivateFieldGet(this, _LiveList_items, "f").length - 1}`);
1166
- }
1167
- const item = __classPrivateFieldGet(this, _LiveList_items, "f")[index];
1168
- item[0]._detach();
1169
- __classPrivateFieldGet(this, _LiveList_items, "f").splice(index, 1);
1170
- if (this._doc) {
1171
- const childRecordId = item[0]._id;
1172
- if (childRecordId) {
1173
- this._doc.addToUndoStack(item[0]._serialize(this._id, item[1]));
1174
- this._doc.dispatch([
1175
- {
1176
- id: childRecordId,
1177
- type: OpType.DeleteCrdt,
1178
- },
1179
- ]);
1180
- }
1181
- }
1182
- this._notify();
1183
- }
1184
- /**
1185
- * Returns an Array of all the elements in the LiveList.
1186
- */
1187
- toArray() {
1188
- return __classPrivateFieldGet(this, _LiveList_items, "f").map((entry) => selfOrRegisterValue(entry[0]));
1189
- }
1190
- /**
1191
- * Tests whether all elements pass the test implemented by the provided function.
1192
- * @param predicate Function to test for each element, taking two arguments (the element and its index).
1193
- * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
1194
- */
1195
- every(predicate) {
1196
- return this.toArray().every(predicate);
1197
- }
1198
- /**
1199
- * Creates an array with all elements that pass the test implemented by the provided function.
1200
- * @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.
1201
- * @returns An array with the elements that pass the test.
1202
- */
1203
- filter(predicate) {
1204
- return this.toArray().filter(predicate);
1205
- }
1206
- /**
1207
- * Returns the first element that satisfies the provided testing function.
1208
- * @param predicate Function to execute on each value.
1209
- * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
1210
- */
1211
- find(predicate) {
1212
- return this.toArray().find(predicate);
1213
- }
1214
- /**
1215
- * Returns the index of the first element in the LiveList that satisfies the provided testing function.
1216
- * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
1217
- * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
1218
- */
1219
- findIndex(predicate) {
1220
- return this.toArray().findIndex(predicate);
1221
- }
1222
- /**
1223
- * Executes a provided function once for each element.
1224
- * @param callbackfn Function to execute on each element.
1225
- */
1226
- forEach(callbackfn) {
1227
- return this.toArray().forEach(callbackfn);
1228
- }
1229
- /**
1230
- * Get the element at the specified index.
1231
- * @param index The index on the element to get.
1232
- * @returns The element at the specified index or undefined.
1233
- */
1234
- get(index) {
1235
- if (index < 0 || index >= __classPrivateFieldGet(this, _LiveList_items, "f").length) {
1236
- return undefined;
1237
- }
1238
- return selfOrRegisterValue(__classPrivateFieldGet(this, _LiveList_items, "f")[index][0]);
1239
- }
1240
- /**
1241
- * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
1242
- * @param searchElement Element to locate.
1243
- * @param fromIndex The index to start the search at.
1244
- * @returns The first index of the element in the LiveList; -1 if not found.
1245
- */
1246
- indexOf(searchElement, fromIndex) {
1247
- return this.toArray().indexOf(searchElement, fromIndex);
1248
- }
1249
- /**
1250
- * 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.
1251
- * @param searchElement Element to locate.
1252
- * @param fromIndex The index at which to start searching backwards.
1253
- * @returns
1254
- */
1255
- lastIndexOf(searchElement, fromIndex) {
1256
- return this.toArray().lastIndexOf(searchElement, fromIndex);
1257
- }
1258
- /**
1259
- * Creates an array populated with the results of calling a provided function on every element.
1260
- * @param callback Function that is called for every element.
1261
- * @returns An array with each element being the result of the callback function.
1262
- */
1263
- map(callback) {
1264
- return __classPrivateFieldGet(this, _LiveList_items, "f").map((entry, i) => callback(selfOrRegisterValue(entry[0]), i));
1265
- }
1266
- /**
1267
- * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
1268
- * @param predicate Function to test for each element.
1269
- * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
1270
- */
1271
- some(predicate) {
1272
- return this.toArray().some(predicate);
1273
- }
1274
- [(_LiveList_items = new WeakMap(), Symbol.iterator)]() {
1275
- return new LiveListIterator(__classPrivateFieldGet(this, _LiveList_items, "f"));
1276
- }
1277
- }
1278
- class LiveListIterator {
1279
- constructor(items) {
1280
- _LiveListIterator_innerIterator.set(this, void 0);
1281
- __classPrivateFieldSet(this, _LiveListIterator_innerIterator, items[Symbol.iterator](), "f");
1282
- }
1283
- [(_LiveListIterator_innerIterator = new WeakMap(), Symbol.iterator)]() {
1284
- return this;
1285
- }
1286
- next() {
1287
- const result = __classPrivateFieldGet(this, _LiveListIterator_innerIterator, "f").next();
1288
- if (result.done) {
1289
- return {
1290
- done: true,
1291
- value: undefined,
1292
- };
1293
- }
1294
- return {
1295
- value: selfOrRegisterValue(result.value[0]),
1296
- };
1297
- }
1298
- }
1299
- function deserialize(entry, parentToChildren, doc) {
1300
- switch (entry[1].type) {
1301
- case CrdtType.Object: {
1302
- return LiveObject._deserialize(entry, parentToChildren, doc);
1303
- }
1304
- case CrdtType.List: {
1305
- return LiveList._deserialize(entry, parentToChildren, doc);
1306
- }
1307
- case CrdtType.Map: {
1308
- return LiveMap._deserialize(entry, parentToChildren, doc);
1309
- }
1310
- case CrdtType.Register: {
1311
- return LiveRegister._deserialize(entry, parentToChildren, doc);
1312
- }
1313
- default: {
1314
- throw new Error("Unexpected CRDT type");
1315
- }
1316
- }
1317
- }
1318
- function isCrdt(obj) {
1319
- return (obj instanceof LiveObject ||
1320
- obj instanceof LiveMap ||
1321
- obj instanceof LiveList ||
1322
- obj instanceof LiveRegister);
1323
- }
1324
- function selfOrRegisterValue(obj) {
1325
- if (obj instanceof LiveRegister) {
1326
- return obj.data;
1327
- }
1328
- return obj;
1329
- }
1330
- function selfOrRegister(obj) {
1331
- if (obj instanceof LiveObject ||
1332
- obj instanceof LiveMap ||
1333
- obj instanceof LiveList) {
1334
- return obj;
1335
- }
1336
- else if (obj instanceof LiveRegister) {
1337
- throw new Error("Internal error. LiveRegister should not be created from selfOrRegister");
1338
- }
1339
- else {
1340
- return new LiveRegister(obj);
1341
- }
1342
- }