@liveblocks/client 0.16.4-beta2 → 0.16.4

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/index.js CHANGED
@@ -1253,7 +1253,7 @@ function makeStateMachine(state, context, mockedEffects) {
1253
1253
  }
1254
1254
  };
1255
1255
  }
1256
- function defaultState(me, defaultStorageRoot) {
1256
+ function defaultState(initialPresence, initialStorage) {
1257
1257
  return {
1258
1258
  connection: {
1259
1259
  state: "closed"
@@ -1277,17 +1277,17 @@ function defaultState(me, defaultStorageRoot) {
1277
1277
  pongTimeout: 0
1278
1278
  },
1279
1279
  buffer: {
1280
- presence: me == null ? {} : me,
1280
+ presence: initialPresence == null ? {} : initialPresence,
1281
1281
  messages: [],
1282
1282
  storageOperations: []
1283
1283
  },
1284
1284
  intervalHandles: {
1285
1285
  heartbeat: 0
1286
1286
  },
1287
- me: me == null ? {} : me,
1287
+ me: initialPresence == null ? {} : initialPresence,
1288
1288
  users: {},
1289
1289
  others: makeOthers({}),
1290
- defaultStorageRoot: defaultStorageRoot,
1290
+ defaultStorageRoot: initialStorage,
1291
1291
  idFactory: null,
1292
1292
  clock: 0,
1293
1293
  opClock: 0,
@@ -1311,7 +1311,11 @@ function defaultState(me, defaultStorageRoot) {
1311
1311
  };
1312
1312
  }
1313
1313
  function createRoom(options, context) {
1314
- var state = defaultState(options.defaultPresence, options.defaultStorageRoot);
1314
+ var _options$initialPrese, _options$initialStora;
1315
+
1316
+ var initialPresence = (_options$initialPrese = options.initialPresence) != null ? _options$initialPrese : options.defaultPresence;
1317
+ var initialStorage = (_options$initialStora = options.initialStorage) != null ? _options$initialStora : options.defaultStorageRoot;
1318
+ var state = defaultState(typeof initialPresence === "function" ? initialPresence(context.roomId) : initialPresence, typeof initialStorage === "function" ? initialStorage(context.roomId) : initialStorage);
1315
1319
  var machine = makeStateMachine(state, context);
1316
1320
  var room = {
1317
1321
  id: context.roomId,
@@ -1478,7 +1482,11 @@ function createClient(options) {
1478
1482
  return internalRoom.room;
1479
1483
  }
1480
1484
 
1485
+ LiveObject.deprecateIf(options.defaultPresence, "Argument `defaultPresence` will be removed in @liveblocks/client 0.18. Please use `initialPresence` instead. For more info, see https://bit.ly/3Niy5aP", "defaultPresence");
1486
+ LiveObject.deprecateIf(options.defaultStorageRoot, "Argument `defaultStorageRoot` will be removed in @liveblocks/client 0.18. Please use `initialStorage` instead. For more info, see https://bit.ly/3Niy5aP", "defaultStorageRoot");
1481
1487
  internalRoom = createRoom({
1488
+ initialPresence: options.initialPresence,
1489
+ initialStorage: options.initialStorage,
1482
1490
  defaultPresence: options.defaultPresence,
1483
1491
  defaultStorageRoot: options.defaultStorageRoot
1484
1492
  }, {
@@ -1568,357 +1576,7 @@ function prepareAuthentication(clientOptions) {
1568
1576
  throw new Error("Invalid Liveblocks client options. For more information: https://liveblocks.io/docs/api-reference/liveblocks-client#createClient");
1569
1577
  }
1570
1578
 
1571
- function lsonObjectToJson(obj) {
1572
- var result = {};
1573
-
1574
- for (var _key in obj) {
1575
- var val = obj[_key];
1576
-
1577
- if (val !== undefined) {
1578
- result[_key] = lsonToJson(val);
1579
- }
1580
- }
1581
-
1582
- return result;
1583
- }
1584
-
1585
- function liveObjectToJson(liveObject) {
1586
- return lsonObjectToJson(liveObject.toObject());
1587
- }
1588
-
1589
- function liveMapToJson(map) {
1590
- var result = {};
1591
-
1592
- for (var _iterator = LiveObject._createForOfIteratorHelperLoose(map.entries()), _step; !(_step = _iterator()).done;) {
1593
- var _step$value = _step.value,
1594
- _key2 = _step$value[0],
1595
- value = _step$value[1];
1596
- result[_key2] = lsonToJson(value);
1597
- }
1598
-
1599
- return result;
1600
- }
1601
-
1602
- function lsonListToJson(value) {
1603
- return value.map(lsonToJson);
1604
- }
1605
-
1606
- function liveListToJson(value) {
1607
- return lsonListToJson(value.toArray());
1608
- }
1609
-
1610
- function lsonToJson(value) {
1611
- if (value instanceof LiveObject.LiveObject) {
1612
- return liveObjectToJson(value);
1613
- } else if (value instanceof LiveObject.LiveList) {
1614
- return liveListToJson(value);
1615
- } else if (value instanceof LiveObject.LiveMap) {
1616
- return liveMapToJson(value);
1617
- } else if (value instanceof LiveObject.LiveRegister) {
1618
- return value.data;
1619
- } else if (value instanceof LiveObject.AbstractCrdt) {
1620
- throw new Error("Unhandled subclass of AbstractCrdt encountered");
1621
- }
1622
-
1623
- if (Array.isArray(value)) {
1624
- return lsonListToJson(value);
1625
- } else if (isPlainObject(value)) {
1626
- return lsonObjectToJson(value);
1627
- }
1628
-
1629
- return value;
1630
- }
1631
-
1632
- function isPlainObject(obj) {
1633
- return obj !== null && Object.prototype.toString.call(obj) === "[object Object]";
1634
- }
1635
-
1636
- function anyToCrdt(obj) {
1637
- if (obj == null) {
1638
- return obj;
1639
- }
1640
-
1641
- if (Array.isArray(obj)) {
1642
- return new LiveObject.LiveList(obj.map(anyToCrdt));
1643
- }
1644
-
1645
- if (isPlainObject(obj)) {
1646
- var init = {};
1647
-
1648
- for (var _key3 in obj) {
1649
- init[_key3] = anyToCrdt(obj[_key3]);
1650
- }
1651
-
1652
- return new LiveObject.LiveObject(init);
1653
- }
1654
-
1655
- return obj;
1656
- }
1657
-
1658
- function patchLiveList(liveList, prev, next) {
1659
- var i = 0;
1660
- var prevEnd = prev.length - 1;
1661
- var nextEnd = next.length - 1;
1662
- var prevNode = prev[0];
1663
- var nextNode = next[0];
1664
-
1665
- outer: {
1666
- while (prevNode === nextNode) {
1667
- ++i;
1668
-
1669
- if (i > prevEnd || i > nextEnd) {
1670
- break outer;
1671
- }
1672
-
1673
- prevNode = prev[i];
1674
- nextNode = next[i];
1675
- }
1676
-
1677
- prevNode = prev[prevEnd];
1678
- nextNode = next[nextEnd];
1679
-
1680
- while (prevNode === nextNode) {
1681
- prevEnd--;
1682
- nextEnd--;
1683
-
1684
- if (i > prevEnd || i > nextEnd) {
1685
- break outer;
1686
- }
1687
-
1688
- prevNode = prev[prevEnd];
1689
- nextNode = next[nextEnd];
1690
- }
1691
- }
1692
-
1693
- if (i > prevEnd) {
1694
- if (i <= nextEnd) {
1695
- while (i <= nextEnd) {
1696
- liveList.insert(anyToCrdt(next[i]), i);
1697
- i++;
1698
- }
1699
- }
1700
- } else if (i > nextEnd) {
1701
- var localI = i;
1702
-
1703
- while (localI <= prevEnd) {
1704
- liveList.delete(i);
1705
- localI++;
1706
- }
1707
- } else {
1708
- while (i <= prevEnd && i <= nextEnd) {
1709
- prevNode = prev[i];
1710
- nextNode = next[i];
1711
- var liveListNode = liveList.get(i);
1712
-
1713
- if (liveListNode instanceof LiveObject.LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
1714
- patchLiveObject(liveListNode, prevNode, nextNode);
1715
- } else {
1716
- liveList.set(i, anyToCrdt(nextNode));
1717
- }
1718
-
1719
- i++;
1720
- }
1721
-
1722
- while (i <= nextEnd) {
1723
- liveList.insert(anyToCrdt(next[i]), i);
1724
- i++;
1725
- }
1726
-
1727
- var _localI = i;
1728
-
1729
- while (_localI <= prevEnd) {
1730
- liveList.delete(i);
1731
- _localI++;
1732
- }
1733
- }
1734
- }
1735
- function patchLiveObjectKey(liveObject, key, prev, next) {
1736
- if (process.env.NODE_ENV !== "production") {
1737
- var nonSerializableValue = LiveObject.findNonSerializableValue(next);
1738
-
1739
- if (nonSerializableValue) {
1740
- console.error("New state path: '" + nonSerializableValue.path + "' value: '" + nonSerializableValue.value + "' is not serializable.\nOnly serializable value can be synced with Liveblocks.");
1741
- return;
1742
- }
1743
- }
1744
-
1745
- var value = liveObject.get(key);
1746
-
1747
- if (next === undefined) {
1748
- liveObject.delete(key);
1749
- } else if (value === undefined) {
1750
- liveObject.set(key, anyToCrdt(next));
1751
- } else if (prev === next) {
1752
- return;
1753
- } else if (value instanceof LiveObject.LiveList && Array.isArray(prev) && Array.isArray(next)) {
1754
- patchLiveList(value, prev, next);
1755
- } else if (value instanceof LiveObject.LiveObject && isPlainObject(prev) && isPlainObject(next)) {
1756
- patchLiveObject(value, prev, next);
1757
- } else {
1758
- liveObject.set(key, anyToCrdt(next));
1759
- }
1760
- }
1761
- function patchLiveObject(root, prev, next) {
1762
- var updates = {};
1763
-
1764
- for (var _key4 in next) {
1765
- patchLiveObjectKey(root, _key4, prev[_key4], next[_key4]);
1766
- }
1767
-
1768
- for (var _key5 in prev) {
1769
- if (next[_key5] === undefined) {
1770
- root.delete(_key5);
1771
- }
1772
- }
1773
-
1774
- if (Object.keys(updates).length > 0) {
1775
- root.update(updates);
1776
- }
1777
- }
1778
-
1779
- function getParentsPath(node) {
1780
- var path = [];
1781
-
1782
- while (node._parentKey != null && node._parent != null) {
1783
- if (node._parent instanceof LiveObject.LiveList) {
1784
- path.push(node._parent._indexOfPosition(node._parentKey));
1785
- } else {
1786
- path.push(node._parentKey);
1787
- }
1788
-
1789
- node = node._parent;
1790
- }
1791
-
1792
- return path;
1793
- }
1794
-
1795
- function patchImmutableObject(state, updates) {
1796
- return updates.reduce(function (state, update) {
1797
- return patchImmutableObjectWithUpdate(state, update);
1798
- }, state);
1799
- }
1800
-
1801
- function patchImmutableObjectWithUpdate(state, update) {
1802
- var path = getParentsPath(update.node);
1803
- return patchImmutableNode(state, path, update);
1804
- }
1805
-
1806
- function patchImmutableNode(state, path, update) {
1807
- var pathItem = path.pop();
1808
-
1809
- if (pathItem === undefined) {
1810
- switch (update.type) {
1811
- case "LiveObject":
1812
- {
1813
- if (typeof state !== "object") {
1814
- throw new Error("Internal: received update on LiveObject but state was not an object");
1815
- }
1816
-
1817
- var newState = Object.assign({}, state);
1818
-
1819
- for (var _key6 in update.updates) {
1820
- var _update$updates$_key, _update$updates$_key2;
1821
-
1822
- if (((_update$updates$_key = update.updates[_key6]) == null ? void 0 : _update$updates$_key.type) === "update") {
1823
- var val = update.node.get(_key6);
1824
-
1825
- if (val !== undefined) {
1826
- newState[_key6] = lsonToJson(val);
1827
- }
1828
- } else if (((_update$updates$_key2 = update.updates[_key6]) == null ? void 0 : _update$updates$_key2.type) === "delete") {
1829
- delete newState[_key6];
1830
- }
1831
- }
1832
-
1833
- return newState;
1834
- }
1835
-
1836
- case "LiveList":
1837
- {
1838
- if (Array.isArray(state) === false) {
1839
- throw new Error("Internal: received update on LiveList but state was not an array");
1840
- }
1841
-
1842
- var _newState = state.map(function (x) {
1843
- return x;
1844
- });
1845
-
1846
- var _loop = function _loop() {
1847
- var listUpdate = _step2.value;
1848
-
1849
- if (listUpdate.type === "set") {
1850
- _newState = _newState.map(function (item, index) {
1851
- return index === listUpdate.index ? listUpdate.item : item;
1852
- });
1853
- } else if (listUpdate.type === "insert") {
1854
- if (listUpdate.index === _newState.length) {
1855
- _newState.push(lsonToJson(listUpdate.item));
1856
- } else {
1857
- _newState = [].concat(_newState.slice(0, listUpdate.index), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index));
1858
- }
1859
- } else if (listUpdate.type === "delete") {
1860
- _newState.splice(listUpdate.index, 1);
1861
- } else if (listUpdate.type === "move") {
1862
- if (listUpdate.previousIndex > listUpdate.index) {
1863
- _newState = [].concat(_newState.slice(0, listUpdate.index), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1));
1864
- } else {
1865
- _newState = [].concat(_newState.slice(0, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index + 1));
1866
- }
1867
- }
1868
- };
1869
-
1870
- for (var _iterator2 = LiveObject._createForOfIteratorHelperLoose(update.updates), _step2; !(_step2 = _iterator2()).done;) {
1871
- _loop();
1872
- }
1873
-
1874
- return _newState;
1875
- }
1876
-
1877
- case "LiveMap":
1878
- {
1879
- if (typeof state !== "object") {
1880
- throw new Error("Internal: received update on LiveMap but state was not an object");
1881
- }
1882
-
1883
- var _newState2 = Object.assign({}, state);
1884
-
1885
- for (var _key7 in update.updates) {
1886
- var _update$updates$_key3, _update$updates$_key4;
1887
-
1888
- if (((_update$updates$_key3 = update.updates[_key7]) == null ? void 0 : _update$updates$_key3.type) === "update") {
1889
- _newState2[_key7] = lsonToJson(update.node.get(_key7));
1890
- } else if (((_update$updates$_key4 = update.updates[_key7]) == null ? void 0 : _update$updates$_key4.type) === "delete") {
1891
- delete _newState2[_key7];
1892
- }
1893
- }
1894
-
1895
- return _newState2;
1896
- }
1897
- }
1898
- }
1899
-
1900
- if (Array.isArray(state)) {
1901
- var newArray = [].concat(state);
1902
- newArray[pathItem] = patchImmutableNode(state[pathItem], path, update);
1903
- return newArray;
1904
- } else {
1905
- var _extends2;
1906
-
1907
- return LiveObject._extends({}, state, (_extends2 = {}, _extends2[pathItem] = patchImmutableNode(state[pathItem], path, update), _extends2));
1908
- }
1909
- }
1910
-
1911
- var internals = {
1912
- liveObjectToJson: liveObjectToJson,
1913
- lsonToJson: lsonToJson,
1914
- patchLiveList: patchLiveList,
1915
- patchImmutableObject: patchImmutableObject,
1916
- patchLiveObject: patchLiveObject,
1917
- patchLiveObjectKey: patchLiveObjectKey
1918
- };
1919
-
1920
1579
  exports.LiveList = LiveObject.LiveList;
1921
1580
  exports.LiveMap = LiveObject.LiveMap;
1922
1581
  exports.LiveObject = LiveObject.LiveObject;
1923
1582
  exports.createClient = createClient;
1924
- exports.internals = internals;