@liveblocks/client 0.15.11 → 0.16.2

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.
@@ -469,7 +469,7 @@ var LiveRegister = function (_AbstractCrdt) {
469
469
  return _this;
470
470
  }
471
471
 
472
- LiveRegister._deserialize = function _deserialize(_ref, parentToChildren, doc) {
472
+ LiveRegister._deserialize = function _deserialize(_ref, _parentToChildren, doc) {
473
473
  var id = _ref[0],
474
474
  item = _ref[1];
475
475
 
@@ -486,7 +486,7 @@ var LiveRegister = function (_AbstractCrdt) {
486
486
 
487
487
  var _proto = LiveRegister.prototype;
488
488
 
489
- _proto._serialize = function _serialize(parentId, parentKey, doc) {
489
+ _proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
490
490
  if (this._id == null || parentId == null || parentKey == null) {
491
491
  throw new Error("Cannot serialize register if parentId or parentKey is undefined");
492
492
  }
@@ -495,6 +495,7 @@ var LiveRegister = function (_AbstractCrdt) {
495
495
  type: OpType.CreateRegister,
496
496
  opId: doc == null ? void 0 : doc.generateOpId(),
497
497
  id: this._id,
498
+ intent: intent,
498
499
  parentId: parentId,
499
500
  parentKey: parentKey,
500
501
  data: this.data
@@ -512,7 +513,7 @@ var LiveRegister = function (_AbstractCrdt) {
512
513
  };
513
514
  };
514
515
 
515
- _proto._attachChild = function _attachChild(_id, _key, _crdt, _opId, _isLocal) {
516
+ _proto._attachChild = function _attachChild(_op, _isLocal) {
516
517
  throw new Error("Method not implemented.");
517
518
  };
518
519
 
@@ -591,7 +592,7 @@ var LiveList = function (_AbstractCrdt) {
591
592
 
592
593
  var _proto = LiveList.prototype;
593
594
 
594
- _proto._serialize = function _serialize(parentId, parentKey, doc) {
595
+ _proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
595
596
  if (this._id == null) {
596
597
  throw new Error("Cannot serialize item is not attached");
597
598
  }
@@ -604,6 +605,7 @@ var LiveList = function (_AbstractCrdt) {
604
605
  var op = {
605
606
  id: this._id,
606
607
  opId: doc == null ? void 0 : doc.generateOpId(),
608
+ intent: intent,
607
609
  type: OpType.CreateList,
608
610
  parentId: parentId,
609
611
  parentKey: parentKey
@@ -648,11 +650,17 @@ var LiveList = function (_AbstractCrdt) {
648
650
  }
649
651
  };
650
652
 
651
- _proto._attachChild = function _attachChild(id, key, child, _opId, isLocal) {
653
+ _proto._attachChild = function _attachChild(op, isLocal) {
652
654
  if (this._doc == null) {
653
655
  throw new Error("Can't attach child if doc is not present");
654
656
  }
655
657
 
658
+ var id = op.id,
659
+ parentKey = op.parentKey,
660
+ intent = op.intent;
661
+ var key = parentKey;
662
+ var child = creationOpToLiveStructure(op);
663
+
656
664
  if (this._doc.getItem(id) !== undefined) {
657
665
  return {
658
666
  modified: false
@@ -670,7 +678,26 @@ var LiveList = function (_AbstractCrdt) {
670
678
  var newKey = key;
671
679
 
672
680
  if (index !== -1) {
673
- if (isLocal) {
681
+ if (intent === "set") {
682
+ var existingItem = this._items[index][0];
683
+
684
+ existingItem._detach();
685
+
686
+ var storageUpdate = {
687
+ node: this,
688
+ type: "LiveList",
689
+ updates: [{
690
+ index: index,
691
+ type: "set",
692
+ item: child instanceof LiveRegister ? child.data : child
693
+ }]
694
+ };
695
+ this._items[index][0] = child;
696
+ return {
697
+ modified: storageUpdate,
698
+ reverse: existingItem._serialize(this._id, key, this._doc, "set")
699
+ };
700
+ } else if (isLocal) {
674
701
  var before = this._items[index] ? this._items[index][1] : undefined;
675
702
  var after = this._items[index + 1] ? this._items[index + 1][1] : undefined;
676
703
  newKey = makePosition(before, after);
@@ -1009,6 +1036,43 @@ var LiveList = function (_AbstractCrdt) {
1009
1036
  }
1010
1037
  };
1011
1038
 
1039
+ _proto.set = function set(index, item) {
1040
+ if (index < 0 || index >= this._items.length) {
1041
+ throw new Error("Cannot set list item at index \"\x1D" + index + "\". index should be between 0 and " + (this._items.length - 1));
1042
+ }
1043
+
1044
+ var _this$_items$index = this._items[index],
1045
+ existingItem = _this$_items$index[0],
1046
+ position = _this$_items$index[1];
1047
+
1048
+ existingItem._detach();
1049
+
1050
+ var value = selfOrRegister(item);
1051
+
1052
+ value._setParentLink(this, position);
1053
+
1054
+ this._items[index][0] = value;
1055
+
1056
+ if (this._doc && this._id) {
1057
+ var _id2 = this._doc.generateId();
1058
+
1059
+ value._attach(_id2, this._doc);
1060
+
1061
+ var storageUpdates = new Map();
1062
+ storageUpdates.set(this._id, {
1063
+ node: this,
1064
+ type: "LiveList",
1065
+ updates: [{
1066
+ index: index,
1067
+ item: value instanceof LiveRegister ? value.data : value,
1068
+ type: "set"
1069
+ }]
1070
+ });
1071
+
1072
+ this._doc.dispatch(value._serialize(this._id, position, this._doc, "set"), existingItem._serialize(this._id, position, undefined, "set"), storageUpdates);
1073
+ }
1074
+ };
1075
+
1012
1076
  _proto.toArray = function toArray() {
1013
1077
  return this._items.map(function (entry) {
1014
1078
  return selfOrRegisterValue(entry[0]);
@@ -1135,7 +1199,7 @@ var LiveMap = function (_AbstractCrdt) {
1135
1199
 
1136
1200
  var _proto = LiveMap.prototype;
1137
1201
 
1138
- _proto._serialize = function _serialize(parentId, parentKey, doc) {
1202
+ _proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
1139
1203
  if (this._id == null) {
1140
1204
  throw new Error("Cannot serialize item is not attached");
1141
1205
  }
@@ -1149,6 +1213,7 @@ var LiveMap = function (_AbstractCrdt) {
1149
1213
  id: this._id,
1150
1214
  opId: doc == null ? void 0 : doc.generateOpId(),
1151
1215
  type: OpType.CreateMap,
1216
+ intent: intent,
1152
1217
  parentId: parentId,
1153
1218
  parentKey: parentKey
1154
1219
  };
@@ -1214,13 +1279,18 @@ var LiveMap = function (_AbstractCrdt) {
1214
1279
  }
1215
1280
  };
1216
1281
 
1217
- _proto._attachChild = function _attachChild(id, key, child, _opId, _isLocal) {
1282
+ _proto._attachChild = function _attachChild(op, _isLocal) {
1218
1283
  var _updates;
1219
1284
 
1220
1285
  if (this._doc == null) {
1221
1286
  throw new Error("Can't attach child if doc is not present");
1222
1287
  }
1223
1288
 
1289
+ var id = op.id,
1290
+ parentKey = op.parentKey;
1291
+ var key = parentKey;
1292
+ var child = creationOpToLiveStructure(op);
1293
+
1224
1294
  if (this._doc.getItem(id) !== undefined) {
1225
1295
  return {
1226
1296
  modified: false
@@ -1465,6 +1535,20 @@ var LiveMap = function (_AbstractCrdt) {
1465
1535
  return LiveMap;
1466
1536
  }(AbstractCrdt);
1467
1537
 
1538
+ function parseJson(rawMessage) {
1539
+ try {
1540
+ return JSON.parse(rawMessage);
1541
+ } catch (e) {
1542
+ return undefined;
1543
+ }
1544
+ }
1545
+ function isJsonArray(data) {
1546
+ return Array.isArray(data);
1547
+ }
1548
+ function isJsonObject(data) {
1549
+ return data !== null && typeof data === "object" && !isJsonArray(data);
1550
+ }
1551
+
1468
1552
  function remove(array, item) {
1469
1553
  for (var i = 0; i < array.length; i++) {
1470
1554
  if (array[i] === item) {
@@ -1473,6 +1557,26 @@ function remove(array, item) {
1473
1557
  }
1474
1558
  }
1475
1559
  }
1560
+ function compact(items) {
1561
+ return items.filter(function (item) {
1562
+ return item != null;
1563
+ });
1564
+ }
1565
+ function creationOpToLiveStructure(op) {
1566
+ switch (op.type) {
1567
+ case OpType.CreateRegister:
1568
+ return new LiveRegister(op.data);
1569
+
1570
+ case OpType.CreateObject:
1571
+ return new LiveObject(op.data);
1572
+
1573
+ case OpType.CreateMap:
1574
+ return new LiveMap();
1575
+
1576
+ case OpType.CreateList:
1577
+ return new LiveList();
1578
+ }
1579
+ }
1476
1580
  function isSameNodeOrChildOf(node, parent) {
1477
1581
  if (node === parent) {
1478
1582
  return true;
@@ -1606,44 +1710,57 @@ function getTreesDiffOperations(currentItems, newItems) {
1606
1710
  });
1607
1711
  return ops;
1608
1712
  }
1609
- function mergeStorageUpdates(first, second) {
1610
- if (!first) {
1611
- return second;
1713
+
1714
+ function mergeObjectStorageUpdates(first, second) {
1715
+ var updates = first.updates;
1716
+
1717
+ for (var _iterator = _createForOfIteratorHelperLoose(entries(second.updates)), _step; !(_step = _iterator()).done;) {
1718
+ var _step$value = _step.value,
1719
+ _key = _step$value[0],
1720
+ value = _step$value[1];
1721
+ updates[_key] = value;
1612
1722
  }
1613
1723
 
1614
- if (second.type === "LiveObject") {
1615
- var updates = first.updates;
1724
+ return _extends({}, second, {
1725
+ updates: updates
1726
+ });
1727
+ }
1616
1728
 
1617
- for (var _i = 0, _Object$entries = Object.entries(second.updates); _i < _Object$entries.length; _i++) {
1618
- var _Object$entries$_i = _Object$entries[_i],
1619
- key = _Object$entries$_i[0],
1620
- value = _Object$entries$_i[1];
1621
- updates[key] = value;
1622
- }
1729
+ function mergeMapStorageUpdates(first, second) {
1730
+ var updates = first.updates;
1623
1731
 
1624
- return _extends({}, second, {
1625
- updates: updates
1626
- });
1627
- } else if (second.type === "LiveMap") {
1628
- var _updates = first.updates;
1732
+ for (var _iterator2 = _createForOfIteratorHelperLoose(entries(second.updates)), _step2; !(_step2 = _iterator2()).done;) {
1733
+ var _step2$value = _step2.value,
1734
+ _key2 = _step2$value[0],
1735
+ value = _step2$value[1];
1736
+ updates[_key2] = value;
1737
+ }
1629
1738
 
1630
- for (var _i2 = 0, _Object$entries2 = Object.entries(second.updates); _i2 < _Object$entries2.length; _i2++) {
1631
- var _Object$entries2$_i = _Object$entries2[_i2],
1632
- _key = _Object$entries2$_i[0],
1633
- _value = _Object$entries2$_i[1];
1634
- _updates[_key] = _value;
1635
- }
1739
+ return _extends({}, second, {
1740
+ updates: updates
1741
+ });
1742
+ }
1636
1743
 
1637
- return _extends({}, second, {
1638
- updates: _updates
1639
- });
1640
- } else if (second.type === "LiveList") {
1641
- var _updates2 = first.updates;
1642
- return _extends({}, second, {
1643
- updates: _updates2.concat(second.updates)
1644
- });
1744
+ function mergeListStorageUpdates(first, second) {
1745
+ var updates = first.updates;
1746
+ return _extends({}, second, {
1747
+ updates: updates.concat(second.updates)
1748
+ });
1749
+ }
1750
+
1751
+ function mergeStorageUpdates(first, second) {
1752
+ if (!first) {
1753
+ return second;
1645
1754
  }
1646
1755
 
1756
+ if (first.type === "LiveObject" && second.type === "LiveObject") {
1757
+ return mergeObjectStorageUpdates(first, second);
1758
+ } else if (first.type === "LiveMap" && second.type === "LiveMap") {
1759
+ return mergeMapStorageUpdates(first, second);
1760
+ } else if (first.type === "LiveList" && second.type === "LiveList") {
1761
+ return mergeListStorageUpdates(first, second);
1762
+ } else ;
1763
+
1647
1764
  return second;
1648
1765
  }
1649
1766
 
@@ -1681,11 +1798,11 @@ function findNonSerializableValue(value, path) {
1681
1798
  return false;
1682
1799
  }
1683
1800
 
1684
- for (var _i3 = 0, _Object$entries3 = Object.entries(value); _i3 < _Object$entries3.length; _i3++) {
1685
- var _Object$entries3$_i = _Object$entries3[_i3],
1686
- key = _Object$entries3$_i[0],
1687
- nestedValue = _Object$entries3$_i[1];
1688
- var nestedPath = path ? path + "." + key : key;
1801
+ for (var _i = 0, _Object$entries = Object.entries(value); _i < _Object$entries.length; _i++) {
1802
+ var _Object$entries$_i = _Object$entries[_i],
1803
+ _key3 = _Object$entries$_i[0],
1804
+ nestedValue = _Object$entries$_i[1];
1805
+ var nestedPath = path ? path + "." + _key3 : _key3;
1689
1806
 
1690
1807
  if (!isPlain(nestedValue)) {
1691
1808
  return {
@@ -1706,19 +1823,15 @@ function findNonSerializableValue(value, path) {
1706
1823
  return false;
1707
1824
  }
1708
1825
  function isTokenValid(token) {
1709
- if (token === null) {
1710
- return false;
1711
- }
1712
-
1713
1826
  var tokenParts = token.split(".");
1714
1827
 
1715
1828
  if (tokenParts.length !== 3) {
1716
1829
  return false;
1717
1830
  }
1718
1831
 
1719
- var data = JSON.parse(atob(tokenParts[1]));
1832
+ var data = parseJson(atob(tokenParts[1]));
1720
1833
 
1721
- if (typeof data.exp !== "number") {
1834
+ if (data === undefined || !isJsonObject(data) || typeof data.exp !== "number") {
1722
1835
  return false;
1723
1836
  }
1724
1837
 
@@ -1730,6 +1843,9 @@ function isTokenValid(token) {
1730
1843
 
1731
1844
  return true;
1732
1845
  }
1846
+ function entries(obj) {
1847
+ return Object.entries(obj);
1848
+ }
1733
1849
 
1734
1850
  var LiveObject = function (_AbstractCrdt) {
1735
1851
  _inheritsLoose(LiveObject, _AbstractCrdt);
@@ -1758,7 +1874,7 @@ var LiveObject = function (_AbstractCrdt) {
1758
1874
 
1759
1875
  var _proto = LiveObject.prototype;
1760
1876
 
1761
- _proto._serialize = function _serialize(parentId, parentKey, doc) {
1877
+ _proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
1762
1878
  if (this._id == null) {
1763
1879
  throw new Error("Cannot serialize item is not attached");
1764
1880
  }
@@ -1767,6 +1883,7 @@ var LiveObject = function (_AbstractCrdt) {
1767
1883
  var op = {
1768
1884
  id: this._id,
1769
1885
  opId: doc == null ? void 0 : doc.generateOpId(),
1886
+ intent: intent,
1770
1887
  type: OpType.CreateObject,
1771
1888
  parentId: parentId,
1772
1889
  parentKey: parentKey,
@@ -1843,13 +1960,19 @@ var LiveObject = function (_AbstractCrdt) {
1843
1960
  }
1844
1961
  };
1845
1962
 
1846
- _proto._attachChild = function _attachChild(id, key, child, opId, isLocal) {
1963
+ _proto._attachChild = function _attachChild(op, isLocal) {
1847
1964
  var _updates;
1848
1965
 
1849
1966
  if (this._doc == null) {
1850
1967
  throw new Error("Can't attach child if doc is not present");
1851
1968
  }
1852
1969
 
1970
+ var id = op.id,
1971
+ parentKey = op.parentKey,
1972
+ opId = op.opId;
1973
+ var key = parentKey;
1974
+ var child = creationOpToLiveStructure(op);
1975
+
1853
1976
  if (this._doc.getItem(id) !== undefined) {
1854
1977
  if (this._propToLastUpdate.get(key) === opId) {
1855
1978
  this._propToLastUpdate.delete(key);
@@ -2353,12 +2476,14 @@ function makeOthers(userMap) {
2353
2476
  function makeStateMachine(state, context, mockedEffects) {
2354
2477
  var effects = mockedEffects || {
2355
2478
  authenticate: function authenticate(auth, createWebSocket) {
2356
- if (isTokenValid(state.token)) {
2357
- var parsedToken = parseToken(state.token);
2358
- var socket = createWebSocket(state.token);
2479
+ var token = state.token;
2480
+
2481
+ if (token && isTokenValid(token)) {
2482
+ var parsedToken = parseToken(token);
2483
+ var socket = createWebSocket(token);
2359
2484
  authenticationSuccess(parsedToken, socket);
2360
2485
  } else {
2361
- return auth(context.room).then(function (_ref2) {
2486
+ return auth(context.roomId).then(function (_ref2) {
2362
2487
  var token = _ref2.token;
2363
2488
 
2364
2489
  if (state.connection.state !== "authenticating") {
@@ -2435,9 +2560,9 @@ function makeStateMachine(state, context, mockedEffects) {
2435
2560
  state.root = load(message.items);
2436
2561
  }
2437
2562
 
2438
- for (var _key2 in state.defaultStorageRoot) {
2439
- if (state.root.get(_key2) == null) {
2440
- state.root.set(_key2, state.defaultStorageRoot[_key2]);
2563
+ for (var _key in state.defaultStorageRoot) {
2564
+ if (state.root.get(_key) == null) {
2565
+ state.root.set(_key, state.defaultStorageRoot[_key]);
2441
2566
  }
2442
2567
  }
2443
2568
  }
@@ -2496,7 +2621,7 @@ function makeStateMachine(state, context, mockedEffects) {
2496
2621
  generateId: generateId,
2497
2622
  generateOpId: generateOpId,
2498
2623
  dispatch: storageDispatch,
2499
- roomId: context.room
2624
+ roomId: context.roomId
2500
2625
  });
2501
2626
  }
2502
2627
 
@@ -2621,8 +2746,8 @@ function makeStateMachine(state, context, mockedEffects) {
2621
2746
  data: {}
2622
2747
  };
2623
2748
 
2624
- for (var _key3 in op.data) {
2625
- reverse.data[_key3] = state.me[_key3];
2749
+ for (var _key2 in op.data) {
2750
+ reverse.data[_key2] = state.me[_key2];
2626
2751
  }
2627
2752
 
2628
2753
  state.me = _extends({}, state.me, op.data);
@@ -2630,8 +2755,8 @@ function makeStateMachine(state, context, mockedEffects) {
2630
2755
  if (state.buffer.presence == null) {
2631
2756
  state.buffer.presence = op.data;
2632
2757
  } else {
2633
- for (var _key4 in op.data) {
2634
- state.buffer.presence[_key4] = op.data;
2758
+ for (var _key3 in op.data) {
2759
+ state.buffer.presence[_key3] = op.data;
2635
2760
  }
2636
2761
  }
2637
2762
 
@@ -2706,55 +2831,19 @@ function makeStateMachine(state, context, mockedEffects) {
2706
2831
  }
2707
2832
 
2708
2833
  case OpType.CreateObject:
2709
- {
2710
- var parent = state.items.get(op.parentId);
2711
-
2712
- if (parent == null) {
2713
- return {
2714
- modified: false
2715
- };
2716
- }
2717
-
2718
- return parent._attachChild(op.id, op.parentKey, new LiveObject(op.data), op.opId, isLocal);
2719
- }
2720
-
2721
2834
  case OpType.CreateList:
2722
- {
2723
- var _parent = state.items.get(op.parentId);
2724
-
2725
- if (_parent == null) {
2726
- return {
2727
- modified: false
2728
- };
2729
- }
2730
-
2731
- return _parent._attachChild(op.id, op.parentKey, new LiveList(), op.opId, isLocal);
2732
- }
2733
-
2734
- case OpType.CreateRegister:
2735
- {
2736
- var _parent2 = state.items.get(op.parentId);
2737
-
2738
- if (_parent2 == null) {
2739
- return {
2740
- modified: false
2741
- };
2742
- }
2743
-
2744
- return _parent2._attachChild(op.id, op.parentKey, new LiveRegister(op.data), op.opId, isLocal);
2745
- }
2746
-
2747
2835
  case OpType.CreateMap:
2836
+ case OpType.CreateRegister:
2748
2837
  {
2749
- var _parent3 = state.items.get(op.parentId);
2838
+ var parent = state.items.get(op.parentId);
2750
2839
 
2751
- if (_parent3 == null) {
2840
+ if (parent == null) {
2752
2841
  return {
2753
2842
  modified: false
2754
2843
  };
2755
2844
  }
2756
2845
 
2757
- return _parent3._attachChild(op.id, op.parentKey, new LiveMap(), op.opId, isLocal);
2846
+ return parent._attachChild(op, isLocal);
2758
2847
  }
2759
2848
  }
2760
2849
 
@@ -2823,9 +2912,9 @@ function makeStateMachine(state, context, mockedEffects) {
2823
2912
  state.buffer.presence = {};
2824
2913
  }
2825
2914
 
2826
- for (var _key5 in overrides) {
2827
- state.buffer.presence[_key5] = overrides[_key5];
2828
- oldValues[_key5] = state.me[_key5];
2915
+ for (var _key4 in overrides) {
2916
+ state.buffer.presence[_key4] = overrides[_key4];
2917
+ oldValues[_key4] = state.me[_key4];
2829
2918
  }
2830
2919
 
2831
2920
  state.me = _extends({}, state.me, overrides);
@@ -2937,10 +3026,10 @@ function makeStateMachine(state, context, mockedEffects) {
2937
3026
  function onRoomStateMessage(message) {
2938
3027
  var newUsers = {};
2939
3028
 
2940
- for (var _key6 in message.users) {
2941
- var _connectionId = Number.parseInt(_key6);
3029
+ for (var _key5 in message.users) {
3030
+ var _connectionId = Number.parseInt(_key5);
2942
3031
 
2943
- var user = message.users[_key6];
3032
+ var user = message.users[_key5];
2944
3033
  newUsers[_connectionId] = {
2945
3034
  connectionId: _connectionId,
2946
3035
  info: user.info,
@@ -2994,19 +3083,38 @@ function makeStateMachine(state, context, mockedEffects) {
2994
3083
  };
2995
3084
  }
2996
3085
 
3086
+ function parseServerMessage(data) {
3087
+ if (!isJsonObject(data)) {
3088
+ return null;
3089
+ }
3090
+
3091
+ return data;
3092
+ }
3093
+
3094
+ function parseServerMessages(text) {
3095
+ var data = parseJson(text);
3096
+
3097
+ if (data === undefined) {
3098
+ return null;
3099
+ } else if (isJsonArray(data)) {
3100
+ return compact(data.map(function (item) {
3101
+ return parseServerMessage(item);
3102
+ }));
3103
+ } else {
3104
+ return compact([parseServerMessage(data)]);
3105
+ }
3106
+ }
3107
+
2997
3108
  function onMessage(event) {
2998
3109
  if (event.data === "pong") {
2999
3110
  clearTimeout(state.timeoutHandles.pongTimeout);
3000
3111
  return;
3001
3112
  }
3002
3113
 
3003
- var message = JSON.parse(event.data);
3004
- var subMessages = [];
3114
+ var messages = parseServerMessages(event.data);
3005
3115
 
3006
- if (Array.isArray(message)) {
3007
- subMessages = message;
3008
- } else {
3009
- subMessages.push(message);
3116
+ if (messages === null || messages.length === 0) {
3117
+ return;
3010
3118
  }
3011
3119
 
3012
3120
  var updates = {
@@ -3014,10 +3122,10 @@ function makeStateMachine(state, context, mockedEffects) {
3014
3122
  others: []
3015
3123
  };
3016
3124
 
3017
- for (var _iterator9 = _createForOfIteratorHelperLoose(subMessages), _step9; !(_step9 = _iterator9()).done;) {
3018
- var subMessage = _step9.value;
3125
+ for (var _iterator9 = _createForOfIteratorHelperLoose(messages), _step9; !(_step9 = _iterator9()).done;) {
3126
+ var message = _step9.value;
3019
3127
 
3020
- switch (subMessage.type) {
3128
+ switch (message.type) {
3021
3129
  case ServerMessageType.UserJoined:
3022
3130
  {
3023
3131
  updates.others.push(onUserJoinedMessage(message));
@@ -3026,7 +3134,7 @@ function makeStateMachine(state, context, mockedEffects) {
3026
3134
 
3027
3135
  case ServerMessageType.UpdatePresence:
3028
3136
  {
3029
- var othersPresenceUpdate = onUpdatePresenceMessage(subMessage);
3137
+ var othersPresenceUpdate = onUpdatePresenceMessage(message);
3030
3138
 
3031
3139
  if (othersPresenceUpdate) {
3032
3140
  updates.others.push(othersPresenceUpdate);
@@ -3037,13 +3145,13 @@ function makeStateMachine(state, context, mockedEffects) {
3037
3145
 
3038
3146
  case ServerMessageType.Event:
3039
3147
  {
3040
- onEvent(subMessage);
3148
+ onEvent(message);
3041
3149
  break;
3042
3150
  }
3043
3151
 
3044
3152
  case ServerMessageType.UserLeft:
3045
3153
  {
3046
- var _event = onUserLeftMessage(subMessage);
3154
+ var _event = onUserLeftMessage(message);
3047
3155
 
3048
3156
  if (_event) {
3049
3157
  updates.others.push(_event);
@@ -3054,14 +3162,14 @@ function makeStateMachine(state, context, mockedEffects) {
3054
3162
 
3055
3163
  case ServerMessageType.RoomState:
3056
3164
  {
3057
- updates.others.push(onRoomStateMessage(subMessage));
3165
+ updates.others.push(onRoomStateMessage(message));
3058
3166
  break;
3059
3167
  }
3060
3168
 
3061
3169
  case ServerMessageType.InitialStorageState:
3062
3170
  {
3063
3171
  var offlineOps = new Map(state.offlineOperations);
3064
- createOrUpdateRootFromMessage(subMessage);
3172
+ createOrUpdateRootFromMessage(message);
3065
3173
  applyAndSendOfflineOps(offlineOps);
3066
3174
  _getInitialStateResolver == null ? void 0 : _getInitialStateResolver();
3067
3175
  break;
@@ -3069,7 +3177,7 @@ function makeStateMachine(state, context, mockedEffects) {
3069
3177
 
3070
3178
  case ServerMessageType.UpdateStorage:
3071
3179
  {
3072
- var applyResult = apply(subMessage.ops, false);
3180
+ var applyResult = apply(message.ops, false);
3073
3181
  applyResult.updates.storageUpdates.forEach(function (value, key) {
3074
3182
  updates.storageUpdates.set(key, mergeStorageUpdates(updates.storageUpdates.get(key), value));
3075
3183
  });
@@ -3346,8 +3454,8 @@ function makeStateMachine(state, context, mockedEffects) {
3346
3454
  }
3347
3455
 
3348
3456
  function clearListeners() {
3349
- for (var _key7 in state.listeners) {
3350
- state.listeners[_key7] = [];
3457
+ for (var _key6 in state.listeners) {
3458
+ state.listeners[_key6] = [];
3351
3459
  }
3352
3460
  }
3353
3461
 
@@ -3629,7 +3737,7 @@ function createRoom(options, context) {
3629
3737
  var state = defaultState(options.defaultPresence, options.defaultStorageRoot);
3630
3738
  var machine = makeStateMachine(state, context);
3631
3739
  var room = {
3632
- id: context.room,
3740
+ id: context.roomId,
3633
3741
  getConnectionState: machine.selectors.getConnectionState,
3634
3742
  getSelf: machine.selectors.getSelf,
3635
3743
  subscribe: machine.subscribe,
@@ -3681,13 +3789,17 @@ function parseToken(token) {
3681
3789
  throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
3682
3790
  }
3683
3791
 
3684
- var data = JSON.parse(atob(tokenParts[1]));
3792
+ var data = parseJson(atob(tokenParts[1]));
3685
3793
 
3686
- if (typeof data.actor !== "number") {
3687
- throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
3794
+ if (data !== undefined && isJsonObject(data) && typeof data.actor === "number" && (data.id === undefined || typeof data.id === "string")) {
3795
+ return {
3796
+ actor: data.actor,
3797
+ id: data.id,
3798
+ info: data.info
3799
+ };
3688
3800
  }
3689
3801
 
3690
- return data;
3802
+ throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
3691
3803
  }
3692
3804
 
3693
3805
  function prepareCreateWebSocket(liveblocksServer, WebSocketPolyfill) {
@@ -3793,7 +3905,7 @@ function createClient(options) {
3793
3905
  defaultPresence: options.defaultPresence,
3794
3906
  defaultStorageRoot: options.defaultStorageRoot
3795
3907
  }, {
3796
- room: roomId,
3908
+ roomId: roomId,
3797
3909
  throttleDelay: throttleDelay,
3798
3910
  WebSocketPolyfill: clientOptions.WebSocketPolyfill,
3799
3911
  fetchPolyfill: clientOptions.fetchPolyfill,
@@ -3879,33 +3991,46 @@ function prepareAuthentication(clientOptions) {
3879
3991
  throw new Error("Invalid Liveblocks client options. For more information: https://liveblocks.io/docs/api-reference/liveblocks-client#createClient");
3880
3992
  }
3881
3993
 
3882
- function liveObjectToJson(liveObject) {
3994
+ function lsonObjectToJson(obj) {
3883
3995
  var result = {};
3884
- var obj = liveObject.toObject();
3885
3996
 
3886
3997
  for (var _key in obj) {
3887
- result[_key] = liveNodeToJson(obj[_key]);
3998
+ var val = obj[_key];
3999
+
4000
+ if (val !== undefined) {
4001
+ result[_key] = lsonToJson(val);
4002
+ }
3888
4003
  }
3889
4004
 
3890
4005
  return result;
3891
4006
  }
3892
4007
 
4008
+ function liveObjectToJson(liveObject) {
4009
+ return lsonObjectToJson(liveObject.toObject());
4010
+ }
4011
+
3893
4012
  function liveMapToJson(map) {
3894
4013
  var result = {};
3895
- var obj = Object.fromEntries(map);
3896
4014
 
3897
- for (var _key2 in obj) {
3898
- result[_key2] = liveNodeToJson(obj[_key2]);
4015
+ for (var _iterator = _createForOfIteratorHelperLoose(map.entries()), _step; !(_step = _iterator()).done;) {
4016
+ var _step$value = _step.value,
4017
+ _key2 = _step$value[0],
4018
+ value = _step$value[1];
4019
+ result[_key2] = lsonToJson(value);
3899
4020
  }
3900
4021
 
3901
4022
  return result;
3902
4023
  }
3903
4024
 
4025
+ function lsonListToJson(value) {
4026
+ return value.map(lsonToJson);
4027
+ }
4028
+
3904
4029
  function liveListToJson(value) {
3905
- return value.toArray().map(liveNodeToJson);
4030
+ return lsonListToJson(value.toArray());
3906
4031
  }
3907
4032
 
3908
- function liveNodeToJson(value) {
4033
+ function lsonToJson(value) {
3909
4034
  if (value instanceof LiveObject) {
3910
4035
  return liveObjectToJson(value);
3911
4036
  } else if (value instanceof LiveList) {
@@ -3914,13 +4039,21 @@ function liveNodeToJson(value) {
3914
4039
  return liveMapToJson(value);
3915
4040
  } else if (value instanceof LiveRegister) {
3916
4041
  return value.data;
4042
+ } else if (value instanceof AbstractCrdt) {
4043
+ throw new Error("Unhandled subclass of AbstractCrdt encountered");
4044
+ }
4045
+
4046
+ if (Array.isArray(value)) {
4047
+ return lsonListToJson(value);
4048
+ } else if (isPlainObject(value)) {
4049
+ return lsonObjectToJson(value);
3917
4050
  }
3918
4051
 
3919
4052
  return value;
3920
4053
  }
3921
4054
 
3922
4055
  function isPlainObject(obj) {
3923
- return Object.prototype.toString.call(obj) === "[object Object]";
4056
+ return obj !== null && Object.prototype.toString.call(obj) === "[object Object]";
3924
4057
  }
3925
4058
 
3926
4059
  function anyToCrdt(obj) {
@@ -4003,8 +4136,7 @@ function patchLiveList(liveList, prev, next) {
4003
4136
  if (liveListNode instanceof LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
4004
4137
  patchLiveObject(liveListNode, prevNode, nextNode);
4005
4138
  } else {
4006
- liveList.delete(i);
4007
- liveList.insert(anyToCrdt(nextNode), i);
4139
+ liveList.set(i, anyToCrdt(nextNode));
4008
4140
  }
4009
4141
 
4010
4142
  i++;
@@ -4015,9 +4147,11 @@ function patchLiveList(liveList, prev, next) {
4015
4147
  i++;
4016
4148
  }
4017
4149
 
4018
- while (i <= prevEnd) {
4150
+ var _localI = i;
4151
+
4152
+ while (_localI <= prevEnd) {
4019
4153
  liveList.delete(i);
4020
- i++;
4154
+ _localI++;
4021
4155
  }
4022
4156
  }
4023
4157
  }
@@ -4109,7 +4243,11 @@ function patchImmutableNode(state, path, update) {
4109
4243
  var _update$updates$_key, _update$updates$_key2;
4110
4244
 
4111
4245
  if (((_update$updates$_key = update.updates[_key6]) == null ? void 0 : _update$updates$_key.type) === "update") {
4112
- newState[_key6] = liveNodeToJson(update.node.get(_key6));
4246
+ var val = update.node.get(_key6);
4247
+
4248
+ if (val !== undefined) {
4249
+ newState[_key6] = lsonToJson(val);
4250
+ }
4113
4251
  } else if (((_update$updates$_key2 = update.updates[_key6]) == null ? void 0 : _update$updates$_key2.type) === "delete") {
4114
4252
  delete newState[_key6];
4115
4253
  }
@@ -4128,24 +4266,32 @@ function patchImmutableNode(state, path, update) {
4128
4266
  return x;
4129
4267
  });
4130
4268
 
4131
- for (var _iterator = _createForOfIteratorHelperLoose(update.updates), _step; !(_step = _iterator()).done;) {
4132
- var listUpdate = _step.value;
4269
+ var _loop = function _loop() {
4270
+ var listUpdate = _step2.value;
4133
4271
 
4134
- if (listUpdate.type === "insert") {
4272
+ if (listUpdate.type === "set") {
4273
+ _newState = _newState.map(function (item, index) {
4274
+ return index === listUpdate.index ? listUpdate.item : item;
4275
+ });
4276
+ } else if (listUpdate.type === "insert") {
4135
4277
  if (listUpdate.index === _newState.length) {
4136
- _newState.push(liveNodeToJson(listUpdate.item));
4278
+ _newState.push(lsonToJson(listUpdate.item));
4137
4279
  } else {
4138
- _newState = [].concat(_newState.slice(0, listUpdate.index), [liveNodeToJson(listUpdate.item)], _newState.slice(listUpdate.index));
4280
+ _newState = [].concat(_newState.slice(0, listUpdate.index), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index));
4139
4281
  }
4140
4282
  } else if (listUpdate.type === "delete") {
4141
4283
  _newState.splice(listUpdate.index, 1);
4142
4284
  } else if (listUpdate.type === "move") {
4143
4285
  if (listUpdate.previousIndex > listUpdate.index) {
4144
- _newState = [].concat(_newState.slice(0, listUpdate.index), [liveNodeToJson(listUpdate.item)], _newState.slice(listUpdate.index, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1));
4286
+ _newState = [].concat(_newState.slice(0, listUpdate.index), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1));
4145
4287
  } else {
4146
- _newState = [].concat(_newState.slice(0, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1), [liveNodeToJson(listUpdate.item)], _newState.slice(listUpdate.index + 1));
4288
+ _newState = [].concat(_newState.slice(0, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index + 1));
4147
4289
  }
4148
4290
  }
4291
+ };
4292
+
4293
+ for (var _iterator2 = _createForOfIteratorHelperLoose(update.updates), _step2; !(_step2 = _iterator2()).done;) {
4294
+ _loop();
4149
4295
  }
4150
4296
 
4151
4297
  return _newState;
@@ -4163,7 +4309,7 @@ function patchImmutableNode(state, path, update) {
4163
4309
  var _update$updates$_key3, _update$updates$_key4;
4164
4310
 
4165
4311
  if (((_update$updates$_key3 = update.updates[_key7]) == null ? void 0 : _update$updates$_key3.type) === "update") {
4166
- _newState2[_key7] = liveNodeToJson(update.node.get(_key7));
4312
+ _newState2[_key7] = lsonToJson(update.node.get(_key7));
4167
4313
  } else if (((_update$updates$_key4 = update.updates[_key7]) == null ? void 0 : _update$updates$_key4.type) === "delete") {
4168
4314
  delete _newState2[_key7];
4169
4315
  }
@@ -4187,7 +4333,7 @@ function patchImmutableNode(state, path, update) {
4187
4333
 
4188
4334
  var internals = {
4189
4335
  liveObjectToJson: liveObjectToJson,
4190
- liveNodeToJson: liveNodeToJson,
4336
+ lsonToJson: lsonToJson,
4191
4337
  patchLiveList: patchLiveList,
4192
4338
  patchImmutableObject: patchImmutableObject,
4193
4339
  patchLiveObject: patchLiveObject,