@liveblocks/client 0.13.2 → 0.14.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 (41) hide show
  1. package/lib/cjs/AbstractCrdt.d.ts +8 -4
  2. package/lib/cjs/AbstractCrdt.js +2 -2
  3. package/lib/cjs/LiveList.d.ts +9 -4
  4. package/lib/cjs/LiveList.js +58 -9
  5. package/lib/cjs/LiveMap.d.ts +7 -3
  6. package/lib/cjs/LiveMap.js +23 -5
  7. package/lib/cjs/LiveObject.d.ts +17 -7
  8. package/lib/cjs/LiveObject.js +45 -15
  9. package/lib/cjs/LiveRegister.d.ts +8 -4
  10. package/lib/cjs/LiveRegister.js +17 -4
  11. package/lib/cjs/client.js +50 -7
  12. package/lib/cjs/{immutable/index.d.ts → immutable.d.ts} +5 -3
  13. package/lib/cjs/{immutable/index.js → immutable.js} +98 -21
  14. package/lib/cjs/index.d.ts +1 -1
  15. package/lib/cjs/live.d.ts +7 -0
  16. package/lib/cjs/room.d.ts +17 -9
  17. package/lib/cjs/room.js +203 -81
  18. package/lib/cjs/types.d.ts +26 -1
  19. package/lib/cjs/utils.d.ts +2 -1
  20. package/lib/cjs/utils.js +76 -1
  21. package/lib/esm/AbstractCrdt.d.ts +8 -4
  22. package/lib/esm/AbstractCrdt.js +2 -2
  23. package/lib/esm/LiveList.d.ts +9 -4
  24. package/lib/esm/LiveList.js +59 -10
  25. package/lib/esm/LiveMap.d.ts +7 -3
  26. package/lib/esm/LiveMap.js +23 -5
  27. package/lib/esm/LiveObject.d.ts +17 -7
  28. package/lib/esm/LiveObject.js +45 -15
  29. package/lib/esm/LiveRegister.d.ts +8 -4
  30. package/lib/esm/LiveRegister.js +18 -5
  31. package/lib/esm/client.js +50 -7
  32. package/lib/esm/{immutable/index.d.ts → immutable.d.ts} +5 -3
  33. package/lib/esm/{immutable/index.js → immutable.js} +96 -21
  34. package/lib/esm/index.d.ts +1 -1
  35. package/lib/esm/live.d.ts +7 -0
  36. package/lib/esm/room.d.ts +17 -9
  37. package/lib/esm/room.js +203 -62
  38. package/lib/esm/types.d.ts +26 -1
  39. package/lib/esm/utils.d.ts +2 -1
  40. package/lib/esm/utils.js +75 -1
  41. package/package.json +6 -2
@@ -1,4 +1,4 @@
1
- import { Op } from "./live";
1
+ import { Op, SerializedCrdt } from "./live";
2
2
  export declare type ApplyResult = {
3
3
  reverse: Op[];
4
4
  modified: AbstractCrdt;
@@ -33,7 +33,7 @@ export declare abstract class AbstractCrdt {
33
33
  /**
34
34
  * INTERNAL
35
35
  */
36
- _apply(op: Op): ApplyResult;
36
+ _apply(op: Op, isLocal: boolean): ApplyResult;
37
37
  /**
38
38
  * INTERNAL
39
39
  */
@@ -45,7 +45,7 @@ export declare abstract class AbstractCrdt {
45
45
  /**
46
46
  * INTERNAL
47
47
  */
48
- abstract _attachChild(id: string, key: string, crdt: AbstractCrdt): ApplyResult;
48
+ abstract _attachChild(id: string, key: string, crdt: AbstractCrdt, isLocal: boolean): ApplyResult;
49
49
  /**
50
50
  * INTERNAL
51
51
  */
@@ -57,5 +57,9 @@ export declare abstract class AbstractCrdt {
57
57
  /**
58
58
  * INTERNAL
59
59
  */
60
- abstract _serialize(parentId: string, parentKey: string): Op[];
60
+ abstract _serialize(parentId: string, parentKey: string, doc?: Doc): Op[];
61
+ /**
62
+ * INTERNAL
63
+ */
64
+ abstract _toSerializedCrdt(): SerializedCrdt;
61
65
  }
@@ -45,12 +45,12 @@ export class AbstractCrdt {
45
45
  /**
46
46
  * INTERNAL
47
47
  */
48
- _apply(op) {
48
+ _apply(op, isLocal) {
49
49
  switch (op.type) {
50
50
  case OpType.DeleteCrdt: {
51
51
  if (this._parent != null && this._parentKey != null) {
52
52
  const parent = this._parent;
53
- const reverse = this._serialize(this._parent._id, this._parentKey);
53
+ const reverse = this._serialize(this._parent._id, this._parentKey, __classPrivateFieldGet(this, _AbstractCrdt_doc, "f"));
54
54
  this._parent._detachChild(this);
55
55
  return { modified: parent, reverse };
56
56
  }
@@ -1,5 +1,5 @@
1
1
  import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
- import { SerializedList, SerializedCrdtWithId, Op } from "./live";
2
+ import { SerializedList, SerializedCrdtWithId, Op, SerializedCrdt } from "./live";
3
3
  /**
4
4
  * The LiveList class represents an ordered collection of items that is synchorinized across clients.
5
5
  */
@@ -13,7 +13,7 @@ export declare class LiveList<T> extends AbstractCrdt {
13
13
  /**
14
14
  * INTERNAL
15
15
  */
16
- _serialize(parentId?: string, parentKey?: string): Op[];
16
+ _serialize(parentId?: string, parentKey?: string, doc?: Doc): Op[];
17
17
  /**
18
18
  * INTERNAL
19
19
  */
@@ -25,7 +25,7 @@ export declare class LiveList<T> extends AbstractCrdt {
25
25
  /**
26
26
  * INTERNAL
27
27
  */
28
- _attachChild(id: string, key: string, child: AbstractCrdt): ApplyResult;
28
+ _attachChild(id: string, key: string, child: AbstractCrdt, isLocal: boolean): ApplyResult;
29
29
  /**
30
30
  * INTERNAL
31
31
  */
@@ -37,7 +37,11 @@ export declare class LiveList<T> extends AbstractCrdt {
37
37
  /**
38
38
  * INTERNAL
39
39
  */
40
- _apply(op: Op): ApplyResult;
40
+ _apply(op: Op, isLocal: boolean): ApplyResult;
41
+ /**
42
+ * INTERNAL
43
+ */
44
+ _toSerializedCrdt(): SerializedCrdt;
41
45
  /**
42
46
  * Returns the number of elements.
43
47
  */
@@ -64,6 +68,7 @@ export declare class LiveList<T> extends AbstractCrdt {
64
68
  * @param index The index of the element to delete
65
69
  */
66
70
  delete(index: number): void;
71
+ clear(): void;
67
72
  /**
68
73
  * Returns an Array of all the elements in the LiveList.
69
74
  */
@@ -12,7 +12,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
12
12
  var _LiveList_items, _LiveListIterator_innerIterator;
13
13
  import { AbstractCrdt } from "./AbstractCrdt";
14
14
  import { deserialize, selfOrRegister, selfOrRegisterValue } from "./utils";
15
- import { OpType, } from "./live";
15
+ import { OpType, CrdtType, } from "./live";
16
16
  import { makePosition, compare } from "./position";
17
17
  /**
18
18
  * The LiveList class represents an ordered collection of items that is synchorinized across clients.
@@ -51,7 +51,7 @@ export class LiveList extends AbstractCrdt {
51
51
  /**
52
52
  * INTERNAL
53
53
  */
54
- _serialize(parentId, parentKey) {
54
+ _serialize(parentId, parentKey, doc) {
55
55
  if (this._id == null) {
56
56
  throw new Error("Cannot serialize item is not attached");
57
57
  }
@@ -61,13 +61,14 @@ export class LiveList extends AbstractCrdt {
61
61
  const ops = [];
62
62
  const op = {
63
63
  id: this._id,
64
+ opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
64
65
  type: OpType.CreateList,
65
66
  parentId,
66
67
  parentKey,
67
68
  };
68
69
  ops.push(op);
69
70
  for (const [value, key] of __classPrivateFieldGet(this, _LiveList_items, "f")) {
70
- ops.push(...value._serialize(this._id, key));
71
+ ops.push(...value._serialize(this._id, key, doc));
71
72
  }
72
73
  return ops;
73
74
  }
@@ -92,7 +93,7 @@ export class LiveList extends AbstractCrdt {
92
93
  /**
93
94
  * INTERNAL
94
95
  */
95
- _attachChild(id, key, child) {
96
+ _attachChild(id, key, child, isLocal) {
96
97
  var _a;
97
98
  if (this._doc == null) {
98
99
  throw new Error("Can't attach child if doc is not present");
@@ -100,11 +101,24 @@ export class LiveList extends AbstractCrdt {
100
101
  child._attach(id, this._doc);
101
102
  child._setParentLink(this, key);
102
103
  const index = __classPrivateFieldGet(this, _LiveList_items, "f").findIndex((entry) => entry[1] === key);
103
- // Assign a temporary position until we get the fix from the backend
104
+ let newKey = key;
105
+ // If there is a conflict
104
106
  if (index !== -1) {
105
- __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]);
107
+ if (isLocal) {
108
+ // If change is local => assign a temporary position to newly attached child
109
+ let before = __classPrivateFieldGet(this, _LiveList_items, "f")[index] ? __classPrivateFieldGet(this, _LiveList_items, "f")[index][1] : undefined;
110
+ let after = __classPrivateFieldGet(this, _LiveList_items, "f")[index + 1]
111
+ ? __classPrivateFieldGet(this, _LiveList_items, "f")[index + 1][1]
112
+ : undefined;
113
+ newKey = makePosition(before, after);
114
+ child._setParentLink(this, newKey);
115
+ }
116
+ else {
117
+ // If change is remote => assign a temporary position to existing child until we get the fix from the backend
118
+ __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]);
119
+ }
106
120
  }
107
- __classPrivateFieldGet(this, _LiveList_items, "f").push([child, key]);
121
+ __classPrivateFieldGet(this, _LiveList_items, "f").push([child, newKey]);
108
122
  __classPrivateFieldGet(this, _LiveList_items, "f").sort((itemA, itemB) => compare(itemA[1], itemB[1]));
109
123
  return { reverse: [{ type: OpType.DeleteCrdt, id }], modified: this };
110
124
  }
@@ -138,8 +152,19 @@ export class LiveList extends AbstractCrdt {
138
152
  /**
139
153
  * INTERNAL
140
154
  */
141
- _apply(op) {
142
- return super._apply(op);
155
+ _apply(op, isLocal) {
156
+ return super._apply(op, isLocal);
157
+ }
158
+ /**
159
+ * INTERNAL
160
+ */
161
+ _toSerializedCrdt() {
162
+ var _a;
163
+ return {
164
+ type: CrdtType.List,
165
+ parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
166
+ parentKey: this._parentKey,
167
+ };
143
168
  }
144
169
  /**
145
170
  * Returns the number of elements.
@@ -173,7 +198,7 @@ export class LiveList extends AbstractCrdt {
173
198
  if (this._doc && this._id) {
174
199
  const id = this._doc.generateId();
175
200
  value._attach(id, this._doc);
176
- this._doc.dispatch(value._serialize(this._id, position), [{ type: OpType.DeleteCrdt, id }], [this]);
201
+ this._doc.dispatch(value._serialize(this._id, position, this._doc), [{ type: OpType.DeleteCrdt, id }], [this]);
177
202
  }
178
203
  }
179
204
  /**
@@ -219,6 +244,7 @@ export class LiveList extends AbstractCrdt {
219
244
  {
220
245
  type: OpType.SetParentKey,
221
246
  id: item[0]._id,
247
+ opId: this._doc.generateOpId(),
222
248
  parentKey: position,
223
249
  },
224
250
  ], [
@@ -247,12 +273,35 @@ export class LiveList extends AbstractCrdt {
247
273
  this._doc.dispatch([
248
274
  {
249
275
  id: childRecordId,
276
+ opId: this._doc.generateOpId(),
250
277
  type: OpType.DeleteCrdt,
251
278
  },
252
279
  ], item[0]._serialize(this._id, item[1]), [this]);
253
280
  }
254
281
  }
255
282
  }
283
+ clear() {
284
+ if (this._doc) {
285
+ let ops = [];
286
+ let reverseOps = [];
287
+ for (const item of __classPrivateFieldGet(this, _LiveList_items, "f")) {
288
+ item[0]._detach();
289
+ const childId = item[0]._id;
290
+ if (childId) {
291
+ ops.push({ id: childId, type: OpType.DeleteCrdt });
292
+ reverseOps.push(...item[0]._serialize(this._id, item[1]));
293
+ }
294
+ }
295
+ __classPrivateFieldSet(this, _LiveList_items, [], "f");
296
+ this._doc.dispatch(ops, reverseOps, [this]);
297
+ }
298
+ else {
299
+ for (const item of __classPrivateFieldGet(this, _LiveList_items, "f")) {
300
+ item[0]._detach();
301
+ }
302
+ __classPrivateFieldSet(this, _LiveList_items, [], "f");
303
+ }
304
+ }
256
305
  /**
257
306
  * Returns an Array of all the elements in the LiveList.
258
307
  */
@@ -1,5 +1,5 @@
1
1
  import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
- import { Op, SerializedCrdtWithId } from "./live";
2
+ import { Op, SerializedCrdtWithId, SerializedCrdt } from "./live";
3
3
  /**
4
4
  * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
5
5
  * Keys should be a string, and values should be serializable to JSON.
@@ -11,7 +11,7 @@ export declare class LiveMap<TKey extends string, TValue> extends AbstractCrdt {
11
11
  /**
12
12
  * INTERNAL
13
13
  */
14
- _serialize(parentId?: string, parentKey?: string): Op[];
14
+ _serialize(parentId?: string, parentKey?: string, doc?: Doc): Op[];
15
15
  /**
16
16
  * INTERNAL
17
17
  */
@@ -23,7 +23,7 @@ export declare class LiveMap<TKey extends string, TValue> extends AbstractCrdt {
23
23
  /**
24
24
  * INTERNAL
25
25
  */
26
- _attachChild(id: string, key: TKey, child: AbstractCrdt): ApplyResult;
26
+ _attachChild(id: string, key: TKey, child: AbstractCrdt, isLocal: boolean): ApplyResult;
27
27
  /**
28
28
  * INTERNAL
29
29
  */
@@ -32,6 +32,10 @@ export declare class LiveMap<TKey extends string, TValue> extends AbstractCrdt {
32
32
  * INTERNAL
33
33
  */
34
34
  _detachChild(child: AbstractCrdt): void;
35
+ /**
36
+ * INTERNAL
37
+ */
38
+ _toSerializedCrdt(): SerializedCrdt;
35
39
  /**
36
40
  * Returns a specified element from the LiveMap.
37
41
  * @param key The key of the element to return.
@@ -38,7 +38,7 @@ export class LiveMap extends AbstractCrdt {
38
38
  /**
39
39
  * INTERNAL
40
40
  */
41
- _serialize(parentId, parentKey) {
41
+ _serialize(parentId, parentKey, doc) {
42
42
  if (this._id == null) {
43
43
  throw new Error("Cannot serialize item is not attached");
44
44
  }
@@ -48,13 +48,14 @@ export class LiveMap extends AbstractCrdt {
48
48
  const ops = [];
49
49
  const op = {
50
50
  id: this._id,
51
+ opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
51
52
  type: OpType.CreateMap,
52
53
  parentId,
53
54
  parentKey,
54
55
  };
55
56
  ops.push(op);
56
57
  for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
57
- ops.push(...value._serialize(this._id, key));
58
+ ops.push(...value._serialize(this._id, key, doc));
58
59
  }
59
60
  return ops;
60
61
  }
@@ -96,7 +97,7 @@ export class LiveMap extends AbstractCrdt {
96
97
  /**
97
98
  * INTERNAL
98
99
  */
99
- _attachChild(id, key, child) {
100
+ _attachChild(id, key, child, isLocal) {
100
101
  if (this._doc == null) {
101
102
  throw new Error("Can't attach child if doc is not present");
102
103
  }
@@ -134,6 +135,17 @@ export class LiveMap extends AbstractCrdt {
134
135
  }
135
136
  child._detach();
136
137
  }
138
+ /**
139
+ * INTERNAL
140
+ */
141
+ _toSerializedCrdt() {
142
+ var _a;
143
+ return {
144
+ type: CrdtType.Map,
145
+ parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
146
+ parentKey: this._parentKey,
147
+ };
148
+ }
137
149
  /**
138
150
  * Returns a specified element from the LiveMap.
139
151
  * @param key The key of the element to return.
@@ -162,7 +174,7 @@ export class LiveMap extends AbstractCrdt {
162
174
  if (this._doc && this._id) {
163
175
  const id = this._doc.generateId();
164
176
  item._attach(id, this._doc);
165
- this._doc.dispatch(item._serialize(this._id, key), oldValue
177
+ this._doc.dispatch(item._serialize(this._id, key, this._doc), oldValue
166
178
  ? oldValue._serialize(this._id, key)
167
179
  : [{ type: OpType.DeleteCrdt, id }], [this]);
168
180
  }
@@ -192,7 +204,13 @@ export class LiveMap extends AbstractCrdt {
192
204
  }
193
205
  item._detach();
194
206
  if (this._doc && item._id) {
195
- this._doc.dispatch([{ type: OpType.DeleteCrdt, id: item._id }], item._serialize(this._id, key), [this]);
207
+ this._doc.dispatch([
208
+ {
209
+ type: OpType.DeleteCrdt,
210
+ id: item._id,
211
+ opId: this._doc.generateOpId(),
212
+ },
213
+ ], item._serialize(this._id, key), [this]);
196
214
  }
197
215
  __classPrivateFieldGet(this, _LiveMap_map, "f").delete(key);
198
216
  return true;
@@ -1,5 +1,5 @@
1
1
  import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
- import { Op, SerializedCrdtWithId } from "./live";
2
+ import { Op, SerializedCrdt, SerializedCrdtWithId } from "./live";
3
3
  /**
4
4
  * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
5
5
  * Keys should be a string, and values should be serializable to JSON.
@@ -11,13 +11,15 @@ export declare class LiveObject<T extends Record<string, any> = Record<string, a
11
11
  /**
12
12
  * INTERNAL
13
13
  */
14
- _serialize(parentId?: string, parentKey?: string): Op[];
14
+ _serialize(parentId?: string, parentKey?: string, doc?: Doc): Op[];
15
15
  /**
16
16
  * INTERNAL
17
17
  */
18
- static _deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveObject<{
19
- [key: string]: any;
20
- }>;
18
+ static _deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveObject<Record<string, any>>;
19
+ /**
20
+ * INTERNAL
21
+ */
22
+ static _deserializeChildren(object: LiveObject, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveObject<Record<string, any>>;
21
23
  /**
22
24
  * INTERNAL
23
25
  */
@@ -25,11 +27,15 @@ export declare class LiveObject<T extends Record<string, any> = Record<string, a
25
27
  /**
26
28
  * INTERNAL
27
29
  */
28
- _attachChild(id: string, key: keyof T, child: AbstractCrdt): ApplyResult;
30
+ _attachChild(id: string, key: keyof T, child: AbstractCrdt, isLocal: boolean): ApplyResult;
29
31
  /**
30
32
  * INTERNAL
31
33
  */
32
34
  _detachChild(child: AbstractCrdt): void;
35
+ /**
36
+ * INTERNAL
37
+ */
38
+ _detachChildren(): void;
33
39
  /**
34
40
  * INTERNAL
35
41
  */
@@ -37,7 +43,11 @@ export declare class LiveObject<T extends Record<string, any> = Record<string, a
37
43
  /**
38
44
  * INTERNAL
39
45
  */
40
- _apply(op: Op): ApplyResult;
46
+ _apply(op: Op, isLocal: boolean): ApplyResult;
47
+ /**
48
+ * INTERNAL
49
+ */
50
+ _toSerializedCrdt(): SerializedCrdt;
41
51
  /**
42
52
  * Transform the LiveObject into a javascript object
43
53
  */
@@ -35,13 +35,14 @@ export class LiveObject extends AbstractCrdt {
35
35
  /**
36
36
  * INTERNAL
37
37
  */
38
- _serialize(parentId, parentKey) {
38
+ _serialize(parentId, parentKey, doc) {
39
39
  if (this._id == null) {
40
40
  throw new Error("Cannot serialize item is not attached");
41
41
  }
42
42
  const ops = [];
43
43
  const op = {
44
44
  id: this._id,
45
+ opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
45
46
  type: OpType.CreateObject,
46
47
  parentId,
47
48
  parentKey,
@@ -50,7 +51,7 @@ export class LiveObject extends AbstractCrdt {
50
51
  ops.push(op);
51
52
  for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
52
53
  if (value instanceof AbstractCrdt) {
53
- ops.push(...value._serialize(this._id, key));
54
+ ops.push(...value._serialize(this._id, key, doc));
54
55
  }
55
56
  else {
56
57
  op.data[key] = value;
@@ -67,7 +68,13 @@ export class LiveObject extends AbstractCrdt {
67
68
  }
68
69
  const object = new LiveObject(item.data);
69
70
  object._attach(id, doc);
70
- const children = parentToChildren.get(id);
71
+ return this._deserializeChildren(object, parentToChildren, doc);
72
+ }
73
+ /**
74
+ * INTERNAL
75
+ */
76
+ static _deserializeChildren(object, parentToChildren, doc) {
77
+ const children = parentToChildren.get(object._id);
71
78
  if (children == null) {
72
79
  return object;
73
80
  }
@@ -96,7 +103,7 @@ export class LiveObject extends AbstractCrdt {
96
103
  /**
97
104
  * INTERNAL
98
105
  */
99
- _attachChild(id, key, child) {
106
+ _attachChild(id, key, child, isLocal) {
100
107
  if (this._doc == null) {
101
108
  throw new Error("Can't attach child if doc is not present");
102
109
  }
@@ -138,6 +145,15 @@ export class LiveObject extends AbstractCrdt {
138
145
  child._detach();
139
146
  }
140
147
  }
148
+ /**
149
+ * INTERNAL
150
+ */
151
+ _detachChildren() {
152
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
153
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
154
+ value._detach();
155
+ }
156
+ }
141
157
  /**
142
158
  * INTERNAL
143
159
  */
@@ -152,14 +168,26 @@ export class LiveObject extends AbstractCrdt {
152
168
  /**
153
169
  * INTERNAL
154
170
  */
155
- _apply(op) {
171
+ _apply(op, isLocal) {
156
172
  if (op.type === OpType.UpdateObject) {
157
- return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyUpdate).call(this, op);
173
+ return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyUpdate).call(this, op, isLocal);
158
174
  }
159
175
  else if (op.type === OpType.DeleteObjectKey) {
160
176
  return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyDeleteObjectKey).call(this, op);
161
177
  }
162
- return super._apply(op);
178
+ return super._apply(op, isLocal);
179
+ }
180
+ /**
181
+ * INTERNAL
182
+ */
183
+ _toSerializedCrdt() {
184
+ var _a;
185
+ return {
186
+ type: CrdtType.Object,
187
+ parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
188
+ parentKey: this._parentKey,
189
+ data: this.toObject(),
190
+ };
163
191
  }
164
192
  /**
165
193
  * Transform the LiveObject into a javascript object
@@ -215,7 +243,14 @@ export class LiveObject extends AbstractCrdt {
215
243
  ];
216
244
  }
217
245
  __classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
218
- this._doc.dispatch([{ type: OpType.DeleteObjectKey, key: keyAsString, id: this._id }], reverse, [this]);
246
+ this._doc.dispatch([
247
+ {
248
+ type: OpType.DeleteObjectKey,
249
+ key: keyAsString,
250
+ id: this._id,
251
+ opId: this._doc.generateOpId(),
252
+ },
253
+ ], reverse, [this]);
219
254
  }
220
255
  /**
221
256
  * Adds or updates multiple properties at once with an object.
@@ -262,7 +297,7 @@ export class LiveObject extends AbstractCrdt {
262
297
  if (newValue instanceof AbstractCrdt) {
263
298
  newValue._setParentLink(this, key);
264
299
  newValue._attach(this._doc.generateId(), this._doc);
265
- ops.push(...newValue._serialize(this._id, key));
300
+ ops.push(...newValue._serialize(this._id, key, this._doc));
266
301
  }
267
302
  else {
268
303
  updatedProps[key] = newValue;
@@ -283,7 +318,7 @@ export class LiveObject extends AbstractCrdt {
283
318
  this._doc.dispatch(ops, reverseOps, [this]);
284
319
  }
285
320
  }
286
- _LiveObject_map = new WeakMap(), _LiveObject_propToLastUpdate = new WeakMap(), _LiveObject_instances = new WeakSet(), _LiveObject_applyUpdate = function _LiveObject_applyUpdate(op) {
321
+ _LiveObject_map = new WeakMap(), _LiveObject_propToLastUpdate = new WeakMap(), _LiveObject_instances = new WeakSet(), _LiveObject_applyUpdate = function _LiveObject_applyUpdate(op, isLocal) {
287
322
  let isModified = false;
288
323
  const reverse = [];
289
324
  const reverseUpdate = {
@@ -305,11 +340,6 @@ _LiveObject_map = new WeakMap(), _LiveObject_propToLastUpdate = new WeakMap(), _
305
340
  reverse.push({ type: OpType.DeleteObjectKey, id: this._id, key });
306
341
  }
307
342
  }
308
- let isLocal = false;
309
- if (op.opId == null) {
310
- isLocal = true;
311
- op.opId = this._doc.generateOpId();
312
- }
313
343
  for (const key in op.data) {
314
344
  if (isLocal) {
315
345
  __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, op.opId);
@@ -1,5 +1,5 @@
1
1
  import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
- import { SerializedCrdtWithId, Op } from "./live";
2
+ import { SerializedCrdtWithId, Op, SerializedCrdt } from "./live";
3
3
  /**
4
4
  * INTERNAL
5
5
  */
@@ -14,8 +14,12 @@ export declare class LiveRegister<TValue = any> extends AbstractCrdt {
14
14
  /**
15
15
  * INTERNAL
16
16
  */
17
- _serialize(parentId: string, parentKey: string): Op[];
18
- _attachChild(id: string, key: string, crdt: AbstractCrdt): ApplyResult;
17
+ _serialize(parentId: string, parentKey: string, doc?: Doc): Op[];
18
+ /**
19
+ * INTERNAL
20
+ */
21
+ _toSerializedCrdt(): SerializedCrdt;
22
+ _attachChild(id: string, key: string, crdt: AbstractCrdt, isLocal: boolean): ApplyResult;
19
23
  _detachChild(crdt: AbstractCrdt): void;
20
- _apply(op: Op): ApplyResult;
24
+ _apply(op: Op, isLocal: boolean): ApplyResult;
21
25
  }
@@ -11,7 +11,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
11
11
  };
12
12
  var _LiveRegister_data;
13
13
  import { AbstractCrdt } from "./AbstractCrdt";
14
- import { CrdtType, OpType } from "./live";
14
+ import { CrdtType, OpType, } from "./live";
15
15
  /**
16
16
  * INTERNAL
17
17
  */
@@ -38,13 +38,14 @@ export class LiveRegister extends AbstractCrdt {
38
38
  /**
39
39
  * INTERNAL
40
40
  */
41
- _serialize(parentId, parentKey) {
41
+ _serialize(parentId, parentKey, doc) {
42
42
  if (this._id == null || parentId == null || parentKey == null) {
43
43
  throw new Error("Cannot serialize register if parentId or parentKey is undefined");
44
44
  }
45
45
  return [
46
46
  {
47
47
  type: OpType.CreateRegister,
48
+ opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
48
49
  id: this._id,
49
50
  parentId,
50
51
  parentKey,
@@ -52,14 +53,26 @@ export class LiveRegister extends AbstractCrdt {
52
53
  },
53
54
  ];
54
55
  }
55
- _attachChild(id, key, crdt) {
56
+ /**
57
+ * INTERNAL
58
+ */
59
+ _toSerializedCrdt() {
60
+ var _a;
61
+ return {
62
+ type: CrdtType.Register,
63
+ parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
64
+ parentKey: this._parentKey,
65
+ data: this.data,
66
+ };
67
+ }
68
+ _attachChild(id, key, crdt, isLocal) {
56
69
  throw new Error("Method not implemented.");
57
70
  }
58
71
  _detachChild(crdt) {
59
72
  throw new Error("Method not implemented.");
60
73
  }
61
- _apply(op) {
62
- return super._apply(op);
74
+ _apply(op, isLocal) {
75
+ return super._apply(op, isLocal);
63
76
  }
64
77
  }
65
78
  _LiveRegister_data = new WeakMap();