@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
@@ -0,0 +1,268 @@
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 _LiveMap_map;
13
+ import { AbstractCrdt } from "./AbstractCrdt";
14
+ import { deserialize, isCrdt, selfOrRegister, selfOrRegisterValue, } from "./utils";
15
+ import { OpType, CrdtType, } from "./live";
16
+ /**
17
+ * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
18
+ * Keys should be a string, and values should be serializable to JSON.
19
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
20
+ */
21
+ export class LiveMap extends AbstractCrdt {
22
+ constructor(entries) {
23
+ super();
24
+ _LiveMap_map.set(this, void 0);
25
+ if (entries) {
26
+ const mappedEntries = [];
27
+ for (const entry of entries) {
28
+ const value = selfOrRegister(entry[1]);
29
+ value._setParentLink(this, entry[0]);
30
+ mappedEntries.push([entry[0], value]);
31
+ }
32
+ __classPrivateFieldSet(this, _LiveMap_map, new Map(mappedEntries), "f");
33
+ }
34
+ else {
35
+ __classPrivateFieldSet(this, _LiveMap_map, new Map(), "f");
36
+ }
37
+ }
38
+ /**
39
+ * INTERNAL
40
+ */
41
+ _serialize(parentId, parentKey) {
42
+ if (this._id == null) {
43
+ throw new Error("Cannot serialize item is not attached");
44
+ }
45
+ if (parentId == null || parentKey == null) {
46
+ throw new Error("Cannot serialize map if parentId or parentKey is undefined");
47
+ }
48
+ const ops = [];
49
+ const op = {
50
+ id: this._id,
51
+ type: OpType.CreateMap,
52
+ parentId,
53
+ parentKey,
54
+ };
55
+ ops.push(op);
56
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
57
+ ops.push(...value._serialize(this._id, key));
58
+ }
59
+ return ops;
60
+ }
61
+ /**
62
+ * INTERNAL
63
+ */
64
+ static _deserialize([id, item], parentToChildren, doc) {
65
+ if (item.type !== CrdtType.Map) {
66
+ throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
67
+ }
68
+ const map = new LiveMap();
69
+ map._attach(id, doc);
70
+ const children = parentToChildren.get(id);
71
+ if (children == null) {
72
+ return map;
73
+ }
74
+ for (const entry of children) {
75
+ const crdt = entry[1];
76
+ if (crdt.parentKey == null) {
77
+ throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
78
+ }
79
+ const child = deserialize(entry, parentToChildren, doc);
80
+ child._setParentLink(map, crdt.parentKey);
81
+ __classPrivateFieldGet(map, _LiveMap_map, "f").set(crdt.parentKey, child);
82
+ }
83
+ return map;
84
+ }
85
+ /**
86
+ * INTERNAL
87
+ */
88
+ _attach(id, doc) {
89
+ super._attach(id, doc);
90
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
91
+ if (isCrdt(value)) {
92
+ value._attach(doc.generateId(), doc);
93
+ }
94
+ }
95
+ }
96
+ /**
97
+ * INTERNAL
98
+ */
99
+ _attachChild(id, key, child) {
100
+ if (this._doc == null) {
101
+ throw new Error("Can't attach child if doc is not present");
102
+ }
103
+ const previousValue = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
104
+ let reverse;
105
+ if (previousValue) {
106
+ reverse = previousValue._serialize(this._id, key);
107
+ previousValue._detach();
108
+ }
109
+ else {
110
+ reverse = [{ type: OpType.DeleteCrdt, id }];
111
+ }
112
+ child._setParentLink(this, key);
113
+ child._attach(id, this._doc);
114
+ __classPrivateFieldGet(this, _LiveMap_map, "f").set(key, child);
115
+ return { modified: this, reverse };
116
+ }
117
+ /**
118
+ * INTERNAL
119
+ */
120
+ _detach() {
121
+ super._detach();
122
+ for (const item of __classPrivateFieldGet(this, _LiveMap_map, "f").values()) {
123
+ item._detach();
124
+ }
125
+ }
126
+ /**
127
+ * INTERNAL
128
+ */
129
+ _detachChild(child) {
130
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
131
+ if (value === child) {
132
+ __classPrivateFieldGet(this, _LiveMap_map, "f").delete(key);
133
+ }
134
+ }
135
+ child._detach();
136
+ }
137
+ /**
138
+ * Returns a specified element from the LiveMap.
139
+ * @param key The key of the element to return.
140
+ * @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
141
+ */
142
+ get(key) {
143
+ const value = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
144
+ if (value == undefined) {
145
+ return undefined;
146
+ }
147
+ return selfOrRegisterValue(value);
148
+ }
149
+ /**
150
+ * Adds or updates an element with a specified key and a value.
151
+ * @param key The key of the element to add. Should be a string.
152
+ * @param value The value of the element to add. Should be serializable to JSON.
153
+ */
154
+ set(key, value) {
155
+ const oldValue = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
156
+ if (oldValue) {
157
+ oldValue._detach();
158
+ }
159
+ const item = selfOrRegister(value);
160
+ item._setParentLink(this, key);
161
+ __classPrivateFieldGet(this, _LiveMap_map, "f").set(key, item);
162
+ if (this._doc && this._id) {
163
+ const id = this._doc.generateId();
164
+ item._attach(id, this._doc);
165
+ this._doc.dispatch(item._serialize(this._id, key), oldValue
166
+ ? oldValue._serialize(this._id, key)
167
+ : [{ type: OpType.DeleteCrdt, id }], [this]);
168
+ }
169
+ }
170
+ /**
171
+ * Returns the number of elements in the LiveMap.
172
+ */
173
+ get size() {
174
+ return __classPrivateFieldGet(this, _LiveMap_map, "f").size;
175
+ }
176
+ /**
177
+ * Returns a boolean indicating whether an element with the specified key exists or not.
178
+ * @param key The key of the element to test for presence.
179
+ */
180
+ has(key) {
181
+ return __classPrivateFieldGet(this, _LiveMap_map, "f").has(key);
182
+ }
183
+ /**
184
+ * Removes the specified element by key.
185
+ * @param key The key of the element to remove.
186
+ * @returns true if an element existed and has been removed, or false if the element does not exist.
187
+ */
188
+ delete(key) {
189
+ const item = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
190
+ if (item == null) {
191
+ return false;
192
+ }
193
+ item._detach();
194
+ if (this._doc && item._id) {
195
+ this._doc.dispatch([{ type: OpType.DeleteCrdt, id: item._id }], item._serialize(this._id, key), [this]);
196
+ }
197
+ __classPrivateFieldGet(this, _LiveMap_map, "f").delete(key);
198
+ return true;
199
+ }
200
+ /**
201
+ * Returns a new Iterator object that contains the [key, value] pairs for each element.
202
+ */
203
+ entries() {
204
+ const innerIterator = __classPrivateFieldGet(this, _LiveMap_map, "f").entries();
205
+ return {
206
+ [Symbol.iterator]: function () {
207
+ return this;
208
+ },
209
+ next() {
210
+ const iteratorValue = innerIterator.next();
211
+ if (iteratorValue.done) {
212
+ return {
213
+ done: true,
214
+ value: undefined,
215
+ };
216
+ }
217
+ const entry = iteratorValue.value;
218
+ return {
219
+ value: [entry[0], selfOrRegisterValue(iteratorValue.value[1])],
220
+ };
221
+ },
222
+ };
223
+ }
224
+ /**
225
+ * Same function object as the initial value of the entries method.
226
+ */
227
+ [(_LiveMap_map = new WeakMap(), Symbol.iterator)]() {
228
+ return this.entries();
229
+ }
230
+ /**
231
+ * Returns a new Iterator object that contains the keys for each element.
232
+ */
233
+ keys() {
234
+ return __classPrivateFieldGet(this, _LiveMap_map, "f").keys();
235
+ }
236
+ /**
237
+ * Returns a new Iterator object that contains the values for each element.
238
+ */
239
+ values() {
240
+ const innerIterator = __classPrivateFieldGet(this, _LiveMap_map, "f").values();
241
+ return {
242
+ [Symbol.iterator]: function () {
243
+ return this;
244
+ },
245
+ next() {
246
+ const iteratorValue = innerIterator.next();
247
+ if (iteratorValue.done) {
248
+ return {
249
+ done: true,
250
+ value: undefined,
251
+ };
252
+ }
253
+ return {
254
+ value: selfOrRegisterValue(iteratorValue.value),
255
+ };
256
+ },
257
+ };
258
+ }
259
+ /**
260
+ * Executes a provided function once per each key/value pair in the Map object, in insertion order.
261
+ * @param callback Function to execute for each entry in the map.
262
+ */
263
+ forEach(callback) {
264
+ for (const entry of this) {
265
+ callback(entry[1], entry[0], this);
266
+ }
267
+ }
268
+ }
@@ -0,0 +1,66 @@
1
+ import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
+ import { Op, SerializedCrdtWithId } from "./live";
3
+ /**
4
+ * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
5
+ * Keys should be a string, and values should be serializable to JSON.
6
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
7
+ */
8
+ export declare class LiveObject<T extends Record<string, any> = Record<string, any>> extends AbstractCrdt {
9
+ #private;
10
+ constructor(object?: T);
11
+ /**
12
+ * INTERNAL
13
+ */
14
+ _serialize(parentId?: string, parentKey?: string): Op[];
15
+ /**
16
+ * INTERNAL
17
+ */
18
+ static _deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveObject<{
19
+ [key: string]: any;
20
+ }>;
21
+ /**
22
+ * INTERNAL
23
+ */
24
+ _attach(id: string, doc: Doc): void;
25
+ /**
26
+ * INTERNAL
27
+ */
28
+ _attachChild(id: string, key: keyof T, child: AbstractCrdt): ApplyResult;
29
+ /**
30
+ * INTERNAL
31
+ */
32
+ _detachChild(child: AbstractCrdt): void;
33
+ /**
34
+ * INTERNAL
35
+ */
36
+ _detach(): void;
37
+ /**
38
+ * INTERNAL
39
+ */
40
+ _apply(op: Op): ApplyResult;
41
+ /**
42
+ * Transform the LiveObject into a javascript object
43
+ */
44
+ toObject(): T;
45
+ /**
46
+ * Adds or updates a property with a specified key and a value.
47
+ * @param key The key of the property to add
48
+ * @param value The value of the property to add
49
+ */
50
+ set<TKey extends keyof T>(key: TKey, value: T[TKey]): void;
51
+ /**
52
+ * Returns a specified property from the LiveObject.
53
+ * @param key The key of the property to get
54
+ */
55
+ get<TKey extends keyof T>(key: TKey): T[TKey];
56
+ /**
57
+ * Deletes a key from the LiveObject
58
+ * @param key The key of the property to delete
59
+ */
60
+ delete(key: keyof T): void;
61
+ /**
62
+ * Adds or updates multiple properties at once with an object.
63
+ * @param overrides The object used to overrides properties
64
+ */
65
+ update(overrides: Partial<T>): void;
66
+ }
@@ -0,0 +1,364 @@
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 _LiveObject_instances, _LiveObject_map, _LiveObject_propToLastUpdate, _LiveObject_applyUpdate, _LiveObject_applyDeleteObjectKey;
13
+ import { AbstractCrdt } from "./AbstractCrdt";
14
+ import { deserialize, isCrdt } from "./utils";
15
+ import { CrdtType, OpType, } from "./live";
16
+ /**
17
+ * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
18
+ * Keys should be a string, and values should be serializable to JSON.
19
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
20
+ */
21
+ export class LiveObject extends AbstractCrdt {
22
+ constructor(object = {}) {
23
+ super();
24
+ _LiveObject_instances.add(this);
25
+ _LiveObject_map.set(this, void 0);
26
+ _LiveObject_propToLastUpdate.set(this, new Map());
27
+ for (const key in object) {
28
+ const value = object[key];
29
+ if (value instanceof AbstractCrdt) {
30
+ value._setParentLink(this, key);
31
+ }
32
+ }
33
+ __classPrivateFieldSet(this, _LiveObject_map, new Map(Object.entries(object)), "f");
34
+ }
35
+ /**
36
+ * INTERNAL
37
+ */
38
+ _serialize(parentId, parentKey) {
39
+ if (this._id == null) {
40
+ throw new Error("Cannot serialize item is not attached");
41
+ }
42
+ const ops = [];
43
+ const op = {
44
+ id: this._id,
45
+ type: OpType.CreateObject,
46
+ parentId,
47
+ parentKey,
48
+ data: {},
49
+ };
50
+ ops.push(op);
51
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
52
+ if (value instanceof AbstractCrdt) {
53
+ ops.push(...value._serialize(this._id, key));
54
+ }
55
+ else {
56
+ op.data[key] = value;
57
+ }
58
+ }
59
+ return ops;
60
+ }
61
+ /**
62
+ * INTERNAL
63
+ */
64
+ static _deserialize([id, item], parentToChildren, doc) {
65
+ if (item.type !== CrdtType.Object) {
66
+ throw new Error(`Tried to deserialize a record but item type is "${item.type}"`);
67
+ }
68
+ const object = new LiveObject(item.data);
69
+ object._attach(id, doc);
70
+ const children = parentToChildren.get(id);
71
+ if (children == null) {
72
+ return object;
73
+ }
74
+ for (const entry of children) {
75
+ const crdt = entry[1];
76
+ if (crdt.parentKey == null) {
77
+ throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
78
+ }
79
+ const child = deserialize(entry, parentToChildren, doc);
80
+ child._setParentLink(object, crdt.parentKey);
81
+ __classPrivateFieldGet(object, _LiveObject_map, "f").set(crdt.parentKey, child);
82
+ }
83
+ return object;
84
+ }
85
+ /**
86
+ * INTERNAL
87
+ */
88
+ _attach(id, doc) {
89
+ super._attach(id, doc);
90
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
91
+ if (value instanceof AbstractCrdt) {
92
+ value._attach(doc.generateId(), doc);
93
+ }
94
+ }
95
+ }
96
+ /**
97
+ * INTERNAL
98
+ */
99
+ _attachChild(id, key, child) {
100
+ if (this._doc == null) {
101
+ throw new Error("Can't attach child if doc is not present");
102
+ }
103
+ const previousValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
104
+ let reverse;
105
+ if (isCrdt(previousValue)) {
106
+ reverse = previousValue._serialize(this._id, key);
107
+ previousValue._detach();
108
+ }
109
+ else if (previousValue === undefined) {
110
+ reverse = [
111
+ { type: OpType.DeleteObjectKey, id: this._id, key: key },
112
+ ];
113
+ }
114
+ else {
115
+ reverse = [
116
+ {
117
+ type: OpType.UpdateObject,
118
+ id: this._id,
119
+ data: { [key]: previousValue },
120
+ },
121
+ ];
122
+ }
123
+ __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, child);
124
+ child._setParentLink(this, key);
125
+ child._attach(id, this._doc);
126
+ return { reverse, modified: this };
127
+ }
128
+ /**
129
+ * INTERNAL
130
+ */
131
+ _detachChild(child) {
132
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
133
+ if (value === child) {
134
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
135
+ }
136
+ }
137
+ if (child) {
138
+ child._detach();
139
+ }
140
+ }
141
+ /**
142
+ * INTERNAL
143
+ */
144
+ _detach() {
145
+ super._detach();
146
+ for (const value of __classPrivateFieldGet(this, _LiveObject_map, "f").values()) {
147
+ if (isCrdt(value)) {
148
+ value._detach();
149
+ }
150
+ }
151
+ }
152
+ /**
153
+ * INTERNAL
154
+ */
155
+ _apply(op) {
156
+ if (op.type === OpType.UpdateObject) {
157
+ return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyUpdate).call(this, op);
158
+ }
159
+ else if (op.type === OpType.DeleteObjectKey) {
160
+ return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyDeleteObjectKey).call(this, op);
161
+ }
162
+ return super._apply(op);
163
+ }
164
+ /**
165
+ * Transform the LiveObject into a javascript object
166
+ */
167
+ toObject() {
168
+ return Object.fromEntries(__classPrivateFieldGet(this, _LiveObject_map, "f"));
169
+ }
170
+ /**
171
+ * Adds or updates a property with a specified key and a value.
172
+ * @param key The key of the property to add
173
+ * @param value The value of the property to add
174
+ */
175
+ set(key, value) {
176
+ // TODO: Find out why typescript complains
177
+ this.update({ [key]: value });
178
+ }
179
+ /**
180
+ * Returns a specified property from the LiveObject.
181
+ * @param key The key of the property to get
182
+ */
183
+ get(key) {
184
+ return __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
185
+ }
186
+ /**
187
+ * Deletes a key from the LiveObject
188
+ * @param key The key of the property to delete
189
+ */
190
+ delete(key) {
191
+ const keyAsString = key;
192
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(keyAsString);
193
+ if (oldValue === undefined) {
194
+ return;
195
+ }
196
+ if (this._doc == null || this._id == null) {
197
+ if (oldValue instanceof AbstractCrdt) {
198
+ oldValue._detach();
199
+ }
200
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
201
+ return;
202
+ }
203
+ let reverse;
204
+ if (oldValue instanceof AbstractCrdt) {
205
+ oldValue._detach();
206
+ reverse = oldValue._serialize(this._id, keyAsString);
207
+ }
208
+ else {
209
+ reverse = [
210
+ {
211
+ type: OpType.UpdateObject,
212
+ data: { [keyAsString]: oldValue },
213
+ id: this._id,
214
+ },
215
+ ];
216
+ }
217
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
218
+ this._doc.dispatch([{ type: OpType.DeleteObjectKey, key: keyAsString, id: this._id }], reverse, [this]);
219
+ }
220
+ /**
221
+ * Adds or updates multiple properties at once with an object.
222
+ * @param overrides The object used to overrides properties
223
+ */
224
+ update(overrides) {
225
+ if (this._doc == null || this._id == null) {
226
+ for (const key in overrides) {
227
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
228
+ if (oldValue instanceof AbstractCrdt) {
229
+ oldValue._detach();
230
+ }
231
+ const newValue = overrides[key];
232
+ if (newValue instanceof AbstractCrdt) {
233
+ newValue._setParentLink(this, key);
234
+ }
235
+ __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
236
+ }
237
+ return;
238
+ }
239
+ const ops = [];
240
+ const reverseOps = [];
241
+ const opId = this._doc.generateOpId();
242
+ const updatedProps = {};
243
+ const reverseUpdateOp = {
244
+ id: this._id,
245
+ type: OpType.UpdateObject,
246
+ data: {},
247
+ };
248
+ for (const key in overrides) {
249
+ __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, opId);
250
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
251
+ if (oldValue instanceof AbstractCrdt) {
252
+ reverseOps.push(...oldValue._serialize(this._id, key));
253
+ oldValue._detach();
254
+ }
255
+ else if (oldValue === undefined) {
256
+ reverseOps.push({ type: OpType.DeleteObjectKey, id: this._id, key });
257
+ }
258
+ else {
259
+ reverseUpdateOp.data[key] = oldValue;
260
+ }
261
+ const newValue = overrides[key];
262
+ if (newValue instanceof AbstractCrdt) {
263
+ newValue._setParentLink(this, key);
264
+ newValue._attach(this._doc.generateId(), this._doc);
265
+ ops.push(...newValue._serialize(this._id, key));
266
+ }
267
+ else {
268
+ updatedProps[key] = newValue;
269
+ }
270
+ __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
271
+ }
272
+ if (Object.keys(reverseUpdateOp.data).length !== 0) {
273
+ reverseOps.unshift(reverseUpdateOp);
274
+ }
275
+ if (Object.keys(updatedProps).length !== 0) {
276
+ ops.unshift({
277
+ opId,
278
+ id: this._id,
279
+ type: OpType.UpdateObject,
280
+ data: updatedProps,
281
+ });
282
+ }
283
+ this._doc.dispatch(ops, reverseOps, [this]);
284
+ }
285
+ }
286
+ _LiveObject_map = new WeakMap(), _LiveObject_propToLastUpdate = new WeakMap(), _LiveObject_instances = new WeakSet(), _LiveObject_applyUpdate = function _LiveObject_applyUpdate(op) {
287
+ let isModified = false;
288
+ const reverse = [];
289
+ const reverseUpdate = {
290
+ type: OpType.UpdateObject,
291
+ id: this._id,
292
+ data: {},
293
+ };
294
+ reverse.push(reverseUpdate);
295
+ for (const key in op.data) {
296
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
297
+ if (oldValue instanceof AbstractCrdt) {
298
+ reverse.push(...oldValue._serialize(this._id, key));
299
+ oldValue._detach();
300
+ }
301
+ else if (oldValue !== undefined) {
302
+ reverseUpdate.data[key] = oldValue;
303
+ }
304
+ else if (oldValue === undefined) {
305
+ reverse.push({ type: OpType.DeleteObjectKey, id: this._id, key });
306
+ }
307
+ }
308
+ let isLocal = false;
309
+ if (op.opId == null) {
310
+ isLocal = true;
311
+ op.opId = this._doc.generateOpId();
312
+ }
313
+ for (const key in op.data) {
314
+ if (isLocal) {
315
+ __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, op.opId);
316
+ }
317
+ else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) == null) {
318
+ // Not modified localy so we apply update
319
+ isModified = true;
320
+ }
321
+ else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) === op.opId) {
322
+ // Acknowlegment from local operation
323
+ __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").delete(key);
324
+ continue;
325
+ }
326
+ else {
327
+ // Conflict, ignore remote operation
328
+ continue;
329
+ }
330
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
331
+ if (isCrdt(oldValue)) {
332
+ oldValue._detach();
333
+ }
334
+ isModified = true;
335
+ __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, op.data[key]);
336
+ }
337
+ if (Object.keys(reverseUpdate.data).length !== 0) {
338
+ reverse.unshift(reverseUpdate);
339
+ }
340
+ return isModified ? { modified: this, reverse } : { modified: false };
341
+ }, _LiveObject_applyDeleteObjectKey = function _LiveObject_applyDeleteObjectKey(op) {
342
+ const key = op.key;
343
+ // If property does not exist, exit without notifying
344
+ if (__classPrivateFieldGet(this, _LiveObject_map, "f").has(key) === false) {
345
+ return { modified: false };
346
+ }
347
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
348
+ let reverse = [];
349
+ if (isCrdt(oldValue)) {
350
+ reverse = oldValue._serialize(this._id, op.key);
351
+ oldValue._detach();
352
+ }
353
+ else if (oldValue !== undefined) {
354
+ reverse = [
355
+ {
356
+ type: OpType.UpdateObject,
357
+ id: this._id,
358
+ data: { [key]: oldValue },
359
+ },
360
+ ];
361
+ }
362
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
363
+ return { modified: this, reverse };
364
+ };