@liveblocks/client 0.16.13 → 0.16.14

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,27 @@ 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
+ }();
302
199
 
303
- case "NoParent":
304
- return null;
200
+ function isJsonArray(data) {
201
+ return Array.isArray(data);
202
+ }
305
203
 
306
- case "Orphaned":
307
- return this.parent.oldKey;
204
+ function isJsonObject(data) {
205
+ return null !== data && "object" == typeof data && !isJsonArray(data);
206
+ }
308
207
 
309
- default:
310
- return assertNever(this.parent, "Unknown state");
311
- }
312
- }
313
- } ]), AbstractCrdt;
314
- }(), LiveRegister = function(_AbstractCrdt) {
208
+ var _Symbol$iterator$1, _Symbol$iterator2, LiveRegister = function(_AbstractCrdt) {
315
209
  function LiveRegister(data) {
316
210
  var _this;
317
211
  return (_this = _AbstractCrdt.call(this) || this)._data = void 0, _this._data = data,
@@ -322,25 +216,26 @@ var _Symbol$iterator$1, _Symbol$iterator2, AbstractCrdt = function() {
322
216
  return register._attach(id, doc), register;
323
217
  };
324
218
  var _proto = LiveRegister.prototype;
325
- return _proto._serialize = function(parentId, parentKey, doc) {
219
+ return _proto._serialize = function(parentId, parentKey, doc, intent) {
326
220
  if (null == this._id || null == parentId || null == parentKey) throw new Error("Cannot serialize register if parentId or parentKey is undefined");
327
221
  return [ {
328
222
  type: exports.OpCode.CREATE_REGISTER,
329
223
  opId: null == doc ? void 0 : doc.generateOpId(),
330
224
  id: this._id,
225
+ intent: intent,
331
226
  parentId: parentId,
332
227
  parentKey: parentKey,
333
228
  data: this.data
334
229
  } ];
335
230
  }, _proto._toSerializedCrdt = function() {
336
- if ("HasParent" !== this.parent.type) throw new Error("Cannot serialize LiveRegister if parent is missing");
231
+ var _this$_parent;
337
232
  return {
338
233
  type: exports.CrdtType.REGISTER,
339
- parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
340
- parentKey: this.parent.key,
234
+ parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
235
+ parentKey: this._parentKey,
341
236
  data: this.data
342
237
  };
343
- }, _proto._attachChild = function(_op) {
238
+ }, _proto._attachChild = function(_op, _isLocal) {
344
239
  throw new Error("Method not implemented.");
345
240
  }, _proto._detachChild = function(_crdt) {
346
241
  throw new Error("Method not implemented.");
@@ -355,7 +250,7 @@ var _Symbol$iterator$1, _Symbol$iterator2, AbstractCrdt = function() {
355
250
  }(AbstractCrdt);
356
251
 
357
252
  function makePosition(before, after) {
358
- return null != before && null != after ? pos(makePositionFromCodes(posCodes(before), posCodes(after))) : null != before ? function(before) {
253
+ return null == before && null == after ? pos([ 33 ]) : null != before && null == after ? function(before) {
359
254
  for (var result = [], beforeCodes = posCodes(before), i = 0; i < beforeCodes.length; i++) {
360
255
  var code = beforeCodes[i];
361
256
  if (126 !== code) {
@@ -368,7 +263,7 @@ function makePosition(before, after) {
368
263
  }
369
264
  }
370
265
  return pos(result);
371
- }(before) : null != after ? function(after) {
266
+ }(before) : null == before && null != after ? function(after) {
372
267
  for (var result = [], afterCodes = posCodes(after), i = 0; i < afterCodes.length; i++) {
373
268
  var code = afterCodes[i];
374
269
  if (!(code <= 33)) {
@@ -381,7 +276,7 @@ function makePosition(before, after) {
381
276
  }
382
277
  }
383
278
  return pos(result);
384
- }(after) : pos([ 33 ]);
279
+ }(after) : pos(makePositionFromCodes(posCodes(before), posCodes(after)));
385
280
  }
386
281
 
387
282
  function makePositionFromCodes(before, after) {
@@ -425,322 +320,206 @@ var LiveList = function(_AbstractCrdt) {
425
320
  function LiveList(items) {
426
321
  var _this;
427
322
  void 0 === items && (items = []), (_this = _AbstractCrdt.call(this) || this)._items = void 0,
428
- _this._implicitlyDeletedItems = void 0, _this._items = [], _this._implicitlyDeletedItems = new Set;
323
+ _this._items = [];
429
324
  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;
325
+ var newPosition = makePosition(position), item = selfOrRegister(items[i]);
326
+ _this._items.push([ item, newPosition ]), position = newPosition;
433
327
  }
434
328
  return _this;
435
329
  }
436
330
  _inheritsLoose(LiveList, _AbstractCrdt), LiveList._deserialize = function(_ref, parentToChildren, doc) {
437
- var id = _ref[0], list = new LiveList;
331
+ var id = _ref[0], list = new LiveList([]);
438
332
  list._attach(id, doc);
439
333
  var children = parentToChildren.get(id);
440
334
  if (null == children) return list;
441
335
  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);
336
+ var entry = _step.value, child = deserialize(entry, parentToChildren, doc);
337
+ child._setParentLink(list, entry[1].parentKey), list._items.push([ child, entry[1].parentKey ]),
338
+ list._items.sort((function(itemA, itemB) {
339
+ return comparePosition(itemA[1], itemB[1]);
340
+ }));
444
341
  }
445
342
  return list;
446
343
  };
447
344
  var _proto = LiveList.prototype;
448
- return _proto._serialize = function(parentId, parentKey, doc) {
345
+ return _proto._serialize = function(parentId, parentKey, doc, intent) {
449
346
  if (null == this._id) throw new Error("Cannot serialize item is not attached");
347
+ if (null == parentId || null == parentKey) throw new Error("Cannot serialize list if parentId or parentKey is undefined");
450
348
  var ops = [], op = {
451
349
  id: this._id,
452
350
  opId: null == doc ? void 0 : doc.generateOpId(),
351
+ intent: intent,
453
352
  type: exports.OpCode.CREATE_LIST,
454
353
  parentId: parentId,
455
354
  parentKey: parentKey
456
355
  };
457
356
  ops.push(op);
458
357
  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));
358
+ var _step2$value = _step2.value, _value = _step2$value[0], key = _step2$value[1];
359
+ ops.push.apply(ops, _value._serialize(this._id, key, doc));
461
360
  }
462
361
  return ops;
463
362
  }, _proto._indexOfPosition = function(position) {
464
363
  return this._items.findIndex((function(item) {
465
- return item._getParentKeyOrThrow() === position;
364
+ return item[1] === position;
466
365
  }));
467
366
  }, _proto._attach = function(id, doc) {
468
367
  _AbstractCrdt.prototype._attach.call(this, id, doc);
469
368
  for (var _step3, _iterator3 = _createForOfIteratorHelperLoose(this._items); !(_step3 = _iterator3()).done; ) {
470
- _step3.value._attach(doc.generateId(), doc);
369
+ _step3.value[0]._attach(doc.generateId(), doc);
471
370
  }
472
371
  }, _proto._detach = function() {
473
372
  _AbstractCrdt.prototype._detach.call(this);
474
373
  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
- };
495
- }
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
- };
374
+ _step4.value[0]._detach();
535
375
  }
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) {
376
+ }, _proto._attachChild = function(op, isLocal) {
548
377
  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 {
378
+ var id = op.id, parentKey = op.parentKey, intent = op.intent, key = parentKey, child = creationOpToLiveStructure(op);
379
+ if (void 0 !== this._doc.getItem(id)) return {
607
380
  modified: !1
608
381
  };
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
- };
382
+ child._attach(id, this._doc), child._setParentLink(this, key);
383
+ var index = this._items.findIndex((function(entry) {
384
+ return entry[1] === key;
385
+ })), newKey = key;
386
+ if (-1 !== index) {
387
+ if ("set" === intent) {
388
+ var existingItem = this._items[index][0];
389
+ existingItem._detach();
390
+ var storageUpdate = {
391
+ node: this,
392
+ type: "LiveList",
393
+ updates: [ {
394
+ index: index,
395
+ type: "set",
396
+ item: child instanceof LiveRegister ? child.data : child
397
+ } ]
398
+ };
399
+ return this._items[index][0] = child, {
400
+ modified: storageUpdate,
401
+ reverse: existingItem._serialize(this._id, key, this._doc, "set")
402
+ };
403
+ }
404
+ if (isLocal) {
405
+ var before = this._items[index] ? this._items[index][1] : void 0, after = this._items[index + 1] ? this._items[index + 1][1] : void 0;
406
+ newKey = makePosition(before, after), child._setParentLink(this, newKey);
407
+ } else {
408
+ var _this$_items;
409
+ this._items[index][1] = makePosition(key, null == (_this$_items = this._items[index + 1]) ? void 0 : _this$_items[1]);
410
+ }
622
411
  }
623
- this._items.push(child), sortListItem(this._items), this._detachItemAssociatedToSetOperation(op.deletedId);
624
- var newIndex = this._indexOfPosition(newKey);
412
+ this._items.push([ child, newKey ]), this._items.sort((function(itemA, itemB) {
413
+ return comparePosition(itemA[1], itemB[1]);
414
+ }));
415
+ var newIndex = this._items.findIndex((function(entry) {
416
+ return entry[1] === newKey;
417
+ }));
625
418
  return {
626
419
  reverse: [ {
627
420
  type: exports.OpCode.DELETE_CRDT,
628
421
  id: id
629
422
  } ],
630
- modified: makeUpdate(this, [ insertDelta(newIndex, child) ])
423
+ modified: {
424
+ node: this,
425
+ type: "LiveList",
426
+ updates: [ {
427
+ index: newIndex,
428
+ type: "insert",
429
+ item: child instanceof LiveRegister ? child.data : child
430
+ } ]
431
+ }
631
432
  };
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
433
  }, _proto._detachChild = function(child) {
641
434
  if (child) {
642
- var parentKey = nn(child._parentKey), reverse = child._serialize(nn(this._id), parentKey, this._doc), indexToDelete = this._items.indexOf(child);
435
+ var reverse = child._serialize(this._id, child._parentKey, this._doc), indexToDelete = this._items.findIndex((function(item) {
436
+ return item[0] === child;
437
+ }));
643
438
  return this._items.splice(indexToDelete, 1), child._detach(), {
644
- modified: makeUpdate(this, [ deleteDelta(indexToDelete) ]),
439
+ modified: {
440
+ node: this,
441
+ type: "LiveList",
442
+ updates: [ {
443
+ index: indexToDelete,
444
+ type: "delete"
445
+ } ]
446
+ },
645
447
  reverse: reverse
646
448
  };
647
449
  }
648
450
  return {
649
451
  modified: !1
650
452
  };
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) ]),
453
+ }, _proto._setChildKey = function(key, child, previousKey) {
454
+ child._setParentLink(this, key);
455
+ var _this$_items2, previousIndex = this._items.findIndex((function(entry) {
456
+ return entry[0]._id === child._id;
457
+ })), index = this._items.findIndex((function(entry) {
458
+ return entry[1] === key;
459
+ }));
460
+ -1 !== index && (this._items[index][1] = makePosition(key, null == (_this$_items2 = this._items[index + 1]) ? void 0 : _this$_items2[1]));
461
+ var item = this._items.find((function(item) {
462
+ return item[0] === child;
463
+ }));
464
+ item && (item[1] = key), this._items.sort((function(itemA, itemB) {
465
+ return comparePosition(itemA[1], itemB[1]);
466
+ }));
467
+ var newIndex = this._items.findIndex((function(entry) {
468
+ return entry[0]._id === child._id;
469
+ }));
470
+ return {
471
+ modified: {
472
+ node: this,
473
+ type: "LiveList",
474
+ updates: newIndex === previousIndex ? [] : [ {
475
+ index: newIndex,
476
+ item: child instanceof LiveRegister ? child.data : child,
477
+ previousIndex: previousIndex,
478
+ type: "move"
479
+ } ]
480
+ },
715
481
  reverse: [ {
716
482
  type: exports.OpCode.SET_PARENT_KEY,
717
- id: nn(child._id),
483
+ id: null == item ? void 0 : item[0]._id,
718
484
  parentKey: previousKey
719
485
  } ]
720
486
  };
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
487
  }, _proto._apply = function(op, isLocal) {
724
488
  return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
725
489
  }, _proto._toSerializedCrdt = function() {
726
- if ("HasParent" !== this.parent.type) throw new Error("Cannot serialize LiveList if parent is missing");
490
+ var _this$_parent;
727
491
  return {
728
492
  type: exports.CrdtType.LIST,
729
- parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
730
- parentKey: this.parent.key
493
+ parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
494
+ parentKey: this._parentKey
731
495
  };
732
496
  }, _proto.push = function(element) {
733
497
  return this.insert(element, this.length);
734
498
  }, _proto.insert = function(element, index) {
735
499
  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) {
500
+ 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);
501
+ value._setParentLink(this, position), this._items.push([ value, position ]), this._items.sort((function(itemA, itemB) {
502
+ return comparePosition(itemA[1], itemB[1]);
503
+ }));
504
+ var newIndex = this._items.findIndex((function(entry) {
505
+ return entry[1] === position;
506
+ }));
507
+ if (this._doc && this._id) {
739
508
  var id = this._doc.generateId();
740
- value._attach(id, this._doc), this._doc.dispatch(value._serialize(this._id, position, this._doc), [ {
509
+ value._attach(id, this._doc);
510
+ var storageUpdates = new Map;
511
+ storageUpdates.set(this._id, {
512
+ node: this,
513
+ type: "LiveList",
514
+ updates: [ {
515
+ index: newIndex,
516
+ item: value instanceof LiveRegister ? value.data : value,
517
+ type: "insert"
518
+ } ]
519
+ }), this._doc.dispatch(value._serialize(this._id, position, this._doc), [ {
741
520
  type: exports.OpCode.DELETE_CRDT,
742
521
  id: id
743
- } ], new Map([ [ this._id, makeUpdate(this, [ insertDelta(index, value) ]) ] ]));
522
+ } ], storageUpdates);
744
523
  }
745
524
  }, _proto.move = function(index, targetIndex) {
746
525
  if (targetIndex < 0) throw new Error("targetIndex cannot be less than 0");
@@ -748,77 +527,108 @@ var LiveList = function(_AbstractCrdt) {
748
527
  if (index < 0) throw new Error("index cannot be less than 0");
749
528
  if (index >= this._items.length) throw new Error("index cannot be greater or equal than the list length");
750
529
  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([ {
530
+ index < targetIndex ? (afterPosition = targetIndex === this._items.length - 1 ? void 0 : this._items[targetIndex + 1][1],
531
+ beforePosition = this._items[targetIndex][1]) : (afterPosition = this._items[targetIndex][1],
532
+ beforePosition = 0 === targetIndex ? void 0 : this._items[targetIndex - 1][1]);
533
+ var position = makePosition(beforePosition, afterPosition), item = this._items[index], previousPosition = item[1];
534
+ item[1] = position, item[0]._setParentLink(this, position), this._items.sort((function(itemA, itemB) {
535
+ return comparePosition(itemA[1], itemB[1]);
536
+ }));
537
+ var newIndex = this._items.findIndex((function(entry) {
538
+ return entry[1] === position;
539
+ }));
540
+ if (this._doc && this._id) {
541
+ var storageUpdates = new Map;
542
+ storageUpdates.set(this._id, {
543
+ node: this,
544
+ type: "LiveList",
545
+ updates: [ {
546
+ index: newIndex,
547
+ previousIndex: index,
548
+ item: item[0],
549
+ type: "move"
550
+ } ]
551
+ }), this._doc.dispatch([ {
758
552
  type: exports.OpCode.SET_PARENT_KEY,
759
- id: nn(item._id),
553
+ id: item[0]._id,
760
554
  opId: this._doc.generateOpId(),
761
555
  parentKey: position
762
556
  } ], [ {
763
557
  type: exports.OpCode.SET_PARENT_KEY,
764
- id: nn(item._id),
558
+ id: item[0]._id,
765
559
  parentKey: previousPosition
766
560
  } ], storageUpdates);
767
561
  }
768
562
  }, _proto.delete = function(index) {
769
563
  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
564
  var item = this._items[index];
771
- if (item._detach(), this._items.splice(index, 1), this._doc) {
772
- var childRecordId = item._id;
565
+ if (item[0]._detach(), this._items.splice(index, 1), this._doc) {
566
+ var childRecordId = item[0]._id;
773
567
  if (childRecordId) {
774
568
  var storageUpdates = new Map;
775
- storageUpdates.set(nn(this._id), makeUpdate(this, [ deleteDelta(index) ])), this._doc.dispatch([ {
569
+ storageUpdates.set(this._id, {
570
+ node: this,
571
+ type: "LiveList",
572
+ updates: [ {
573
+ index: index,
574
+ type: "delete"
575
+ } ]
576
+ }), this._doc.dispatch([ {
776
577
  id: childRecordId,
777
578
  opId: this._doc.generateOpId(),
778
579
  type: exports.OpCode.DELETE_CRDT
779
- } ], item._serialize(nn(this._id), item._getParentKeyOrThrow()), storageUpdates);
580
+ } ], item[0]._serialize(this._id, item[1]), storageUpdates);
780
581
  }
781
582
  }
782
583
  }, _proto.clear = function() {
783
584
  if (this._doc) {
784
585
  for (var _step5, ops = [], reverseOps = [], updateDelta = [], i = 0, _iterator5 = _createForOfIteratorHelperLoose(this._items); !(_step5 = _iterator5()).done; ) {
785
586
  var item = _step5.value;
786
- item._detach();
787
- var childId = item._id;
587
+ item[0]._detach();
588
+ var childId = item[0]._id;
788
589
  childId && (ops.push({
789
- type: exports.OpCode.DELETE_CRDT,
790
590
  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++;
591
+ type: exports.OpCode.DELETE_CRDT
592
+ }), reverseOps.push.apply(reverseOps, item[0]._serialize(this._id, item[1])), updateDelta.push({
593
+ index: i,
594
+ type: "delete"
595
+ })), i++;
794
596
  }
795
597
  this._items = [];
796
598
  var storageUpdates = new Map;
797
- storageUpdates.set(nn(this._id), makeUpdate(this, updateDelta)), this._doc.dispatch(ops, reverseOps, storageUpdates);
599
+ storageUpdates.set(this._id, {
600
+ node: this,
601
+ type: "LiveList",
602
+ updates: updateDelta
603
+ }), this._doc.dispatch(ops, reverseOps, storageUpdates);
798
604
  } else {
799
605
  for (var _step6, _iterator6 = _createForOfIteratorHelperLoose(this._items); !(_step6 = _iterator6()).done; ) {
800
- _step6.value._detach();
606
+ _step6.value[0]._detach();
801
607
  }
802
608
  this._items = [];
803
609
  }
804
610
  }, _proto.set = function(index, item) {
805
611
  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;
612
+ var _this$_items$index = this._items[index], existingItem = _this$_items$index[0], position = _this$_items$index[1];
807
613
  existingItem._detach();
808
- var value = lsonToLiveNode(item);
809
- if (value._setParentLink(this, position), this._items[index] = value, this._doc && this._id) {
614
+ var value = selfOrRegister(item);
615
+ if (value._setParentLink(this, position), this._items[index][0] = value, this._doc && this._id) {
810
616
  var id = this._doc.generateId();
811
617
  value._attach(id, this._doc);
812
618
  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);
619
+ storageUpdates.set(this._id, {
620
+ node: this,
621
+ type: "LiveList",
622
+ updates: [ {
623
+ index: index,
624
+ item: value instanceof LiveRegister ? value.data : value,
625
+ type: "set"
626
+ } ]
627
+ }), this._doc.dispatch(value._serialize(this._id, position, this._doc, "set"), existingItem._serialize(this._id, position, void 0, "set"), storageUpdates);
818
628
  }
819
629
  }, _proto.toArray = function() {
820
630
  return this._items.map((function(entry) {
821
- return liveNodeToLson(entry);
631
+ return selfOrRegisterValue(entry[0]);
822
632
  }));
823
633
  }, _proto.every = function(predicate) {
824
634
  return this.toArray().every(predicate);
@@ -831,29 +641,19 @@ var LiveList = function(_AbstractCrdt) {
831
641
  }, _proto.forEach = function(callbackfn) {
832
642
  return this.toArray().forEach(callbackfn);
833
643
  }, _proto.get = function(index) {
834
- if (!(index < 0 || index >= this._items.length)) return liveNodeToLson(this._items[index]);
644
+ if (!(index < 0 || index >= this._items.length)) return selfOrRegisterValue(this._items[index][0]);
835
645
  }, _proto.indexOf = function(searchElement, fromIndex) {
836
646
  return this.toArray().indexOf(searchElement, fromIndex);
837
647
  }, _proto.lastIndexOf = function(searchElement, fromIndex) {
838
648
  return this.toArray().lastIndexOf(searchElement, fromIndex);
839
649
  }, _proto.map = function(callback) {
840
650
  return this._items.map((function(entry, i) {
841
- return callback(liveNodeToLson(entry), i);
651
+ return callback(selfOrRegisterValue(entry[0]), i);
842
652
  }));
843
653
  }, _proto.some = function(predicate) {
844
654
  return this.toArray().some(predicate);
845
655
  }, _proto[_Symbol$iterator$1] = function() {
846
656
  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
657
  }, _createClass(LiveList, [ {
858
658
  key: "length",
859
659
  get: function() {
@@ -877,62 +677,25 @@ var _Symbol$iterator, LiveListIterator = function() {
877
677
  done: !0,
878
678
  value: void 0
879
679
  } : {
880
- value: liveNodeToLson(result.value)
680
+ value: selfOrRegisterValue(result.value[0])
881
681
  };
882
682
  }, 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
- }
683
+ }(), _emittedDeprecationWarnings = new Set;
900
684
 
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
- };
685
+ function deprecate(message, key) {
686
+ void 0 === key && (key = message), "production" !== process.env.NODE_ENV && (_emittedDeprecationWarnings.has(key) || (_emittedDeprecationWarnings.add(key),
687
+ console.error("DEPRECATION WARNING: " + message)));
923
688
  }
924
689
 
925
- function sortListItem(items) {
926
- items.sort((function(itemA, itemB) {
927
- return comparePosition(itemA._getParentKeyOrThrow(), itemB._getParentKeyOrThrow());
928
- }));
690
+ function deprecateIf(condition, message, key) {
691
+ void 0 === key && (key = message), "production" !== process.env.NODE_ENV && condition && deprecate(message, key);
929
692
  }
930
693
 
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;
694
+ function throwUsageError(message) {
695
+ if ("production" !== process.env.NODE_ENV) {
696
+ var usageError = new Error(message);
697
+ throw usageError.name = "Usage error", usageError;
698
+ }
936
699
  }
937
700
 
938
701
  _Symbol$iterator = Symbol.iterator;
@@ -940,10 +703,10 @@ _Symbol$iterator = Symbol.iterator;
940
703
  var LiveMap = function(_AbstractCrdt) {
941
704
  function LiveMap(entries) {
942
705
  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([])`."),
706
+ 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
707
  entries) {
945
708
  for (var _step, mappedEntries = [], _iterator = _createForOfIteratorHelperLoose(entries); !(_step = _iterator()).done; ) {
946
- var entry = _step.value, _value = lsonToLiveNode(entry[1]);
709
+ var entry = _step.value, _value = selfOrRegister(entry[1]);
947
710
  _value._setParentLink(_assertThisInitialized(_this), entry[0]), mappedEntries.push([ entry[0], _value ]);
948
711
  }
949
712
  _this._map = new Map(mappedEntries);
@@ -952,12 +715,14 @@ var LiveMap = function(_AbstractCrdt) {
952
715
  }
953
716
  _inheritsLoose(LiveMap, _AbstractCrdt);
954
717
  var _proto = LiveMap.prototype;
955
- return _proto._serialize = function(parentId, parentKey, doc) {
718
+ return _proto._serialize = function(parentId, parentKey, doc, intent) {
956
719
  if (null == this._id) throw new Error("Cannot serialize item is not attached");
720
+ if (null == parentId || null == parentKey) throw new Error("Cannot serialize map if parentId or parentKey is undefined");
957
721
  var ops = [], op = {
958
722
  id: this._id,
959
723
  opId: null == doc ? void 0 : doc.generateOpId(),
960
724
  type: exports.OpCode.CREATE_MAP,
725
+ intent: intent,
961
726
  parentId: parentId,
962
727
  parentKey: parentKey
963
728
  };
@@ -975,7 +740,9 @@ var LiveMap = function(_AbstractCrdt) {
975
740
  var children = parentToChildren.get(id);
976
741
  if (null == children) return map;
977
742
  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);
743
+ var entry = _step3.value, crdt = entry[1];
744
+ if (null == crdt.parentKey) throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
745
+ var child = deserialize(entry, parentToChildren, doc);
979
746
  child._setParentLink(map, crdt.parentKey), map._map.set(crdt.parentKey, child);
980
747
  }
981
748
  return map;
@@ -985,24 +752,20 @@ var LiveMap = function(_AbstractCrdt) {
985
752
  var _step4$value = _step4.value;
986
753
  _step4$value[0];
987
754
  var _value3 = _step4$value[1];
988
- isLiveNode(_value3) && _value3._attach(doc.generateId(), doc);
755
+ isCrdt(_value3) && _value3._attach(doc.generateId(), doc);
989
756
  }
990
- }, _proto._attachChild = function(op) {
757
+ }, _proto._attachChild = function(op, _isLocal) {
991
758
  var _updates;
992
759
  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);
760
+ var id = op.id, key = op.parentKey, child = creationOpToLiveStructure(op);
994
761
  if (void 0 !== this._doc.getItem(id)) return {
995
762
  modified: !1
996
763
  };
997
764
  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 = [ {
765
+ return previousValue ? (reverse = previousValue._serialize(this._id, key), previousValue._detach()) : reverse = [ {
1002
766
  type: exports.OpCode.DELETE_CRDT,
1003
767
  id: id
1004
- } ];
1005
- return child._setParentLink(this, key), child._attach(id, this._doc), this._map.set(key, child),
768
+ } ], child._setParentLink(this, key), child._attach(id, this._doc), this._map.set(key, child),
1006
769
  {
1007
770
  modified: {
1008
771
  node: this,
@@ -1019,7 +782,7 @@ var LiveMap = function(_AbstractCrdt) {
1019
782
  _step5.value._detach();
1020
783
  }
1021
784
  }, _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; ) {
785
+ for (var _updates2, _step6, reverse = child._serialize(this._id, child._parentKey, this._doc), _iterator6 = _createForOfIteratorHelperLoose(this._map); !(_step6 = _iterator6()).done; ) {
1023
786
  var _step6$value = _step6.value, _key3 = _step6$value[0];
1024
787
  _step6$value[1] === child && this._map.delete(_key3);
1025
788
  }
@@ -1027,26 +790,26 @@ var LiveMap = function(_AbstractCrdt) {
1027
790
  modified: {
1028
791
  node: this,
1029
792
  type: "LiveMap",
1030
- updates: (_updates2 = {}, _updates2[parentKey] = {
793
+ updates: (_updates2 = {}, _updates2[child._parentKey] = {
1031
794
  type: "delete"
1032
795
  }, _updates2)
1033
796
  },
1034
797
  reverse: reverse
1035
798
  };
1036
799
  }, _proto._toSerializedCrdt = function() {
1037
- if ("HasParent" !== this.parent.type) throw new Error("Cannot serialize LiveMap if parent is missing");
800
+ var _this$_parent;
1038
801
  return {
1039
802
  type: exports.CrdtType.MAP,
1040
- parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
1041
- parentKey: this.parent.key
803
+ parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
804
+ parentKey: this._parentKey
1042
805
  };
1043
806
  }, _proto.get = function(key) {
1044
807
  var value = this._map.get(key);
1045
- if (null != value) return liveNodeToLson(value);
808
+ if (null != value) return selfOrRegisterValue(value);
1046
809
  }, _proto.set = function(key, value) {
1047
810
  var oldValue = this._map.get(key);
1048
811
  oldValue && oldValue._detach();
1049
- var item = lsonToLiveNode(value);
812
+ var item = selfOrRegister(value);
1050
813
  if (item._setParentLink(this, key), this._map.set(key, item), this._doc && this._id) {
1051
814
  var _updates3, id = this._doc.generateId();
1052
815
  item._attach(id, this._doc);
@@ -1068,8 +831,8 @@ var LiveMap = function(_AbstractCrdt) {
1068
831
  var item = this._map.get(key);
1069
832
  if (null == item) return !1;
1070
833
  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, {
834
+ var _updates4, storageUpdates = new Map;
835
+ storageUpdates.set(this._id, {
1073
836
  node: this,
1074
837
  type: "LiveMap",
1075
838
  updates: (_updates4 = {}, _updates4[key] = {
@@ -1079,7 +842,7 @@ var LiveMap = function(_AbstractCrdt) {
1079
842
  type: exports.OpCode.DELETE_CRDT,
1080
843
  id: item._id,
1081
844
  opId: this._doc.generateOpId()
1082
- } ], item._serialize(thisId, key), storageUpdates);
845
+ } ], item._serialize(this._id, key), storageUpdates);
1083
846
  }
1084
847
  return !0;
1085
848
  }, _proto.entries = function() {
@@ -1092,7 +855,7 @@ var LiveMap = function(_AbstractCrdt) {
1092
855
  done: !0,
1093
856
  value: void 0
1094
857
  } : {
1095
- value: [ iteratorValue.value[0], liveNodeToLson(iteratorValue.value[1]) ]
858
+ value: [ iteratorValue.value[0], selfOrRegisterValue(iteratorValue.value[1]) ]
1096
859
  };
1097
860
  }, _ref2;
1098
861
  }, _proto[_Symbol$iterator] = function() {
@@ -1109,7 +872,7 @@ var LiveMap = function(_AbstractCrdt) {
1109
872
  done: !0,
1110
873
  value: void 0
1111
874
  } : {
1112
- value: liveNodeToLson(iteratorValue.value)
875
+ value: selfOrRegisterValue(iteratorValue.value)
1113
876
  };
1114
877
  }, _ref3;
1115
878
  }, _proto.forEach = function(callback) {
@@ -1125,15 +888,7 @@ var LiveMap = function(_AbstractCrdt) {
1125
888
  } ]), LiveMap;
1126
889
  }(AbstractCrdt);
1127
890
 
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) {
891
+ function creationOpToLiveStructure(op) {
1137
892
  switch (op.type) {
1138
893
  case exports.OpCode.CREATE_REGISTER:
1139
894
  return new LiveRegister(op.data);
@@ -1146,9 +901,6 @@ function creationOpToLiveNode(op) {
1146
901
 
1147
902
  case exports.OpCode.CREATE_LIST:
1148
903
  return new LiveList;
1149
-
1150
- default:
1151
- return assertNever(0, "Unknown creation Op");
1152
904
  }
1153
905
  }
1154
906
 
@@ -1172,28 +924,18 @@ function deserialize(_ref, parentToChildren, doc) {
1172
924
  }
1173
925
  }
1174
926
 
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);
1181
- }
1182
-
1183
- function isLiveList(value) {
1184
- return value instanceof LiveList;
927
+ function isCrdt(obj) {
928
+ return obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList || obj instanceof LiveRegister;
1185
929
  }
1186
930
 
1187
- function isLiveObject(value) {
1188
- return value instanceof LiveObject;
931
+ function selfOrRegisterValue(obj) {
932
+ return obj instanceof LiveRegister ? obj.data : obj;
1189
933
  }
1190
934
 
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);
935
+ function selfOrRegister(obj) {
936
+ if (obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList) return obj;
937
+ if (obj instanceof LiveRegister) throw new Error("Internal error. LiveRegister should not be created from selfOrRegister");
938
+ return new LiveRegister(obj);
1197
939
  }
1198
940
 
1199
941
  function isPlain(value) {
@@ -1226,18 +968,19 @@ var LiveObject = function(_AbstractCrdt) {
1226
968
  for (var key in void 0 === obj && (obj = {}), (_this = _AbstractCrdt.call(this) || this)._map = void 0,
1227
969
  _this._propToLastUpdate = void 0, _this._propToLastUpdate = new Map, obj) {
1228
970
  var value = obj[key];
1229
- isLiveNode(value) && value._setParentLink(_assertThisInitialized(_this), key);
971
+ value instanceof AbstractCrdt && value._setParentLink(_assertThisInitialized(_this), key);
1230
972
  }
1231
973
  return _this._map = new Map(Object.entries(obj)), _this;
1232
974
  }
1233
975
  _inheritsLoose(LiveObject, _AbstractCrdt);
1234
976
  var _proto = LiveObject.prototype;
1235
- return _proto._serialize = function(parentId, parentKey, doc) {
977
+ return _proto._serialize = function(parentId, parentKey, doc, intent) {
1236
978
  if (null == this._id) throw new Error("Cannot serialize item is not attached");
1237
979
  var opId = null == doc ? void 0 : doc.generateOpId(), ops = [], op = void 0 !== parentId && void 0 !== parentKey ? {
1238
980
  type: exports.OpCode.CREATE_OBJECT,
1239
981
  id: this._id,
1240
982
  opId: opId,
983
+ intent: intent,
1241
984
  parentId: parentId,
1242
985
  parentKey: parentKey,
1243
986
  data: {}
@@ -1245,22 +988,25 @@ var LiveObject = function(_AbstractCrdt) {
1245
988
  type: exports.OpCode.CREATE_OBJECT,
1246
989
  id: this._id,
1247
990
  opId: opId,
991
+ intent: intent,
1248
992
  data: {}
1249
993
  };
1250
994
  ops.push(op);
1251
995
  for (var _step, _iterator = _createForOfIteratorHelperLoose(this._map); !(_step = _iterator()).done; ) {
1252
996
  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;
997
+ value instanceof AbstractCrdt ? ops.push.apply(ops, value._serialize(this._id, key, doc)) : op.data[key] = value;
1254
998
  }
1255
999
  return ops;
1256
1000
  }, LiveObject._deserialize = function(_ref, parentToChildren, doc) {
1257
1001
  var id = _ref[0], liveObj = new LiveObject(_ref[1].data);
1258
1002
  return liveObj._attach(id, doc), this._deserializeChildren(liveObj, parentToChildren, doc);
1259
1003
  }, LiveObject._deserializeChildren = function(liveObj, parentToChildren, doc) {
1260
- var children = parentToChildren.get(nn(liveObj._id));
1004
+ var children = parentToChildren.get(liveObj._id);
1261
1005
  if (null == children) return liveObj;
1262
1006
  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);
1007
+ var entry = _step2.value, crdt = entry[1];
1008
+ if (null == crdt.parentKey) throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
1009
+ var child = deserialize(entry, parentToChildren, doc);
1264
1010
  child._setParentLink(liveObj, crdt.parentKey), liveObj._map.set(crdt.parentKey, child);
1265
1011
  }
1266
1012
  return liveObj;
@@ -1270,33 +1016,32 @@ var LiveObject = function(_AbstractCrdt) {
1270
1016
  var _step3$value = _step3.value;
1271
1017
  _step3$value[0];
1272
1018
  var value = _step3$value[1];
1273
- isLiveNode(value) && value._attach(doc.generateId(), doc);
1019
+ value instanceof AbstractCrdt && value._attach(doc.generateId(), doc);
1274
1020
  }
1275
- }, _proto._attachChild = function(op, source) {
1021
+ }, _proto._attachChild = function(op, isLocal) {
1276
1022
  var _updates;
1277
1023
  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);
1024
+ var id = op.id, parentKey = op.parentKey, opId = op.opId, key = parentKey, child = creationOpToLiveStructure(op);
1279
1025
  if (void 0 !== this._doc.getItem(id)) return this._propToLastUpdate.get(key) === opId && this._propToLastUpdate.delete(key),
1280
1026
  {
1281
1027
  modified: !1
1282
1028
  };
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),
1029
+ 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
1030
  {
1285
1031
  modified: !1
1286
1032
  }) : {
1287
1033
  modified: !1
1288
1034
  };
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 = [ {
1035
+ var reverse, previousValue = this._map.get(key);
1036
+ if (isCrdt(previousValue)) reverse = previousValue._serialize(this._id, key), previousValue._detach(); else if (void 0 === previousValue) reverse = [ {
1292
1037
  type: exports.OpCode.DELETE_OBJECT_KEY,
1293
- id: thisId,
1038
+ id: this._id,
1294
1039
  key: key
1295
1040
  } ]; else {
1296
1041
  var _data;
1297
1042
  reverse = [ {
1298
1043
  type: exports.OpCode.UPDATE_OBJECT,
1299
- id: thisId,
1044
+ id: this._id,
1300
1045
  data: (_data = {}, _data[key] = previousValue, _data)
1301
1046
  } ];
1302
1047
  }
@@ -1313,7 +1058,7 @@ var LiveObject = function(_AbstractCrdt) {
1313
1058
  };
1314
1059
  }, _proto._detachChild = function(child) {
1315
1060
  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; ) {
1061
+ for (var _updates2, _step4, reverse = child._serialize(this._id, child._parentKey, this._doc), _iterator4 = _createForOfIteratorHelperLoose(this._map); !(_step4 = _iterator4()).done; ) {
1317
1062
  var _step4$value = _step4.value, key = _step4$value[0];
1318
1063
  _step4$value[1] === child && this._map.delete(key);
1319
1064
  }
@@ -1321,7 +1066,7 @@ var LiveObject = function(_AbstractCrdt) {
1321
1066
  modified: {
1322
1067
  node: this,
1323
1068
  type: "LiveObject",
1324
- updates: (_updates2 = {}, _updates2[parentKey] = {
1069
+ updates: (_updates2 = {}, _updates2[child._parentKey] = {
1325
1070
  type: "delete"
1326
1071
  }, _updates2)
1327
1072
  },
@@ -1331,46 +1076,51 @@ var LiveObject = function(_AbstractCrdt) {
1331
1076
  return {
1332
1077
  modified: !1
1333
1078
  };
1079
+ }, _proto._detachChildren = function() {
1080
+ for (var _step5, _iterator5 = _createForOfIteratorHelperLoose(this._map); !(_step5 = _iterator5()).done; ) {
1081
+ var _step5$value = _step5.value, key = _step5$value[0], value = _step5$value[1];
1082
+ this._map.delete(key), value._detach();
1083
+ }
1334
1084
  }, _proto._detach = function() {
1335
1085
  _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();
1086
+ for (var _step6, _iterator6 = _createForOfIteratorHelperLoose(this._map.values()); !(_step6 = _iterator6()).done; ) {
1087
+ var value = _step6.value;
1088
+ isCrdt(value) && value._detach();
1339
1089
  }
1340
1090
  }, _proto._apply = function(op, isLocal) {
1341
1091
  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
1092
  }, _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);
1093
+ for (var _this$_parent, _step7, data = {}, _iterator7 = _createForOfIteratorHelperLoose(this._map); !(_step7 = _iterator7()).done; ) {
1094
+ var _step7$value = _step7.value, key = _step7$value[0], value = _step7$value[1];
1095
+ value instanceof AbstractCrdt == !1 && (data[key] = value);
1346
1096
  }
1347
- return "HasParent" === this.parent.type && this.parent.node._id ? {
1097
+ return void 0 !== (null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id) && void 0 !== this._parentKey ? {
1348
1098
  type: exports.CrdtType.OBJECT,
1349
- parentId: this.parent.node._id,
1350
- parentKey: this.parent.key,
1099
+ parentId: this._parent._id,
1100
+ parentKey: this._parentKey,
1351
1101
  data: data
1352
1102
  } : {
1353
1103
  type: exports.CrdtType.OBJECT,
1354
1104
  data: data
1355
1105
  };
1356
1106
  }, _proto._applyUpdate = function(op, isLocal) {
1357
- var isModified = !1, id = nn(this._id), reverse = [], reverseUpdate = {
1107
+ var isModified = !1, reverse = [], reverseUpdate = {
1358
1108
  type: exports.OpCode.UPDATE_OBJECT,
1359
- id: id,
1109
+ id: this._id,
1360
1110
  data: {}
1361
1111
  };
1362
1112
  for (var key in reverse.push(reverseUpdate), op.data) {
1363
1113
  var oldValue = this._map.get(key);
1364
- isLiveNode(oldValue) ? (reverse.push.apply(reverse, oldValue._serialize(id, key)),
1114
+ oldValue instanceof AbstractCrdt ? (reverse.push.apply(reverse, oldValue._serialize(this._id, key)),
1365
1115
  oldValue._detach()) : void 0 !== oldValue ? reverseUpdate.data[key] = oldValue : void 0 === oldValue && reverse.push({
1366
1116
  type: exports.OpCode.DELETE_OBJECT_KEY,
1367
- id: id,
1117
+ id: this._id,
1368
1118
  key: key
1369
1119
  });
1370
1120
  }
1371
1121
  var updateDelta = {};
1372
1122
  for (var _key2 in op.data) {
1373
- if (isLocal) this._propToLastUpdate.set(_key2, nn(op.opId)); else {
1123
+ if (isLocal) this._propToLastUpdate.set(_key2, op.opId); else {
1374
1124
  if (null != this._propToLastUpdate.get(_key2)) {
1375
1125
  if (this._propToLastUpdate.get(_key2) === op.opId) {
1376
1126
  this._propToLastUpdate.delete(_key2);
@@ -1381,7 +1131,7 @@ var LiveObject = function(_AbstractCrdt) {
1381
1131
  isModified = !0;
1382
1132
  }
1383
1133
  var _oldValue = this._map.get(_key2);
1384
- isLiveNode(_oldValue) && _oldValue._detach(), isModified = !0, updateDelta[_key2] = {
1134
+ isCrdt(_oldValue) && _oldValue._detach(), isModified = !0, updateDelta[_key2] = {
1385
1135
  type: "update"
1386
1136
  }, this._map.set(_key2, op.data[_key2]);
1387
1137
  }
@@ -1404,12 +1154,12 @@ var LiveObject = function(_AbstractCrdt) {
1404
1154
  if (void 0 !== this._propToLastUpdate.get(key)) return {
1405
1155
  modified: !1
1406
1156
  };
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) {
1157
+ var oldValue = this._map.get(key), reverse = [];
1158
+ if (isCrdt(oldValue)) reverse = oldValue._serialize(this._id, op.key), oldValue._detach(); else if (void 0 !== oldValue) {
1409
1159
  var _data2;
1410
1160
  reverse = [ {
1411
1161
  type: exports.OpCode.UPDATE_OBJECT,
1412
- id: id,
1162
+ id: this._id,
1413
1163
  data: (_data2 = {}, _data2[key] = oldValue, _data2)
1414
1164
  } ];
1415
1165
  }
@@ -1439,10 +1189,10 @@ var LiveObject = function(_AbstractCrdt) {
1439
1189
  }, _proto.delete = function(key) {
1440
1190
  var _updates4, keyAsString = key, oldValue = this._map.get(keyAsString);
1441
1191
  if (void 0 !== oldValue) {
1442
- if (null == this._doc || null == this._id) return isLiveNode(oldValue) && oldValue._detach(),
1192
+ if (null == this._doc || null == this._id) return oldValue instanceof AbstractCrdt && oldValue._detach(),
1443
1193
  void this._map.delete(keyAsString);
1444
1194
  var reverse, _data3;
1445
- if (isLiveNode(oldValue)) oldValue._detach(), reverse = oldValue._serialize(this._id, keyAsString); else reverse = [ {
1195
+ if (oldValue instanceof AbstractCrdt) oldValue._detach(), reverse = oldValue._serialize(this._id, keyAsString); else reverse = [ {
1446
1196
  type: exports.OpCode.UPDATE_OBJECT,
1447
1197
  data: (_data3 = {}, _data3[keyAsString] = oldValue, _data3),
1448
1198
  id: this._id
@@ -1472,19 +1222,19 @@ var LiveObject = function(_AbstractCrdt) {
1472
1222
  }, updateDelta = {};
1473
1223
  for (var _key3 in overrides) {
1474
1224
  var _oldValue2 = this._map.get(_key3);
1475
- isLiveNode(_oldValue2) ? (reverseOps.push.apply(reverseOps, _oldValue2._serialize(this._id, _key3)),
1225
+ _oldValue2 instanceof AbstractCrdt ? (reverseOps.push.apply(reverseOps, _oldValue2._serialize(this._id, _key3)),
1476
1226
  _oldValue2._detach()) : void 0 === _oldValue2 ? reverseOps.push({
1477
1227
  type: exports.OpCode.DELETE_OBJECT_KEY,
1478
1228
  id: this._id,
1479
1229
  key: _key3
1480
1230
  }) : reverseUpdateOp.data[_key3] = _oldValue2;
1481
1231
  var _newValue = overrides[_key3];
1482
- if (isLiveNode(_newValue)) {
1232
+ if (_newValue instanceof AbstractCrdt) {
1483
1233
  _newValue._setParentLink(this, _key3), _newValue._attach(this._doc.generateId(), this._doc);
1484
1234
  var newAttachChildOps = _newValue._serialize(this._id, _key3, this._doc), createCrdtOp = newAttachChildOps.find((function(op) {
1485
1235
  return op.parentId === _this2._id;
1486
1236
  }));
1487
- createCrdtOp && this._propToLastUpdate.set(_key3, nn(createCrdtOp.opId)), ops.push.apply(ops, newAttachChildOps);
1237
+ createCrdtOp && this._propToLastUpdate.set(_key3, createCrdtOp.opId), ops.push.apply(ops, newAttachChildOps);
1488
1238
  } else updatedProps[_key3] = _newValue, this._propToLastUpdate.set(_key3, opId);
1489
1239
  this._map.set(_key3, _newValue), updateDelta[_key3] = {
1490
1240
  type: "update"
@@ -1505,22 +1255,23 @@ var LiveObject = function(_AbstractCrdt) {
1505
1255
  }), this._doc.dispatch(ops, reverseOps, storageUpdates);
1506
1256
  } else for (var key in overrides) {
1507
1257
  var oldValue = this._map.get(key);
1508
- isLiveNode(oldValue) && oldValue._detach();
1258
+ oldValue instanceof AbstractCrdt && oldValue._detach();
1509
1259
  var newValue = overrides[key];
1510
- isLiveNode(newValue) && newValue._setParentLink(this, key), this._map.set(key, newValue);
1260
+ newValue instanceof AbstractCrdt && newValue._setParentLink(this, key), this._map.set(key, newValue);
1511
1261
  }
1512
1262
  }, LiveObject;
1513
1263
  }(AbstractCrdt);
1514
1264
 
1515
- exports.LiveList = LiveList, exports.LiveMap = LiveMap, exports.LiveObject = LiveObject,
1516
- exports.LiveRegister = LiveRegister, exports._createForOfIteratorHelperLoose = _createForOfIteratorHelperLoose,
1265
+ exports.AbstractCrdt = AbstractCrdt, exports.LiveList = LiveList, exports.LiveMap = LiveMap,
1266
+ exports.LiveObject = LiveObject, exports.LiveRegister = LiveRegister, exports._createForOfIteratorHelperLoose = _createForOfIteratorHelperLoose,
1517
1267
  exports._extends = _extends, exports._inheritsLoose = _inheritsLoose, exports._objectWithoutPropertiesLoose = function(source, excluded) {
1518
1268
  if (null == source) return {};
1519
1269
  var key, i, target = {}, sourceKeys = Object.keys(source);
1520
1270
  for (i = 0; i < sourceKeys.length; i++) key = sourceKeys[i], excluded.indexOf(key) >= 0 || (target[key] = source[key]);
1521
1271
  return target;
1522
- }, exports._wrapNativeSuper = _wrapNativeSuper, exports.assertNever = assertNever,
1523
- exports.b64decode = function(b64value) {
1272
+ }, exports._wrapNativeSuper = _wrapNativeSuper, exports.assertNever = function(_value, errmsg) {
1273
+ throw new Error(errmsg);
1274
+ }, exports.b64decode = function(b64value) {
1524
1275
  try {
1525
1276
  var formattedValue = b64value.replace(/-/g, "+").replace(/_/g, "/");
1526
1277
  return decodeURIComponent(atob(formattedValue).split("").map((function(c) {
@@ -1533,9 +1284,10 @@ exports.b64decode = function(b64value) {
1533
1284
  return items.filter((function(item) {
1534
1285
  return null != item;
1535
1286
  }));
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) {
1287
+ }, exports.comparePosition = comparePosition, exports.deprecate = deprecate, exports.deprecateIf = deprecateIf,
1288
+ exports.errorIf = function(condition, message) {
1289
+ "production" !== process.env.NODE_ENV && condition && throwUsageError(message);
1290
+ }, exports.findNonSerializableValue = function findNonSerializableValue(value, path) {
1539
1291
  if (void 0 === path && (path = ""), !isPlain) return {
1540
1292
  path: path || "root",
1541
1293
  value: value
@@ -1569,7 +1321,7 @@ exports.b64decode = function(b64value) {
1569
1321
  })), crdt.parentKey !== currentCrdt.parentKey && ops.push({
1570
1322
  type: exports.OpCode.SET_PARENT_KEY,
1571
1323
  id: id,
1572
- parentKey: nn(crdt.parentKey, "Parent key must not be missing")
1324
+ parentKey: crdt.parentKey
1573
1325
  }); else switch (crdt.type) {
1574
1326
  case exports.CrdtType.REGISTER:
1575
1327
  ops.push({
@@ -1614,11 +1366,10 @@ exports.b64decode = function(b64value) {
1614
1366
  }
1615
1367
  })), ops;
1616
1368
  }, exports.isChildCrdt = isChildCrdt, exports.isJsonArray = isJsonArray, exports.isJsonObject = isJsonObject,
1617
- exports.isLiveList = isLiveList, exports.isLiveNode = isLiveNode, exports.isLiveObject = isLiveObject,
1618
1369
  exports.isRootCrdt = function(crdt) {
1619
1370
  return crdt.type === exports.CrdtType.OBJECT && !isChildCrdt(crdt);
1620
1371
  }, exports.isSameNodeOrChildOf = function isSameNodeOrChildOf(node, parent) {
1621
- return node === parent || "HasParent" === node.parent.type && isSameNodeOrChildOf(node.parent.node, parent);
1372
+ return node === parent || !!node._parent && isSameNodeOrChildOf(node._parent, parent);
1622
1373
  }, exports.isTokenValid = function(token) {
1623
1374
  var tokenParts = token.split(".");
1624
1375
  if (3 !== tokenParts.length) return !1;
@@ -1646,7 +1397,7 @@ exports.isRootCrdt = function(crdt) {
1646
1397
  updates: first.updates.concat(second.updates)
1647
1398
  });
1648
1399
  }(first, second) : second : second;
1649
- }, exports.nn = nn, exports.remove = function(array, item) {
1400
+ }, exports.remove = function(array, item) {
1650
1401
  for (var i = 0; i < array.length; i++) if (array[i] === item) {
1651
1402
  array.splice(i, 1);
1652
1403
  break;