@liveblocks/client 0.15.0-alpha.3 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +5 -10
  2. package/lib/esm/index.js +2991 -5
  3. package/lib/esm/index.mjs +2991 -0
  4. package/lib/esm/internal.js +149 -0
  5. package/lib/esm/internal.mjs +149 -0
  6. package/lib/{esm/types.d.ts → index.d.ts} +253 -61
  7. package/lib/index.js +4237 -0
  8. package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
  9. package/lib/internal.js +193 -0
  10. package/package.json +32 -10
  11. package/lib/cjs/AbstractCrdt.d.ts +0 -68
  12. package/lib/cjs/AbstractCrdt.js +0 -95
  13. package/lib/cjs/LiveList.d.ts +0 -144
  14. package/lib/cjs/LiveList.js +0 -530
  15. package/lib/cjs/LiveMap.d.ts +0 -91
  16. package/lib/cjs/LiveMap.js +0 -325
  17. package/lib/cjs/LiveObject.d.ts +0 -80
  18. package/lib/cjs/LiveObject.js +0 -485
  19. package/lib/cjs/LiveRegister.d.ts +0 -29
  20. package/lib/cjs/LiveRegister.js +0 -88
  21. package/lib/cjs/client.d.ts +0 -27
  22. package/lib/cjs/client.js +0 -123
  23. package/lib/cjs/immutable.d.ts +0 -9
  24. package/lib/cjs/immutable.js +0 -299
  25. package/lib/cjs/index.d.ts +0 -6
  26. package/lib/cjs/index.js +0 -18
  27. package/lib/cjs/live.d.ts +0 -181
  28. package/lib/cjs/live.js +0 -49
  29. package/lib/cjs/position.d.ts +0 -6
  30. package/lib/cjs/position.js +0 -113
  31. package/lib/cjs/room.d.ts +0 -159
  32. package/lib/cjs/room.js +0 -1129
  33. package/lib/cjs/types.d.ts +0 -502
  34. package/lib/cjs/types.js +0 -2
  35. package/lib/cjs/utils.d.ts +0 -15
  36. package/lib/cjs/utils.js +0 -225
  37. package/lib/esm/AbstractCrdt.d.ts +0 -68
  38. package/lib/esm/AbstractCrdt.js +0 -91
  39. package/lib/esm/LiveList.d.ts +0 -144
  40. package/lib/esm/LiveList.js +0 -526
  41. package/lib/esm/LiveMap.d.ts +0 -91
  42. package/lib/esm/LiveMap.js +0 -321
  43. package/lib/esm/LiveObject.d.ts +0 -80
  44. package/lib/esm/LiveObject.js +0 -481
  45. package/lib/esm/LiveRegister.d.ts +0 -29
  46. package/lib/esm/LiveRegister.js +0 -84
  47. package/lib/esm/client.d.ts +0 -27
  48. package/lib/esm/client.js +0 -119
  49. package/lib/esm/immutable.d.ts +0 -9
  50. package/lib/esm/immutable.js +0 -290
  51. package/lib/esm/index.d.ts +0 -6
  52. package/lib/esm/live.js +0 -46
  53. package/lib/esm/position.d.ts +0 -6
  54. package/lib/esm/position.js +0 -106
  55. package/lib/esm/room.d.ts +0 -159
  56. package/lib/esm/room.js +0 -1123
  57. package/lib/esm/types.js +0 -1
  58. package/lib/esm/utils.d.ts +0 -15
  59. package/lib/esm/utils.js +0 -213
@@ -1,144 +0,0 @@
1
- import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
- import { SerializedList, SerializedCrdtWithId, Op, SerializedCrdt } from "./live";
3
- /**
4
- * The LiveList class represents an ordered collection of items that is synchorinized across clients.
5
- */
6
- export declare class LiveList<T> extends AbstractCrdt {
7
- #private;
8
- constructor(items?: T[]);
9
- /**
10
- * INTERNAL
11
- */
12
- static _deserialize([id, item]: [id: string, item: SerializedList], parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveList<never>;
13
- /**
14
- * INTERNAL
15
- */
16
- _serialize(parentId?: string, parentKey?: string, doc?: Doc): Op[];
17
- _indexOfPosition(position: string): number;
18
- /**
19
- * INTERNAL
20
- */
21
- _attach(id: string, doc: Doc): void;
22
- /**
23
- * INTERNAL
24
- */
25
- _detach(): void;
26
- /**
27
- * INTERNAL
28
- */
29
- _attachChild(id: string, key: string, child: AbstractCrdt, opId: string, isLocal: boolean): ApplyResult;
30
- /**
31
- * INTERNAL
32
- */
33
- _detachChild(child: AbstractCrdt): ApplyResult;
34
- /**
35
- * INTERNAL
36
- */
37
- _setChildKey(key: string, child: AbstractCrdt, previousKey: string): ApplyResult;
38
- /**
39
- * INTERNAL
40
- */
41
- _apply(op: Op, isLocal: boolean): ApplyResult;
42
- /**
43
- * INTERNAL
44
- */
45
- _getType(): string;
46
- /**
47
- * INTERNAL
48
- */
49
- _toSerializedCrdt(): SerializedCrdt;
50
- /**
51
- * Returns the number of elements.
52
- */
53
- get length(): number;
54
- /**
55
- * Adds one element to the end of the LiveList.
56
- * @param element The element to add to the end of the LiveList.
57
- */
58
- push(element: T): void;
59
- /**
60
- * Inserts one element at a specified index.
61
- * @param element The element to insert.
62
- * @param index The index at which you want to insert the element.
63
- */
64
- insert(element: T, index: number): void;
65
- /**
66
- * Move one element from one index to another.
67
- * @param index The index of the element to move
68
- * @param targetIndex The index where the element should be after moving.
69
- */
70
- move(index: number, targetIndex: number): void;
71
- /**
72
- * Deletes an element at the specified index
73
- * @param index The index of the element to delete
74
- */
75
- delete(index: number): void;
76
- clear(): void;
77
- /**
78
- * Returns an Array of all the elements in the LiveList.
79
- */
80
- toArray(): T[];
81
- toCrdtArray(): AbstractCrdt[];
82
- /**
83
- * Tests whether all elements pass the test implemented by the provided function.
84
- * @param predicate Function to test for each element, taking two arguments (the element and its index).
85
- * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
86
- */
87
- every(predicate: (value: T, index: number) => unknown): boolean;
88
- /**
89
- * Creates an array with all elements that pass the test implemented by the provided function.
90
- * @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.
91
- * @returns An array with the elements that pass the test.
92
- */
93
- filter(predicate: (value: T, index: number) => unknown): T[];
94
- /**
95
- * Returns the first element that satisfies the provided testing function.
96
- * @param predicate Function to execute on each value.
97
- * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
98
- */
99
- find(predicate: (value: T, index: number) => unknown): T | undefined;
100
- /**
101
- * Returns the index of the first element in the LiveList that satisfies the provided testing function.
102
- * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
103
- * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
104
- */
105
- findIndex(predicate: (value: T, index: number) => unknown): number;
106
- /**
107
- * Executes a provided function once for each element.
108
- * @param callbackfn Function to execute on each element.
109
- */
110
- forEach(callbackfn: (value: T, index: number) => void): void;
111
- /**
112
- * Get the element at the specified index.
113
- * @param index The index on the element to get.
114
- * @returns The element at the specified index or undefined.
115
- */
116
- get(index: number): T | undefined;
117
- /**
118
- * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
119
- * @param searchElement Element to locate.
120
- * @param fromIndex The index to start the search at.
121
- * @returns The first index of the element in the LiveList; -1 if not found.
122
- */
123
- indexOf(searchElement: T, fromIndex?: number): number;
124
- /**
125
- * 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.
126
- * @param searchElement Element to locate.
127
- * @param fromIndex The index at which to start searching backwards.
128
- * @returns
129
- */
130
- lastIndexOf(searchElement: T, fromIndex?: number): number;
131
- /**
132
- * Creates an array populated with the results of calling a provided function on every element.
133
- * @param callback Function that is called for every element.
134
- * @returns An array with each element being the result of the callback function.
135
- */
136
- map<U>(callback: (value: T, index: number) => U): U[];
137
- /**
138
- * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
139
- * @param predicate Function to test for each element.
140
- * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
141
- */
142
- some(predicate: (value: T, index: number) => unknown): boolean;
143
- [Symbol.iterator](): IterableIterator<T>;
144
- }
@@ -1,530 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- 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");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8
- if (kind === "m") throw new TypeError("Private method is not writable");
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10
- 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");
11
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
- };
13
- var _LiveList_items, _LiveListIterator_innerIterator;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.LiveList = void 0;
16
- const AbstractCrdt_1 = require("./AbstractCrdt");
17
- const utils_1 = require("./utils");
18
- const live_1 = require("./live");
19
- const position_1 = require("./position");
20
- const LiveRegister_1 = require("./LiveRegister");
21
- /**
22
- * The LiveList class represents an ordered collection of items that is synchorinized across clients.
23
- */
24
- class LiveList extends AbstractCrdt_1.AbstractCrdt {
25
- constructor(items = []) {
26
- super();
27
- // TODO: Naive array at first, find a better data structure. Maybe an Order statistics tree?
28
- _LiveList_items.set(this, []);
29
- let position = undefined;
30
- for (let i = 0; i < items.length; i++) {
31
- const newPosition = (0, position_1.makePosition)(position);
32
- const item = (0, utils_1.selfOrRegister)(items[i]);
33
- __classPrivateFieldGet(this, _LiveList_items, "f").push([item, newPosition]);
34
- position = newPosition;
35
- }
36
- }
37
- /**
38
- * INTERNAL
39
- */
40
- static _deserialize([id, item], parentToChildren, doc) {
41
- const list = new LiveList([]);
42
- list._attach(id, doc);
43
- const children = parentToChildren.get(id);
44
- if (children == null) {
45
- return list;
46
- }
47
- for (const entry of children) {
48
- const child = (0, utils_1.deserialize)(entry, parentToChildren, doc);
49
- child._setParentLink(list, entry[1].parentKey);
50
- __classPrivateFieldGet(list, _LiveList_items, "f").push([child, entry[1].parentKey]);
51
- __classPrivateFieldGet(list, _LiveList_items, "f").sort((itemA, itemB) => (0, position_1.compare)(itemA[1], itemB[1]));
52
- }
53
- return list;
54
- }
55
- /**
56
- * INTERNAL
57
- */
58
- _serialize(parentId, parentKey, doc) {
59
- if (this._id == null) {
60
- throw new Error("Cannot serialize item is not attached");
61
- }
62
- if (parentId == null || parentKey == null) {
63
- throw new Error("Cannot serialize list if parentId or parentKey is undefined");
64
- }
65
- const ops = [];
66
- const op = {
67
- id: this._id,
68
- opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
69
- type: live_1.OpType.CreateList,
70
- parentId,
71
- parentKey,
72
- };
73
- ops.push(op);
74
- for (const [value, key] of __classPrivateFieldGet(this, _LiveList_items, "f")) {
75
- ops.push(...value._serialize(this._id, key, doc));
76
- }
77
- return ops;
78
- }
79
- _indexOfPosition(position) {
80
- return __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((item) => item[1] === position);
81
- }
82
- /**
83
- * INTERNAL
84
- */
85
- _attach(id, doc) {
86
- super._attach(id, doc);
87
- for (const [item, position] of __classPrivateFieldGet(this, _LiveList_items, "f")) {
88
- item._attach(doc.generateId(), doc);
89
- }
90
- }
91
- /**
92
- * INTERNAL
93
- */
94
- _detach() {
95
- super._detach();
96
- for (const [value] of __classPrivateFieldGet(this, _LiveList_items, "f")) {
97
- value._detach();
98
- }
99
- }
100
- /**
101
- * INTERNAL
102
- */
103
- _attachChild(id, key, child, opId, isLocal) {
104
- var _a;
105
- if (this._doc == null) {
106
- throw new Error("Can't attach child if doc is not present");
107
- }
108
- if (this._doc.getItem(id) !== undefined) {
109
- return { modified: false };
110
- }
111
- child._attach(id, this._doc);
112
- child._setParentLink(this, key);
113
- const index = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[1] === key);
114
- let newKey = key;
115
- // If there is a conflict
116
- if (index !== -1) {
117
- if (isLocal) {
118
- // If change is local => assign a temporary position to newly attached child
119
- let before = __classPrivateFieldGet(this, _LiveList_items, "f")[index] ? __classPrivateFieldGet(this, _LiveList_items, "f")[index][1] : undefined;
120
- let after = __classPrivateFieldGet(this, _LiveList_items, "f")[index + 1]
121
- ? __classPrivateFieldGet(this, _LiveList_items, "f")[index + 1][1]
122
- : undefined;
123
- newKey = (0, position_1.makePosition)(before, after);
124
- child._setParentLink(this, newKey);
125
- }
126
- else {
127
- // If change is remote => assign a temporary position to existing child until we get the fix from the backend
128
- __classPrivateFieldGet(this, _LiveList_items, "f")[index][1] = (0, position_1.makePosition)(key, (_a = __classPrivateFieldGet(this, _LiveList_items, "f")[index + 1]) === null || _a === void 0 ? void 0 : _a[1]);
129
- }
130
- }
131
- __classPrivateFieldGet(this, _LiveList_items, "f").push([child, newKey]);
132
- __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => (0, position_1.compare)(itemA[1], itemB[1]));
133
- const newIndex = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[1] === newKey);
134
- return {
135
- reverse: [{ type: live_1.OpType.DeleteCrdt, id }],
136
- modified: {
137
- node: this,
138
- type: "LiveList",
139
- updates: [
140
- {
141
- index: newIndex,
142
- type: "insert",
143
- item: child instanceof LiveRegister_1.LiveRegister ? child.data : child,
144
- },
145
- ],
146
- },
147
- };
148
- }
149
- /**
150
- * INTERNAL
151
- */
152
- _detachChild(child) {
153
- if (child) {
154
- const reverse = child._serialize(this._id, child._parentKey, this._doc);
155
- const indexToDelete = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((item) => item[0] === child);
156
- __classPrivateFieldGet(this, _LiveList_items, "f").splice(indexToDelete, 1);
157
- child._detach();
158
- const storageUpdate = {
159
- node: this,
160
- type: "LiveList",
161
- updates: [{ index: indexToDelete, type: "delete" }],
162
- };
163
- return { modified: storageUpdate, reverse };
164
- }
165
- return { modified: false };
166
- }
167
- /**
168
- * INTERNAL
169
- */
170
- _setChildKey(key, child, previousKey) {
171
- var _a;
172
- child._setParentLink(this, key);
173
- const previousIndex = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[0]._id === child._id);
174
- const index = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[1] === key);
175
- // Assign a temporary position until we get the fix from the backend
176
- if (index !== -1) {
177
- __classPrivateFieldGet(this, _LiveList_items, "f")[index][1] = (0, position_1.makePosition)(key, (_a = __classPrivateFieldGet(this, _LiveList_items, "f")[index + 1]) === null || _a === void 0 ? void 0 : _a[1]);
178
- }
179
- const item = __classPrivateFieldGet(this, _LiveList_items, "f").find((item) => item[0] === child);
180
- if (item) {
181
- item[1] = key;
182
- }
183
- __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => (0, position_1.compare)(itemA[1], itemB[1]));
184
- const newIndex = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[0]._id === child._id);
185
- const updatesDelta = newIndex === previousIndex
186
- ? []
187
- : [
188
- {
189
- index: newIndex,
190
- item: child instanceof LiveRegister_1.LiveRegister ? child.data : child,
191
- previousIndex: previousIndex,
192
- type: "move",
193
- },
194
- ];
195
- return {
196
- modified: {
197
- node: this,
198
- type: "LiveList",
199
- updates: updatesDelta,
200
- },
201
- reverse: [
202
- {
203
- type: live_1.OpType.SetParentKey,
204
- id: item === null || item === void 0 ? void 0 : item[0]._id,
205
- parentKey: previousKey,
206
- },
207
- ],
208
- };
209
- }
210
- /**
211
- * INTERNAL
212
- */
213
- _apply(op, isLocal) {
214
- return super._apply(op, isLocal);
215
- }
216
- /**
217
- * INTERNAL
218
- */
219
- _getType() {
220
- return "LiveList";
221
- }
222
- /**
223
- * INTERNAL
224
- */
225
- _toSerializedCrdt() {
226
- var _a;
227
- return {
228
- type: live_1.CrdtType.List,
229
- parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
230
- parentKey: this._parentKey,
231
- };
232
- }
233
- /**
234
- * Returns the number of elements.
235
- */
236
- get length() {
237
- return __classPrivateFieldGet(this, _LiveList_items, "f").length;
238
- }
239
- /**
240
- * Adds one element to the end of the LiveList.
241
- * @param element The element to add to the end of the LiveList.
242
- */
243
- push(element) {
244
- return this.insert(element, this.length);
245
- }
246
- /**
247
- * Inserts one element at a specified index.
248
- * @param element The element to insert.
249
- * @param index The index at which you want to insert the element.
250
- */
251
- insert(element, index) {
252
- if (index < 0 || index > __classPrivateFieldGet(this, _LiveList_items, "f").length) {
253
- throw new Error(`Cannot insert list item at index "${index}". index should be between 0 and ${__classPrivateFieldGet(this, _LiveList_items, "f").length}`);
254
- }
255
- let before = __classPrivateFieldGet(this, _LiveList_items, "f")[index - 1] ? __classPrivateFieldGet(this, _LiveList_items, "f")[index - 1][1] : undefined;
256
- let after = __classPrivateFieldGet(this, _LiveList_items, "f")[index] ? __classPrivateFieldGet(this, _LiveList_items, "f")[index][1] : undefined;
257
- const position = (0, position_1.makePosition)(before, after);
258
- const value = (0, utils_1.selfOrRegister)(element);
259
- value._setParentLink(this, position);
260
- __classPrivateFieldGet(this, _LiveList_items, "f").push([value, position]);
261
- __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => (0, position_1.compare)(itemA[1], itemB[1]));
262
- const newIndex = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[1] === position);
263
- if (this._doc && this._id) {
264
- const id = this._doc.generateId();
265
- value._attach(id, this._doc);
266
- const storageUpdates = new Map();
267
- storageUpdates.set(this._id, {
268
- node: this,
269
- type: "LiveList",
270
- updates: [
271
- {
272
- index: newIndex,
273
- item: value instanceof LiveRegister_1.LiveRegister ? value.data : value,
274
- type: "insert",
275
- },
276
- ],
277
- });
278
- this._doc.dispatch(value._serialize(this._id, position, this._doc), [{ type: live_1.OpType.DeleteCrdt, id }], storageUpdates);
279
- }
280
- }
281
- /**
282
- * Move one element from one index to another.
283
- * @param index The index of the element to move
284
- * @param targetIndex The index where the element should be after moving.
285
- */
286
- move(index, targetIndex) {
287
- if (targetIndex < 0) {
288
- throw new Error("targetIndex cannot be less than 0");
289
- }
290
- if (targetIndex >= __classPrivateFieldGet(this, _LiveList_items, "f").length) {
291
- throw new Error("targetIndex cannot be greater or equal than the list length");
292
- }
293
- if (index < 0) {
294
- throw new Error("index cannot be less than 0");
295
- }
296
- if (index >= __classPrivateFieldGet(this, _LiveList_items, "f").length) {
297
- throw new Error("index cannot be greater or equal than the list length");
298
- }
299
- let beforePosition = null;
300
- let afterPosition = null;
301
- if (index < targetIndex) {
302
- afterPosition =
303
- targetIndex === __classPrivateFieldGet(this, _LiveList_items, "f").length - 1
304
- ? undefined
305
- : __classPrivateFieldGet(this, _LiveList_items, "f")[targetIndex + 1][1];
306
- beforePosition = __classPrivateFieldGet(this, _LiveList_items, "f")[targetIndex][1];
307
- }
308
- else {
309
- afterPosition = __classPrivateFieldGet(this, _LiveList_items, "f")[targetIndex][1];
310
- beforePosition =
311
- targetIndex === 0 ? undefined : __classPrivateFieldGet(this, _LiveList_items, "f")[targetIndex - 1][1];
312
- }
313
- const position = (0, position_1.makePosition)(beforePosition, afterPosition);
314
- const item = __classPrivateFieldGet(this, _LiveList_items, "f")[index];
315
- const previousPosition = item[1];
316
- item[1] = position;
317
- item[0]._setParentLink(this, position);
318
- __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => (0, position_1.compare)(itemA[1], itemB[1]));
319
- const newIndex = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[1] === position);
320
- if (this._doc && this._id) {
321
- const storageUpdates = new Map();
322
- storageUpdates.set(this._id, {
323
- node: this,
324
- type: "LiveList",
325
- updates: [
326
- {
327
- index: newIndex,
328
- previousIndex: index,
329
- item: item[0],
330
- type: "move",
331
- },
332
- ],
333
- });
334
- this._doc.dispatch([
335
- {
336
- type: live_1.OpType.SetParentKey,
337
- id: item[0]._id,
338
- opId: this._doc.generateOpId(),
339
- parentKey: position,
340
- },
341
- ], [
342
- {
343
- type: live_1.OpType.SetParentKey,
344
- id: item[0]._id,
345
- parentKey: previousPosition,
346
- },
347
- ], storageUpdates);
348
- }
349
- }
350
- /**
351
- * Deletes an element at the specified index
352
- * @param index The index of the element to delete
353
- */
354
- delete(index) {
355
- if (index < 0 || index >= __classPrivateFieldGet(this, _LiveList_items, "f").length) {
356
- throw new Error(`Cannot delete list item at index "${index}". index should be between 0 and ${__classPrivateFieldGet(this, _LiveList_items, "f").length - 1}`);
357
- }
358
- const item = __classPrivateFieldGet(this, _LiveList_items, "f")[index];
359
- item[0]._detach();
360
- __classPrivateFieldGet(this, _LiveList_items, "f").splice(index, 1);
361
- if (this._doc) {
362
- const childRecordId = item[0]._id;
363
- if (childRecordId) {
364
- const storageUpdates = new Map();
365
- storageUpdates.set(this._id, {
366
- node: this,
367
- type: "LiveList",
368
- updates: [{ index: index, type: "delete" }],
369
- });
370
- this._doc.dispatch([
371
- {
372
- id: childRecordId,
373
- opId: this._doc.generateOpId(),
374
- type: live_1.OpType.DeleteCrdt,
375
- },
376
- ], item[0]._serialize(this._id, item[1]), storageUpdates);
377
- }
378
- }
379
- }
380
- clear() {
381
- if (this._doc) {
382
- let ops = [];
383
- let reverseOps = [];
384
- let updateDelta = [];
385
- let i = 0;
386
- for (const item of __classPrivateFieldGet(this, _LiveList_items, "f")) {
387
- item[0]._detach();
388
- const childId = item[0]._id;
389
- if (childId) {
390
- ops.push({ id: childId, type: live_1.OpType.DeleteCrdt });
391
- reverseOps.push(...item[0]._serialize(this._id, item[1]));
392
- updateDelta.push({ index: i, type: "delete" });
393
- }
394
- i++;
395
- }
396
- __classPrivateFieldSet(this, _LiveList_items, [], "f");
397
- const storageUpdates = new Map();
398
- storageUpdates.set(this._id, {
399
- node: this,
400
- type: "LiveList",
401
- updates: updateDelta,
402
- });
403
- this._doc.dispatch(ops, reverseOps, storageUpdates);
404
- }
405
- else {
406
- for (const item of __classPrivateFieldGet(this, _LiveList_items, "f")) {
407
- item[0]._detach();
408
- }
409
- __classPrivateFieldSet(this, _LiveList_items, [], "f");
410
- }
411
- }
412
- /**
413
- * Returns an Array of all the elements in the LiveList.
414
- */
415
- toArray() {
416
- return __classPrivateFieldGet(this, _LiveList_items, "f").map((entry) => (0, utils_1.selfOrRegisterValue)(entry[0]));
417
- }
418
- toCrdtArray() {
419
- return __classPrivateFieldGet(this, _LiveList_items, "f").map((entry) => entry[0]);
420
- }
421
- /**
422
- * Tests whether all elements pass the test implemented by the provided function.
423
- * @param predicate Function to test for each element, taking two arguments (the element and its index).
424
- * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
425
- */
426
- every(predicate) {
427
- return this.toArray().every(predicate);
428
- }
429
- /**
430
- * Creates an array with all elements that pass the test implemented by the provided function.
431
- * @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.
432
- * @returns An array with the elements that pass the test.
433
- */
434
- filter(predicate) {
435
- return this.toArray().filter(predicate);
436
- }
437
- /**
438
- * Returns the first element that satisfies the provided testing function.
439
- * @param predicate Function to execute on each value.
440
- * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
441
- */
442
- find(predicate) {
443
- return this.toArray().find(predicate);
444
- }
445
- /**
446
- * Returns the index of the first element in the LiveList that satisfies the provided testing function.
447
- * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
448
- * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
449
- */
450
- findIndex(predicate) {
451
- return this.toArray().findIndex(predicate);
452
- }
453
- /**
454
- * Executes a provided function once for each element.
455
- * @param callbackfn Function to execute on each element.
456
- */
457
- forEach(callbackfn) {
458
- return this.toArray().forEach(callbackfn);
459
- }
460
- /**
461
- * Get the element at the specified index.
462
- * @param index The index on the element to get.
463
- * @returns The element at the specified index or undefined.
464
- */
465
- get(index) {
466
- if (index < 0 || index >= __classPrivateFieldGet(this, _LiveList_items, "f").length) {
467
- return undefined;
468
- }
469
- return (0, utils_1.selfOrRegisterValue)(__classPrivateFieldGet(this, _LiveList_items, "f")[index][0]);
470
- }
471
- /**
472
- * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
473
- * @param searchElement Element to locate.
474
- * @param fromIndex The index to start the search at.
475
- * @returns The first index of the element in the LiveList; -1 if not found.
476
- */
477
- indexOf(searchElement, fromIndex) {
478
- return this.toArray().indexOf(searchElement, fromIndex);
479
- }
480
- /**
481
- * 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.
482
- * @param searchElement Element to locate.
483
- * @param fromIndex The index at which to start searching backwards.
484
- * @returns
485
- */
486
- lastIndexOf(searchElement, fromIndex) {
487
- return this.toArray().lastIndexOf(searchElement, fromIndex);
488
- }
489
- /**
490
- * Creates an array populated with the results of calling a provided function on every element.
491
- * @param callback Function that is called for every element.
492
- * @returns An array with each element being the result of the callback function.
493
- */
494
- map(callback) {
495
- return __classPrivateFieldGet(this, _LiveList_items, "f").map((entry, i) => callback((0, utils_1.selfOrRegisterValue)(entry[0]), i));
496
- }
497
- /**
498
- * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
499
- * @param predicate Function to test for each element.
500
- * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
501
- */
502
- some(predicate) {
503
- return this.toArray().some(predicate);
504
- }
505
- [(_LiveList_items = new WeakMap(), Symbol.iterator)]() {
506
- return new LiveListIterator(__classPrivateFieldGet(this, _LiveList_items, "f"));
507
- }
508
- }
509
- exports.LiveList = LiveList;
510
- class LiveListIterator {
511
- constructor(items) {
512
- _LiveListIterator_innerIterator.set(this, void 0);
513
- __classPrivateFieldSet(this, _LiveListIterator_innerIterator, items[Symbol.iterator](), "f");
514
- }
515
- [(_LiveListIterator_innerIterator = new WeakMap(), Symbol.iterator)]() {
516
- return this;
517
- }
518
- next() {
519
- const result = __classPrivateFieldGet(this, _LiveListIterator_innerIterator, "f").next();
520
- if (result.done) {
521
- return {
522
- done: true,
523
- value: undefined,
524
- };
525
- }
526
- return {
527
- value: (0, utils_1.selfOrRegisterValue)(result.value[0]),
528
- };
529
- }
530
- }