@liveblocks/client 0.16.13 → 0.16.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/shared.js CHANGED
@@ -122,141 +122,52 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
122
122
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
123
123
  }
124
124
 
125
- var ClientMsgCode, OpCode, CrdtType, ServerMsgCode, WebsocketCloseCodes, OpSource, _emittedDeprecationWarnings = new Set;
126
-
127
- function deprecate(message, key) {
128
- void 0 === key && (key = message), "production" !== process.env.NODE_ENV && (_emittedDeprecationWarnings.has(key) || (_emittedDeprecationWarnings.add(key),
129
- console.error("DEPRECATION WARNING: " + message)));
130
- }
131
-
132
- function throwUsageError(message) {
133
- if ("production" !== process.env.NODE_ENV) {
134
- var usageError = new Error(message);
135
- throw usageError.name = "Usage error", usageError;
136
- }
137
- }
138
-
139
- function errorIf(condition, message) {
140
- "production" !== process.env.NODE_ENV && condition && throwUsageError(message);
141
- }
142
-
143
- function assertNever(_value, errmsg) {
144
- throw new Error(errmsg);
145
- }
146
-
147
- function nn(value, errmsg) {
148
- return void 0 === errmsg && (errmsg = "Expected value to be non-nullable"), function(condition, errmsg) {
149
- if ("production" !== process.env.NODE_ENV && !condition) {
150
- var err = new Error(errmsg);
151
- throw err.name = "Assertion failure", err;
152
- }
153
- }(null != value, errmsg), value;
154
- }
125
+ var ServerMsgCode, ClientMsgCode, CrdtType, OpCode, WebsocketCloseCodes;
155
126
 
156
127
  function isChildCrdt(crdt) {
157
128
  return void 0 !== crdt.parentId && void 0 !== crdt.parentKey;
158
129
  }
159
130
 
160
- function HasParent(node, key) {
161
- return Object.freeze({
162
- type: "HasParent",
163
- node: node,
164
- key: key
165
- });
166
- }
167
-
168
- exports.ClientMsgCode = void 0, (ClientMsgCode = exports.ClientMsgCode || (exports.ClientMsgCode = {}))[ClientMsgCode.UPDATE_PRESENCE = 100] = "UPDATE_PRESENCE",
169
- ClientMsgCode[ClientMsgCode.BROADCAST_EVENT = 103] = "BROADCAST_EVENT", ClientMsgCode[ClientMsgCode.FETCH_STORAGE = 200] = "FETCH_STORAGE",
170
- ClientMsgCode[ClientMsgCode.UPDATE_STORAGE = 201] = "UPDATE_STORAGE", exports.OpCode = void 0,
171
- (OpCode = exports.OpCode || (exports.OpCode = {}))[OpCode.INIT = 0] = "INIT", OpCode[OpCode.SET_PARENT_KEY = 1] = "SET_PARENT_KEY",
172
- OpCode[OpCode.CREATE_LIST = 2] = "CREATE_LIST", OpCode[OpCode.UPDATE_OBJECT = 3] = "UPDATE_OBJECT",
173
- OpCode[OpCode.CREATE_OBJECT = 4] = "CREATE_OBJECT", OpCode[OpCode.DELETE_CRDT = 5] = "DELETE_CRDT",
174
- OpCode[OpCode.DELETE_OBJECT_KEY = 6] = "DELETE_OBJECT_KEY", OpCode[OpCode.CREATE_MAP = 7] = "CREATE_MAP",
175
- OpCode[OpCode.CREATE_REGISTER = 8] = "CREATE_REGISTER", exports.CrdtType = void 0,
176
- (CrdtType = exports.CrdtType || (exports.CrdtType = {}))[CrdtType.OBJECT = 0] = "OBJECT",
177
- CrdtType[CrdtType.LIST = 1] = "LIST", CrdtType[CrdtType.MAP = 2] = "MAP", CrdtType[CrdtType.REGISTER = 3] = "REGISTER",
178
131
  exports.ServerMsgCode = void 0, (ServerMsgCode = exports.ServerMsgCode || (exports.ServerMsgCode = {}))[ServerMsgCode.UPDATE_PRESENCE = 100] = "UPDATE_PRESENCE",
179
132
  ServerMsgCode[ServerMsgCode.USER_JOINED = 101] = "USER_JOINED", ServerMsgCode[ServerMsgCode.USER_LEFT = 102] = "USER_LEFT",
180
133
  ServerMsgCode[ServerMsgCode.BROADCASTED_EVENT = 103] = "BROADCASTED_EVENT", ServerMsgCode[ServerMsgCode.ROOM_STATE = 104] = "ROOM_STATE",
181
134
  ServerMsgCode[ServerMsgCode.INITIAL_STORAGE_STATE = 200] = "INITIAL_STORAGE_STATE",
182
- ServerMsgCode[ServerMsgCode.UPDATE_STORAGE = 201] = "UPDATE_STORAGE", exports.WebsocketCloseCodes = void 0,
183
- (WebsocketCloseCodes = exports.WebsocketCloseCodes || (exports.WebsocketCloseCodes = {}))[WebsocketCloseCodes.CLOSE_ABNORMAL = 1006] = "CLOSE_ABNORMAL",
135
+ ServerMsgCode[ServerMsgCode.UPDATE_STORAGE = 201] = "UPDATE_STORAGE", exports.ClientMsgCode = void 0,
136
+ (ClientMsgCode = exports.ClientMsgCode || (exports.ClientMsgCode = {}))[ClientMsgCode.UPDATE_PRESENCE = 100] = "UPDATE_PRESENCE",
137
+ ClientMsgCode[ClientMsgCode.BROADCAST_EVENT = 103] = "BROADCAST_EVENT", ClientMsgCode[ClientMsgCode.FETCH_STORAGE = 200] = "FETCH_STORAGE",
138
+ ClientMsgCode[ClientMsgCode.UPDATE_STORAGE = 201] = "UPDATE_STORAGE", exports.CrdtType = void 0,
139
+ (CrdtType = exports.CrdtType || (exports.CrdtType = {}))[CrdtType.OBJECT = 0] = "OBJECT",
140
+ CrdtType[CrdtType.LIST = 1] = "LIST", CrdtType[CrdtType.MAP = 2] = "MAP", CrdtType[CrdtType.REGISTER = 3] = "REGISTER",
141
+ exports.OpCode = void 0, (OpCode = exports.OpCode || (exports.OpCode = {}))[OpCode.INIT = 0] = "INIT",
142
+ OpCode[OpCode.SET_PARENT_KEY = 1] = "SET_PARENT_KEY", OpCode[OpCode.CREATE_LIST = 2] = "CREATE_LIST",
143
+ OpCode[OpCode.UPDATE_OBJECT = 3] = "UPDATE_OBJECT", OpCode[OpCode.CREATE_OBJECT = 4] = "CREATE_OBJECT",
144
+ OpCode[OpCode.DELETE_CRDT = 5] = "DELETE_CRDT", OpCode[OpCode.DELETE_OBJECT_KEY = 6] = "DELETE_OBJECT_KEY",
145
+ OpCode[OpCode.CREATE_MAP = 7] = "CREATE_MAP", OpCode[OpCode.CREATE_REGISTER = 8] = "CREATE_REGISTER",
146
+ exports.WebsocketCloseCodes = void 0, (WebsocketCloseCodes = exports.WebsocketCloseCodes || (exports.WebsocketCloseCodes = {}))[WebsocketCloseCodes.CLOSE_ABNORMAL = 1006] = "CLOSE_ABNORMAL",
184
147
  WebsocketCloseCodes[WebsocketCloseCodes.INVALID_MESSAGE_FORMAT = 4e3] = "INVALID_MESSAGE_FORMAT",
185
148
  WebsocketCloseCodes[WebsocketCloseCodes.NOT_ALLOWED = 4001] = "NOT_ALLOWED", WebsocketCloseCodes[WebsocketCloseCodes.MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS",
186
149
  WebsocketCloseCodes[WebsocketCloseCodes.MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS",
187
150
  WebsocketCloseCodes[WebsocketCloseCodes.MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP",
188
151
  WebsocketCloseCodes[WebsocketCloseCodes.MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM",
189
- WebsocketCloseCodes[WebsocketCloseCodes.CLOSE_WITHOUT_RETRY = 4999] = "CLOSE_WITHOUT_RETRY",
190
- exports.OpSource = void 0, (OpSource = exports.OpSource || (exports.OpSource = {}))[OpSource.UNDOREDO_RECONNECT = 0] = "UNDOREDO_RECONNECT",
191
- OpSource[OpSource.REMOTE = 1] = "REMOTE", OpSource[OpSource.ACK = 2] = "ACK";
192
-
193
- var NoParent = Object.freeze({
194
- type: "NoParent"
195
- });
196
-
197
- function Orphaned(oldKey) {
198
- return Object.freeze({
199
- type: "Orphaned",
200
- oldKey: oldKey
201
- });
202
- }
152
+ WebsocketCloseCodes[WebsocketCloseCodes.CLOSE_WITHOUT_RETRY = 4999] = "CLOSE_WITHOUT_RETRY";
203
153
 
204
- var _Symbol$iterator$1, _Symbol$iterator2, AbstractCrdt = function() {
154
+ var AbstractCrdt = function() {
205
155
  function AbstractCrdt() {
206
- this.__doc = void 0, this.__id = void 0, this._parent = NoParent;
156
+ this.__parent = void 0, this.__doc = void 0, this.__id = void 0, this.__parentKey = void 0;
207
157
  }
208
158
  var _proto = AbstractCrdt.prototype;
209
- return _proto._getParentKeyOrThrow = function() {
210
- switch (this.parent.type) {
211
- case "HasParent":
212
- return this.parent.key;
213
-
214
- case "NoParent":
215
- throw new Error("Parent key is missing");
216
-
217
- case "Orphaned":
218
- return this.parent.oldKey;
219
-
220
- default:
221
- return assertNever(this.parent, "Unknown state");
222
- }
223
- }, _proto._apply = function(op, _isLocal) {
224
- return op.type === exports.OpCode.DELETE_CRDT && "HasParent" === this.parent.type ? this.parent.node._detachChild(this) : {
159
+ return _proto._apply = function(op, _isLocal) {
160
+ return op.type === exports.OpCode.DELETE_CRDT && null != this._parent && null != this._parentKey ? this._parent._detachChild(this) : {
225
161
  modified: !1
226
162
  };
227
- }, _proto._setParentLink = function(newParentNode, newParentKey) {
228
- switch (this.parent.type) {
229
- case "HasParent":
230
- if (this.parent.node !== newParentNode) throw new Error("Cannot attach parent if it already exist");
231
- return void (this._parent = HasParent(newParentNode, newParentKey));
232
-
233
- case "Orphaned":
234
- case "NoParent":
235
- return void (this._parent = HasParent(newParentNode, newParentKey));
236
-
237
- default:
238
- return assertNever(this.parent, "Unknown state");
239
- }
163
+ }, _proto._setParentLink = function(parent, key) {
164
+ if (null != this.__parent && this.__parent !== parent) throw new Error("Cannot attach parent if it already exist");
165
+ this.__parentKey = key, this.__parent = parent;
240
166
  }, _proto._attach = function(id, doc) {
241
167
  if (this.__id || this.__doc) throw new Error("Cannot attach if CRDT is already attached");
242
168
  doc.addItem(id, this), this.__id = id, this.__doc = doc;
243
169
  }, _proto._detach = function() {
244
- switch (this.__doc && this.__id && this.__doc.deleteItem(this.__id), this.parent.type) {
245
- case "HasParent":
246
- this._parent = Orphaned(this.parent.key);
247
- break;
248
-
249
- case "NoParent":
250
- this._parent = NoParent;
251
- break;
252
-
253
- case "Orphaned":
254
- this._parent = Orphaned(this.parent.oldKey);
255
- break;
256
-
257
- default:
258
- assertNever(this.parent, "Unknown state");
259
- }
170
+ this.__doc && this.__id && this.__doc.deleteItem(this.__id), this.__parent = void 0,
260
171
  this.__doc = void 0;
261
172
  }, _createClass(AbstractCrdt, [ {
262
173
  key: "_doc",
@@ -274,44 +185,31 @@ var _Symbol$iterator$1, _Symbol$iterator2, AbstractCrdt = function() {
274
185
  return this.__id;
275
186
  }
276
187
  }, {
277
- key: "parent",
278
- get: function() {
279
- return this._parent;
280
- }
281
- }, {
282
- key: "_parentNode",
188
+ key: "_parent",
283
189
  get: function() {
284
- switch (this.parent.type) {
285
- case "HasParent":
286
- return this.parent.node;
287
-
288
- case "NoParent":
289
- case "Orphaned":
290
- return null;
291
-
292
- default:
293
- return assertNever(this.parent, "Unknown state");
294
- }
190
+ return this.__parent;
295
191
  }
296
192
  }, {
297
193
  key: "_parentKey",
298
194
  get: function() {
299
- switch (this.parent.type) {
300
- case "HasParent":
301
- return this.parent.key;
195
+ return this.__parentKey;
196
+ }
197
+ } ]), AbstractCrdt;
198
+ }();
199
+
200
+ function isJsonScalar(data) {
201
+ return null === data || "string" == typeof data || "number" == typeof data || "boolean" == typeof data;
202
+ }
302
203
 
303
- case "NoParent":
304
- return null;
204
+ function isJsonArray(data) {
205
+ return Array.isArray(data);
206
+ }
305
207
 
306
- case "Orphaned":
307
- return this.parent.oldKey;
208
+ function isJsonObject(data) {
209
+ return !isJsonScalar(data) && !isJsonArray(data);
210
+ }
308
211
 
309
- default:
310
- return assertNever(this.parent, "Unknown state");
311
- }
312
- }
313
- } ]), AbstractCrdt;
314
- }(), LiveRegister = function(_AbstractCrdt) {
212
+ var _Symbol$iterator$1, _Symbol$iterator2, LiveRegister = function(_AbstractCrdt) {
315
213
  function LiveRegister(data) {
316
214
  var _this;
317
215
  return (_this = _AbstractCrdt.call(this) || this)._data = void 0, _this._data = data,
@@ -322,25 +220,26 @@ var _Symbol$iterator$1, _Symbol$iterator2, AbstractCrdt = function() {
322
220
  return register._attach(id, doc), register;
323
221
  };
324
222
  var _proto = LiveRegister.prototype;
325
- return _proto._serialize = function(parentId, parentKey, doc) {
223
+ return _proto._serialize = function(parentId, parentKey, doc, intent) {
326
224
  if (null == this._id || null == parentId || null == parentKey) throw new Error("Cannot serialize register if parentId or parentKey is undefined");
327
225
  return [ {
328
226
  type: exports.OpCode.CREATE_REGISTER,
329
227
  opId: null == doc ? void 0 : doc.generateOpId(),
330
228
  id: this._id,
229
+ intent: intent,
331
230
  parentId: parentId,
332
231
  parentKey: parentKey,
333
232
  data: this.data
334
233
  } ];
335
234
  }, _proto._toSerializedCrdt = function() {
336
- if ("HasParent" !== this.parent.type) throw new Error("Cannot serialize LiveRegister if parent is missing");
235
+ var _this$_parent;
337
236
  return {
338
237
  type: exports.CrdtType.REGISTER,
339
- parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
340
- parentKey: this.parent.key,
238
+ parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
239
+ parentKey: this._parentKey,
341
240
  data: this.data
342
241
  };
343
- }, _proto._attachChild = function(_op) {
242
+ }, _proto._attachChild = function(_op, _isLocal) {
344
243
  throw new Error("Method not implemented.");
345
244
  }, _proto._detachChild = function(_crdt) {
346
245
  throw new Error("Method not implemented.");
@@ -355,7 +254,7 @@ var _Symbol$iterator$1, _Symbol$iterator2, AbstractCrdt = function() {
355
254
  }(AbstractCrdt);
356
255
 
357
256
  function makePosition(before, after) {
358
- return null != before && null != after ? pos(makePositionFromCodes(posCodes(before), posCodes(after))) : null != before ? function(before) {
257
+ return null == before && null == after ? pos([ 33 ]) : null != before && null == after ? function(before) {
359
258
  for (var result = [], beforeCodes = posCodes(before), i = 0; i < beforeCodes.length; i++) {
360
259
  var code = beforeCodes[i];
361
260
  if (126 !== code) {
@@ -368,7 +267,7 @@ function makePosition(before, after) {
368
267
  }
369
268
  }
370
269
  return pos(result);
371
- }(before) : null != after ? function(after) {
270
+ }(before) : null == before && null != after ? function(after) {
372
271
  for (var result = [], afterCodes = posCodes(after), i = 0; i < afterCodes.length; i++) {
373
272
  var code = afterCodes[i];
374
273
  if (!(code <= 33)) {
@@ -381,7 +280,7 @@ function makePosition(before, after) {
381
280
  }
382
281
  }
383
282
  return pos(result);
384
- }(after) : pos([ 33 ]);
283
+ }(after) : pos(makePositionFromCodes(posCodes(before), posCodes(after)));
385
284
  }
386
285
 
387
286
  function makePositionFromCodes(before, after) {
@@ -425,322 +324,206 @@ var LiveList = function(_AbstractCrdt) {
425
324
  function LiveList(items) {
426
325
  var _this;
427
326
  void 0 === items && (items = []), (_this = _AbstractCrdt.call(this) || this)._items = void 0,
428
- _this._implicitlyDeletedItems = void 0, _this._items = [], _this._implicitlyDeletedItems = new Set;
327
+ _this._items = [];
429
328
  for (var position = void 0, i = 0; i < items.length; i++) {
430
- var newPosition = makePosition(position), item = lsonToLiveNode(items[i]);
431
- item._setParentLink(_assertThisInitialized(_this), newPosition), _this._items.push(item),
432
- position = newPosition;
329
+ var newPosition = makePosition(position), item = selfOrRegister(items[i]);
330
+ _this._items.push([ item, newPosition ]), position = newPosition;
433
331
  }
434
332
  return _this;
435
333
  }
436
334
  _inheritsLoose(LiveList, _AbstractCrdt), LiveList._deserialize = function(_ref, parentToChildren, doc) {
437
- var id = _ref[0], list = new LiveList;
335
+ var id = _ref[0], list = new LiveList([]);
438
336
  list._attach(id, doc);
439
337
  var children = parentToChildren.get(id);
440
338
  if (null == children) return list;
441
339
  for (var _step, _iterator = _createForOfIteratorHelperLoose(children); !(_step = _iterator()).done; ) {
442
- var _step$value = _step.value, _id = _step$value[0], crdt = _step$value[1], child = deserialize([ _id, crdt ], parentToChildren, doc);
443
- child._setParentLink(list, crdt.parentKey), list._items.push(child), sortListItem(list._items);
340
+ var entry = _step.value, child = deserialize(entry, parentToChildren, doc);
341
+ child._setParentLink(list, entry[1].parentKey), list._items.push([ child, entry[1].parentKey ]),
342
+ list._items.sort((function(itemA, itemB) {
343
+ return comparePosition(itemA[1], itemB[1]);
344
+ }));
444
345
  }
445
346
  return list;
446
347
  };
447
348
  var _proto = LiveList.prototype;
448
- return _proto._serialize = function(parentId, parentKey, doc) {
349
+ return _proto._serialize = function(parentId, parentKey, doc, intent) {
449
350
  if (null == this._id) throw new Error("Cannot serialize item is not attached");
351
+ if (null == parentId || null == parentKey) throw new Error("Cannot serialize list if parentId or parentKey is undefined");
450
352
  var ops = [], op = {
451
353
  id: this._id,
452
354
  opId: null == doc ? void 0 : doc.generateOpId(),
355
+ intent: intent,
453
356
  type: exports.OpCode.CREATE_LIST,
454
357
  parentId: parentId,
455
358
  parentKey: parentKey
456
359
  };
457
360
  ops.push(op);
458
361
  for (var _step2, _iterator2 = _createForOfIteratorHelperLoose(this._items); !(_step2 = _iterator2()).done; ) {
459
- var item = _step2.value;
460
- ops.push.apply(ops, item._serialize(this._id, item._getParentKeyOrThrow(), doc));
362
+ var _step2$value = _step2.value, _value = _step2$value[0], key = _step2$value[1];
363
+ ops.push.apply(ops, _value._serialize(this._id, key, doc));
461
364
  }
462
365
  return ops;
463
366
  }, _proto._indexOfPosition = function(position) {
464
367
  return this._items.findIndex((function(item) {
465
- return item._getParentKeyOrThrow() === position;
368
+ return item[1] === position;
466
369
  }));
467
370
  }, _proto._attach = function(id, doc) {
468
371
  _AbstractCrdt.prototype._attach.call(this, id, doc);
469
372
  for (var _step3, _iterator3 = _createForOfIteratorHelperLoose(this._items); !(_step3 = _iterator3()).done; ) {
470
- _step3.value._attach(doc.generateId(), doc);
373
+ _step3.value[0]._attach(doc.generateId(), doc);
471
374
  }
472
375
  }, _proto._detach = function() {
473
376
  _AbstractCrdt.prototype._detach.call(this);
474
377
  for (var _step4, _iterator4 = _createForOfIteratorHelperLoose(this._items); !(_step4 = _iterator4()).done; ) {
475
- _step4.value._detach();
476
- }
477
- }, _proto._applyRemoteSet = function(op) {
478
- if (null == this._doc) throw new Error("Can't attach child if doc is not present");
479
- var id = op.id, key = op.parentKey, child = creationOpToLiveNode(op);
480
- child._attach(id, this._doc), child._setParentLink(this, key);
481
- var deletedId = op.deletedId, existingItemIndex = this._indexOfPosition(key);
482
- if (-1 !== existingItemIndex) {
483
- var existingItem = this._items[existingItemIndex];
484
- if (existingItem._id === deletedId) return existingItem._detach(), this._items[existingItemIndex] = child,
485
- {
486
- modified: makeUpdate(this, [ setDelta(existingItemIndex, child) ]),
487
- reverse: []
488
- };
489
- this._implicitlyDeletedItems.add(existingItem), this._items[existingItemIndex] = child;
490
- var delta = [ setDelta(existingItemIndex, child) ], _deleteDelta = this._detachItemAssociatedToSetOperation(op.deletedId);
491
- return _deleteDelta && delta.push(_deleteDelta), {
492
- modified: makeUpdate(this, delta),
493
- reverse: []
494
- };
378
+ _step4.value[0]._detach();
495
379
  }
496
- var updates = [], _deleteDelta2 = this._detachItemAssociatedToSetOperation(op.deletedId);
497
- return _deleteDelta2 && updates.push(_deleteDelta2), this._items.push(child), sortListItem(this._items),
498
- updates.push(insertDelta(this._indexOfPosition(key), child)), {
499
- reverse: [],
500
- modified: makeUpdate(this, updates)
501
- };
502
- }, _proto._applySetAck = function(op) {
503
- var delta = [], deletedDelta = this._detachItemAssociatedToSetOperation(op.deletedId);
504
- deletedDelta && delta.push(deletedDelta);
505
- var indexOfItemWithSamePosition = this._indexOfPosition(op.parentKey), existingItem = this._items.find((function(item) {
506
- return item._id === op.id;
507
- }));
508
- if (null != existingItem) {
509
- if (existingItem._parentKey === op.parentKey) return delta.length > 0 ? {
510
- modified: makeUpdate(this, delta),
511
- reverse: []
512
- } : {
513
- modified: !1
514
- };
515
- -1 !== indexOfItemWithSamePosition && (this._implicitlyDeletedItems.add(this._items[indexOfItemWithSamePosition]),
516
- this._items.splice(indexOfItemWithSamePosition, 1), delta.push(deleteDelta(indexOfItemWithSamePosition)));
517
- var previousIndex = this._items.indexOf(existingItem);
518
- existingItem._setParentLink(this, op.parentKey), sortListItem(this._items);
519
- var newIndex = this._items.indexOf(existingItem);
520
- return newIndex !== previousIndex && delta.push(moveDelta(previousIndex, newIndex, existingItem)),
521
- {
522
- modified: delta.length > 0 && makeUpdate(this, delta),
523
- reverse: []
524
- };
525
- }
526
- var orphan = nn(this._doc).getItem(op.id);
527
- if (orphan && this._implicitlyDeletedItems.has(orphan)) {
528
- orphan._setParentLink(this, op.parentKey), this._implicitlyDeletedItems.delete(orphan),
529
- this._items.push(orphan), sortListItem(this._items);
530
- var recreatedItemIndex = this._items.indexOf(orphan);
531
- return {
532
- modified: makeUpdate(this, [ -1 === indexOfItemWithSamePosition ? insertDelta(recreatedItemIndex, orphan) : setDelta(recreatedItemIndex, orphan) ].concat(delta)),
533
- reverse: []
534
- };
535
- }
536
- var _this$_createAttachIt = this._createAttachItemAndSort(op, op.parentKey), newItem = _this$_createAttachIt.newItem, _newIndex = _this$_createAttachIt.newIndex;
537
- return {
538
- modified: makeUpdate(this, [ -1 === indexOfItemWithSamePosition ? insertDelta(_newIndex, newItem) : setDelta(_newIndex, newItem) ].concat(delta)),
539
- reverse: []
540
- };
541
- }, _proto._detachItemAssociatedToSetOperation = function(deletedId) {
542
- if (null == deletedId || null == this._doc) return null;
543
- var deletedItem = this._doc.getItem(deletedId);
544
- if (null == deletedItem) return null;
545
- var result = this._detachChild(deletedItem);
546
- return !1 === result.modified ? null : result.modified.updates[0];
547
- }, _proto._applyRemoteInsert = function(op) {
380
+ }, _proto._attachChild = function(op, isLocal) {
548
381
  if (null == this._doc) throw new Error("Can't attach child if doc is not present");
549
- var key = op.parentKey, existingItemIndex = this._indexOfPosition(key);
550
- -1 !== existingItemIndex && this._shiftItemPosition(existingItemIndex, key);
551
- var _this$_createAttachIt2 = this._createAttachItemAndSort(op, key), newItem = _this$_createAttachIt2.newItem;
552
- return {
553
- modified: makeUpdate(this, [ insertDelta(_this$_createAttachIt2.newIndex, newItem) ]),
554
- reverse: []
555
- };
556
- }, _proto._applyInsertAck = function(op) {
557
- var existingItem = this._items.find((function(item) {
558
- return item._id === op.id;
559
- })), key = op.parentKey, itemIndexAtPosition = this._indexOfPosition(key);
560
- if (existingItem) {
561
- if (existingItem._parentKey === key) return {
562
- modified: !1
563
- };
564
- var oldPositionIndex = this._items.indexOf(existingItem);
565
- -1 !== itemIndexAtPosition && this._shiftItemPosition(itemIndexAtPosition, key),
566
- existingItem._setParentLink(this, key), sortListItem(this._items);
567
- var newIndex = this._indexOfPosition(key);
568
- return newIndex === oldPositionIndex ? {
569
- modified: !1
570
- } : {
571
- modified: makeUpdate(this, [ moveDelta(oldPositionIndex, newIndex, existingItem) ]),
572
- reverse: []
573
- };
574
- }
575
- var orphan = nn(this._doc).getItem(op.id);
576
- if (orphan && this._implicitlyDeletedItems.has(orphan)) return orphan._setParentLink(this, key),
577
- this._implicitlyDeletedItems.delete(orphan), this._items.push(orphan), sortListItem(this._items),
578
- {
579
- modified: makeUpdate(this, [ insertDelta(this._indexOfPosition(key), orphan) ]),
580
- reverse: []
581
- };
582
- -1 !== itemIndexAtPosition && this._shiftItemPosition(itemIndexAtPosition, key);
583
- var _this$_createAttachIt3 = this._createAttachItemAndSort(op, key), newItem = _this$_createAttachIt3.newItem;
584
- return {
585
- modified: makeUpdate(this, [ insertDelta(_this$_createAttachIt3.newIndex, newItem) ]),
586
- reverse: []
587
- };
588
- }, _proto._applyInsertUndoRedo = function(op) {
589
- var _this$_doc, id = op.id, key = op.parentKey, child = creationOpToLiveNode(op);
590
- if (void 0 !== (null == (_this$_doc = this._doc) ? void 0 : _this$_doc.getItem(id))) return {
591
- modified: !1
592
- };
593
- child._attach(id, nn(this._doc)), child._setParentLink(this, key);
594
- var existingItemIndex = this._indexOfPosition(key), newKey = key;
595
- -1 !== existingItemIndex && (newKey = makePosition(this._items[existingItemIndex] ? this._items[existingItemIndex]._getParentKeyOrThrow() : void 0, this._items[existingItemIndex + 1] ? this._items[existingItemIndex + 1]._getParentKeyOrThrow() : void 0),
596
- child._setParentLink(this, newKey));
597
- return this._items.push(child), sortListItem(this._items), {
598
- modified: makeUpdate(this, [ insertDelta(this._indexOfPosition(newKey), child) ]),
599
- reverse: [ {
600
- type: exports.OpCode.DELETE_CRDT,
601
- id: id
602
- } ]
603
- };
604
- }, _proto._applySetUndoRedo = function(op) {
605
- var _this$_doc2, id = op.id, key = op.parentKey, child = creationOpToLiveNode(op);
606
- if (void 0 !== (null == (_this$_doc2 = this._doc) ? void 0 : _this$_doc2.getItem(id))) return {
382
+ var id = op.id, parentKey = op.parentKey, intent = op.intent, key = parentKey, child = creationOpToLiveStructure(op);
383
+ if (void 0 !== this._doc.getItem(id)) return {
607
384
  modified: !1
608
385
  };
609
- var indexOfItemWithSameKey = this._indexOfPosition(key);
610
- child._attach(id, nn(this._doc)), child._setParentLink(this, key);
611
- var newKey = key;
612
- if (-1 !== indexOfItemWithSameKey) {
613
- var existingItem = this._items[indexOfItemWithSameKey];
614
- existingItem._detach(), this._items[indexOfItemWithSameKey] = child;
615
- var reverse = existingItem._serialize(nn(this._id), key, this._doc);
616
- addIntentAndDeletedIdToOperation(reverse, op.id);
617
- var delta = [ setDelta(indexOfItemWithSameKey, child) ], deletedDelta = this._detachItemAssociatedToSetOperation(op.deletedId);
618
- return deletedDelta && delta.push(deletedDelta), {
619
- modified: makeUpdate(this, delta),
620
- reverse: reverse
621
- };
386
+ child._attach(id, this._doc), child._setParentLink(this, key);
387
+ var index = this._items.findIndex((function(entry) {
388
+ return entry[1] === key;
389
+ })), newKey = key;
390
+ if (-1 !== index) {
391
+ if ("set" === intent) {
392
+ var existingItem = this._items[index][0];
393
+ existingItem._detach();
394
+ var storageUpdate = {
395
+ node: this,
396
+ type: "LiveList",
397
+ updates: [ {
398
+ index: index,
399
+ type: "set",
400
+ item: child instanceof LiveRegister ? child.data : child
401
+ } ]
402
+ };
403
+ return this._items[index][0] = child, {
404
+ modified: storageUpdate,
405
+ reverse: existingItem._serialize(this._id, key, this._doc, "set")
406
+ };
407
+ }
408
+ if (isLocal) {
409
+ var before = this._items[index] ? this._items[index][1] : void 0, after = this._items[index + 1] ? this._items[index + 1][1] : void 0;
410
+ newKey = makePosition(before, after), child._setParentLink(this, newKey);
411
+ } else {
412
+ var _this$_items;
413
+ this._items[index][1] = makePosition(key, null == (_this$_items = this._items[index + 1]) ? void 0 : _this$_items[1]);
414
+ }
622
415
  }
623
- this._items.push(child), sortListItem(this._items), this._detachItemAssociatedToSetOperation(op.deletedId);
624
- var newIndex = this._indexOfPosition(newKey);
416
+ this._items.push([ child, newKey ]), this._items.sort((function(itemA, itemB) {
417
+ return comparePosition(itemA[1], itemB[1]);
418
+ }));
419
+ var newIndex = this._items.findIndex((function(entry) {
420
+ return entry[1] === newKey;
421
+ }));
625
422
  return {
626
423
  reverse: [ {
627
424
  type: exports.OpCode.DELETE_CRDT,
628
425
  id: id
629
426
  } ],
630
- modified: makeUpdate(this, [ insertDelta(newIndex, child) ])
427
+ modified: {
428
+ node: this,
429
+ type: "LiveList",
430
+ updates: [ {
431
+ index: newIndex,
432
+ type: "insert",
433
+ item: child instanceof LiveRegister ? child.data : child
434
+ } ]
435
+ }
631
436
  };
632
- }, _proto._attachChild = function(op, source) {
633
- if (null == this._doc) throw new Error("Can't attach child if doc is not present");
634
- if ("set" === op.intent) {
635
- if (source === exports.OpSource.REMOTE) return this._applyRemoteSet(op);
636
- if (source === exports.OpSource.UNDOREDO_RECONNECT) return this._applySetUndoRedo(op);
637
- if (source === exports.OpSource.ACK) return this._applySetAck(op);
638
- }
639
- return source === exports.OpSource.REMOTE ? this._applyRemoteInsert(op) : source === exports.OpSource.ACK ? this._applyInsertAck(op) : this._applyInsertUndoRedo(op);
640
437
  }, _proto._detachChild = function(child) {
641
438
  if (child) {
642
- var parentKey = nn(child._parentKey), reverse = child._serialize(nn(this._id), parentKey, this._doc), indexToDelete = this._items.indexOf(child);
439
+ var reverse = child._serialize(this._id, child._parentKey, this._doc), indexToDelete = this._items.findIndex((function(item) {
440
+ return item[0] === child;
441
+ }));
643
442
  return this._items.splice(indexToDelete, 1), child._detach(), {
644
- modified: makeUpdate(this, [ deleteDelta(indexToDelete) ]),
443
+ modified: {
444
+ node: this,
445
+ type: "LiveList",
446
+ updates: [ {
447
+ index: indexToDelete,
448
+ type: "delete"
449
+ } ]
450
+ },
645
451
  reverse: reverse
646
452
  };
647
453
  }
648
454
  return {
649
455
  modified: !1
650
456
  };
651
- }, _proto._applySetChildKeyRemote = function(newKey, child) {
652
- if (this._implicitlyDeletedItems.has(child)) return this._implicitlyDeletedItems.delete(child),
653
- child._setParentLink(this, newKey), this._items.push(child), sortListItem(this._items),
654
- {
655
- modified: makeUpdate(this, [ insertDelta(this._items.indexOf(child), child) ]),
656
- reverse: []
657
- };
658
- if (newKey === child._parentKey) return {
659
- modified: !1
660
- };
661
- var _this$_items, existingItemIndex = this._indexOfPosition(newKey);
662
- if (-1 === existingItemIndex) {
663
- var previousIndex = this._items.indexOf(child);
664
- child._setParentLink(this, newKey), sortListItem(this._items);
665
- var _newIndex4 = this._items.indexOf(child);
666
- return _newIndex4 === previousIndex ? {
667
- modified: !1
668
- } : {
669
- modified: makeUpdate(this, [ moveDelta(previousIndex, _newIndex4, child) ]),
670
- reverse: []
671
- };
672
- }
673
- this._items[existingItemIndex]._setParentLink(this, makePosition(newKey, null == (_this$_items = this._items[existingItemIndex + 1]) ? void 0 : _this$_items._getParentKeyOrThrow()));
674
- var _previousIndex = this._items.indexOf(child);
675
- child._setParentLink(this, newKey), sortListItem(this._items);
676
- var _newIndex5 = this._items.indexOf(child);
677
- return _newIndex5 === _previousIndex ? {
678
- modified: !1
679
- } : {
680
- modified: makeUpdate(this, [ moveDelta(_previousIndex, _newIndex5, child) ]),
681
- reverse: []
682
- };
683
- }, _proto._applySetChildKeyAck = function(newKey, child) {
684
- var previousKey = nn(child._parentKey);
685
- if (this._implicitlyDeletedItems.has(child)) {
686
- var _this$_items2, existingItemIndex = this._indexOfPosition(newKey);
687
- if (this._implicitlyDeletedItems.delete(child), -1 !== existingItemIndex) this._items[existingItemIndex]._setParentLink(this, makePosition(newKey, null == (_this$_items2 = this._items[existingItemIndex + 1]) ? void 0 : _this$_items2._getParentKeyOrThrow()));
688
- return child._setParentLink(this, newKey), this._items.push(child), sortListItem(this._items),
689
- {
690
- modified: !1
691
- };
692
- }
693
- if (newKey === previousKey) return {
694
- modified: !1
695
- };
696
- var _this$_items3, previousIndex = this._items.indexOf(child), _existingItemIndex = this._indexOfPosition(newKey);
697
- -1 !== _existingItemIndex && this._items[_existingItemIndex]._setParentLink(this, makePosition(newKey, null == (_this$_items3 = this._items[_existingItemIndex + 1]) ? void 0 : _this$_items3._getParentKeyOrThrow())),
698
- child._setParentLink(this, newKey), sortListItem(this._items);
699
- var newIndex = this._items.indexOf(child);
700
- return previousIndex === newIndex ? {
701
- modified: !1
702
- } : {
703
- modified: makeUpdate(this, [ moveDelta(previousIndex, newIndex, child) ]),
704
- reverse: []
705
- };
706
- }, _proto._applySetChildKeyUndoRedo = function(newKey, child) {
707
- var _this$_items4, previousKey = nn(child._parentKey), previousIndex = this._items.indexOf(child), existingItemIndex = this._indexOfPosition(newKey);
708
- -1 !== existingItemIndex && this._items[existingItemIndex]._setParentLink(this, makePosition(newKey, null == (_this$_items4 = this._items[existingItemIndex + 1]) ? void 0 : _this$_items4._getParentKeyOrThrow()));
709
- child._setParentLink(this, newKey), sortListItem(this._items);
710
- var newIndex = this._items.indexOf(child);
711
- return previousIndex === newIndex ? {
712
- modified: !1
713
- } : {
714
- modified: makeUpdate(this, [ moveDelta(previousIndex, newIndex, child) ]),
457
+ }, _proto._setChildKey = function(key, child, previousKey) {
458
+ child._setParentLink(this, key);
459
+ var _this$_items2, previousIndex = this._items.findIndex((function(entry) {
460
+ return entry[0]._id === child._id;
461
+ })), index = this._items.findIndex((function(entry) {
462
+ return entry[1] === key;
463
+ }));
464
+ -1 !== index && (this._items[index][1] = makePosition(key, null == (_this$_items2 = this._items[index + 1]) ? void 0 : _this$_items2[1]));
465
+ var item = this._items.find((function(item) {
466
+ return item[0] === child;
467
+ }));
468
+ item && (item[1] = key), this._items.sort((function(itemA, itemB) {
469
+ return comparePosition(itemA[1], itemB[1]);
470
+ }));
471
+ var newIndex = this._items.findIndex((function(entry) {
472
+ return entry[0]._id === child._id;
473
+ }));
474
+ return {
475
+ modified: {
476
+ node: this,
477
+ type: "LiveList",
478
+ updates: newIndex === previousIndex ? [] : [ {
479
+ index: newIndex,
480
+ item: child instanceof LiveRegister ? child.data : child,
481
+ previousIndex: previousIndex,
482
+ type: "move"
483
+ } ]
484
+ },
715
485
  reverse: [ {
716
486
  type: exports.OpCode.SET_PARENT_KEY,
717
- id: nn(child._id),
487
+ id: null == item ? void 0 : item[0]._id,
718
488
  parentKey: previousKey
719
489
  } ]
720
490
  };
721
- }, _proto._setChildKey = function(newKey, child, source) {
722
- return source === exports.OpSource.REMOTE ? this._applySetChildKeyRemote(newKey, child) : source === exports.OpSource.ACK ? this._applySetChildKeyAck(newKey, child) : this._applySetChildKeyUndoRedo(newKey, child);
723
491
  }, _proto._apply = function(op, isLocal) {
724
492
  return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
725
493
  }, _proto._toSerializedCrdt = function() {
726
- if ("HasParent" !== this.parent.type) throw new Error("Cannot serialize LiveList if parent is missing");
494
+ var _this$_parent;
727
495
  return {
728
496
  type: exports.CrdtType.LIST,
729
- parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
730
- parentKey: this.parent.key
497
+ parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
498
+ parentKey: this._parentKey
731
499
  };
732
500
  }, _proto.push = function(element) {
733
501
  return this.insert(element, this.length);
734
502
  }, _proto.insert = function(element, index) {
735
503
  if (index < 0 || index > this._items.length) throw new Error('Cannot insert list item at index "' + index + '". index should be between 0 and ' + this._items.length);
736
- var position = makePosition(this._items[index - 1] ? this._items[index - 1]._getParentKeyOrThrow() : void 0, this._items[index] ? this._items[index]._getParentKeyOrThrow() : void 0), value = lsonToLiveNode(element);
737
- if (value._setParentLink(this, position), this._items.push(value), sortListItem(this._items),
738
- this._doc && this._id) {
504
+ var position = makePosition(this._items[index - 1] ? this._items[index - 1][1] : void 0, this._items[index] ? this._items[index][1] : void 0), value = selfOrRegister(element);
505
+ value._setParentLink(this, position), this._items.push([ value, position ]), this._items.sort((function(itemA, itemB) {
506
+ return comparePosition(itemA[1], itemB[1]);
507
+ }));
508
+ var newIndex = this._items.findIndex((function(entry) {
509
+ return entry[1] === position;
510
+ }));
511
+ if (this._doc && this._id) {
739
512
  var id = this._doc.generateId();
740
- value._attach(id, this._doc), this._doc.dispatch(value._serialize(this._id, position, this._doc), [ {
513
+ value._attach(id, this._doc);
514
+ var storageUpdates = new Map;
515
+ storageUpdates.set(this._id, {
516
+ node: this,
517
+ type: "LiveList",
518
+ updates: [ {
519
+ index: newIndex,
520
+ item: value instanceof LiveRegister ? value.data : value,
521
+ type: "insert"
522
+ } ]
523
+ }), this._doc.dispatch(value._serialize(this._id, position, this._doc), [ {
741
524
  type: exports.OpCode.DELETE_CRDT,
742
525
  id: id
743
- } ], new Map([ [ this._id, makeUpdate(this, [ insertDelta(index, value) ]) ] ]));
526
+ } ], storageUpdates);
744
527
  }
745
528
  }, _proto.move = function(index, targetIndex) {
746
529
  if (targetIndex < 0) throw new Error("targetIndex cannot be less than 0");
@@ -748,77 +531,108 @@ var LiveList = function(_AbstractCrdt) {
748
531
  if (index < 0) throw new Error("index cannot be less than 0");
749
532
  if (index >= this._items.length) throw new Error("index cannot be greater or equal than the list length");
750
533
  var beforePosition = null, afterPosition = null;
751
- index < targetIndex ? (afterPosition = targetIndex === this._items.length - 1 ? void 0 : this._items[targetIndex + 1]._getParentKeyOrThrow(),
752
- beforePosition = this._items[targetIndex]._getParentKeyOrThrow()) : (afterPosition = this._items[targetIndex]._getParentKeyOrThrow(),
753
- beforePosition = 0 === targetIndex ? void 0 : this._items[targetIndex - 1]._getParentKeyOrThrow());
754
- var position = makePosition(beforePosition, afterPosition), item = this._items[index], previousPosition = item._getParentKeyOrThrow();
755
- if (item._setParentLink(this, position), sortListItem(this._items), this._doc && this._id) {
756
- var storageUpdates = new Map([ [ this._id, makeUpdate(this, [ moveDelta(index, targetIndex, item) ]) ] ]);
757
- this._doc.dispatch([ {
534
+ index < targetIndex ? (afterPosition = targetIndex === this._items.length - 1 ? void 0 : this._items[targetIndex + 1][1],
535
+ beforePosition = this._items[targetIndex][1]) : (afterPosition = this._items[targetIndex][1],
536
+ beforePosition = 0 === targetIndex ? void 0 : this._items[targetIndex - 1][1]);
537
+ var position = makePosition(beforePosition, afterPosition), item = this._items[index], previousPosition = item[1];
538
+ item[1] = position, item[0]._setParentLink(this, position), this._items.sort((function(itemA, itemB) {
539
+ return comparePosition(itemA[1], itemB[1]);
540
+ }));
541
+ var newIndex = this._items.findIndex((function(entry) {
542
+ return entry[1] === position;
543
+ }));
544
+ if (this._doc && this._id) {
545
+ var storageUpdates = new Map;
546
+ storageUpdates.set(this._id, {
547
+ node: this,
548
+ type: "LiveList",
549
+ updates: [ {
550
+ index: newIndex,
551
+ previousIndex: index,
552
+ item: item[0],
553
+ type: "move"
554
+ } ]
555
+ }), this._doc.dispatch([ {
758
556
  type: exports.OpCode.SET_PARENT_KEY,
759
- id: nn(item._id),
557
+ id: item[0]._id,
760
558
  opId: this._doc.generateOpId(),
761
559
  parentKey: position
762
560
  } ], [ {
763
561
  type: exports.OpCode.SET_PARENT_KEY,
764
- id: nn(item._id),
562
+ id: item[0]._id,
765
563
  parentKey: previousPosition
766
564
  } ], storageUpdates);
767
565
  }
768
566
  }, _proto.delete = function(index) {
769
567
  if (index < 0 || index >= this._items.length) throw new Error('Cannot delete list item at index "' + index + '". index should be between 0 and ' + (this._items.length - 1));
770
568
  var item = this._items[index];
771
- if (item._detach(), this._items.splice(index, 1), this._doc) {
772
- var childRecordId = item._id;
569
+ if (item[0]._detach(), this._items.splice(index, 1), this._doc) {
570
+ var childRecordId = item[0]._id;
773
571
  if (childRecordId) {
774
572
  var storageUpdates = new Map;
775
- storageUpdates.set(nn(this._id), makeUpdate(this, [ deleteDelta(index) ])), this._doc.dispatch([ {
573
+ storageUpdates.set(this._id, {
574
+ node: this,
575
+ type: "LiveList",
576
+ updates: [ {
577
+ index: index,
578
+ type: "delete"
579
+ } ]
580
+ }), this._doc.dispatch([ {
776
581
  id: childRecordId,
777
582
  opId: this._doc.generateOpId(),
778
583
  type: exports.OpCode.DELETE_CRDT
779
- } ], item._serialize(nn(this._id), item._getParentKeyOrThrow()), storageUpdates);
584
+ } ], item[0]._serialize(this._id, item[1]), storageUpdates);
780
585
  }
781
586
  }
782
587
  }, _proto.clear = function() {
783
588
  if (this._doc) {
784
589
  for (var _step5, ops = [], reverseOps = [], updateDelta = [], i = 0, _iterator5 = _createForOfIteratorHelperLoose(this._items); !(_step5 = _iterator5()).done; ) {
785
590
  var item = _step5.value;
786
- item._detach();
787
- var childId = item._id;
591
+ item[0]._detach();
592
+ var childId = item[0]._id;
788
593
  childId && (ops.push({
789
- type: exports.OpCode.DELETE_CRDT,
790
594
  id: childId,
791
- opId: this._doc.generateOpId()
792
- }), reverseOps.push.apply(reverseOps, item._serialize(nn(this._id), item._getParentKeyOrThrow())),
793
- updateDelta.push(deleteDelta(i))), i++;
595
+ type: exports.OpCode.DELETE_CRDT
596
+ }), reverseOps.push.apply(reverseOps, item[0]._serialize(this._id, item[1])), updateDelta.push({
597
+ index: i,
598
+ type: "delete"
599
+ })), i++;
794
600
  }
795
601
  this._items = [];
796
602
  var storageUpdates = new Map;
797
- storageUpdates.set(nn(this._id), makeUpdate(this, updateDelta)), this._doc.dispatch(ops, reverseOps, storageUpdates);
603
+ storageUpdates.set(this._id, {
604
+ node: this,
605
+ type: "LiveList",
606
+ updates: updateDelta
607
+ }), this._doc.dispatch(ops, reverseOps, storageUpdates);
798
608
  } else {
799
609
  for (var _step6, _iterator6 = _createForOfIteratorHelperLoose(this._items); !(_step6 = _iterator6()).done; ) {
800
- _step6.value._detach();
610
+ _step6.value[0]._detach();
801
611
  }
802
612
  this._items = [];
803
613
  }
804
614
  }, _proto.set = function(index, item) {
805
615
  if (index < 0 || index >= this._items.length) throw new Error('Cannot set list item at index "' + index + '". index should be between 0 and ' + (this._items.length - 1));
806
- var existingItem = this._items[index], position = existingItem._getParentKeyOrThrow(), existingId = existingItem._id;
616
+ var _this$_items$index = this._items[index], existingItem = _this$_items$index[0], position = _this$_items$index[1];
807
617
  existingItem._detach();
808
- var value = lsonToLiveNode(item);
809
- if (value._setParentLink(this, position), this._items[index] = value, this._doc && this._id) {
618
+ var value = selfOrRegister(item);
619
+ if (value._setParentLink(this, position), this._items[index][0] = value, this._doc && this._id) {
810
620
  var id = this._doc.generateId();
811
621
  value._attach(id, this._doc);
812
622
  var storageUpdates = new Map;
813
- storageUpdates.set(this._id, makeUpdate(this, [ setDelta(index, value) ]));
814
- var ops = value._serialize(this._id, position, this._doc);
815
- addIntentAndDeletedIdToOperation(ops, existingId);
816
- var reverseOps = existingItem._serialize(this._id, position, void 0);
817
- addIntentAndDeletedIdToOperation(reverseOps, id), this._doc.dispatch(ops, reverseOps, storageUpdates);
623
+ storageUpdates.set(this._id, {
624
+ node: this,
625
+ type: "LiveList",
626
+ updates: [ {
627
+ index: index,
628
+ item: value instanceof LiveRegister ? value.data : value,
629
+ type: "set"
630
+ } ]
631
+ }), this._doc.dispatch(value._serialize(this._id, position, this._doc, "set"), existingItem._serialize(this._id, position, void 0, "set"), storageUpdates);
818
632
  }
819
633
  }, _proto.toArray = function() {
820
634
  return this._items.map((function(entry) {
821
- return liveNodeToLson(entry);
635
+ return selfOrRegisterValue(entry[0]);
822
636
  }));
823
637
  }, _proto.every = function(predicate) {
824
638
  return this.toArray().every(predicate);
@@ -831,29 +645,19 @@ var LiveList = function(_AbstractCrdt) {
831
645
  }, _proto.forEach = function(callbackfn) {
832
646
  return this.toArray().forEach(callbackfn);
833
647
  }, _proto.get = function(index) {
834
- if (!(index < 0 || index >= this._items.length)) return liveNodeToLson(this._items[index]);
648
+ if (!(index < 0 || index >= this._items.length)) return selfOrRegisterValue(this._items[index][0]);
835
649
  }, _proto.indexOf = function(searchElement, fromIndex) {
836
650
  return this.toArray().indexOf(searchElement, fromIndex);
837
651
  }, _proto.lastIndexOf = function(searchElement, fromIndex) {
838
652
  return this.toArray().lastIndexOf(searchElement, fromIndex);
839
653
  }, _proto.map = function(callback) {
840
654
  return this._items.map((function(entry, i) {
841
- return callback(liveNodeToLson(entry), i);
655
+ return callback(selfOrRegisterValue(entry[0]), i);
842
656
  }));
843
657
  }, _proto.some = function(predicate) {
844
658
  return this.toArray().some(predicate);
845
659
  }, _proto[_Symbol$iterator$1] = function() {
846
660
  return new LiveListIterator(this._items);
847
- }, _proto._createAttachItemAndSort = function(op, key) {
848
- var newItem = creationOpToLiveNode(op);
849
- return newItem._attach(op.id, nn(this._doc)), newItem._setParentLink(this, key),
850
- this._items.push(newItem), sortListItem(this._items), {
851
- newItem: newItem,
852
- newIndex: this._indexOfPosition(key)
853
- };
854
- }, _proto._shiftItemPosition = function(index, key) {
855
- var _this$_items5, shiftedPosition = makePosition(key, this._items.length > index + 1 ? null == (_this$_items5 = this._items[index + 1]) ? void 0 : _this$_items5._getParentKeyOrThrow() : void 0);
856
- this._items[index]._setParentLink(this, shiftedPosition);
857
661
  }, _createClass(LiveList, [ {
858
662
  key: "length",
859
663
  get: function() {
@@ -877,62 +681,25 @@ var _Symbol$iterator, LiveListIterator = function() {
877
681
  done: !0,
878
682
  value: void 0
879
683
  } : {
880
- value: liveNodeToLson(result.value)
684
+ value: selfOrRegisterValue(result.value[0])
881
685
  };
882
686
  }, LiveListIterator;
883
- }();
884
-
885
- function makeUpdate(liveList, deltaUpdates) {
886
- return {
887
- node: liveList,
888
- type: "LiveList",
889
- updates: deltaUpdates
890
- };
891
- }
892
-
893
- function setDelta(index, item) {
894
- return {
895
- index: index,
896
- type: "set",
897
- item: item instanceof LiveRegister ? item.data : item
898
- };
899
- }
687
+ }(), _emittedDeprecationWarnings = new Set;
900
688
 
901
- function deleteDelta(index) {
902
- return {
903
- index: index,
904
- type: "delete"
905
- };
906
- }
907
-
908
- function insertDelta(index, item) {
909
- return {
910
- index: index,
911
- type: "insert",
912
- item: item instanceof LiveRegister ? item.data : item
913
- };
914
- }
915
-
916
- function moveDelta(previousIndex, index, item) {
917
- return {
918
- index: index,
919
- type: "move",
920
- previousIndex: previousIndex,
921
- item: item instanceof LiveRegister ? item.data : item
922
- };
689
+ function deprecate(message, key) {
690
+ void 0 === key && (key = message), "production" !== process.env.NODE_ENV && (_emittedDeprecationWarnings.has(key) || (_emittedDeprecationWarnings.add(key),
691
+ console.error("DEPRECATION WARNING: " + message)));
923
692
  }
924
693
 
925
- function sortListItem(items) {
926
- items.sort((function(itemA, itemB) {
927
- return comparePosition(itemA._getParentKeyOrThrow(), itemB._getParentKeyOrThrow());
928
- }));
694
+ function deprecateIf(condition, message, key) {
695
+ void 0 === key && (key = message), "production" !== process.env.NODE_ENV && condition && deprecate(message, key);
929
696
  }
930
697
 
931
- function addIntentAndDeletedIdToOperation(ops, deletedId) {
932
- if (0 === ops.length) throw new Error("Internal error. Serialized LiveStructure should have at least 1 operation");
933
- var firstOp = ops[0];
934
- if (firstOp.type !== exports.OpCode.CREATE_LIST && firstOp.type !== exports.OpCode.CREATE_OBJECT && firstOp.type !== exports.OpCode.CREATE_REGISTER && firstOp.type !== exports.OpCode.CREATE_MAP) throw new Error("Internal error. Serialized LiveStructure first op should be CreateOp");
935
- firstOp.intent = "set", firstOp.deletedId = deletedId;
698
+ function throwUsageError(message) {
699
+ if ("production" !== process.env.NODE_ENV) {
700
+ var usageError = new Error(message);
701
+ throw usageError.name = "Usage error", usageError;
702
+ }
936
703
  }
937
704
 
938
705
  _Symbol$iterator = Symbol.iterator;
@@ -940,10 +707,10 @@ _Symbol$iterator = Symbol.iterator;
940
707
  var LiveMap = function(_AbstractCrdt) {
941
708
  function LiveMap(entries) {
942
709
  var _this;
943
- if ((_this = _AbstractCrdt.call(this) || this)._map = void 0, errorIf(null === entries, "Support for calling `new LiveMap(null)` will be removed in @liveblocks/client 0.18. Please call as `new LiveMap()`, or `new LiveMap([])`."),
710
+ if ((_this = _AbstractCrdt.call(this) || this)._map = void 0, deprecateIf(null === entries, "Support for calling `new LiveMap(null)` will be removed in @liveblocks/client 0.18. Please call as `new LiveMap()`, or `new LiveMap([])`."),
944
711
  entries) {
945
712
  for (var _step, mappedEntries = [], _iterator = _createForOfIteratorHelperLoose(entries); !(_step = _iterator()).done; ) {
946
- var entry = _step.value, _value = lsonToLiveNode(entry[1]);
713
+ var entry = _step.value, _value = selfOrRegister(entry[1]);
947
714
  _value._setParentLink(_assertThisInitialized(_this), entry[0]), mappedEntries.push([ entry[0], _value ]);
948
715
  }
949
716
  _this._map = new Map(mappedEntries);
@@ -952,12 +719,14 @@ var LiveMap = function(_AbstractCrdt) {
952
719
  }
953
720
  _inheritsLoose(LiveMap, _AbstractCrdt);
954
721
  var _proto = LiveMap.prototype;
955
- return _proto._serialize = function(parentId, parentKey, doc) {
722
+ return _proto._serialize = function(parentId, parentKey, doc, intent) {
956
723
  if (null == this._id) throw new Error("Cannot serialize item is not attached");
724
+ if (null == parentId || null == parentKey) throw new Error("Cannot serialize map if parentId or parentKey is undefined");
957
725
  var ops = [], op = {
958
726
  id: this._id,
959
727
  opId: null == doc ? void 0 : doc.generateOpId(),
960
728
  type: exports.OpCode.CREATE_MAP,
729
+ intent: intent,
961
730
  parentId: parentId,
962
731
  parentKey: parentKey
963
732
  };
@@ -975,7 +744,9 @@ var LiveMap = function(_AbstractCrdt) {
975
744
  var children = parentToChildren.get(id);
976
745
  if (null == children) return map;
977
746
  for (var _step3, _iterator3 = _createForOfIteratorHelperLoose(children); !(_step3 = _iterator3()).done; ) {
978
- var _step3$value = _step3.value, _id = _step3$value[0], crdt = _step3$value[1], child = deserialize([ _id, crdt ], parentToChildren, doc);
747
+ var entry = _step3.value, crdt = entry[1];
748
+ if (null == crdt.parentKey) throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
749
+ var child = deserialize(entry, parentToChildren, doc);
979
750
  child._setParentLink(map, crdt.parentKey), map._map.set(crdt.parentKey, child);
980
751
  }
981
752
  return map;
@@ -985,24 +756,20 @@ var LiveMap = function(_AbstractCrdt) {
985
756
  var _step4$value = _step4.value;
986
757
  _step4$value[0];
987
758
  var _value3 = _step4$value[1];
988
- isLiveNode(_value3) && _value3._attach(doc.generateId(), doc);
759
+ isCrdt(_value3) && _value3._attach(doc.generateId(), doc);
989
760
  }
990
- }, _proto._attachChild = function(op) {
761
+ }, _proto._attachChild = function(op, _isLocal) {
991
762
  var _updates;
992
763
  if (null == this._doc) throw new Error("Can't attach child if doc is not present");
993
- var id = op.id, key = op.parentKey, child = creationOpToLiveNode(op);
764
+ var id = op.id, key = op.parentKey, child = creationOpToLiveStructure(op);
994
765
  if (void 0 !== this._doc.getItem(id)) return {
995
766
  modified: !1
996
767
  };
997
768
  var reverse, previousValue = this._map.get(key);
998
- if (previousValue) {
999
- var thisId = nn(this._id);
1000
- reverse = previousValue._serialize(thisId, key), previousValue._detach();
1001
- } else reverse = [ {
769
+ return previousValue ? (reverse = previousValue._serialize(this._id, key), previousValue._detach()) : reverse = [ {
1002
770
  type: exports.OpCode.DELETE_CRDT,
1003
771
  id: id
1004
- } ];
1005
- return child._setParentLink(this, key), child._attach(id, this._doc), this._map.set(key, child),
772
+ } ], child._setParentLink(this, key), child._attach(id, this._doc), this._map.set(key, child),
1006
773
  {
1007
774
  modified: {
1008
775
  node: this,
@@ -1019,7 +786,7 @@ var LiveMap = function(_AbstractCrdt) {
1019
786
  _step5.value._detach();
1020
787
  }
1021
788
  }, _proto._detachChild = function(child) {
1022
- for (var _updates2, _step6, id = nn(this._id), parentKey = nn(child._parentKey), reverse = child._serialize(id, parentKey, this._doc), _iterator6 = _createForOfIteratorHelperLoose(this._map); !(_step6 = _iterator6()).done; ) {
789
+ for (var _updates2, _step6, reverse = child._serialize(this._id, child._parentKey, this._doc), _iterator6 = _createForOfIteratorHelperLoose(this._map); !(_step6 = _iterator6()).done; ) {
1023
790
  var _step6$value = _step6.value, _key3 = _step6$value[0];
1024
791
  _step6$value[1] === child && this._map.delete(_key3);
1025
792
  }
@@ -1027,26 +794,26 @@ var LiveMap = function(_AbstractCrdt) {
1027
794
  modified: {
1028
795
  node: this,
1029
796
  type: "LiveMap",
1030
- updates: (_updates2 = {}, _updates2[parentKey] = {
797
+ updates: (_updates2 = {}, _updates2[child._parentKey] = {
1031
798
  type: "delete"
1032
799
  }, _updates2)
1033
800
  },
1034
801
  reverse: reverse
1035
802
  };
1036
803
  }, _proto._toSerializedCrdt = function() {
1037
- if ("HasParent" !== this.parent.type) throw new Error("Cannot serialize LiveMap if parent is missing");
804
+ var _this$_parent;
1038
805
  return {
1039
806
  type: exports.CrdtType.MAP,
1040
- parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
1041
- parentKey: this.parent.key
807
+ parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
808
+ parentKey: this._parentKey
1042
809
  };
1043
810
  }, _proto.get = function(key) {
1044
811
  var value = this._map.get(key);
1045
- if (null != value) return liveNodeToLson(value);
812
+ if (null != value) return selfOrRegisterValue(value);
1046
813
  }, _proto.set = function(key, value) {
1047
814
  var oldValue = this._map.get(key);
1048
815
  oldValue && oldValue._detach();
1049
- var item = lsonToLiveNode(value);
816
+ var item = selfOrRegister(value);
1050
817
  if (item._setParentLink(this, key), this._map.set(key, item), this._doc && this._id) {
1051
818
  var _updates3, id = this._doc.generateId();
1052
819
  item._attach(id, this._doc);
@@ -1068,8 +835,8 @@ var LiveMap = function(_AbstractCrdt) {
1068
835
  var item = this._map.get(key);
1069
836
  if (null == item) return !1;
1070
837
  if (item._detach(), this._map.delete(key), this._doc && item._id) {
1071
- var _updates4, thisId = nn(this._id), storageUpdates = new Map;
1072
- storageUpdates.set(thisId, {
838
+ var _updates4, storageUpdates = new Map;
839
+ storageUpdates.set(this._id, {
1073
840
  node: this,
1074
841
  type: "LiveMap",
1075
842
  updates: (_updates4 = {}, _updates4[key] = {
@@ -1079,7 +846,7 @@ var LiveMap = function(_AbstractCrdt) {
1079
846
  type: exports.OpCode.DELETE_CRDT,
1080
847
  id: item._id,
1081
848
  opId: this._doc.generateOpId()
1082
- } ], item._serialize(thisId, key), storageUpdates);
849
+ } ], item._serialize(this._id, key), storageUpdates);
1083
850
  }
1084
851
  return !0;
1085
852
  }, _proto.entries = function() {
@@ -1092,7 +859,7 @@ var LiveMap = function(_AbstractCrdt) {
1092
859
  done: !0,
1093
860
  value: void 0
1094
861
  } : {
1095
- value: [ iteratorValue.value[0], liveNodeToLson(iteratorValue.value[1]) ]
862
+ value: [ iteratorValue.value[0], selfOrRegisterValue(iteratorValue.value[1]) ]
1096
863
  };
1097
864
  }, _ref2;
1098
865
  }, _proto[_Symbol$iterator] = function() {
@@ -1109,7 +876,7 @@ var LiveMap = function(_AbstractCrdt) {
1109
876
  done: !0,
1110
877
  value: void 0
1111
878
  } : {
1112
- value: liveNodeToLson(iteratorValue.value)
879
+ value: selfOrRegisterValue(iteratorValue.value)
1113
880
  };
1114
881
  }, _ref3;
1115
882
  }, _proto.forEach = function(callback) {
@@ -1125,15 +892,7 @@ var LiveMap = function(_AbstractCrdt) {
1125
892
  } ]), LiveMap;
1126
893
  }(AbstractCrdt);
1127
894
 
1128
- function isJsonArray(data) {
1129
- return Array.isArray(data);
1130
- }
1131
-
1132
- function isJsonObject(data) {
1133
- return null !== data && "object" == typeof data && !isJsonArray(data);
1134
- }
1135
-
1136
- function creationOpToLiveNode(op) {
895
+ function creationOpToLiveStructure(op) {
1137
896
  switch (op.type) {
1138
897
  case exports.OpCode.CREATE_REGISTER:
1139
898
  return new LiveRegister(op.data);
@@ -1146,9 +905,6 @@ function creationOpToLiveNode(op) {
1146
905
 
1147
906
  case exports.OpCode.CREATE_LIST:
1148
907
  return new LiveList;
1149
-
1150
- default:
1151
- return assertNever(0, "Unknown creation Op");
1152
908
  }
1153
909
  }
1154
910
 
@@ -1172,40 +928,27 @@ function deserialize(_ref, parentToChildren, doc) {
1172
928
  }
1173
929
  }
1174
930
 
1175
- function isLiveNode(value) {
1176
- return isLiveList(value) || function(value) {
1177
- return value instanceof LiveMap;
1178
- }(value) || isLiveObject(value) || function(value) {
1179
- return value instanceof LiveRegister;
1180
- }(value);
931
+ function isCrdt(obj) {
932
+ return obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList || obj instanceof LiveRegister;
1181
933
  }
1182
934
 
1183
- function isLiveList(value) {
1184
- return value instanceof LiveList;
935
+ function selfOrRegisterValue(obj) {
936
+ return obj instanceof LiveRegister ? obj.data : obj;
1185
937
  }
1186
938
 
1187
- function isLiveObject(value) {
1188
- return value instanceof LiveObject;
1189
- }
1190
-
1191
- function liveNodeToLson(obj) {
1192
- return obj instanceof LiveRegister ? obj.data : obj instanceof LiveList || obj instanceof LiveMap || obj instanceof LiveObject ? obj : assertNever(0, "Unknown AbstractCrdt");
1193
- }
1194
-
1195
- function lsonToLiveNode(value) {
1196
- return value instanceof LiveObject || value instanceof LiveMap || value instanceof LiveList ? value : new LiveRegister(value);
939
+ function selfOrRegister(obj) {
940
+ if (obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList) return obj;
941
+ if (obj instanceof LiveRegister) throw new Error("Internal error. LiveRegister should not be created from selfOrRegister");
942
+ return new LiveRegister(obj);
1197
943
  }
1198
944
 
1199
945
  function isPlain(value) {
1200
946
  var type = typeof value;
1201
- return "undefined" === type || null === value || "string" === type || "boolean" === type || "number" === type || Array.isArray(value) || function(value) {
1202
- if ("object" != typeof value || null === value) return !1;
1203
- var proto = Object.getPrototypeOf(value);
1204
- if (null === proto) return !0;
1205
- var baseProto = proto;
1206
- for (;null !== Object.getPrototypeOf(baseProto); ) baseProto = Object.getPrototypeOf(baseProto);
1207
- return proto === baseProto;
1208
- }(value);
947
+ return "undefined" === type || null === value || "string" === type || "boolean" === type || "number" === type || Array.isArray(value) || isPlainObject(value);
948
+ }
949
+
950
+ function isPlainObject(blob) {
951
+ return null !== blob && "object" == typeof blob && "[object Object]" === Object.prototype.toString.call(blob);
1209
952
  }
1210
953
 
1211
954
  function entries(obj) {
@@ -1226,18 +969,19 @@ var LiveObject = function(_AbstractCrdt) {
1226
969
  for (var key in void 0 === obj && (obj = {}), (_this = _AbstractCrdt.call(this) || this)._map = void 0,
1227
970
  _this._propToLastUpdate = void 0, _this._propToLastUpdate = new Map, obj) {
1228
971
  var value = obj[key];
1229
- isLiveNode(value) && value._setParentLink(_assertThisInitialized(_this), key);
972
+ value instanceof AbstractCrdt && value._setParentLink(_assertThisInitialized(_this), key);
1230
973
  }
1231
974
  return _this._map = new Map(Object.entries(obj)), _this;
1232
975
  }
1233
976
  _inheritsLoose(LiveObject, _AbstractCrdt);
1234
977
  var _proto = LiveObject.prototype;
1235
- return _proto._serialize = function(parentId, parentKey, doc) {
978
+ return _proto._serialize = function(parentId, parentKey, doc, intent) {
1236
979
  if (null == this._id) throw new Error("Cannot serialize item is not attached");
1237
980
  var opId = null == doc ? void 0 : doc.generateOpId(), ops = [], op = void 0 !== parentId && void 0 !== parentKey ? {
1238
981
  type: exports.OpCode.CREATE_OBJECT,
1239
982
  id: this._id,
1240
983
  opId: opId,
984
+ intent: intent,
1241
985
  parentId: parentId,
1242
986
  parentKey: parentKey,
1243
987
  data: {}
@@ -1245,22 +989,25 @@ var LiveObject = function(_AbstractCrdt) {
1245
989
  type: exports.OpCode.CREATE_OBJECT,
1246
990
  id: this._id,
1247
991
  opId: opId,
992
+ intent: intent,
1248
993
  data: {}
1249
994
  };
1250
995
  ops.push(op);
1251
996
  for (var _step, _iterator = _createForOfIteratorHelperLoose(this._map); !(_step = _iterator()).done; ) {
1252
997
  var _step$value = _step.value, key = _step$value[0], value = _step$value[1];
1253
- isLiveNode(value) ? ops.push.apply(ops, value._serialize(this._id, key, doc)) : op.data[key] = value;
998
+ value instanceof AbstractCrdt ? ops.push.apply(ops, value._serialize(this._id, key, doc)) : op.data[key] = value;
1254
999
  }
1255
1000
  return ops;
1256
1001
  }, LiveObject._deserialize = function(_ref, parentToChildren, doc) {
1257
1002
  var id = _ref[0], liveObj = new LiveObject(_ref[1].data);
1258
1003
  return liveObj._attach(id, doc), this._deserializeChildren(liveObj, parentToChildren, doc);
1259
1004
  }, LiveObject._deserializeChildren = function(liveObj, parentToChildren, doc) {
1260
- var children = parentToChildren.get(nn(liveObj._id));
1005
+ var children = parentToChildren.get(liveObj._id);
1261
1006
  if (null == children) return liveObj;
1262
1007
  for (var _step2, _iterator2 = _createForOfIteratorHelperLoose(children); !(_step2 = _iterator2()).done; ) {
1263
- var _step2$value = _step2.value, id = _step2$value[0], crdt = _step2$value[1], child = deserialize([ id, crdt ], parentToChildren, doc);
1008
+ var entry = _step2.value, crdt = entry[1];
1009
+ if (null == crdt.parentKey) throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
1010
+ var child = deserialize(entry, parentToChildren, doc);
1264
1011
  child._setParentLink(liveObj, crdt.parentKey), liveObj._map.set(crdt.parentKey, child);
1265
1012
  }
1266
1013
  return liveObj;
@@ -1270,33 +1017,32 @@ var LiveObject = function(_AbstractCrdt) {
1270
1017
  var _step3$value = _step3.value;
1271
1018
  _step3$value[0];
1272
1019
  var value = _step3$value[1];
1273
- isLiveNode(value) && value._attach(doc.generateId(), doc);
1020
+ value instanceof AbstractCrdt && value._attach(doc.generateId(), doc);
1274
1021
  }
1275
- }, _proto._attachChild = function(op, source) {
1022
+ }, _proto._attachChild = function(op, isLocal) {
1276
1023
  var _updates;
1277
1024
  if (null == this._doc) throw new Error("Can't attach child if doc is not present");
1278
- var id = op.id, opId = op.opId, key = op.parentKey, child = creationOpToLiveNode(op);
1025
+ var id = op.id, parentKey = op.parentKey, opId = op.opId, key = parentKey, child = creationOpToLiveStructure(op);
1279
1026
  if (void 0 !== this._doc.getItem(id)) return this._propToLastUpdate.get(key) === opId && this._propToLastUpdate.delete(key),
1280
1027
  {
1281
1028
  modified: !1
1282
1029
  };
1283
- if (source === exports.OpSource.UNDOREDO_RECONNECT) this._propToLastUpdate.set(key, nn(opId)); else if (void 0 !== this._propToLastUpdate.get(key)) return this._propToLastUpdate.get(key) === opId ? (this._propToLastUpdate.delete(key),
1030
+ if (isLocal) this._propToLastUpdate.set(key, opId); else if (void 0 !== this._propToLastUpdate.get(key)) return this._propToLastUpdate.get(key) === opId ? (this._propToLastUpdate.delete(key),
1284
1031
  {
1285
1032
  modified: !1
1286
1033
  }) : {
1287
1034
  modified: !1
1288
1035
  };
1289
- var reverse, thisId = nn(this._id), previousValue = this._map.get(key);
1290
- if (isLiveNode(previousValue)) reverse = previousValue._serialize(thisId, key),
1291
- previousValue._detach(); else if (void 0 === previousValue) reverse = [ {
1036
+ var reverse, previousValue = this._map.get(key);
1037
+ if (isCrdt(previousValue)) reverse = previousValue._serialize(this._id, key), previousValue._detach(); else if (void 0 === previousValue) reverse = [ {
1292
1038
  type: exports.OpCode.DELETE_OBJECT_KEY,
1293
- id: thisId,
1039
+ id: this._id,
1294
1040
  key: key
1295
1041
  } ]; else {
1296
1042
  var _data;
1297
1043
  reverse = [ {
1298
1044
  type: exports.OpCode.UPDATE_OBJECT,
1299
- id: thisId,
1045
+ id: this._id,
1300
1046
  data: (_data = {}, _data[key] = previousValue, _data)
1301
1047
  } ];
1302
1048
  }
@@ -1313,7 +1059,7 @@ var LiveObject = function(_AbstractCrdt) {
1313
1059
  };
1314
1060
  }, _proto._detachChild = function(child) {
1315
1061
  if (child) {
1316
- for (var _updates2, _step4, id = nn(this._id), parentKey = nn(child._parentKey), reverse = child._serialize(id, parentKey, this._doc), _iterator4 = _createForOfIteratorHelperLoose(this._map); !(_step4 = _iterator4()).done; ) {
1062
+ for (var _updates2, _step4, reverse = child._serialize(this._id, child._parentKey, this._doc), _iterator4 = _createForOfIteratorHelperLoose(this._map); !(_step4 = _iterator4()).done; ) {
1317
1063
  var _step4$value = _step4.value, key = _step4$value[0];
1318
1064
  _step4$value[1] === child && this._map.delete(key);
1319
1065
  }
@@ -1321,7 +1067,7 @@ var LiveObject = function(_AbstractCrdt) {
1321
1067
  modified: {
1322
1068
  node: this,
1323
1069
  type: "LiveObject",
1324
- updates: (_updates2 = {}, _updates2[parentKey] = {
1070
+ updates: (_updates2 = {}, _updates2[child._parentKey] = {
1325
1071
  type: "delete"
1326
1072
  }, _updates2)
1327
1073
  },
@@ -1331,46 +1077,51 @@ var LiveObject = function(_AbstractCrdt) {
1331
1077
  return {
1332
1078
  modified: !1
1333
1079
  };
1080
+ }, _proto._detachChildren = function() {
1081
+ for (var _step5, _iterator5 = _createForOfIteratorHelperLoose(this._map); !(_step5 = _iterator5()).done; ) {
1082
+ var _step5$value = _step5.value, key = _step5$value[0], value = _step5$value[1];
1083
+ this._map.delete(key), value._detach();
1084
+ }
1334
1085
  }, _proto._detach = function() {
1335
1086
  _AbstractCrdt.prototype._detach.call(this);
1336
- for (var _step5, _iterator5 = _createForOfIteratorHelperLoose(this._map.values()); !(_step5 = _iterator5()).done; ) {
1337
- var value = _step5.value;
1338
- isLiveNode(value) && value._detach();
1087
+ for (var _step6, _iterator6 = _createForOfIteratorHelperLoose(this._map.values()); !(_step6 = _iterator6()).done; ) {
1088
+ var value = _step6.value;
1089
+ isCrdt(value) && value._detach();
1339
1090
  }
1340
1091
  }, _proto._apply = function(op, isLocal) {
1341
1092
  return op.type === exports.OpCode.UPDATE_OBJECT ? this._applyUpdate(op, isLocal) : op.type === exports.OpCode.DELETE_OBJECT_KEY ? this._applyDeleteObjectKey(op) : _AbstractCrdt.prototype._apply.call(this, op, isLocal);
1342
1093
  }, _proto._toSerializedCrdt = function() {
1343
- for (var _step6, data = {}, _iterator6 = _createForOfIteratorHelperLoose(this._map); !(_step6 = _iterator6()).done; ) {
1344
- var _step6$value = _step6.value, key = _step6$value[0], value = _step6$value[1];
1345
- isLiveNode(value) || (data[key] = value);
1094
+ for (var _this$_parent, _step7, data = {}, _iterator7 = _createForOfIteratorHelperLoose(this._map); !(_step7 = _iterator7()).done; ) {
1095
+ var _step7$value = _step7.value, key = _step7$value[0], value = _step7$value[1];
1096
+ value instanceof AbstractCrdt == !1 && (data[key] = value);
1346
1097
  }
1347
- return "HasParent" === this.parent.type && this.parent.node._id ? {
1098
+ return void 0 !== (null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id) && void 0 !== this._parentKey ? {
1348
1099
  type: exports.CrdtType.OBJECT,
1349
- parentId: this.parent.node._id,
1350
- parentKey: this.parent.key,
1100
+ parentId: this._parent._id,
1101
+ parentKey: this._parentKey,
1351
1102
  data: data
1352
1103
  } : {
1353
1104
  type: exports.CrdtType.OBJECT,
1354
1105
  data: data
1355
1106
  };
1356
1107
  }, _proto._applyUpdate = function(op, isLocal) {
1357
- var isModified = !1, id = nn(this._id), reverse = [], reverseUpdate = {
1108
+ var isModified = !1, reverse = [], reverseUpdate = {
1358
1109
  type: exports.OpCode.UPDATE_OBJECT,
1359
- id: id,
1110
+ id: this._id,
1360
1111
  data: {}
1361
1112
  };
1362
1113
  for (var key in reverse.push(reverseUpdate), op.data) {
1363
1114
  var oldValue = this._map.get(key);
1364
- isLiveNode(oldValue) ? (reverse.push.apply(reverse, oldValue._serialize(id, key)),
1115
+ oldValue instanceof AbstractCrdt ? (reverse.push.apply(reverse, oldValue._serialize(this._id, key)),
1365
1116
  oldValue._detach()) : void 0 !== oldValue ? reverseUpdate.data[key] = oldValue : void 0 === oldValue && reverse.push({
1366
1117
  type: exports.OpCode.DELETE_OBJECT_KEY,
1367
- id: id,
1118
+ id: this._id,
1368
1119
  key: key
1369
1120
  });
1370
1121
  }
1371
1122
  var updateDelta = {};
1372
1123
  for (var _key2 in op.data) {
1373
- if (isLocal) this._propToLastUpdate.set(_key2, nn(op.opId)); else {
1124
+ if (isLocal) this._propToLastUpdate.set(_key2, op.opId); else {
1374
1125
  if (null != this._propToLastUpdate.get(_key2)) {
1375
1126
  if (this._propToLastUpdate.get(_key2) === op.opId) {
1376
1127
  this._propToLastUpdate.delete(_key2);
@@ -1381,7 +1132,7 @@ var LiveObject = function(_AbstractCrdt) {
1381
1132
  isModified = !0;
1382
1133
  }
1383
1134
  var _oldValue = this._map.get(_key2);
1384
- isLiveNode(_oldValue) && _oldValue._detach(), isModified = !0, updateDelta[_key2] = {
1135
+ isCrdt(_oldValue) && _oldValue._detach(), isModified = !0, updateDelta[_key2] = {
1385
1136
  type: "update"
1386
1137
  }, this._map.set(_key2, op.data[_key2]);
1387
1138
  }
@@ -1404,12 +1155,12 @@ var LiveObject = function(_AbstractCrdt) {
1404
1155
  if (void 0 !== this._propToLastUpdate.get(key)) return {
1405
1156
  modified: !1
1406
1157
  };
1407
- var oldValue = this._map.get(key), id = nn(this._id), reverse = [];
1408
- if (isLiveNode(oldValue)) reverse = oldValue._serialize(id, op.key), oldValue._detach(); else if (void 0 !== oldValue) {
1158
+ var oldValue = this._map.get(key), reverse = [];
1159
+ if (isCrdt(oldValue)) reverse = oldValue._serialize(this._id, op.key), oldValue._detach(); else if (void 0 !== oldValue) {
1409
1160
  var _data2;
1410
1161
  reverse = [ {
1411
1162
  type: exports.OpCode.UPDATE_OBJECT,
1412
- id: id,
1163
+ id: this._id,
1413
1164
  data: (_data2 = {}, _data2[key] = oldValue, _data2)
1414
1165
  } ];
1415
1166
  }
@@ -1439,10 +1190,10 @@ var LiveObject = function(_AbstractCrdt) {
1439
1190
  }, _proto.delete = function(key) {
1440
1191
  var _updates4, keyAsString = key, oldValue = this._map.get(keyAsString);
1441
1192
  if (void 0 !== oldValue) {
1442
- if (null == this._doc || null == this._id) return isLiveNode(oldValue) && oldValue._detach(),
1193
+ if (null == this._doc || null == this._id) return oldValue instanceof AbstractCrdt && oldValue._detach(),
1443
1194
  void this._map.delete(keyAsString);
1444
1195
  var reverse, _data3;
1445
- if (isLiveNode(oldValue)) oldValue._detach(), reverse = oldValue._serialize(this._id, keyAsString); else reverse = [ {
1196
+ if (oldValue instanceof AbstractCrdt) oldValue._detach(), reverse = oldValue._serialize(this._id, keyAsString); else reverse = [ {
1446
1197
  type: exports.OpCode.UPDATE_OBJECT,
1447
1198
  data: (_data3 = {}, _data3[keyAsString] = oldValue, _data3),
1448
1199
  id: this._id
@@ -1472,19 +1223,19 @@ var LiveObject = function(_AbstractCrdt) {
1472
1223
  }, updateDelta = {};
1473
1224
  for (var _key3 in overrides) {
1474
1225
  var _oldValue2 = this._map.get(_key3);
1475
- isLiveNode(_oldValue2) ? (reverseOps.push.apply(reverseOps, _oldValue2._serialize(this._id, _key3)),
1226
+ _oldValue2 instanceof AbstractCrdt ? (reverseOps.push.apply(reverseOps, _oldValue2._serialize(this._id, _key3)),
1476
1227
  _oldValue2._detach()) : void 0 === _oldValue2 ? reverseOps.push({
1477
1228
  type: exports.OpCode.DELETE_OBJECT_KEY,
1478
1229
  id: this._id,
1479
1230
  key: _key3
1480
1231
  }) : reverseUpdateOp.data[_key3] = _oldValue2;
1481
1232
  var _newValue = overrides[_key3];
1482
- if (isLiveNode(_newValue)) {
1233
+ if (_newValue instanceof AbstractCrdt) {
1483
1234
  _newValue._setParentLink(this, _key3), _newValue._attach(this._doc.generateId(), this._doc);
1484
1235
  var newAttachChildOps = _newValue._serialize(this._id, _key3, this._doc), createCrdtOp = newAttachChildOps.find((function(op) {
1485
1236
  return op.parentId === _this2._id;
1486
1237
  }));
1487
- createCrdtOp && this._propToLastUpdate.set(_key3, nn(createCrdtOp.opId)), ops.push.apply(ops, newAttachChildOps);
1238
+ createCrdtOp && this._propToLastUpdate.set(_key3, createCrdtOp.opId), ops.push.apply(ops, newAttachChildOps);
1488
1239
  } else updatedProps[_key3] = _newValue, this._propToLastUpdate.set(_key3, opId);
1489
1240
  this._map.set(_key3, _newValue), updateDelta[_key3] = {
1490
1241
  type: "update"
@@ -1505,22 +1256,23 @@ var LiveObject = function(_AbstractCrdt) {
1505
1256
  }), this._doc.dispatch(ops, reverseOps, storageUpdates);
1506
1257
  } else for (var key in overrides) {
1507
1258
  var oldValue = this._map.get(key);
1508
- isLiveNode(oldValue) && oldValue._detach();
1259
+ oldValue instanceof AbstractCrdt && oldValue._detach();
1509
1260
  var newValue = overrides[key];
1510
- isLiveNode(newValue) && newValue._setParentLink(this, key), this._map.set(key, newValue);
1261
+ newValue instanceof AbstractCrdt && newValue._setParentLink(this, key), this._map.set(key, newValue);
1511
1262
  }
1512
1263
  }, LiveObject;
1513
1264
  }(AbstractCrdt);
1514
1265
 
1515
- exports.LiveList = LiveList, exports.LiveMap = LiveMap, exports.LiveObject = LiveObject,
1516
- exports.LiveRegister = LiveRegister, exports._createForOfIteratorHelperLoose = _createForOfIteratorHelperLoose,
1266
+ exports.AbstractCrdt = AbstractCrdt, exports.LiveList = LiveList, exports.LiveMap = LiveMap,
1267
+ exports.LiveObject = LiveObject, exports.LiveRegister = LiveRegister, exports._createForOfIteratorHelperLoose = _createForOfIteratorHelperLoose,
1517
1268
  exports._extends = _extends, exports._inheritsLoose = _inheritsLoose, exports._objectWithoutPropertiesLoose = function(source, excluded) {
1518
1269
  if (null == source) return {};
1519
1270
  var key, i, target = {}, sourceKeys = Object.keys(source);
1520
1271
  for (i = 0; i < sourceKeys.length; i++) key = sourceKeys[i], excluded.indexOf(key) >= 0 || (target[key] = source[key]);
1521
1272
  return target;
1522
- }, exports._wrapNativeSuper = _wrapNativeSuper, exports.assertNever = assertNever,
1523
- exports.b64decode = function(b64value) {
1273
+ }, exports._wrapNativeSuper = _wrapNativeSuper, exports.assertNever = function(_value, errmsg) {
1274
+ throw new Error(errmsg);
1275
+ }, exports.b64decode = function(b64value) {
1524
1276
  try {
1525
1277
  var formattedValue = b64value.replace(/-/g, "+").replace(/_/g, "/");
1526
1278
  return decodeURIComponent(atob(formattedValue).split("").map((function(c) {
@@ -1533,9 +1285,10 @@ exports.b64decode = function(b64value) {
1533
1285
  return items.filter((function(item) {
1534
1286
  return null != item;
1535
1287
  }));
1536
- }, exports.comparePosition = comparePosition, exports.deprecate = deprecate, exports.deprecateIf = function(condition, message, key) {
1537
- void 0 === key && (key = message), "production" !== process.env.NODE_ENV && condition && deprecate(message, key);
1538
- }, exports.errorIf = errorIf, exports.findNonSerializableValue = function findNonSerializableValue(value, path) {
1288
+ }, exports.comparePosition = comparePosition, exports.deprecate = deprecate, exports.deprecateIf = deprecateIf,
1289
+ exports.errorIf = function(condition, message) {
1290
+ "production" !== process.env.NODE_ENV && condition && throwUsageError(message);
1291
+ }, exports.findNonSerializableValue = function findNonSerializableValue(value, path) {
1539
1292
  if (void 0 === path && (path = ""), !isPlain) return {
1540
1293
  path: path || "root",
1541
1294
  value: value
@@ -1569,7 +1322,7 @@ exports.b64decode = function(b64value) {
1569
1322
  })), crdt.parentKey !== currentCrdt.parentKey && ops.push({
1570
1323
  type: exports.OpCode.SET_PARENT_KEY,
1571
1324
  id: id,
1572
- parentKey: nn(crdt.parentKey, "Parent key must not be missing")
1325
+ parentKey: crdt.parentKey
1573
1326
  }); else switch (crdt.type) {
1574
1327
  case exports.CrdtType.REGISTER:
1575
1328
  ops.push({
@@ -1614,11 +1367,10 @@ exports.b64decode = function(b64value) {
1614
1367
  }
1615
1368
  })), ops;
1616
1369
  }, exports.isChildCrdt = isChildCrdt, exports.isJsonArray = isJsonArray, exports.isJsonObject = isJsonObject,
1617
- exports.isLiveList = isLiveList, exports.isLiveNode = isLiveNode, exports.isLiveObject = isLiveObject,
1618
- exports.isRootCrdt = function(crdt) {
1370
+ exports.isJsonScalar = isJsonScalar, exports.isPlainObject = isPlainObject, exports.isRootCrdt = function(crdt) {
1619
1371
  return crdt.type === exports.CrdtType.OBJECT && !isChildCrdt(crdt);
1620
1372
  }, exports.isSameNodeOrChildOf = function isSameNodeOrChildOf(node, parent) {
1621
- return node === parent || "HasParent" === node.parent.type && isSameNodeOrChildOf(node.parent.node, parent);
1373
+ return node === parent || !!node._parent && isSameNodeOrChildOf(node._parent, parent);
1622
1374
  }, exports.isTokenValid = function(token) {
1623
1375
  var tokenParts = token.split(".");
1624
1376
  if (3 !== tokenParts.length) return !1;
@@ -1646,7 +1398,7 @@ exports.isRootCrdt = function(crdt) {
1646
1398
  updates: first.updates.concat(second.updates)
1647
1399
  });
1648
1400
  }(first, second) : second : second;
1649
- }, exports.nn = nn, exports.remove = function(array, item) {
1401
+ }, exports.remove = function(array, item) {
1650
1402
  for (var i = 0; i < array.length; i++) if (array[i] === item) {
1651
1403
  array.splice(i, 1);
1652
1404
  break;