@liveblocks/client 0.16.1 → 0.16.4-beta1
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.d.ts +31 -0
- package/index.js +1924 -0
- package/index.mjs +1641 -0
- package/{lib/internal.d.ts → internal.d.ts} +15 -19
- package/internal.js +36 -0
- package/internal.mjs +1 -0
- package/package.json +11 -22
- package/{lib/index.d.ts → shared.d.ts} +32 -34
- package/{lib/index.js → shared.js} +117 -1968
- package/shared.mjs +1947 -0
- package/lib/esm/index.js +0 -3054
- package/lib/esm/index.mjs +0 -3054
- package/lib/esm/internal.js +0 -150
- package/lib/esm/internal.mjs +0 -150
- package/lib/internal.js +0 -194
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
function _defineProperties(target, props) {
|
|
6
4
|
for (var i = 0; i < props.length; i++) {
|
|
7
5
|
var descriptor = props[i];
|
|
@@ -192,7 +190,7 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
|
192
190
|
return it.next.bind(it);
|
|
193
191
|
}
|
|
194
192
|
|
|
195
|
-
|
|
193
|
+
exports.ServerMessageType = void 0;
|
|
196
194
|
|
|
197
195
|
(function (ServerMessageType) {
|
|
198
196
|
ServerMessageType[ServerMessageType["UpdatePresence"] = 100] = "UpdatePresence";
|
|
@@ -202,27 +200,27 @@ var ServerMessageType;
|
|
|
202
200
|
ServerMessageType[ServerMessageType["RoomState"] = 104] = "RoomState";
|
|
203
201
|
ServerMessageType[ServerMessageType["InitialStorageState"] = 200] = "InitialStorageState";
|
|
204
202
|
ServerMessageType[ServerMessageType["UpdateStorage"] = 201] = "UpdateStorage";
|
|
205
|
-
})(ServerMessageType || (ServerMessageType = {}));
|
|
203
|
+
})(exports.ServerMessageType || (exports.ServerMessageType = {}));
|
|
206
204
|
|
|
207
|
-
|
|
205
|
+
exports.ClientMessageType = void 0;
|
|
208
206
|
|
|
209
207
|
(function (ClientMessageType) {
|
|
210
208
|
ClientMessageType[ClientMessageType["UpdatePresence"] = 100] = "UpdatePresence";
|
|
211
209
|
ClientMessageType[ClientMessageType["ClientEvent"] = 103] = "ClientEvent";
|
|
212
210
|
ClientMessageType[ClientMessageType["FetchStorage"] = 200] = "FetchStorage";
|
|
213
211
|
ClientMessageType[ClientMessageType["UpdateStorage"] = 201] = "UpdateStorage";
|
|
214
|
-
})(ClientMessageType || (ClientMessageType = {}));
|
|
212
|
+
})(exports.ClientMessageType || (exports.ClientMessageType = {}));
|
|
215
213
|
|
|
216
|
-
|
|
214
|
+
exports.CrdtType = void 0;
|
|
217
215
|
|
|
218
216
|
(function (CrdtType) {
|
|
219
217
|
CrdtType[CrdtType["Object"] = 0] = "Object";
|
|
220
218
|
CrdtType[CrdtType["List"] = 1] = "List";
|
|
221
219
|
CrdtType[CrdtType["Map"] = 2] = "Map";
|
|
222
220
|
CrdtType[CrdtType["Register"] = 3] = "Register";
|
|
223
|
-
})(CrdtType || (CrdtType = {}));
|
|
221
|
+
})(exports.CrdtType || (exports.CrdtType = {}));
|
|
224
222
|
|
|
225
|
-
|
|
223
|
+
exports.OpType = void 0;
|
|
226
224
|
|
|
227
225
|
(function (OpType) {
|
|
228
226
|
OpType[OpType["Init"] = 0] = "Init";
|
|
@@ -234,9 +232,9 @@ var OpType;
|
|
|
234
232
|
OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey";
|
|
235
233
|
OpType[OpType["CreateMap"] = 7] = "CreateMap";
|
|
236
234
|
OpType[OpType["CreateRegister"] = 8] = "CreateRegister";
|
|
237
|
-
})(OpType || (OpType = {}));
|
|
235
|
+
})(exports.OpType || (exports.OpType = {}));
|
|
238
236
|
|
|
239
|
-
|
|
237
|
+
exports.WebsocketCloseCodes = void 0;
|
|
240
238
|
|
|
241
239
|
(function (WebsocketCloseCodes) {
|
|
242
240
|
WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
|
|
@@ -247,7 +245,7 @@ var WebsocketCloseCodes;
|
|
|
247
245
|
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
|
|
248
246
|
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
|
|
249
247
|
WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_WITHOUT_RETRY"] = 4999] = "CLOSE_WITHOUT_RETRY";
|
|
250
|
-
})(WebsocketCloseCodes || (WebsocketCloseCodes = {}));
|
|
248
|
+
})(exports.WebsocketCloseCodes || (exports.WebsocketCloseCodes = {}));
|
|
251
249
|
|
|
252
250
|
var AbstractCrdt = function () {
|
|
253
251
|
function AbstractCrdt() {}
|
|
@@ -256,7 +254,7 @@ var AbstractCrdt = function () {
|
|
|
256
254
|
|
|
257
255
|
_proto._apply = function _apply(op, _isLocal) {
|
|
258
256
|
switch (op.type) {
|
|
259
|
-
case OpType.DeleteCrdt:
|
|
257
|
+
case exports.OpType.DeleteCrdt:
|
|
260
258
|
{
|
|
261
259
|
if (this._parent != null && this._parentKey != null) {
|
|
262
260
|
return this._parent._detachChild(this);
|
|
@@ -473,7 +471,7 @@ var LiveRegister = function (_AbstractCrdt) {
|
|
|
473
471
|
var id = _ref[0],
|
|
474
472
|
item = _ref[1];
|
|
475
473
|
|
|
476
|
-
if (item.type !== CrdtType.Register) {
|
|
474
|
+
if (item.type !== exports.CrdtType.Register) {
|
|
477
475
|
throw new Error("Tried to deserialize a map but item type is \"" + item.type + "\"");
|
|
478
476
|
}
|
|
479
477
|
|
|
@@ -492,7 +490,7 @@ var LiveRegister = function (_AbstractCrdt) {
|
|
|
492
490
|
}
|
|
493
491
|
|
|
494
492
|
return [{
|
|
495
|
-
type: OpType.CreateRegister,
|
|
493
|
+
type: exports.OpType.CreateRegister,
|
|
496
494
|
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
497
495
|
id: this._id,
|
|
498
496
|
intent: intent,
|
|
@@ -506,7 +504,7 @@ var LiveRegister = function (_AbstractCrdt) {
|
|
|
506
504
|
var _this$_parent;
|
|
507
505
|
|
|
508
506
|
return {
|
|
509
|
-
type: CrdtType.Register,
|
|
507
|
+
type: exports.CrdtType.Register,
|
|
510
508
|
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
511
509
|
parentKey: this._parentKey,
|
|
512
510
|
data: this.data
|
|
@@ -606,7 +604,7 @@ var LiveList = function (_AbstractCrdt) {
|
|
|
606
604
|
id: this._id,
|
|
607
605
|
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
608
606
|
intent: intent,
|
|
609
|
-
type: OpType.CreateList,
|
|
607
|
+
type: exports.OpType.CreateList,
|
|
610
608
|
parentId: parentId,
|
|
611
609
|
parentKey: parentKey
|
|
612
610
|
};
|
|
@@ -722,7 +720,7 @@ var LiveList = function (_AbstractCrdt) {
|
|
|
722
720
|
|
|
723
721
|
return {
|
|
724
722
|
reverse: [{
|
|
725
|
-
type: OpType.DeleteCrdt,
|
|
723
|
+
type: exports.OpType.DeleteCrdt,
|
|
726
724
|
id: id
|
|
727
725
|
}],
|
|
728
726
|
modified: {
|
|
@@ -814,7 +812,7 @@ var LiveList = function (_AbstractCrdt) {
|
|
|
814
812
|
updates: updatesDelta
|
|
815
813
|
},
|
|
816
814
|
reverse: [{
|
|
817
|
-
type: OpType.SetParentKey,
|
|
815
|
+
type: exports.OpType.SetParentKey,
|
|
818
816
|
id: item == null ? void 0 : item[0]._id,
|
|
819
817
|
parentKey: previousKey
|
|
820
818
|
}]
|
|
@@ -829,7 +827,7 @@ var LiveList = function (_AbstractCrdt) {
|
|
|
829
827
|
var _this$_parent;
|
|
830
828
|
|
|
831
829
|
return {
|
|
832
|
-
type: CrdtType.List,
|
|
830
|
+
type: exports.CrdtType.List,
|
|
833
831
|
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
834
832
|
parentKey: this._parentKey
|
|
835
833
|
};
|
|
@@ -878,7 +876,7 @@ var LiveList = function (_AbstractCrdt) {
|
|
|
878
876
|
});
|
|
879
877
|
|
|
880
878
|
this._doc.dispatch(value._serialize(this._id, position, this._doc), [{
|
|
881
|
-
type: OpType.DeleteCrdt,
|
|
879
|
+
type: exports.OpType.DeleteCrdt,
|
|
882
880
|
id: _id
|
|
883
881
|
}], storageUpdates);
|
|
884
882
|
}
|
|
@@ -941,12 +939,12 @@ var LiveList = function (_AbstractCrdt) {
|
|
|
941
939
|
});
|
|
942
940
|
|
|
943
941
|
this._doc.dispatch([{
|
|
944
|
-
type: OpType.SetParentKey,
|
|
942
|
+
type: exports.OpType.SetParentKey,
|
|
945
943
|
id: item[0]._id,
|
|
946
944
|
opId: this._doc.generateOpId(),
|
|
947
945
|
parentKey: position
|
|
948
946
|
}], [{
|
|
949
|
-
type: OpType.SetParentKey,
|
|
947
|
+
type: exports.OpType.SetParentKey,
|
|
950
948
|
id: item[0]._id,
|
|
951
949
|
parentKey: previousPosition
|
|
952
950
|
}], storageUpdates);
|
|
@@ -981,7 +979,7 @@ var LiveList = function (_AbstractCrdt) {
|
|
|
981
979
|
this._doc.dispatch([{
|
|
982
980
|
id: childRecordId,
|
|
983
981
|
opId: this._doc.generateOpId(),
|
|
984
|
-
type: OpType.DeleteCrdt
|
|
982
|
+
type: exports.OpType.DeleteCrdt
|
|
985
983
|
}], item[0]._serialize(this._id, item[1]), storageUpdates);
|
|
986
984
|
}
|
|
987
985
|
}
|
|
@@ -1004,7 +1002,7 @@ var LiveList = function (_AbstractCrdt) {
|
|
|
1004
1002
|
if (childId) {
|
|
1005
1003
|
ops.push({
|
|
1006
1004
|
id: childId,
|
|
1007
|
-
type: OpType.DeleteCrdt
|
|
1005
|
+
type: exports.OpType.DeleteCrdt
|
|
1008
1006
|
});
|
|
1009
1007
|
reverseOps.push.apply(reverseOps, _item3[0]._serialize(this._id, _item3[1]));
|
|
1010
1008
|
updateDelta.push({
|
|
@@ -1212,7 +1210,7 @@ var LiveMap = function (_AbstractCrdt) {
|
|
|
1212
1210
|
var op = {
|
|
1213
1211
|
id: this._id,
|
|
1214
1212
|
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
1215
|
-
type: OpType.CreateMap,
|
|
1213
|
+
type: exports.OpType.CreateMap,
|
|
1216
1214
|
intent: intent,
|
|
1217
1215
|
parentId: parentId,
|
|
1218
1216
|
parentKey: parentKey
|
|
@@ -1233,7 +1231,7 @@ var LiveMap = function (_AbstractCrdt) {
|
|
|
1233
1231
|
var id = _ref[0],
|
|
1234
1232
|
item = _ref[1];
|
|
1235
1233
|
|
|
1236
|
-
if (item.type !== CrdtType.Map) {
|
|
1234
|
+
if (item.type !== exports.CrdtType.Map) {
|
|
1237
1235
|
throw new Error("Tried to deserialize a map but item type is \"" + item.type + "\"");
|
|
1238
1236
|
}
|
|
1239
1237
|
|
|
@@ -1307,7 +1305,7 @@ var LiveMap = function (_AbstractCrdt) {
|
|
|
1307
1305
|
previousValue._detach();
|
|
1308
1306
|
} else {
|
|
1309
1307
|
reverse = [{
|
|
1310
|
-
type: OpType.DeleteCrdt,
|
|
1308
|
+
type: exports.OpType.DeleteCrdt,
|
|
1311
1309
|
id: id
|
|
1312
1310
|
}];
|
|
1313
1311
|
}
|
|
@@ -1374,7 +1372,7 @@ var LiveMap = function (_AbstractCrdt) {
|
|
|
1374
1372
|
var _this$_parent;
|
|
1375
1373
|
|
|
1376
1374
|
return {
|
|
1377
|
-
type: CrdtType.Map,
|
|
1375
|
+
type: exports.CrdtType.Map,
|
|
1378
1376
|
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
1379
1377
|
parentKey: this._parentKey
|
|
1380
1378
|
};
|
|
@@ -1420,7 +1418,7 @@ var LiveMap = function (_AbstractCrdt) {
|
|
|
1420
1418
|
});
|
|
1421
1419
|
|
|
1422
1420
|
this._doc.dispatch(item._serialize(this._id, key, this._doc), oldValue ? oldValue._serialize(this._id, key) : [{
|
|
1423
|
-
type: OpType.DeleteCrdt,
|
|
1421
|
+
type: exports.OpType.DeleteCrdt,
|
|
1424
1422
|
id: id
|
|
1425
1423
|
}], storageUpdates);
|
|
1426
1424
|
}
|
|
@@ -1454,7 +1452,7 @@ var LiveMap = function (_AbstractCrdt) {
|
|
|
1454
1452
|
});
|
|
1455
1453
|
|
|
1456
1454
|
this._doc.dispatch([{
|
|
1457
|
-
type: OpType.DeleteCrdt,
|
|
1455
|
+
type: exports.OpType.DeleteCrdt,
|
|
1458
1456
|
id: item._id,
|
|
1459
1457
|
opId: this._doc.generateOpId()
|
|
1460
1458
|
}], item._serialize(this._id, key), storageUpdates);
|
|
@@ -1549,6 +1547,32 @@ function isJsonObject(data) {
|
|
|
1549
1547
|
return data !== null && typeof data === "object" && !isJsonArray(data);
|
|
1550
1548
|
}
|
|
1551
1549
|
|
|
1550
|
+
var _emittedDeprecationWarnings = new Set();
|
|
1551
|
+
|
|
1552
|
+
function deprecate(message, key) {
|
|
1553
|
+
if (key === void 0) {
|
|
1554
|
+
key = message;
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
if (process.env.NODE_ENV !== "production") {
|
|
1558
|
+
if (!_emittedDeprecationWarnings.has(key)) {
|
|
1559
|
+
_emittedDeprecationWarnings.add(key);
|
|
1560
|
+
|
|
1561
|
+
console.warn("DEPRECATION WARNING: " + message);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
function deprecateIf(condition, message, key) {
|
|
1566
|
+
if (key === void 0) {
|
|
1567
|
+
key = message;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
if (process.env.NODE_ENV !== "production") {
|
|
1571
|
+
if (condition) {
|
|
1572
|
+
deprecate(message, key);
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1552
1576
|
function remove(array, item) {
|
|
1553
1577
|
for (var i = 0; i < array.length; i++) {
|
|
1554
1578
|
if (array[i] === item) {
|
|
@@ -1564,16 +1588,16 @@ function compact(items) {
|
|
|
1564
1588
|
}
|
|
1565
1589
|
function creationOpToLiveStructure(op) {
|
|
1566
1590
|
switch (op.type) {
|
|
1567
|
-
case OpType.CreateRegister:
|
|
1591
|
+
case exports.OpType.CreateRegister:
|
|
1568
1592
|
return new LiveRegister(op.data);
|
|
1569
1593
|
|
|
1570
|
-
case OpType.CreateObject:
|
|
1594
|
+
case exports.OpType.CreateObject:
|
|
1571
1595
|
return new LiveObject(op.data);
|
|
1572
1596
|
|
|
1573
|
-
case OpType.CreateMap:
|
|
1597
|
+
case exports.OpType.CreateMap:
|
|
1574
1598
|
return new LiveMap();
|
|
1575
1599
|
|
|
1576
|
-
case OpType.CreateList:
|
|
1600
|
+
case exports.OpType.CreateList:
|
|
1577
1601
|
return new LiveList();
|
|
1578
1602
|
}
|
|
1579
1603
|
}
|
|
@@ -1590,22 +1614,22 @@ function isSameNodeOrChildOf(node, parent) {
|
|
|
1590
1614
|
}
|
|
1591
1615
|
function deserialize(entry, parentToChildren, doc) {
|
|
1592
1616
|
switch (entry[1].type) {
|
|
1593
|
-
case CrdtType.Object:
|
|
1617
|
+
case exports.CrdtType.Object:
|
|
1594
1618
|
{
|
|
1595
1619
|
return LiveObject._deserialize(entry, parentToChildren, doc);
|
|
1596
1620
|
}
|
|
1597
1621
|
|
|
1598
|
-
case CrdtType.List:
|
|
1622
|
+
case exports.CrdtType.List:
|
|
1599
1623
|
{
|
|
1600
1624
|
return LiveList._deserialize(entry, parentToChildren, doc);
|
|
1601
1625
|
}
|
|
1602
1626
|
|
|
1603
|
-
case CrdtType.Map:
|
|
1627
|
+
case exports.CrdtType.Map:
|
|
1604
1628
|
{
|
|
1605
1629
|
return LiveMap._deserialize(entry, parentToChildren, doc);
|
|
1606
1630
|
}
|
|
1607
1631
|
|
|
1608
|
-
case CrdtType.Register:
|
|
1632
|
+
case exports.CrdtType.Register:
|
|
1609
1633
|
{
|
|
1610
1634
|
return LiveRegister._deserialize(entry, parentToChildren, doc);
|
|
1611
1635
|
}
|
|
@@ -1640,7 +1664,7 @@ function getTreesDiffOperations(currentItems, newItems) {
|
|
|
1640
1664
|
currentItems.forEach(function (_, id) {
|
|
1641
1665
|
if (!newItems.get(id)) {
|
|
1642
1666
|
ops.push({
|
|
1643
|
-
type: OpType.DeleteCrdt,
|
|
1667
|
+
type: exports.OpType.DeleteCrdt,
|
|
1644
1668
|
id: id
|
|
1645
1669
|
});
|
|
1646
1670
|
}
|
|
@@ -1649,10 +1673,10 @@ function getTreesDiffOperations(currentItems, newItems) {
|
|
|
1649
1673
|
var currentCrdt = currentItems.get(id);
|
|
1650
1674
|
|
|
1651
1675
|
if (currentCrdt) {
|
|
1652
|
-
if (crdt.type === CrdtType.Object) {
|
|
1676
|
+
if (crdt.type === exports.CrdtType.Object) {
|
|
1653
1677
|
if (JSON.stringify(crdt.data) !== JSON.stringify(currentCrdt.data)) {
|
|
1654
1678
|
ops.push({
|
|
1655
|
-
type: OpType.UpdateObject,
|
|
1679
|
+
type: exports.OpType.UpdateObject,
|
|
1656
1680
|
id: id,
|
|
1657
1681
|
data: crdt.data
|
|
1658
1682
|
});
|
|
@@ -1661,16 +1685,16 @@ function getTreesDiffOperations(currentItems, newItems) {
|
|
|
1661
1685
|
|
|
1662
1686
|
if (crdt.parentKey !== currentCrdt.parentKey) {
|
|
1663
1687
|
ops.push({
|
|
1664
|
-
type: OpType.SetParentKey,
|
|
1688
|
+
type: exports.OpType.SetParentKey,
|
|
1665
1689
|
id: id,
|
|
1666
1690
|
parentKey: crdt.parentKey
|
|
1667
1691
|
});
|
|
1668
1692
|
}
|
|
1669
1693
|
} else {
|
|
1670
1694
|
switch (crdt.type) {
|
|
1671
|
-
case CrdtType.Register:
|
|
1695
|
+
case exports.CrdtType.Register:
|
|
1672
1696
|
ops.push({
|
|
1673
|
-
type: OpType.CreateRegister,
|
|
1697
|
+
type: exports.OpType.CreateRegister,
|
|
1674
1698
|
id: id,
|
|
1675
1699
|
parentId: crdt.parentId,
|
|
1676
1700
|
parentKey: crdt.parentKey,
|
|
@@ -1678,18 +1702,18 @@ function getTreesDiffOperations(currentItems, newItems) {
|
|
|
1678
1702
|
});
|
|
1679
1703
|
break;
|
|
1680
1704
|
|
|
1681
|
-
case CrdtType.List:
|
|
1705
|
+
case exports.CrdtType.List:
|
|
1682
1706
|
ops.push({
|
|
1683
|
-
type: OpType.CreateList,
|
|
1707
|
+
type: exports.OpType.CreateList,
|
|
1684
1708
|
id: id,
|
|
1685
1709
|
parentId: crdt.parentId,
|
|
1686
1710
|
parentKey: crdt.parentKey
|
|
1687
1711
|
});
|
|
1688
1712
|
break;
|
|
1689
1713
|
|
|
1690
|
-
case CrdtType.Object:
|
|
1714
|
+
case exports.CrdtType.Object:
|
|
1691
1715
|
ops.push({
|
|
1692
|
-
type: OpType.CreateObject,
|
|
1716
|
+
type: exports.OpType.CreateObject,
|
|
1693
1717
|
id: id,
|
|
1694
1718
|
parentId: crdt.parentId,
|
|
1695
1719
|
parentKey: crdt.parentKey,
|
|
@@ -1697,9 +1721,9 @@ function getTreesDiffOperations(currentItems, newItems) {
|
|
|
1697
1721
|
});
|
|
1698
1722
|
break;
|
|
1699
1723
|
|
|
1700
|
-
case CrdtType.Map:
|
|
1724
|
+
case exports.CrdtType.Map:
|
|
1701
1725
|
ops.push({
|
|
1702
|
-
type: OpType.CreateMap,
|
|
1726
|
+
type: exports.OpType.CreateMap,
|
|
1703
1727
|
id: id,
|
|
1704
1728
|
parentId: crdt.parentId,
|
|
1705
1729
|
parentKey: crdt.parentKey
|
|
@@ -1766,10 +1790,10 @@ function mergeStorageUpdates(first, second) {
|
|
|
1766
1790
|
|
|
1767
1791
|
function isPlain(value) {
|
|
1768
1792
|
var type = typeof value;
|
|
1769
|
-
return type === "undefined" || value === null || type === "string" || type === "boolean" || type === "number" || Array.isArray(value) || isPlainObject
|
|
1793
|
+
return type === "undefined" || value === null || type === "string" || type === "boolean" || type === "number" || Array.isArray(value) || isPlainObject(value);
|
|
1770
1794
|
}
|
|
1771
1795
|
|
|
1772
|
-
function isPlainObject
|
|
1796
|
+
function isPlainObject(value) {
|
|
1773
1797
|
if (typeof value !== "object" || value === null) return false;
|
|
1774
1798
|
var proto = Object.getPrototypeOf(value);
|
|
1775
1799
|
if (proto === null) return true;
|
|
@@ -1884,7 +1908,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
1884
1908
|
id: this._id,
|
|
1885
1909
|
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
1886
1910
|
intent: intent,
|
|
1887
|
-
type: OpType.CreateObject,
|
|
1911
|
+
type: exports.OpType.CreateObject,
|
|
1888
1912
|
parentId: parentId,
|
|
1889
1913
|
parentKey: parentKey,
|
|
1890
1914
|
data: {}
|
|
@@ -1910,7 +1934,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
1910
1934
|
var id = _ref[0],
|
|
1911
1935
|
item = _ref[1];
|
|
1912
1936
|
|
|
1913
|
-
if (item.type !== CrdtType.Object) {
|
|
1937
|
+
if (item.type !== exports.CrdtType.Object) {
|
|
1914
1938
|
throw new Error("Tried to deserialize a record but item type is \"" + item.type + "\"");
|
|
1915
1939
|
}
|
|
1916
1940
|
|
|
@@ -2007,7 +2031,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2007
2031
|
previousValue._detach();
|
|
2008
2032
|
} else if (previousValue === undefined) {
|
|
2009
2033
|
reverse = [{
|
|
2010
|
-
type: OpType.DeleteObjectKey,
|
|
2034
|
+
type: exports.OpType.DeleteObjectKey,
|
|
2011
2035
|
id: this._id,
|
|
2012
2036
|
key: key
|
|
2013
2037
|
}];
|
|
@@ -2015,7 +2039,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2015
2039
|
var _data;
|
|
2016
2040
|
|
|
2017
2041
|
reverse = [{
|
|
2018
|
-
type: OpType.UpdateObject,
|
|
2042
|
+
type: exports.OpType.UpdateObject,
|
|
2019
2043
|
id: this._id,
|
|
2020
2044
|
data: (_data = {}, _data[key] = previousValue, _data)
|
|
2021
2045
|
}];
|
|
@@ -2100,9 +2124,9 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2100
2124
|
};
|
|
2101
2125
|
|
|
2102
2126
|
_proto._apply = function _apply(op, isLocal) {
|
|
2103
|
-
if (op.type === OpType.UpdateObject) {
|
|
2127
|
+
if (op.type === exports.OpType.UpdateObject) {
|
|
2104
2128
|
return this._applyUpdate(op, isLocal);
|
|
2105
|
-
} else if (op.type === OpType.DeleteObjectKey) {
|
|
2129
|
+
} else if (op.type === exports.OpType.DeleteObjectKey) {
|
|
2106
2130
|
return this._applyDeleteObjectKey(op);
|
|
2107
2131
|
}
|
|
2108
2132
|
|
|
@@ -2125,7 +2149,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2125
2149
|
}
|
|
2126
2150
|
|
|
2127
2151
|
return {
|
|
2128
|
-
type: CrdtType.Object,
|
|
2152
|
+
type: exports.CrdtType.Object,
|
|
2129
2153
|
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
2130
2154
|
parentKey: this._parentKey,
|
|
2131
2155
|
data: data
|
|
@@ -2136,7 +2160,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2136
2160
|
var isModified = false;
|
|
2137
2161
|
var reverse = [];
|
|
2138
2162
|
var reverseUpdate = {
|
|
2139
|
-
type: OpType.UpdateObject,
|
|
2163
|
+
type: exports.OpType.UpdateObject,
|
|
2140
2164
|
id: this._id,
|
|
2141
2165
|
data: {}
|
|
2142
2166
|
};
|
|
@@ -2153,7 +2177,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2153
2177
|
reverseUpdate.data[key] = oldValue;
|
|
2154
2178
|
} else if (oldValue === undefined) {
|
|
2155
2179
|
reverse.push({
|
|
2156
|
-
type: OpType.DeleteObjectKey,
|
|
2180
|
+
type: exports.OpType.DeleteObjectKey,
|
|
2157
2181
|
id: this._id,
|
|
2158
2182
|
key: key
|
|
2159
2183
|
});
|
|
@@ -2234,7 +2258,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2234
2258
|
var _data2;
|
|
2235
2259
|
|
|
2236
2260
|
reverse = [{
|
|
2237
|
-
type: OpType.UpdateObject,
|
|
2261
|
+
type: exports.OpType.UpdateObject,
|
|
2238
2262
|
id: this._id,
|
|
2239
2263
|
data: (_data2 = {}, _data2[key] = oldValue, _data2)
|
|
2240
2264
|
}];
|
|
@@ -2299,7 +2323,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2299
2323
|
var _data3;
|
|
2300
2324
|
|
|
2301
2325
|
reverse = [{
|
|
2302
|
-
type: OpType.UpdateObject,
|
|
2326
|
+
type: exports.OpType.UpdateObject,
|
|
2303
2327
|
data: (_data3 = {}, _data3[keyAsString] = oldValue, _data3),
|
|
2304
2328
|
id: this._id
|
|
2305
2329
|
}];
|
|
@@ -2317,7 +2341,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2317
2341
|
});
|
|
2318
2342
|
|
|
2319
2343
|
this._doc.dispatch([{
|
|
2320
|
-
type: OpType.DeleteObjectKey,
|
|
2344
|
+
type: exports.OpType.DeleteObjectKey,
|
|
2321
2345
|
key: keyAsString,
|
|
2322
2346
|
id: this._id,
|
|
2323
2347
|
opId: this._doc.generateOpId()
|
|
@@ -2355,7 +2379,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2355
2379
|
var updatedProps = {};
|
|
2356
2380
|
var reverseUpdateOp = {
|
|
2357
2381
|
id: this._id,
|
|
2358
|
-
type: OpType.UpdateObject,
|
|
2382
|
+
type: exports.OpType.UpdateObject,
|
|
2359
2383
|
data: {}
|
|
2360
2384
|
};
|
|
2361
2385
|
var updateDelta = {};
|
|
@@ -2369,7 +2393,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2369
2393
|
_oldValue2._detach();
|
|
2370
2394
|
} else if (_oldValue2 === undefined) {
|
|
2371
2395
|
reverseOps.push({
|
|
2372
|
-
type: OpType.DeleteObjectKey,
|
|
2396
|
+
type: exports.OpType.DeleteObjectKey,
|
|
2373
2397
|
id: this._id,
|
|
2374
2398
|
key: _key3
|
|
2375
2399
|
});
|
|
@@ -2416,7 +2440,7 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2416
2440
|
ops.unshift({
|
|
2417
2441
|
opId: opId,
|
|
2418
2442
|
id: this._id,
|
|
2419
|
-
type: OpType.UpdateObject,
|
|
2443
|
+
type: exports.OpType.UpdateObject,
|
|
2420
2444
|
data: updatedProps
|
|
2421
2445
|
});
|
|
2422
2446
|
}
|
|
@@ -2434,1906 +2458,31 @@ var LiveObject = function (_AbstractCrdt) {
|
|
|
2434
2458
|
return LiveObject;
|
|
2435
2459
|
}(AbstractCrdt);
|
|
2436
2460
|
|
|
2437
|
-
|
|
2438
|
-
var BACKOFF_RETRY_DELAYS_SLOW = [2000, 30000, 60000, 300000];
|
|
2439
|
-
var HEARTBEAT_INTERVAL = 30000;
|
|
2440
|
-
var PONG_TIMEOUT = 2000;
|
|
2441
|
-
|
|
2442
|
-
function isValidRoomEventType(value) {
|
|
2443
|
-
return value === "my-presence" || value === "others" || value === "event" || value === "error" || value === "connection";
|
|
2444
|
-
}
|
|
2445
|
-
|
|
2446
|
-
function makeIdFactory(connectionId) {
|
|
2447
|
-
var count = 0;
|
|
2448
|
-
return function () {
|
|
2449
|
-
return connectionId + ":" + count++;
|
|
2450
|
-
};
|
|
2451
|
-
}
|
|
2452
|
-
|
|
2453
|
-
function makeOthers(userMap) {
|
|
2454
|
-
var _ref;
|
|
2455
|
-
|
|
2456
|
-
var users = Object.values(userMap).map(function (user) {
|
|
2457
|
-
user._hasReceivedInitialPresence;
|
|
2458
|
-
var publicKeys = _objectWithoutPropertiesLoose(user, ["_hasReceivedInitialPresence"]);
|
|
2459
|
-
|
|
2460
|
-
return publicKeys;
|
|
2461
|
-
});
|
|
2462
|
-
return _ref = {
|
|
2463
|
-
get count() {
|
|
2464
|
-
return users.length;
|
|
2465
|
-
}
|
|
2466
|
-
|
|
2467
|
-
}, _ref[Symbol.iterator] = function () {
|
|
2468
|
-
return users[Symbol.iterator]();
|
|
2469
|
-
}, _ref.map = function map(callback) {
|
|
2470
|
-
return users.map(callback);
|
|
2471
|
-
}, _ref.toArray = function toArray() {
|
|
2472
|
-
return users;
|
|
2473
|
-
}, _ref;
|
|
2474
|
-
}
|
|
2475
|
-
|
|
2476
|
-
function makeStateMachine(state, context, mockedEffects) {
|
|
2477
|
-
var effects = mockedEffects || {
|
|
2478
|
-
authenticate: function authenticate(auth, createWebSocket) {
|
|
2479
|
-
var token = state.token;
|
|
2480
|
-
|
|
2481
|
-
if (token && isTokenValid(token)) {
|
|
2482
|
-
var parsedToken = parseToken(token);
|
|
2483
|
-
var socket = createWebSocket(token);
|
|
2484
|
-
authenticationSuccess(parsedToken, socket);
|
|
2485
|
-
} else {
|
|
2486
|
-
return auth(context.roomId).then(function (_ref2) {
|
|
2487
|
-
var token = _ref2.token;
|
|
2488
|
-
|
|
2489
|
-
if (state.connection.state !== "authenticating") {
|
|
2490
|
-
return;
|
|
2491
|
-
}
|
|
2492
|
-
|
|
2493
|
-
var parsedToken = parseToken(token);
|
|
2494
|
-
var socket = createWebSocket(token);
|
|
2495
|
-
authenticationSuccess(parsedToken, socket);
|
|
2496
|
-
state.token = token;
|
|
2497
|
-
}).catch(function (er) {
|
|
2498
|
-
return authenticationFailure(er);
|
|
2499
|
-
});
|
|
2500
|
-
}
|
|
2501
|
-
},
|
|
2502
|
-
send: function send(messageOrMessages) {
|
|
2503
|
-
if (state.socket == null) {
|
|
2504
|
-
throw new Error("Can't send message if socket is null");
|
|
2505
|
-
}
|
|
2506
|
-
|
|
2507
|
-
state.socket.send(JSON.stringify(messageOrMessages));
|
|
2508
|
-
},
|
|
2509
|
-
delayFlush: function delayFlush(delay) {
|
|
2510
|
-
return setTimeout(tryFlushing, delay);
|
|
2511
|
-
},
|
|
2512
|
-
startHeartbeatInterval: function startHeartbeatInterval() {
|
|
2513
|
-
return setInterval(heartbeat, HEARTBEAT_INTERVAL);
|
|
2514
|
-
},
|
|
2515
|
-
schedulePongTimeout: function schedulePongTimeout() {
|
|
2516
|
-
return setTimeout(pongTimeout, PONG_TIMEOUT);
|
|
2517
|
-
},
|
|
2518
|
-
scheduleReconnect: function scheduleReconnect(delay) {
|
|
2519
|
-
return setTimeout(connect, delay);
|
|
2520
|
-
}
|
|
2521
|
-
};
|
|
2522
|
-
|
|
2523
|
-
function genericSubscribe(callback) {
|
|
2524
|
-
state.listeners.storage.push(callback);
|
|
2525
|
-
return function () {
|
|
2526
|
-
return remove(state.listeners.storage, callback);
|
|
2527
|
-
};
|
|
2528
|
-
}
|
|
2529
|
-
|
|
2530
|
-
function crdtSubscribe(crdt, innerCallback, options) {
|
|
2531
|
-
var cb = function cb(updates) {
|
|
2532
|
-
var relatedUpdates = [];
|
|
2533
|
-
|
|
2534
|
-
for (var _iterator = _createForOfIteratorHelperLoose(updates), _step; !(_step = _iterator()).done;) {
|
|
2535
|
-
var update = _step.value;
|
|
2536
|
-
|
|
2537
|
-
if (options != null && options.isDeep && isSameNodeOrChildOf(update.node, crdt)) {
|
|
2538
|
-
relatedUpdates.push(update);
|
|
2539
|
-
} else if (update.node._id === crdt._id) {
|
|
2540
|
-
innerCallback(update.node);
|
|
2541
|
-
}
|
|
2542
|
-
}
|
|
2543
|
-
|
|
2544
|
-
if (options != null && options.isDeep && relatedUpdates.length > 0) {
|
|
2545
|
-
innerCallback(relatedUpdates);
|
|
2546
|
-
}
|
|
2547
|
-
};
|
|
2548
|
-
|
|
2549
|
-
return genericSubscribe(cb);
|
|
2550
|
-
}
|
|
2551
|
-
|
|
2552
|
-
function createOrUpdateRootFromMessage(message) {
|
|
2553
|
-
if (message.items.length === 0) {
|
|
2554
|
-
throw new Error("Internal error: cannot load storage without items");
|
|
2555
|
-
}
|
|
2556
|
-
|
|
2557
|
-
if (state.root) {
|
|
2558
|
-
updateRoot(message.items);
|
|
2559
|
-
} else {
|
|
2560
|
-
state.root = load(message.items);
|
|
2561
|
-
}
|
|
2562
|
-
|
|
2563
|
-
for (var _key in state.defaultStorageRoot) {
|
|
2564
|
-
if (state.root.get(_key) == null) {
|
|
2565
|
-
state.root.set(_key, state.defaultStorageRoot[_key]);
|
|
2566
|
-
}
|
|
2567
|
-
}
|
|
2568
|
-
}
|
|
2569
|
-
|
|
2570
|
-
function buildRootAndParentToChildren(items) {
|
|
2571
|
-
var parentToChildren = new Map();
|
|
2572
|
-
var root = null;
|
|
2573
|
-
|
|
2574
|
-
for (var _iterator2 = _createForOfIteratorHelperLoose(items), _step2; !(_step2 = _iterator2()).done;) {
|
|
2575
|
-
var tuple = _step2.value;
|
|
2576
|
-
var parentId = tuple[1].parentId;
|
|
2577
|
-
|
|
2578
|
-
if (parentId == null) {
|
|
2579
|
-
root = tuple;
|
|
2580
|
-
} else {
|
|
2581
|
-
var children = parentToChildren.get(parentId);
|
|
2582
|
-
|
|
2583
|
-
if (children != null) {
|
|
2584
|
-
children.push(tuple);
|
|
2585
|
-
} else {
|
|
2586
|
-
parentToChildren.set(parentId, [tuple]);
|
|
2587
|
-
}
|
|
2588
|
-
}
|
|
2589
|
-
}
|
|
2590
|
-
|
|
2591
|
-
if (root == null) {
|
|
2592
|
-
throw new Error("Root can't be null");
|
|
2593
|
-
}
|
|
2594
|
-
|
|
2595
|
-
return [root, parentToChildren];
|
|
2596
|
-
}
|
|
2597
|
-
|
|
2598
|
-
function updateRoot(items) {
|
|
2599
|
-
if (!state.root) {
|
|
2600
|
-
return;
|
|
2601
|
-
}
|
|
2602
|
-
|
|
2603
|
-
var currentItems = new Map();
|
|
2604
|
-
state.items.forEach(function (liveCrdt, id) {
|
|
2605
|
-
currentItems.set(id, liveCrdt._toSerializedCrdt());
|
|
2606
|
-
});
|
|
2607
|
-
var ops = getTreesDiffOperations(currentItems, new Map(items));
|
|
2608
|
-
var result = apply(ops, false);
|
|
2609
|
-
notify(result.updates);
|
|
2610
|
-
}
|
|
2611
|
-
|
|
2612
|
-
function load(items) {
|
|
2613
|
-
var _buildRootAndParentTo = buildRootAndParentToChildren(items),
|
|
2614
|
-
root = _buildRootAndParentTo[0],
|
|
2615
|
-
parentToChildren = _buildRootAndParentTo[1];
|
|
2616
|
-
|
|
2617
|
-
return LiveObject._deserialize(root, parentToChildren, {
|
|
2618
|
-
getItem: getItem,
|
|
2619
|
-
addItem: addItem,
|
|
2620
|
-
deleteItem: deleteItem,
|
|
2621
|
-
generateId: generateId,
|
|
2622
|
-
generateOpId: generateOpId,
|
|
2623
|
-
dispatch: storageDispatch,
|
|
2624
|
-
roomId: context.roomId
|
|
2625
|
-
});
|
|
2626
|
-
}
|
|
2627
|
-
|
|
2628
|
-
function addItem(id, item) {
|
|
2629
|
-
state.items.set(id, item);
|
|
2630
|
-
}
|
|
2631
|
-
|
|
2632
|
-
function deleteItem(id) {
|
|
2633
|
-
state.items.delete(id);
|
|
2634
|
-
}
|
|
2635
|
-
|
|
2636
|
-
function getItem(id) {
|
|
2637
|
-
return state.items.get(id);
|
|
2638
|
-
}
|
|
2639
|
-
|
|
2640
|
-
function addToUndoStack(historyItem) {
|
|
2641
|
-
if (state.undoStack.length >= 50) {
|
|
2642
|
-
state.undoStack.shift();
|
|
2643
|
-
}
|
|
2644
|
-
|
|
2645
|
-
if (state.isHistoryPaused) {
|
|
2646
|
-
var _state$pausedHistory;
|
|
2647
|
-
|
|
2648
|
-
(_state$pausedHistory = state.pausedHistory).unshift.apply(_state$pausedHistory, historyItem);
|
|
2649
|
-
} else {
|
|
2650
|
-
state.undoStack.push(historyItem);
|
|
2651
|
-
}
|
|
2652
|
-
}
|
|
2653
|
-
|
|
2654
|
-
function storageDispatch(ops, reverse, storageUpdates) {
|
|
2655
|
-
if (state.isBatching) {
|
|
2656
|
-
var _state$batch$ops, _state$batch$reverseO;
|
|
2657
|
-
|
|
2658
|
-
(_state$batch$ops = state.batch.ops).push.apply(_state$batch$ops, ops);
|
|
2659
|
-
|
|
2660
|
-
storageUpdates.forEach(function (value, key) {
|
|
2661
|
-
state.batch.updates.storageUpdates.set(key, mergeStorageUpdates(state.batch.updates.storageUpdates.get(key), value));
|
|
2662
|
-
});
|
|
2663
|
-
|
|
2664
|
-
(_state$batch$reverseO = state.batch.reverseOps).push.apply(_state$batch$reverseO, reverse);
|
|
2665
|
-
} else {
|
|
2666
|
-
addToUndoStack(reverse);
|
|
2667
|
-
state.redoStack = [];
|
|
2668
|
-
dispatch(ops);
|
|
2669
|
-
notify({
|
|
2670
|
-
storageUpdates: storageUpdates
|
|
2671
|
-
});
|
|
2672
|
-
}
|
|
2673
|
-
}
|
|
2674
|
-
|
|
2675
|
-
function notify(_ref3) {
|
|
2676
|
-
var _ref3$storageUpdates = _ref3.storageUpdates,
|
|
2677
|
-
storageUpdates = _ref3$storageUpdates === void 0 ? new Map() : _ref3$storageUpdates,
|
|
2678
|
-
_ref3$presence = _ref3.presence,
|
|
2679
|
-
presence = _ref3$presence === void 0 ? false : _ref3$presence,
|
|
2680
|
-
_ref3$others = _ref3.others,
|
|
2681
|
-
otherEvents = _ref3$others === void 0 ? [] : _ref3$others;
|
|
2682
|
-
|
|
2683
|
-
if (otherEvents.length > 0) {
|
|
2684
|
-
state.others = makeOthers(state.users);
|
|
2685
|
-
|
|
2686
|
-
for (var _iterator3 = _createForOfIteratorHelperLoose(otherEvents), _step3; !(_step3 = _iterator3()).done;) {
|
|
2687
|
-
var event = _step3.value;
|
|
2688
|
-
|
|
2689
|
-
for (var _iterator4 = _createForOfIteratorHelperLoose(state.listeners.others), _step4; !(_step4 = _iterator4()).done;) {
|
|
2690
|
-
var _listener = _step4.value;
|
|
2691
|
-
|
|
2692
|
-
_listener(state.others, event);
|
|
2693
|
-
}
|
|
2694
|
-
}
|
|
2695
|
-
}
|
|
2696
|
-
|
|
2697
|
-
if (presence) {
|
|
2698
|
-
for (var _iterator5 = _createForOfIteratorHelperLoose(state.listeners["my-presence"]), _step5; !(_step5 = _iterator5()).done;) {
|
|
2699
|
-
var _listener2 = _step5.value;
|
|
2700
|
-
|
|
2701
|
-
_listener2(state.me);
|
|
2702
|
-
}
|
|
2703
|
-
}
|
|
2704
|
-
|
|
2705
|
-
if (storageUpdates.size > 0) {
|
|
2706
|
-
for (var _iterator6 = _createForOfIteratorHelperLoose(state.listeners.storage), _step6; !(_step6 = _iterator6()).done;) {
|
|
2707
|
-
var subscriber = _step6.value;
|
|
2708
|
-
subscriber(Array.from(storageUpdates.values()));
|
|
2709
|
-
}
|
|
2710
|
-
}
|
|
2711
|
-
}
|
|
2712
|
-
|
|
2713
|
-
function getConnectionId() {
|
|
2714
|
-
if (state.connection.state === "open" || state.connection.state === "connecting") {
|
|
2715
|
-
return state.connection.id;
|
|
2716
|
-
} else if (state.lastConnectionId !== null) {
|
|
2717
|
-
return state.lastConnectionId;
|
|
2718
|
-
}
|
|
2719
|
-
|
|
2720
|
-
throw new Error("Internal. Tried to get connection id but connection was never open");
|
|
2721
|
-
}
|
|
2722
|
-
|
|
2723
|
-
function generateId() {
|
|
2724
|
-
return getConnectionId() + ":" + state.clock++;
|
|
2725
|
-
}
|
|
2726
|
-
|
|
2727
|
-
function generateOpId() {
|
|
2728
|
-
return getConnectionId() + ":" + state.opClock++;
|
|
2729
|
-
}
|
|
2730
|
-
|
|
2731
|
-
function apply(item, isLocal) {
|
|
2732
|
-
var result = {
|
|
2733
|
-
reverse: [],
|
|
2734
|
-
updates: {
|
|
2735
|
-
storageUpdates: new Map(),
|
|
2736
|
-
presence: false
|
|
2737
|
-
}
|
|
2738
|
-
};
|
|
2739
|
-
|
|
2740
|
-
for (var _iterator7 = _createForOfIteratorHelperLoose(item), _step7; !(_step7 = _iterator7()).done;) {
|
|
2741
|
-
var op = _step7.value;
|
|
2742
|
-
|
|
2743
|
-
if (op.type === "presence") {
|
|
2744
|
-
var reverse = {
|
|
2745
|
-
type: "presence",
|
|
2746
|
-
data: {}
|
|
2747
|
-
};
|
|
2748
|
-
|
|
2749
|
-
for (var _key2 in op.data) {
|
|
2750
|
-
reverse.data[_key2] = state.me[_key2];
|
|
2751
|
-
}
|
|
2752
|
-
|
|
2753
|
-
state.me = _extends({}, state.me, op.data);
|
|
2754
|
-
|
|
2755
|
-
if (state.buffer.presence == null) {
|
|
2756
|
-
state.buffer.presence = op.data;
|
|
2757
|
-
} else {
|
|
2758
|
-
for (var _key3 in op.data) {
|
|
2759
|
-
state.buffer.presence[_key3] = op.data;
|
|
2760
|
-
}
|
|
2761
|
-
}
|
|
2762
|
-
|
|
2763
|
-
result.reverse.unshift(reverse);
|
|
2764
|
-
result.updates.presence = true;
|
|
2765
|
-
} else {
|
|
2766
|
-
if (isLocal && !op.opId) {
|
|
2767
|
-
op.opId = generateOpId();
|
|
2768
|
-
}
|
|
2769
|
-
|
|
2770
|
-
var applyOpResult = applyOp(op, isLocal);
|
|
2771
|
-
|
|
2772
|
-
if (applyOpResult.modified) {
|
|
2773
|
-
var _result$reverse;
|
|
2774
|
-
|
|
2775
|
-
result.updates.storageUpdates.set(applyOpResult.modified.node._id, mergeStorageUpdates(result.updates.storageUpdates.get(applyOpResult.modified.node._id), applyOpResult.modified));
|
|
2776
|
-
|
|
2777
|
-
(_result$reverse = result.reverse).unshift.apply(_result$reverse, applyOpResult.reverse);
|
|
2778
|
-
}
|
|
2779
|
-
}
|
|
2780
|
-
}
|
|
2781
|
-
|
|
2782
|
-
return result;
|
|
2783
|
-
}
|
|
2784
|
-
|
|
2785
|
-
function applyOp(op, isLocal) {
|
|
2786
|
-
if (op.opId) {
|
|
2787
|
-
state.offlineOperations.delete(op.opId);
|
|
2788
|
-
}
|
|
2789
|
-
|
|
2790
|
-
switch (op.type) {
|
|
2791
|
-
case OpType.DeleteObjectKey:
|
|
2792
|
-
case OpType.UpdateObject:
|
|
2793
|
-
case OpType.DeleteCrdt:
|
|
2794
|
-
{
|
|
2795
|
-
var item = state.items.get(op.id);
|
|
2796
|
-
|
|
2797
|
-
if (item == null) {
|
|
2798
|
-
return {
|
|
2799
|
-
modified: false
|
|
2800
|
-
};
|
|
2801
|
-
}
|
|
2802
|
-
|
|
2803
|
-
return item._apply(op, isLocal);
|
|
2804
|
-
}
|
|
2805
|
-
|
|
2806
|
-
case OpType.SetParentKey:
|
|
2807
|
-
{
|
|
2808
|
-
var _item = state.items.get(op.id);
|
|
2809
|
-
|
|
2810
|
-
if (_item == null) {
|
|
2811
|
-
return {
|
|
2812
|
-
modified: false
|
|
2813
|
-
};
|
|
2814
|
-
}
|
|
2815
|
-
|
|
2816
|
-
if (_item._parent instanceof LiveList) {
|
|
2817
|
-
var previousKey = _item._parentKey;
|
|
2818
|
-
|
|
2819
|
-
if (previousKey === op.parentKey) {
|
|
2820
|
-
return {
|
|
2821
|
-
modified: false
|
|
2822
|
-
};
|
|
2823
|
-
} else {
|
|
2824
|
-
return _item._parent._setChildKey(op.parentKey, _item, previousKey);
|
|
2825
|
-
}
|
|
2826
|
-
}
|
|
2827
|
-
|
|
2828
|
-
return {
|
|
2829
|
-
modified: false
|
|
2830
|
-
};
|
|
2831
|
-
}
|
|
2832
|
-
|
|
2833
|
-
case OpType.CreateObject:
|
|
2834
|
-
case OpType.CreateList:
|
|
2835
|
-
case OpType.CreateMap:
|
|
2836
|
-
case OpType.CreateRegister:
|
|
2837
|
-
{
|
|
2838
|
-
var parent = state.items.get(op.parentId);
|
|
2839
|
-
|
|
2840
|
-
if (parent == null) {
|
|
2841
|
-
return {
|
|
2842
|
-
modified: false
|
|
2843
|
-
};
|
|
2844
|
-
}
|
|
2845
|
-
|
|
2846
|
-
return parent._attachChild(op, isLocal);
|
|
2847
|
-
}
|
|
2848
|
-
}
|
|
2849
|
-
|
|
2850
|
-
return {
|
|
2851
|
-
modified: false
|
|
2852
|
-
};
|
|
2853
|
-
}
|
|
2854
|
-
|
|
2855
|
-
function subscribe(firstParam, listener, options) {
|
|
2856
|
-
if (firstParam instanceof AbstractCrdt) {
|
|
2857
|
-
return crdtSubscribe(firstParam, listener, options);
|
|
2858
|
-
} else if (typeof firstParam === "function") {
|
|
2859
|
-
return genericSubscribe(firstParam);
|
|
2860
|
-
} else if (!isValidRoomEventType(firstParam)) {
|
|
2861
|
-
throw new Error("\"" + firstParam + "\" is not a valid event name");
|
|
2862
|
-
}
|
|
2863
|
-
|
|
2864
|
-
state.listeners[firstParam].push(listener);
|
|
2865
|
-
return function () {
|
|
2866
|
-
var callbacks = state.listeners[firstParam];
|
|
2867
|
-
remove(callbacks, listener);
|
|
2868
|
-
};
|
|
2869
|
-
}
|
|
2870
|
-
|
|
2871
|
-
function unsubscribe(event, callback) {
|
|
2872
|
-
console.warn("unsubscribe is depreacted and will be removed in a future version.\nuse the callback returned by subscribe instead.\nSee v0.13 release notes for more information.\n");
|
|
2873
|
-
|
|
2874
|
-
if (!isValidRoomEventType(event)) {
|
|
2875
|
-
throw new Error("\"" + event + "\" is not a valid event name");
|
|
2876
|
-
}
|
|
2877
|
-
|
|
2878
|
-
var callbacks = state.listeners[event];
|
|
2879
|
-
remove(callbacks, callback);
|
|
2880
|
-
}
|
|
2881
|
-
|
|
2882
|
-
function getConnectionState() {
|
|
2883
|
-
return state.connection.state;
|
|
2884
|
-
}
|
|
2885
|
-
|
|
2886
|
-
function getSelf() {
|
|
2887
|
-
return state.connection.state === "open" || state.connection.state === "connecting" ? {
|
|
2888
|
-
connectionId: state.connection.id,
|
|
2889
|
-
id: state.connection.userId,
|
|
2890
|
-
info: state.connection.userInfo,
|
|
2891
|
-
presence: getPresence()
|
|
2892
|
-
} : null;
|
|
2893
|
-
}
|
|
2894
|
-
|
|
2895
|
-
function connect() {
|
|
2896
|
-
if (state.connection.state !== "closed" && state.connection.state !== "unavailable") {
|
|
2897
|
-
return null;
|
|
2898
|
-
}
|
|
2899
|
-
|
|
2900
|
-
var auth = prepareAuthEndpoint(context.authentication, context.fetchPolyfill);
|
|
2901
|
-
var createWebSocket = prepareCreateWebSocket(context.liveblocksServer, context.WebSocketPolyfill);
|
|
2902
|
-
updateConnection({
|
|
2903
|
-
state: "authenticating"
|
|
2904
|
-
});
|
|
2905
|
-
effects.authenticate(auth, createWebSocket);
|
|
2906
|
-
}
|
|
2907
|
-
|
|
2908
|
-
function updatePresence(overrides, options) {
|
|
2909
|
-
var oldValues = {};
|
|
2910
|
-
|
|
2911
|
-
if (state.buffer.presence == null) {
|
|
2912
|
-
state.buffer.presence = {};
|
|
2913
|
-
}
|
|
2914
|
-
|
|
2915
|
-
for (var _key4 in overrides) {
|
|
2916
|
-
state.buffer.presence[_key4] = overrides[_key4];
|
|
2917
|
-
oldValues[_key4] = state.me[_key4];
|
|
2918
|
-
}
|
|
2919
|
-
|
|
2920
|
-
state.me = _extends({}, state.me, overrides);
|
|
2921
|
-
|
|
2922
|
-
if (state.isBatching) {
|
|
2923
|
-
if (options != null && options.addToHistory) {
|
|
2924
|
-
state.batch.reverseOps.push({
|
|
2925
|
-
type: "presence",
|
|
2926
|
-
data: oldValues
|
|
2927
|
-
});
|
|
2928
|
-
}
|
|
2929
|
-
|
|
2930
|
-
state.batch.updates.presence = true;
|
|
2931
|
-
} else {
|
|
2932
|
-
tryFlushing();
|
|
2933
|
-
|
|
2934
|
-
if (options != null && options.addToHistory) {
|
|
2935
|
-
addToUndoStack([{
|
|
2936
|
-
type: "presence",
|
|
2937
|
-
data: oldValues
|
|
2938
|
-
}]);
|
|
2939
|
-
}
|
|
2940
|
-
|
|
2941
|
-
notify({
|
|
2942
|
-
presence: true
|
|
2943
|
-
});
|
|
2944
|
-
}
|
|
2945
|
-
}
|
|
2946
|
-
|
|
2947
|
-
function authenticationSuccess(token, socket) {
|
|
2948
|
-
socket.addEventListener("message", onMessage);
|
|
2949
|
-
socket.addEventListener("open", onOpen);
|
|
2950
|
-
socket.addEventListener("close", onClose);
|
|
2951
|
-
socket.addEventListener("error", onError);
|
|
2952
|
-
updateConnection({
|
|
2953
|
-
state: "connecting",
|
|
2954
|
-
id: token.actor,
|
|
2955
|
-
userInfo: token.info,
|
|
2956
|
-
userId: token.id
|
|
2957
|
-
});
|
|
2958
|
-
state.idFactory = makeIdFactory(token.actor);
|
|
2959
|
-
state.socket = socket;
|
|
2960
|
-
}
|
|
2961
|
-
|
|
2962
|
-
function authenticationFailure(error) {
|
|
2963
|
-
if (process.env.NODE_ENV !== "production") {
|
|
2964
|
-
console.error("Call to authentication endpoint failed", error);
|
|
2965
|
-
}
|
|
2966
|
-
|
|
2967
|
-
state.token = null;
|
|
2968
|
-
updateConnection({
|
|
2969
|
-
state: "unavailable"
|
|
2970
|
-
});
|
|
2971
|
-
state.numberOfRetry++;
|
|
2972
|
-
state.timeoutHandles.reconnect = effects.scheduleReconnect(getRetryDelay());
|
|
2973
|
-
}
|
|
2974
|
-
|
|
2975
|
-
function onVisibilityChange(visibilityState) {
|
|
2976
|
-
if (visibilityState === "visible" && state.connection.state === "open") {
|
|
2977
|
-
heartbeat();
|
|
2978
|
-
}
|
|
2979
|
-
}
|
|
2980
|
-
|
|
2981
|
-
function onUpdatePresenceMessage(message) {
|
|
2982
|
-
var user = state.users[message.actor];
|
|
2983
|
-
|
|
2984
|
-
if (message.targetActor === undefined && user != null && !user._hasReceivedInitialPresence) {
|
|
2985
|
-
return undefined;
|
|
2986
|
-
}
|
|
2987
|
-
|
|
2988
|
-
if (user == null) {
|
|
2989
|
-
state.users[message.actor] = {
|
|
2990
|
-
connectionId: message.actor,
|
|
2991
|
-
presence: message.data,
|
|
2992
|
-
_hasReceivedInitialPresence: true
|
|
2993
|
-
};
|
|
2994
|
-
} else {
|
|
2995
|
-
state.users[message.actor] = {
|
|
2996
|
-
id: user.id,
|
|
2997
|
-
info: user.info,
|
|
2998
|
-
connectionId: message.actor,
|
|
2999
|
-
presence: _extends({}, user.presence, message.data),
|
|
3000
|
-
_hasReceivedInitialPresence: true
|
|
3001
|
-
};
|
|
3002
|
-
}
|
|
3003
|
-
|
|
3004
|
-
return {
|
|
3005
|
-
type: "update",
|
|
3006
|
-
updates: message.data,
|
|
3007
|
-
user: state.users[message.actor]
|
|
3008
|
-
};
|
|
3009
|
-
}
|
|
3010
|
-
|
|
3011
|
-
function onUserLeftMessage(message) {
|
|
3012
|
-
var userLeftMessage = message;
|
|
3013
|
-
var user = state.users[userLeftMessage.actor];
|
|
3014
|
-
|
|
3015
|
-
if (user) {
|
|
3016
|
-
delete state.users[userLeftMessage.actor];
|
|
3017
|
-
return {
|
|
3018
|
-
type: "leave",
|
|
3019
|
-
user: user
|
|
3020
|
-
};
|
|
3021
|
-
}
|
|
3022
|
-
|
|
3023
|
-
return null;
|
|
3024
|
-
}
|
|
3025
|
-
|
|
3026
|
-
function onRoomStateMessage(message) {
|
|
3027
|
-
var newUsers = {};
|
|
3028
|
-
|
|
3029
|
-
for (var _key5 in message.users) {
|
|
3030
|
-
var _connectionId = Number.parseInt(_key5);
|
|
3031
|
-
|
|
3032
|
-
var user = message.users[_key5];
|
|
3033
|
-
newUsers[_connectionId] = {
|
|
3034
|
-
connectionId: _connectionId,
|
|
3035
|
-
info: user.info,
|
|
3036
|
-
id: user.id
|
|
3037
|
-
};
|
|
3038
|
-
}
|
|
3039
|
-
|
|
3040
|
-
state.users = newUsers;
|
|
3041
|
-
return {
|
|
3042
|
-
type: "reset"
|
|
3043
|
-
};
|
|
3044
|
-
}
|
|
3045
|
-
|
|
3046
|
-
function onNavigatorOnline() {
|
|
3047
|
-
if (state.connection.state === "unavailable") {
|
|
3048
|
-
reconnect();
|
|
3049
|
-
}
|
|
3050
|
-
}
|
|
3051
|
-
|
|
3052
|
-
function onEvent(message) {
|
|
3053
|
-
for (var _iterator8 = _createForOfIteratorHelperLoose(state.listeners.event), _step8; !(_step8 = _iterator8()).done;) {
|
|
3054
|
-
var _listener3 = _step8.value;
|
|
3055
|
-
|
|
3056
|
-
_listener3({
|
|
3057
|
-
connectionId: message.actor,
|
|
3058
|
-
event: message.event
|
|
3059
|
-
});
|
|
3060
|
-
}
|
|
3061
|
-
}
|
|
3062
|
-
|
|
3063
|
-
function onUserJoinedMessage(message) {
|
|
3064
|
-
state.users[message.actor] = {
|
|
3065
|
-
connectionId: message.actor,
|
|
3066
|
-
info: message.info,
|
|
3067
|
-
id: message.id,
|
|
3068
|
-
_hasReceivedInitialPresence: true
|
|
3069
|
-
};
|
|
3070
|
-
|
|
3071
|
-
if (state.me) {
|
|
3072
|
-
state.buffer.messages.push({
|
|
3073
|
-
type: ClientMessageType.UpdatePresence,
|
|
3074
|
-
data: state.me,
|
|
3075
|
-
targetActor: message.actor
|
|
3076
|
-
});
|
|
3077
|
-
tryFlushing();
|
|
3078
|
-
}
|
|
3079
|
-
|
|
3080
|
-
return {
|
|
3081
|
-
type: "enter",
|
|
3082
|
-
user: state.users[message.actor]
|
|
3083
|
-
};
|
|
3084
|
-
}
|
|
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
|
-
|
|
3108
|
-
function onMessage(event) {
|
|
3109
|
-
if (event.data === "pong") {
|
|
3110
|
-
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3111
|
-
return;
|
|
3112
|
-
}
|
|
3113
|
-
|
|
3114
|
-
var messages = parseServerMessages(event.data);
|
|
3115
|
-
|
|
3116
|
-
if (messages === null || messages.length === 0) {
|
|
3117
|
-
return;
|
|
3118
|
-
}
|
|
3119
|
-
|
|
3120
|
-
var updates = {
|
|
3121
|
-
storageUpdates: new Map(),
|
|
3122
|
-
others: []
|
|
3123
|
-
};
|
|
3124
|
-
|
|
3125
|
-
for (var _iterator9 = _createForOfIteratorHelperLoose(messages), _step9; !(_step9 = _iterator9()).done;) {
|
|
3126
|
-
var message = _step9.value;
|
|
3127
|
-
|
|
3128
|
-
switch (message.type) {
|
|
3129
|
-
case ServerMessageType.UserJoined:
|
|
3130
|
-
{
|
|
3131
|
-
updates.others.push(onUserJoinedMessage(message));
|
|
3132
|
-
break;
|
|
3133
|
-
}
|
|
3134
|
-
|
|
3135
|
-
case ServerMessageType.UpdatePresence:
|
|
3136
|
-
{
|
|
3137
|
-
var othersPresenceUpdate = onUpdatePresenceMessage(message);
|
|
3138
|
-
|
|
3139
|
-
if (othersPresenceUpdate) {
|
|
3140
|
-
updates.others.push(othersPresenceUpdate);
|
|
3141
|
-
}
|
|
3142
|
-
|
|
3143
|
-
break;
|
|
3144
|
-
}
|
|
3145
|
-
|
|
3146
|
-
case ServerMessageType.Event:
|
|
3147
|
-
{
|
|
3148
|
-
onEvent(message);
|
|
3149
|
-
break;
|
|
3150
|
-
}
|
|
3151
|
-
|
|
3152
|
-
case ServerMessageType.UserLeft:
|
|
3153
|
-
{
|
|
3154
|
-
var _event = onUserLeftMessage(message);
|
|
3155
|
-
|
|
3156
|
-
if (_event) {
|
|
3157
|
-
updates.others.push(_event);
|
|
3158
|
-
}
|
|
3159
|
-
|
|
3160
|
-
break;
|
|
3161
|
-
}
|
|
3162
|
-
|
|
3163
|
-
case ServerMessageType.RoomState:
|
|
3164
|
-
{
|
|
3165
|
-
updates.others.push(onRoomStateMessage(message));
|
|
3166
|
-
break;
|
|
3167
|
-
}
|
|
3168
|
-
|
|
3169
|
-
case ServerMessageType.InitialStorageState:
|
|
3170
|
-
{
|
|
3171
|
-
var offlineOps = new Map(state.offlineOperations);
|
|
3172
|
-
createOrUpdateRootFromMessage(message);
|
|
3173
|
-
applyAndSendOfflineOps(offlineOps);
|
|
3174
|
-
_getInitialStateResolver == null ? void 0 : _getInitialStateResolver();
|
|
3175
|
-
break;
|
|
3176
|
-
}
|
|
3177
|
-
|
|
3178
|
-
case ServerMessageType.UpdateStorage:
|
|
3179
|
-
{
|
|
3180
|
-
var applyResult = apply(message.ops, false);
|
|
3181
|
-
applyResult.updates.storageUpdates.forEach(function (value, key) {
|
|
3182
|
-
updates.storageUpdates.set(key, mergeStorageUpdates(updates.storageUpdates.get(key), value));
|
|
3183
|
-
});
|
|
3184
|
-
break;
|
|
3185
|
-
}
|
|
3186
|
-
}
|
|
3187
|
-
}
|
|
3188
|
-
|
|
3189
|
-
notify(updates);
|
|
3190
|
-
}
|
|
3191
|
-
|
|
3192
|
-
function onClose(event) {
|
|
3193
|
-
state.socket = null;
|
|
3194
|
-
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3195
|
-
clearInterval(state.intervalHandles.heartbeat);
|
|
3196
|
-
|
|
3197
|
-
if (state.timeoutHandles.flush) {
|
|
3198
|
-
clearTimeout(state.timeoutHandles.flush);
|
|
3199
|
-
}
|
|
3200
|
-
|
|
3201
|
-
clearTimeout(state.timeoutHandles.reconnect);
|
|
3202
|
-
state.users = {};
|
|
3203
|
-
notify({
|
|
3204
|
-
others: [{
|
|
3205
|
-
type: "reset"
|
|
3206
|
-
}]
|
|
3207
|
-
});
|
|
3208
|
-
|
|
3209
|
-
if (event.code >= 4000 && event.code <= 4100) {
|
|
3210
|
-
updateConnection({
|
|
3211
|
-
state: "failed"
|
|
3212
|
-
});
|
|
3213
|
-
var error = new LiveblocksError(event.reason, event.code);
|
|
3214
|
-
|
|
3215
|
-
for (var _iterator10 = _createForOfIteratorHelperLoose(state.listeners.error), _step10; !(_step10 = _iterator10()).done;) {
|
|
3216
|
-
var _listener4 = _step10.value;
|
|
3217
|
-
|
|
3218
|
-
_listener4(error);
|
|
3219
|
-
}
|
|
3220
|
-
|
|
3221
|
-
var _delay = getRetryDelay(true);
|
|
3222
|
-
|
|
3223
|
-
state.numberOfRetry++;
|
|
3224
|
-
|
|
3225
|
-
if (process.env.NODE_ENV !== "production") {
|
|
3226
|
-
console.error("Connection to Liveblocks websocket server closed. Reason: " + error.message + " (code: " + error.code + "). Retrying in " + _delay + "ms.");
|
|
3227
|
-
}
|
|
3228
|
-
|
|
3229
|
-
updateConnection({
|
|
3230
|
-
state: "unavailable"
|
|
3231
|
-
});
|
|
3232
|
-
state.timeoutHandles.reconnect = effects.scheduleReconnect(_delay);
|
|
3233
|
-
} else if (event.code === WebsocketCloseCodes.CLOSE_WITHOUT_RETRY) {
|
|
3234
|
-
updateConnection({
|
|
3235
|
-
state: "closed"
|
|
3236
|
-
});
|
|
3237
|
-
} else {
|
|
3238
|
-
var _delay2 = getRetryDelay();
|
|
3239
|
-
|
|
3240
|
-
state.numberOfRetry++;
|
|
3241
|
-
|
|
3242
|
-
if (process.env.NODE_ENV !== "production") {
|
|
3243
|
-
console.warn("Connection to Liveblocks websocket server closed (code: " + event.code + "). Retrying in " + _delay2 + "ms.");
|
|
3244
|
-
}
|
|
3245
|
-
|
|
3246
|
-
updateConnection({
|
|
3247
|
-
state: "unavailable"
|
|
3248
|
-
});
|
|
3249
|
-
state.timeoutHandles.reconnect = effects.scheduleReconnect(_delay2);
|
|
3250
|
-
}
|
|
3251
|
-
}
|
|
3252
|
-
|
|
3253
|
-
function updateConnection(connection) {
|
|
3254
|
-
state.connection = connection;
|
|
3255
|
-
|
|
3256
|
-
for (var _iterator11 = _createForOfIteratorHelperLoose(state.listeners.connection), _step11; !(_step11 = _iterator11()).done;) {
|
|
3257
|
-
var _listener5 = _step11.value;
|
|
3258
|
-
|
|
3259
|
-
_listener5(connection.state);
|
|
3260
|
-
}
|
|
3261
|
-
}
|
|
3262
|
-
|
|
3263
|
-
function getRetryDelay(slow) {
|
|
3264
|
-
if (slow === void 0) {
|
|
3265
|
-
slow = false;
|
|
3266
|
-
}
|
|
3267
|
-
|
|
3268
|
-
if (slow) {
|
|
3269
|
-
return BACKOFF_RETRY_DELAYS_SLOW[state.numberOfRetry < BACKOFF_RETRY_DELAYS_SLOW.length ? state.numberOfRetry : BACKOFF_RETRY_DELAYS_SLOW.length - 1];
|
|
3270
|
-
}
|
|
3271
|
-
|
|
3272
|
-
return BACKOFF_RETRY_DELAYS[state.numberOfRetry < BACKOFF_RETRY_DELAYS.length ? state.numberOfRetry : BACKOFF_RETRY_DELAYS.length - 1];
|
|
3273
|
-
}
|
|
3274
|
-
|
|
3275
|
-
function onError() {}
|
|
3276
|
-
|
|
3277
|
-
function onOpen() {
|
|
3278
|
-
clearInterval(state.intervalHandles.heartbeat);
|
|
3279
|
-
state.intervalHandles.heartbeat = effects.startHeartbeatInterval();
|
|
3280
|
-
|
|
3281
|
-
if (state.connection.state === "connecting") {
|
|
3282
|
-
updateConnection(_extends({}, state.connection, {
|
|
3283
|
-
state: "open"
|
|
3284
|
-
}));
|
|
3285
|
-
state.numberOfRetry = 0;
|
|
3286
|
-
|
|
3287
|
-
if (state.lastConnectionId !== undefined) {
|
|
3288
|
-
state.buffer.presence = state.me;
|
|
3289
|
-
tryFlushing();
|
|
3290
|
-
}
|
|
3291
|
-
|
|
3292
|
-
state.lastConnectionId = state.connection.id;
|
|
3293
|
-
|
|
3294
|
-
if (state.root) {
|
|
3295
|
-
state.buffer.messages.push({
|
|
3296
|
-
type: ClientMessageType.FetchStorage
|
|
3297
|
-
});
|
|
3298
|
-
}
|
|
3299
|
-
|
|
3300
|
-
tryFlushing();
|
|
3301
|
-
}
|
|
3302
|
-
}
|
|
3303
|
-
|
|
3304
|
-
function heartbeat() {
|
|
3305
|
-
if (state.socket == null) {
|
|
3306
|
-
return;
|
|
3307
|
-
}
|
|
3308
|
-
|
|
3309
|
-
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3310
|
-
state.timeoutHandles.pongTimeout = effects.schedulePongTimeout();
|
|
3311
|
-
|
|
3312
|
-
if (state.socket.readyState === state.socket.OPEN) {
|
|
3313
|
-
state.socket.send("ping");
|
|
3314
|
-
}
|
|
3315
|
-
}
|
|
3316
|
-
|
|
3317
|
-
function pongTimeout() {
|
|
3318
|
-
reconnect();
|
|
3319
|
-
}
|
|
3320
|
-
|
|
3321
|
-
function reconnect() {
|
|
3322
|
-
if (state.socket) {
|
|
3323
|
-
state.socket.removeEventListener("open", onOpen);
|
|
3324
|
-
state.socket.removeEventListener("message", onMessage);
|
|
3325
|
-
state.socket.removeEventListener("close", onClose);
|
|
3326
|
-
state.socket.removeEventListener("error", onError);
|
|
3327
|
-
state.socket.close();
|
|
3328
|
-
state.socket = null;
|
|
3329
|
-
}
|
|
3330
|
-
|
|
3331
|
-
updateConnection({
|
|
3332
|
-
state: "unavailable"
|
|
3333
|
-
});
|
|
3334
|
-
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3335
|
-
|
|
3336
|
-
if (state.timeoutHandles.flush) {
|
|
3337
|
-
clearTimeout(state.timeoutHandles.flush);
|
|
3338
|
-
}
|
|
3339
|
-
|
|
3340
|
-
clearTimeout(state.timeoutHandles.reconnect);
|
|
3341
|
-
clearInterval(state.intervalHandles.heartbeat);
|
|
3342
|
-
connect();
|
|
3343
|
-
}
|
|
3344
|
-
|
|
3345
|
-
function applyAndSendOfflineOps(offlineOps) {
|
|
3346
|
-
if (offlineOps.size === 0) {
|
|
3347
|
-
return;
|
|
3348
|
-
}
|
|
3349
|
-
|
|
3350
|
-
var messages = [];
|
|
3351
|
-
var ops = Array.from(offlineOps.values());
|
|
3352
|
-
var result = apply(ops, true);
|
|
3353
|
-
messages.push({
|
|
3354
|
-
type: ClientMessageType.UpdateStorage,
|
|
3355
|
-
ops: ops
|
|
3356
|
-
});
|
|
3357
|
-
notify(result.updates);
|
|
3358
|
-
effects.send(messages);
|
|
3359
|
-
}
|
|
3360
|
-
|
|
3361
|
-
function tryFlushing() {
|
|
3362
|
-
var storageOps = state.buffer.storageOperations;
|
|
3363
|
-
|
|
3364
|
-
if (storageOps.length > 0) {
|
|
3365
|
-
storageOps.forEach(function (op) {
|
|
3366
|
-
state.offlineOperations.set(op.opId, op);
|
|
3367
|
-
});
|
|
3368
|
-
}
|
|
3369
|
-
|
|
3370
|
-
if (state.socket == null || state.socket.readyState !== state.socket.OPEN) {
|
|
3371
|
-
state.buffer.storageOperations = [];
|
|
3372
|
-
return;
|
|
3373
|
-
}
|
|
3374
|
-
|
|
3375
|
-
var now = Date.now();
|
|
3376
|
-
var elapsedTime = now - state.lastFlushTime;
|
|
3377
|
-
|
|
3378
|
-
if (elapsedTime > context.throttleDelay) {
|
|
3379
|
-
var _messages = flushDataToMessages(state);
|
|
3380
|
-
|
|
3381
|
-
if (_messages.length === 0) {
|
|
3382
|
-
return;
|
|
3383
|
-
}
|
|
3384
|
-
|
|
3385
|
-
effects.send(_messages);
|
|
3386
|
-
state.buffer = {
|
|
3387
|
-
messages: [],
|
|
3388
|
-
storageOperations: [],
|
|
3389
|
-
presence: null
|
|
3390
|
-
};
|
|
3391
|
-
state.lastFlushTime = now;
|
|
3392
|
-
} else {
|
|
3393
|
-
if (state.timeoutHandles.flush != null) {
|
|
3394
|
-
clearTimeout(state.timeoutHandles.flush);
|
|
3395
|
-
}
|
|
3396
|
-
|
|
3397
|
-
state.timeoutHandles.flush = effects.delayFlush(context.throttleDelay - (now - state.lastFlushTime));
|
|
3398
|
-
}
|
|
3399
|
-
}
|
|
3400
|
-
|
|
3401
|
-
function flushDataToMessages(state) {
|
|
3402
|
-
var messages = [];
|
|
3403
|
-
|
|
3404
|
-
if (state.buffer.presence) {
|
|
3405
|
-
messages.push({
|
|
3406
|
-
type: ClientMessageType.UpdatePresence,
|
|
3407
|
-
data: state.buffer.presence
|
|
3408
|
-
});
|
|
3409
|
-
}
|
|
3410
|
-
|
|
3411
|
-
for (var _iterator12 = _createForOfIteratorHelperLoose(state.buffer.messages), _step12; !(_step12 = _iterator12()).done;) {
|
|
3412
|
-
var event = _step12.value;
|
|
3413
|
-
messages.push(event);
|
|
3414
|
-
}
|
|
3415
|
-
|
|
3416
|
-
if (state.buffer.storageOperations.length > 0) {
|
|
3417
|
-
messages.push({
|
|
3418
|
-
type: ClientMessageType.UpdateStorage,
|
|
3419
|
-
ops: state.buffer.storageOperations
|
|
3420
|
-
});
|
|
3421
|
-
}
|
|
3422
|
-
|
|
3423
|
-
return messages;
|
|
3424
|
-
}
|
|
3425
|
-
|
|
3426
|
-
function disconnect() {
|
|
3427
|
-
if (state.socket) {
|
|
3428
|
-
state.socket.removeEventListener("open", onOpen);
|
|
3429
|
-
state.socket.removeEventListener("message", onMessage);
|
|
3430
|
-
state.socket.removeEventListener("close", onClose);
|
|
3431
|
-
state.socket.removeEventListener("error", onError);
|
|
3432
|
-
state.socket.close();
|
|
3433
|
-
state.socket = null;
|
|
3434
|
-
}
|
|
3435
|
-
|
|
3436
|
-
updateConnection({
|
|
3437
|
-
state: "closed"
|
|
3438
|
-
});
|
|
3439
|
-
|
|
3440
|
-
if (state.timeoutHandles.flush) {
|
|
3441
|
-
clearTimeout(state.timeoutHandles.flush);
|
|
3442
|
-
}
|
|
3443
|
-
|
|
3444
|
-
clearTimeout(state.timeoutHandles.reconnect);
|
|
3445
|
-
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3446
|
-
clearInterval(state.intervalHandles.heartbeat);
|
|
3447
|
-
state.users = {};
|
|
3448
|
-
notify({
|
|
3449
|
-
others: [{
|
|
3450
|
-
type: "reset"
|
|
3451
|
-
}]
|
|
3452
|
-
});
|
|
3453
|
-
clearListeners();
|
|
3454
|
-
}
|
|
3455
|
-
|
|
3456
|
-
function clearListeners() {
|
|
3457
|
-
for (var _key6 in state.listeners) {
|
|
3458
|
-
state.listeners[_key6] = [];
|
|
3459
|
-
}
|
|
3460
|
-
}
|
|
3461
|
-
|
|
3462
|
-
function getPresence() {
|
|
3463
|
-
return state.me;
|
|
3464
|
-
}
|
|
3465
|
-
|
|
3466
|
-
function getOthers() {
|
|
3467
|
-
return state.others;
|
|
3468
|
-
}
|
|
3469
|
-
|
|
3470
|
-
function broadcastEvent(event, options) {
|
|
3471
|
-
if (options === void 0) {
|
|
3472
|
-
options = {
|
|
3473
|
-
shouldQueueEventIfNotReady: false
|
|
3474
|
-
};
|
|
3475
|
-
}
|
|
3476
|
-
|
|
3477
|
-
if (state.socket == null && options.shouldQueueEventIfNotReady == false) {
|
|
3478
|
-
return;
|
|
3479
|
-
}
|
|
3480
|
-
|
|
3481
|
-
state.buffer.messages.push({
|
|
3482
|
-
type: ClientMessageType.ClientEvent,
|
|
3483
|
-
event: event
|
|
3484
|
-
});
|
|
3485
|
-
tryFlushing();
|
|
3486
|
-
}
|
|
3487
|
-
|
|
3488
|
-
function dispatch(ops) {
|
|
3489
|
-
var _state$buffer$storage;
|
|
3490
|
-
|
|
3491
|
-
(_state$buffer$storage = state.buffer.storageOperations).push.apply(_state$buffer$storage, ops);
|
|
3492
|
-
|
|
3493
|
-
tryFlushing();
|
|
3494
|
-
}
|
|
3495
|
-
|
|
3496
|
-
var _getInitialStatePromise = null;
|
|
3497
|
-
var _getInitialStateResolver = null;
|
|
3498
|
-
|
|
3499
|
-
function getStorage() {
|
|
3500
|
-
if (state.root) {
|
|
3501
|
-
return new Promise(function (resolve) {
|
|
3502
|
-
return resolve({
|
|
3503
|
-
root: state.root
|
|
3504
|
-
});
|
|
3505
|
-
});
|
|
3506
|
-
}
|
|
3507
|
-
|
|
3508
|
-
if (_getInitialStatePromise == null) {
|
|
3509
|
-
state.buffer.messages.push({
|
|
3510
|
-
type: ClientMessageType.FetchStorage
|
|
3511
|
-
});
|
|
3512
|
-
tryFlushing();
|
|
3513
|
-
_getInitialStatePromise = new Promise(function (resolve) {
|
|
3514
|
-
return _getInitialStateResolver = resolve;
|
|
3515
|
-
});
|
|
3516
|
-
}
|
|
3517
|
-
|
|
3518
|
-
return _getInitialStatePromise.then(function () {
|
|
3519
|
-
return {
|
|
3520
|
-
root: state.root
|
|
3521
|
-
};
|
|
3522
|
-
});
|
|
3523
|
-
}
|
|
3524
|
-
|
|
3525
|
-
function undo() {
|
|
3526
|
-
if (state.isBatching) {
|
|
3527
|
-
throw new Error("undo is not allowed during a batch");
|
|
3528
|
-
}
|
|
3529
|
-
|
|
3530
|
-
var historyItem = state.undoStack.pop();
|
|
3531
|
-
|
|
3532
|
-
if (historyItem == null) {
|
|
3533
|
-
return;
|
|
3534
|
-
}
|
|
3535
|
-
|
|
3536
|
-
state.isHistoryPaused = false;
|
|
3537
|
-
var result = apply(historyItem, true);
|
|
3538
|
-
notify(result.updates);
|
|
3539
|
-
state.redoStack.push(result.reverse);
|
|
3540
|
-
|
|
3541
|
-
for (var _iterator13 = _createForOfIteratorHelperLoose(historyItem), _step13; !(_step13 = _iterator13()).done;) {
|
|
3542
|
-
var op = _step13.value;
|
|
3543
|
-
|
|
3544
|
-
if (op.type !== "presence") {
|
|
3545
|
-
state.buffer.storageOperations.push(op);
|
|
3546
|
-
}
|
|
3547
|
-
}
|
|
3548
|
-
|
|
3549
|
-
tryFlushing();
|
|
3550
|
-
}
|
|
3551
|
-
|
|
3552
|
-
function redo() {
|
|
3553
|
-
if (state.isBatching) {
|
|
3554
|
-
throw new Error("redo is not allowed during a batch");
|
|
3555
|
-
}
|
|
3556
|
-
|
|
3557
|
-
var historyItem = state.redoStack.pop();
|
|
3558
|
-
|
|
3559
|
-
if (historyItem == null) {
|
|
3560
|
-
return;
|
|
3561
|
-
}
|
|
3562
|
-
|
|
3563
|
-
state.isHistoryPaused = false;
|
|
3564
|
-
var result = apply(historyItem, true);
|
|
3565
|
-
notify(result.updates);
|
|
3566
|
-
state.undoStack.push(result.reverse);
|
|
3567
|
-
|
|
3568
|
-
for (var _iterator14 = _createForOfIteratorHelperLoose(historyItem), _step14; !(_step14 = _iterator14()).done;) {
|
|
3569
|
-
var op = _step14.value;
|
|
3570
|
-
|
|
3571
|
-
if (op.type !== "presence") {
|
|
3572
|
-
state.buffer.storageOperations.push(op);
|
|
3573
|
-
}
|
|
3574
|
-
}
|
|
3575
|
-
|
|
3576
|
-
tryFlushing();
|
|
3577
|
-
}
|
|
3578
|
-
|
|
3579
|
-
function batch(callback) {
|
|
3580
|
-
if (state.isBatching) {
|
|
3581
|
-
throw new Error("batch should not be called during a batch");
|
|
3582
|
-
}
|
|
3583
|
-
|
|
3584
|
-
state.isBatching = true;
|
|
3585
|
-
|
|
3586
|
-
try {
|
|
3587
|
-
callback();
|
|
3588
|
-
} finally {
|
|
3589
|
-
state.isBatching = false;
|
|
3590
|
-
|
|
3591
|
-
if (state.batch.reverseOps.length > 0) {
|
|
3592
|
-
addToUndoStack(state.batch.reverseOps);
|
|
3593
|
-
}
|
|
3594
|
-
|
|
3595
|
-
if (state.batch.ops.length > 0) {
|
|
3596
|
-
state.redoStack = [];
|
|
3597
|
-
}
|
|
3598
|
-
|
|
3599
|
-
if (state.batch.ops.length > 0) {
|
|
3600
|
-
dispatch(state.batch.ops);
|
|
3601
|
-
}
|
|
3602
|
-
|
|
3603
|
-
notify(state.batch.updates);
|
|
3604
|
-
state.batch = {
|
|
3605
|
-
ops: [],
|
|
3606
|
-
reverseOps: [],
|
|
3607
|
-
updates: {
|
|
3608
|
-
others: [],
|
|
3609
|
-
storageUpdates: new Map(),
|
|
3610
|
-
presence: false
|
|
3611
|
-
}
|
|
3612
|
-
};
|
|
3613
|
-
tryFlushing();
|
|
3614
|
-
}
|
|
3615
|
-
}
|
|
3616
|
-
|
|
3617
|
-
function pauseHistory() {
|
|
3618
|
-
state.pausedHistory = [];
|
|
3619
|
-
state.isHistoryPaused = true;
|
|
3620
|
-
}
|
|
3621
|
-
|
|
3622
|
-
function resumeHistory() {
|
|
3623
|
-
state.isHistoryPaused = false;
|
|
3624
|
-
|
|
3625
|
-
if (state.pausedHistory.length > 0) {
|
|
3626
|
-
addToUndoStack(state.pausedHistory);
|
|
3627
|
-
}
|
|
3628
|
-
|
|
3629
|
-
state.pausedHistory = [];
|
|
3630
|
-
}
|
|
3631
|
-
|
|
3632
|
-
function simulateSocketClose() {
|
|
3633
|
-
if (state.socket) {
|
|
3634
|
-
state.socket.close();
|
|
3635
|
-
}
|
|
3636
|
-
}
|
|
3637
|
-
|
|
3638
|
-
function simulateSendCloseEvent(event) {
|
|
3639
|
-
if (state.socket) {
|
|
3640
|
-
onClose(event);
|
|
3641
|
-
}
|
|
3642
|
-
}
|
|
3643
|
-
|
|
3644
|
-
return {
|
|
3645
|
-
onClose: onClose,
|
|
3646
|
-
onMessage: onMessage,
|
|
3647
|
-
authenticationSuccess: authenticationSuccess,
|
|
3648
|
-
heartbeat: heartbeat,
|
|
3649
|
-
onNavigatorOnline: onNavigatorOnline,
|
|
3650
|
-
simulateSocketClose: simulateSocketClose,
|
|
3651
|
-
simulateSendCloseEvent: simulateSendCloseEvent,
|
|
3652
|
-
onVisibilityChange: onVisibilityChange,
|
|
3653
|
-
getUndoStack: function getUndoStack() {
|
|
3654
|
-
return state.undoStack;
|
|
3655
|
-
},
|
|
3656
|
-
getItemsCount: function getItemsCount() {
|
|
3657
|
-
return state.items.size;
|
|
3658
|
-
},
|
|
3659
|
-
connect: connect,
|
|
3660
|
-
disconnect: disconnect,
|
|
3661
|
-
subscribe: subscribe,
|
|
3662
|
-
unsubscribe: unsubscribe,
|
|
3663
|
-
updatePresence: updatePresence,
|
|
3664
|
-
broadcastEvent: broadcastEvent,
|
|
3665
|
-
batch: batch,
|
|
3666
|
-
undo: undo,
|
|
3667
|
-
redo: redo,
|
|
3668
|
-
pauseHistory: pauseHistory,
|
|
3669
|
-
resumeHistory: resumeHistory,
|
|
3670
|
-
getStorage: getStorage,
|
|
3671
|
-
selectors: {
|
|
3672
|
-
getConnectionState: getConnectionState,
|
|
3673
|
-
getSelf: getSelf,
|
|
3674
|
-
getPresence: getPresence,
|
|
3675
|
-
getOthers: getOthers
|
|
3676
|
-
}
|
|
3677
|
-
};
|
|
3678
|
-
}
|
|
3679
|
-
function defaultState(me, defaultStorageRoot) {
|
|
3680
|
-
return {
|
|
3681
|
-
connection: {
|
|
3682
|
-
state: "closed"
|
|
3683
|
-
},
|
|
3684
|
-
token: null,
|
|
3685
|
-
lastConnectionId: null,
|
|
3686
|
-
socket: null,
|
|
3687
|
-
listeners: {
|
|
3688
|
-
event: [],
|
|
3689
|
-
others: [],
|
|
3690
|
-
"my-presence": [],
|
|
3691
|
-
error: [],
|
|
3692
|
-
connection: [],
|
|
3693
|
-
storage: []
|
|
3694
|
-
},
|
|
3695
|
-
numberOfRetry: 0,
|
|
3696
|
-
lastFlushTime: 0,
|
|
3697
|
-
timeoutHandles: {
|
|
3698
|
-
flush: null,
|
|
3699
|
-
reconnect: 0,
|
|
3700
|
-
pongTimeout: 0
|
|
3701
|
-
},
|
|
3702
|
-
buffer: {
|
|
3703
|
-
presence: me == null ? {} : me,
|
|
3704
|
-
messages: [],
|
|
3705
|
-
storageOperations: []
|
|
3706
|
-
},
|
|
3707
|
-
intervalHandles: {
|
|
3708
|
-
heartbeat: 0
|
|
3709
|
-
},
|
|
3710
|
-
me: me == null ? {} : me,
|
|
3711
|
-
users: {},
|
|
3712
|
-
others: makeOthers({}),
|
|
3713
|
-
defaultStorageRoot: defaultStorageRoot,
|
|
3714
|
-
idFactory: null,
|
|
3715
|
-
clock: 0,
|
|
3716
|
-
opClock: 0,
|
|
3717
|
-
items: new Map(),
|
|
3718
|
-
root: undefined,
|
|
3719
|
-
undoStack: [],
|
|
3720
|
-
redoStack: [],
|
|
3721
|
-
isHistoryPaused: false,
|
|
3722
|
-
pausedHistory: [],
|
|
3723
|
-
isBatching: false,
|
|
3724
|
-
batch: {
|
|
3725
|
-
ops: [],
|
|
3726
|
-
updates: {
|
|
3727
|
-
storageUpdates: new Map(),
|
|
3728
|
-
presence: false,
|
|
3729
|
-
others: []
|
|
3730
|
-
},
|
|
3731
|
-
reverseOps: []
|
|
3732
|
-
},
|
|
3733
|
-
offlineOperations: new Map()
|
|
3734
|
-
};
|
|
3735
|
-
}
|
|
3736
|
-
function createRoom(options, context) {
|
|
3737
|
-
var state = defaultState(options.defaultPresence, options.defaultStorageRoot);
|
|
3738
|
-
var machine = makeStateMachine(state, context);
|
|
3739
|
-
var room = {
|
|
3740
|
-
id: context.roomId,
|
|
3741
|
-
getConnectionState: machine.selectors.getConnectionState,
|
|
3742
|
-
getSelf: machine.selectors.getSelf,
|
|
3743
|
-
subscribe: machine.subscribe,
|
|
3744
|
-
unsubscribe: machine.unsubscribe,
|
|
3745
|
-
getPresence: machine.selectors.getPresence,
|
|
3746
|
-
updatePresence: machine.updatePresence,
|
|
3747
|
-
getOthers: machine.selectors.getOthers,
|
|
3748
|
-
broadcastEvent: machine.broadcastEvent,
|
|
3749
|
-
getStorage: machine.getStorage,
|
|
3750
|
-
batch: machine.batch,
|
|
3751
|
-
history: {
|
|
3752
|
-
undo: machine.undo,
|
|
3753
|
-
redo: machine.redo,
|
|
3754
|
-
pause: machine.pauseHistory,
|
|
3755
|
-
resume: machine.resumeHistory
|
|
3756
|
-
},
|
|
3757
|
-
internalDevTools: {
|
|
3758
|
-
closeWebsocket: machine.simulateSocketClose,
|
|
3759
|
-
sendCloseEvent: machine.simulateSendCloseEvent
|
|
3760
|
-
}
|
|
3761
|
-
};
|
|
3762
|
-
return {
|
|
3763
|
-
connect: machine.connect,
|
|
3764
|
-
disconnect: machine.disconnect,
|
|
3765
|
-
onNavigatorOnline: machine.onNavigatorOnline,
|
|
3766
|
-
onVisibilityChange: machine.onVisibilityChange,
|
|
3767
|
-
room: room
|
|
3768
|
-
};
|
|
3769
|
-
}
|
|
3770
|
-
|
|
3771
|
-
var LiveblocksError = function (_Error) {
|
|
3772
|
-
_inheritsLoose(LiveblocksError, _Error);
|
|
3773
|
-
|
|
3774
|
-
function LiveblocksError(message, code) {
|
|
3775
|
-
var _this;
|
|
3776
|
-
|
|
3777
|
-
_this = _Error.call(this, message) || this;
|
|
3778
|
-
_this.code = code;
|
|
3779
|
-
return _this;
|
|
3780
|
-
}
|
|
3781
|
-
|
|
3782
|
-
return LiveblocksError;
|
|
3783
|
-
}(_wrapNativeSuper(Error));
|
|
3784
|
-
|
|
3785
|
-
function parseToken(token) {
|
|
3786
|
-
var tokenParts = token.split(".");
|
|
3787
|
-
|
|
3788
|
-
if (tokenParts.length !== 3) {
|
|
3789
|
-
throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
|
|
3790
|
-
}
|
|
3791
|
-
|
|
3792
|
-
var data = parseJson(atob(tokenParts[1]));
|
|
3793
|
-
|
|
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
|
-
};
|
|
3800
|
-
}
|
|
3801
|
-
|
|
3802
|
-
throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
|
|
3803
|
-
}
|
|
3804
|
-
|
|
3805
|
-
function prepareCreateWebSocket(liveblocksServer, WebSocketPolyfill) {
|
|
3806
|
-
if (typeof window === "undefined" && WebSocketPolyfill == null) {
|
|
3807
|
-
throw new Error("To use Liveblocks client in a non-dom environment, you need to provide a WebSocket polyfill.");
|
|
3808
|
-
}
|
|
3809
|
-
|
|
3810
|
-
var ws = WebSocketPolyfill || WebSocket;
|
|
3811
|
-
return function (token) {
|
|
3812
|
-
return new ws(liveblocksServer + "/?token=" + token);
|
|
3813
|
-
};
|
|
3814
|
-
}
|
|
3815
|
-
|
|
3816
|
-
function prepareAuthEndpoint(authentication, fetchPolyfill) {
|
|
3817
|
-
if (authentication.type === "public") {
|
|
3818
|
-
if (typeof window === "undefined" && fetchPolyfill == null) {
|
|
3819
|
-
throw new Error("To use Liveblocks client in a non-dom environment with a publicApiKey, you need to provide a fetch polyfill.");
|
|
3820
|
-
}
|
|
3821
|
-
|
|
3822
|
-
return function (room) {
|
|
3823
|
-
return fetchAuthEndpoint(fetchPolyfill || fetch, authentication.url, {
|
|
3824
|
-
room: room,
|
|
3825
|
-
publicApiKey: authentication.publicApiKey
|
|
3826
|
-
});
|
|
3827
|
-
};
|
|
3828
|
-
}
|
|
3829
|
-
|
|
3830
|
-
if (authentication.type === "private") {
|
|
3831
|
-
if (typeof window === "undefined" && fetchPolyfill == null) {
|
|
3832
|
-
throw new Error("To use Liveblocks client in a non-dom environment with a url as auth endpoint, you need to provide a fetch polyfill.");
|
|
3833
|
-
}
|
|
3834
|
-
|
|
3835
|
-
return function (room) {
|
|
3836
|
-
return fetchAuthEndpoint(fetchPolyfill || fetch, authentication.url, {
|
|
3837
|
-
room: room
|
|
3838
|
-
});
|
|
3839
|
-
};
|
|
3840
|
-
}
|
|
3841
|
-
|
|
3842
|
-
if (authentication.type === "custom") {
|
|
3843
|
-
return authentication.callback;
|
|
3844
|
-
}
|
|
3845
|
-
|
|
3846
|
-
throw new Error("Internal error. Unexpected authentication type");
|
|
3847
|
-
}
|
|
3848
|
-
|
|
3849
|
-
function fetchAuthEndpoint(fetch, endpoint, body) {
|
|
3850
|
-
return fetch(endpoint, {
|
|
3851
|
-
method: "POST",
|
|
3852
|
-
headers: {
|
|
3853
|
-
"Content-Type": "application/json"
|
|
3854
|
-
},
|
|
3855
|
-
body: JSON.stringify(body)
|
|
3856
|
-
}).then(function (res) {
|
|
3857
|
-
if (!res.ok) {
|
|
3858
|
-
throw new AuthenticationError("Expected a status 200 but got " + res.status + " when doing a POST request on \"" + endpoint + "\"");
|
|
3859
|
-
}
|
|
3860
|
-
|
|
3861
|
-
return res.json().catch(function (er) {
|
|
3862
|
-
throw new AuthenticationError("Expected a json when doing a POST request on \"" + endpoint + "\". " + er);
|
|
3863
|
-
});
|
|
3864
|
-
}).then(function (authResponse) {
|
|
3865
|
-
if (typeof authResponse.token !== "string") {
|
|
3866
|
-
throw new AuthenticationError("Expected a json with a string token when doing a POST request on \"" + endpoint + "\", but got " + JSON.stringify(authResponse));
|
|
3867
|
-
}
|
|
3868
|
-
|
|
3869
|
-
return authResponse;
|
|
3870
|
-
});
|
|
3871
|
-
}
|
|
3872
|
-
|
|
3873
|
-
var AuthenticationError = function (_Error2) {
|
|
3874
|
-
_inheritsLoose(AuthenticationError, _Error2);
|
|
3875
|
-
|
|
3876
|
-
function AuthenticationError(message) {
|
|
3877
|
-
return _Error2.call(this, message) || this;
|
|
3878
|
-
}
|
|
3879
|
-
|
|
3880
|
-
return AuthenticationError;
|
|
3881
|
-
}(_wrapNativeSuper(Error));
|
|
3882
|
-
|
|
3883
|
-
function createClient(options) {
|
|
3884
|
-
var clientOptions = options;
|
|
3885
|
-
var throttleDelay = getThrottleDelayFromOptions(options);
|
|
3886
|
-
var rooms = new Map();
|
|
3887
|
-
|
|
3888
|
-
function getRoom(roomId) {
|
|
3889
|
-
var internalRoom = rooms.get(roomId);
|
|
3890
|
-
return internalRoom ? internalRoom.room : null;
|
|
3891
|
-
}
|
|
3892
|
-
|
|
3893
|
-
function enter(roomId, options) {
|
|
3894
|
-
if (options === void 0) {
|
|
3895
|
-
options = {};
|
|
3896
|
-
}
|
|
3897
|
-
|
|
3898
|
-
var internalRoom = rooms.get(roomId);
|
|
3899
|
-
|
|
3900
|
-
if (internalRoom) {
|
|
3901
|
-
return internalRoom.room;
|
|
3902
|
-
}
|
|
3903
|
-
|
|
3904
|
-
internalRoom = createRoom({
|
|
3905
|
-
defaultPresence: options.defaultPresence,
|
|
3906
|
-
defaultStorageRoot: options.defaultStorageRoot
|
|
3907
|
-
}, {
|
|
3908
|
-
roomId: roomId,
|
|
3909
|
-
throttleDelay: throttleDelay,
|
|
3910
|
-
WebSocketPolyfill: clientOptions.WebSocketPolyfill,
|
|
3911
|
-
fetchPolyfill: clientOptions.fetchPolyfill,
|
|
3912
|
-
liveblocksServer: clientOptions.liveblocksServer || "wss://liveblocks.net/v5",
|
|
3913
|
-
authentication: prepareAuthentication(clientOptions)
|
|
3914
|
-
});
|
|
3915
|
-
rooms.set(roomId, internalRoom);
|
|
3916
|
-
|
|
3917
|
-
if (!options.DO_NOT_USE_withoutConnecting) {
|
|
3918
|
-
internalRoom.connect();
|
|
3919
|
-
}
|
|
3920
|
-
|
|
3921
|
-
return internalRoom.room;
|
|
3922
|
-
}
|
|
3923
|
-
|
|
3924
|
-
function leave(roomId) {
|
|
3925
|
-
var room = rooms.get(roomId);
|
|
3926
|
-
|
|
3927
|
-
if (room) {
|
|
3928
|
-
room.disconnect();
|
|
3929
|
-
rooms.delete(roomId);
|
|
3930
|
-
}
|
|
3931
|
-
}
|
|
3932
|
-
|
|
3933
|
-
if (typeof window !== "undefined") {
|
|
3934
|
-
window.addEventListener("online", function () {
|
|
3935
|
-
for (var _iterator = _createForOfIteratorHelperLoose(rooms), _step; !(_step = _iterator()).done;) {
|
|
3936
|
-
var _step$value = _step.value,
|
|
3937
|
-
room = _step$value[1];
|
|
3938
|
-
room.onNavigatorOnline();
|
|
3939
|
-
}
|
|
3940
|
-
});
|
|
3941
|
-
}
|
|
3942
|
-
|
|
3943
|
-
if (typeof document !== "undefined") {
|
|
3944
|
-
document.addEventListener("visibilitychange", function () {
|
|
3945
|
-
for (var _iterator2 = _createForOfIteratorHelperLoose(rooms), _step2; !(_step2 = _iterator2()).done;) {
|
|
3946
|
-
var _step2$value = _step2.value,
|
|
3947
|
-
room = _step2$value[1];
|
|
3948
|
-
room.onVisibilityChange(document.visibilityState);
|
|
3949
|
-
}
|
|
3950
|
-
});
|
|
3951
|
-
}
|
|
3952
|
-
|
|
3953
|
-
return {
|
|
3954
|
-
getRoom: getRoom,
|
|
3955
|
-
enter: enter,
|
|
3956
|
-
leave: leave
|
|
3957
|
-
};
|
|
3958
|
-
}
|
|
3959
|
-
|
|
3960
|
-
function getThrottleDelayFromOptions(options) {
|
|
3961
|
-
if (options.throttle === undefined) {
|
|
3962
|
-
return 100;
|
|
3963
|
-
}
|
|
3964
|
-
|
|
3965
|
-
if (typeof options.throttle !== "number" || options.throttle < 80 || options.throttle > 1000) {
|
|
3966
|
-
throw new Error("throttle should be a number between 80 and 1000.");
|
|
3967
|
-
}
|
|
3968
|
-
|
|
3969
|
-
return options.throttle;
|
|
3970
|
-
}
|
|
3971
|
-
|
|
3972
|
-
function prepareAuthentication(clientOptions) {
|
|
3973
|
-
if (typeof clientOptions.publicApiKey === "string") {
|
|
3974
|
-
return {
|
|
3975
|
-
type: "public",
|
|
3976
|
-
publicApiKey: clientOptions.publicApiKey,
|
|
3977
|
-
url: clientOptions.publicAuthorizeEndpoint || "https://liveblocks.io/api/public/authorize"
|
|
3978
|
-
};
|
|
3979
|
-
} else if (typeof clientOptions.authEndpoint === "string") {
|
|
3980
|
-
return {
|
|
3981
|
-
type: "private",
|
|
3982
|
-
url: clientOptions.authEndpoint
|
|
3983
|
-
};
|
|
3984
|
-
} else if (typeof clientOptions.authEndpoint === "function") {
|
|
3985
|
-
return {
|
|
3986
|
-
type: "custom",
|
|
3987
|
-
callback: clientOptions.authEndpoint
|
|
3988
|
-
};
|
|
3989
|
-
}
|
|
3990
|
-
|
|
3991
|
-
throw new Error("Invalid Liveblocks client options. For more information: https://liveblocks.io/docs/api-reference/liveblocks-client#createClient");
|
|
3992
|
-
}
|
|
3993
|
-
|
|
3994
|
-
function lsonObjectToJson(obj) {
|
|
3995
|
-
var result = {};
|
|
3996
|
-
|
|
3997
|
-
for (var _key in obj) {
|
|
3998
|
-
result[_key] = lsonToJson(obj[_key]);
|
|
3999
|
-
}
|
|
4000
|
-
|
|
4001
|
-
return result;
|
|
4002
|
-
}
|
|
4003
|
-
|
|
4004
|
-
function liveObjectToJson(liveObject) {
|
|
4005
|
-
return lsonObjectToJson(liveObject.toObject());
|
|
4006
|
-
}
|
|
4007
|
-
|
|
4008
|
-
function liveMapToJson(map) {
|
|
4009
|
-
var result = {};
|
|
4010
|
-
|
|
4011
|
-
for (var _iterator = _createForOfIteratorHelperLoose(map.entries()), _step; !(_step = _iterator()).done;) {
|
|
4012
|
-
var _step$value = _step.value,
|
|
4013
|
-
_key2 = _step$value[0],
|
|
4014
|
-
value = _step$value[1];
|
|
4015
|
-
result[_key2] = lsonToJson(value);
|
|
4016
|
-
}
|
|
4017
|
-
|
|
4018
|
-
return result;
|
|
4019
|
-
}
|
|
4020
|
-
|
|
4021
|
-
function lsonListToJson(value) {
|
|
4022
|
-
return value.map(lsonToJson);
|
|
4023
|
-
}
|
|
4024
|
-
|
|
4025
|
-
function liveListToJson(value) {
|
|
4026
|
-
return lsonListToJson(value.toArray());
|
|
4027
|
-
}
|
|
4028
|
-
|
|
4029
|
-
function lsonToJson(value) {
|
|
4030
|
-
if (value instanceof LiveObject) {
|
|
4031
|
-
return liveObjectToJson(value);
|
|
4032
|
-
} else if (value instanceof LiveList) {
|
|
4033
|
-
return liveListToJson(value);
|
|
4034
|
-
} else if (value instanceof LiveMap) {
|
|
4035
|
-
return liveMapToJson(value);
|
|
4036
|
-
} else if (value instanceof LiveRegister) {
|
|
4037
|
-
return value.data;
|
|
4038
|
-
} else if (value instanceof AbstractCrdt) {
|
|
4039
|
-
throw new Error("Unhandled subclass of AbstractCrdt encountered");
|
|
4040
|
-
}
|
|
4041
|
-
|
|
4042
|
-
if (Array.isArray(value)) {
|
|
4043
|
-
return lsonListToJson(value);
|
|
4044
|
-
} else if (isPlainObject(value)) {
|
|
4045
|
-
return lsonObjectToJson(value);
|
|
4046
|
-
}
|
|
4047
|
-
|
|
4048
|
-
return value;
|
|
4049
|
-
}
|
|
4050
|
-
|
|
4051
|
-
function isPlainObject(obj) {
|
|
4052
|
-
return obj !== null && Object.prototype.toString.call(obj) === "[object Object]";
|
|
4053
|
-
}
|
|
4054
|
-
|
|
4055
|
-
function anyToCrdt(obj) {
|
|
4056
|
-
if (obj == null) {
|
|
4057
|
-
return obj;
|
|
4058
|
-
}
|
|
4059
|
-
|
|
4060
|
-
if (Array.isArray(obj)) {
|
|
4061
|
-
return new LiveList(obj.map(anyToCrdt));
|
|
4062
|
-
}
|
|
4063
|
-
|
|
4064
|
-
if (isPlainObject(obj)) {
|
|
4065
|
-
var init = {};
|
|
4066
|
-
|
|
4067
|
-
for (var _key3 in obj) {
|
|
4068
|
-
init[_key3] = anyToCrdt(obj[_key3]);
|
|
4069
|
-
}
|
|
4070
|
-
|
|
4071
|
-
return new LiveObject(init);
|
|
4072
|
-
}
|
|
4073
|
-
|
|
4074
|
-
return obj;
|
|
4075
|
-
}
|
|
4076
|
-
|
|
4077
|
-
function patchLiveList(liveList, prev, next) {
|
|
4078
|
-
var i = 0;
|
|
4079
|
-
var prevEnd = prev.length - 1;
|
|
4080
|
-
var nextEnd = next.length - 1;
|
|
4081
|
-
var prevNode = prev[0];
|
|
4082
|
-
var nextNode = next[0];
|
|
4083
|
-
|
|
4084
|
-
outer: {
|
|
4085
|
-
while (prevNode === nextNode) {
|
|
4086
|
-
++i;
|
|
4087
|
-
|
|
4088
|
-
if (i > prevEnd || i > nextEnd) {
|
|
4089
|
-
break outer;
|
|
4090
|
-
}
|
|
4091
|
-
|
|
4092
|
-
prevNode = prev[i];
|
|
4093
|
-
nextNode = next[i];
|
|
4094
|
-
}
|
|
4095
|
-
|
|
4096
|
-
prevNode = prev[prevEnd];
|
|
4097
|
-
nextNode = next[nextEnd];
|
|
4098
|
-
|
|
4099
|
-
while (prevNode === nextNode) {
|
|
4100
|
-
prevEnd--;
|
|
4101
|
-
nextEnd--;
|
|
4102
|
-
|
|
4103
|
-
if (i > prevEnd || i > nextEnd) {
|
|
4104
|
-
break outer;
|
|
4105
|
-
}
|
|
4106
|
-
|
|
4107
|
-
prevNode = prev[prevEnd];
|
|
4108
|
-
nextNode = next[nextEnd];
|
|
4109
|
-
}
|
|
4110
|
-
}
|
|
4111
|
-
|
|
4112
|
-
if (i > prevEnd) {
|
|
4113
|
-
if (i <= nextEnd) {
|
|
4114
|
-
while (i <= nextEnd) {
|
|
4115
|
-
liveList.insert(anyToCrdt(next[i]), i);
|
|
4116
|
-
i++;
|
|
4117
|
-
}
|
|
4118
|
-
}
|
|
4119
|
-
} else if (i > nextEnd) {
|
|
4120
|
-
var localI = i;
|
|
4121
|
-
|
|
4122
|
-
while (localI <= prevEnd) {
|
|
4123
|
-
liveList.delete(i);
|
|
4124
|
-
localI++;
|
|
4125
|
-
}
|
|
4126
|
-
} else {
|
|
4127
|
-
while (i <= prevEnd && i <= nextEnd) {
|
|
4128
|
-
prevNode = prev[i];
|
|
4129
|
-
nextNode = next[i];
|
|
4130
|
-
var liveListNode = liveList.get(i);
|
|
4131
|
-
|
|
4132
|
-
if (liveListNode instanceof LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
|
|
4133
|
-
patchLiveObject(liveListNode, prevNode, nextNode);
|
|
4134
|
-
} else {
|
|
4135
|
-
liveList.set(i, anyToCrdt(nextNode));
|
|
4136
|
-
}
|
|
4137
|
-
|
|
4138
|
-
i++;
|
|
4139
|
-
}
|
|
4140
|
-
|
|
4141
|
-
while (i <= nextEnd) {
|
|
4142
|
-
liveList.insert(anyToCrdt(next[i]), i);
|
|
4143
|
-
i++;
|
|
4144
|
-
}
|
|
4145
|
-
|
|
4146
|
-
var _localI = i;
|
|
4147
|
-
|
|
4148
|
-
while (_localI <= prevEnd) {
|
|
4149
|
-
liveList.delete(i);
|
|
4150
|
-
_localI++;
|
|
4151
|
-
}
|
|
4152
|
-
}
|
|
4153
|
-
}
|
|
4154
|
-
function patchLiveObjectKey(liveObject, key, prev, next) {
|
|
4155
|
-
if (process.env.NODE_ENV !== "production") {
|
|
4156
|
-
var nonSerializableValue = findNonSerializableValue(next);
|
|
4157
|
-
|
|
4158
|
-
if (nonSerializableValue) {
|
|
4159
|
-
console.error("New state path: '" + nonSerializableValue.path + "' value: '" + nonSerializableValue.value + "' is not serializable.\nOnly serializable value can be synced with Liveblocks.");
|
|
4160
|
-
return;
|
|
4161
|
-
}
|
|
4162
|
-
}
|
|
4163
|
-
|
|
4164
|
-
var value = liveObject.get(key);
|
|
4165
|
-
|
|
4166
|
-
if (next === undefined) {
|
|
4167
|
-
liveObject.delete(key);
|
|
4168
|
-
} else if (value === undefined) {
|
|
4169
|
-
liveObject.set(key, anyToCrdt(next));
|
|
4170
|
-
} else if (prev === next) {
|
|
4171
|
-
return;
|
|
4172
|
-
} else if (value instanceof LiveList && Array.isArray(prev) && Array.isArray(next)) {
|
|
4173
|
-
patchLiveList(value, prev, next);
|
|
4174
|
-
} else if (value instanceof LiveObject && isPlainObject(prev) && isPlainObject(next)) {
|
|
4175
|
-
patchLiveObject(value, prev, next);
|
|
4176
|
-
} else {
|
|
4177
|
-
liveObject.set(key, anyToCrdt(next));
|
|
4178
|
-
}
|
|
4179
|
-
}
|
|
4180
|
-
function patchLiveObject(root, prev, next) {
|
|
4181
|
-
var updates = {};
|
|
4182
|
-
|
|
4183
|
-
for (var _key4 in next) {
|
|
4184
|
-
patchLiveObjectKey(root, _key4, prev[_key4], next[_key4]);
|
|
4185
|
-
}
|
|
4186
|
-
|
|
4187
|
-
for (var _key5 in prev) {
|
|
4188
|
-
if (next[_key5] === undefined) {
|
|
4189
|
-
root.delete(_key5);
|
|
4190
|
-
}
|
|
4191
|
-
}
|
|
4192
|
-
|
|
4193
|
-
if (Object.keys(updates).length > 0) {
|
|
4194
|
-
root.update(updates);
|
|
4195
|
-
}
|
|
4196
|
-
}
|
|
4197
|
-
|
|
4198
|
-
function getParentsPath(node) {
|
|
4199
|
-
var path = [];
|
|
4200
|
-
|
|
4201
|
-
while (node._parentKey != null && node._parent != null) {
|
|
4202
|
-
if (node._parent instanceof LiveList) {
|
|
4203
|
-
path.push(node._parent._indexOfPosition(node._parentKey));
|
|
4204
|
-
} else {
|
|
4205
|
-
path.push(node._parentKey);
|
|
4206
|
-
}
|
|
4207
|
-
|
|
4208
|
-
node = node._parent;
|
|
4209
|
-
}
|
|
4210
|
-
|
|
4211
|
-
return path;
|
|
4212
|
-
}
|
|
4213
|
-
|
|
4214
|
-
function patchImmutableObject(state, updates) {
|
|
4215
|
-
return updates.reduce(function (state, update) {
|
|
4216
|
-
return patchImmutableObjectWithUpdate(state, update);
|
|
4217
|
-
}, state);
|
|
4218
|
-
}
|
|
4219
|
-
|
|
4220
|
-
function patchImmutableObjectWithUpdate(state, update) {
|
|
4221
|
-
var path = getParentsPath(update.node);
|
|
4222
|
-
return patchImmutableNode(state, path, update);
|
|
4223
|
-
}
|
|
4224
|
-
|
|
4225
|
-
function patchImmutableNode(state, path, update) {
|
|
4226
|
-
var pathItem = path.pop();
|
|
4227
|
-
|
|
4228
|
-
if (pathItem === undefined) {
|
|
4229
|
-
switch (update.type) {
|
|
4230
|
-
case "LiveObject":
|
|
4231
|
-
{
|
|
4232
|
-
if (typeof state !== "object") {
|
|
4233
|
-
throw new Error("Internal: received update on LiveObject but state was not an object");
|
|
4234
|
-
}
|
|
4235
|
-
|
|
4236
|
-
var newState = Object.assign({}, state);
|
|
4237
|
-
|
|
4238
|
-
for (var _key6 in update.updates) {
|
|
4239
|
-
var _update$updates$_key, _update$updates$_key2;
|
|
4240
|
-
|
|
4241
|
-
if (((_update$updates$_key = update.updates[_key6]) == null ? void 0 : _update$updates$_key.type) === "update") {
|
|
4242
|
-
newState[_key6] = lsonToJson(update.node.get(_key6));
|
|
4243
|
-
} else if (((_update$updates$_key2 = update.updates[_key6]) == null ? void 0 : _update$updates$_key2.type) === "delete") {
|
|
4244
|
-
delete newState[_key6];
|
|
4245
|
-
}
|
|
4246
|
-
}
|
|
4247
|
-
|
|
4248
|
-
return newState;
|
|
4249
|
-
}
|
|
4250
|
-
|
|
4251
|
-
case "LiveList":
|
|
4252
|
-
{
|
|
4253
|
-
if (Array.isArray(state) === false) {
|
|
4254
|
-
throw new Error("Internal: received update on LiveList but state was not an array");
|
|
4255
|
-
}
|
|
4256
|
-
|
|
4257
|
-
var _newState = state.map(function (x) {
|
|
4258
|
-
return x;
|
|
4259
|
-
});
|
|
4260
|
-
|
|
4261
|
-
var _loop = function _loop() {
|
|
4262
|
-
var listUpdate = _step2.value;
|
|
4263
|
-
|
|
4264
|
-
if (listUpdate.type === "set") {
|
|
4265
|
-
_newState = _newState.map(function (item, index) {
|
|
4266
|
-
return index === listUpdate.index ? listUpdate.item : item;
|
|
4267
|
-
});
|
|
4268
|
-
} else if (listUpdate.type === "insert") {
|
|
4269
|
-
if (listUpdate.index === _newState.length) {
|
|
4270
|
-
_newState.push(lsonToJson(listUpdate.item));
|
|
4271
|
-
} else {
|
|
4272
|
-
_newState = [].concat(_newState.slice(0, listUpdate.index), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index));
|
|
4273
|
-
}
|
|
4274
|
-
} else if (listUpdate.type === "delete") {
|
|
4275
|
-
_newState.splice(listUpdate.index, 1);
|
|
4276
|
-
} else if (listUpdate.type === "move") {
|
|
4277
|
-
if (listUpdate.previousIndex > listUpdate.index) {
|
|
4278
|
-
_newState = [].concat(_newState.slice(0, listUpdate.index), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1));
|
|
4279
|
-
} else {
|
|
4280
|
-
_newState = [].concat(_newState.slice(0, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index + 1));
|
|
4281
|
-
}
|
|
4282
|
-
}
|
|
4283
|
-
};
|
|
4284
|
-
|
|
4285
|
-
for (var _iterator2 = _createForOfIteratorHelperLoose(update.updates), _step2; !(_step2 = _iterator2()).done;) {
|
|
4286
|
-
_loop();
|
|
4287
|
-
}
|
|
4288
|
-
|
|
4289
|
-
return _newState;
|
|
4290
|
-
}
|
|
4291
|
-
|
|
4292
|
-
case "LiveMap":
|
|
4293
|
-
{
|
|
4294
|
-
if (typeof state !== "object") {
|
|
4295
|
-
throw new Error("Internal: received update on LiveMap but state was not an object");
|
|
4296
|
-
}
|
|
4297
|
-
|
|
4298
|
-
var _newState2 = Object.assign({}, state);
|
|
4299
|
-
|
|
4300
|
-
for (var _key7 in update.updates) {
|
|
4301
|
-
var _update$updates$_key3, _update$updates$_key4;
|
|
4302
|
-
|
|
4303
|
-
if (((_update$updates$_key3 = update.updates[_key7]) == null ? void 0 : _update$updates$_key3.type) === "update") {
|
|
4304
|
-
_newState2[_key7] = lsonToJson(update.node.get(_key7));
|
|
4305
|
-
} else if (((_update$updates$_key4 = update.updates[_key7]) == null ? void 0 : _update$updates$_key4.type) === "delete") {
|
|
4306
|
-
delete _newState2[_key7];
|
|
4307
|
-
}
|
|
4308
|
-
}
|
|
4309
|
-
|
|
4310
|
-
return _newState2;
|
|
4311
|
-
}
|
|
4312
|
-
}
|
|
4313
|
-
}
|
|
4314
|
-
|
|
4315
|
-
if (Array.isArray(state)) {
|
|
4316
|
-
var newArray = [].concat(state);
|
|
4317
|
-
newArray[pathItem] = patchImmutableNode(state[pathItem], path, update);
|
|
4318
|
-
return newArray;
|
|
4319
|
-
} else {
|
|
4320
|
-
var _extends2;
|
|
4321
|
-
|
|
4322
|
-
return _extends({}, state, (_extends2 = {}, _extends2[pathItem] = patchImmutableNode(state[pathItem], path, update), _extends2));
|
|
4323
|
-
}
|
|
4324
|
-
}
|
|
4325
|
-
|
|
4326
|
-
var internals = {
|
|
4327
|
-
liveObjectToJson: liveObjectToJson,
|
|
4328
|
-
lsonToJson: lsonToJson,
|
|
4329
|
-
patchLiveList: patchLiveList,
|
|
4330
|
-
patchImmutableObject: patchImmutableObject,
|
|
4331
|
-
patchLiveObject: patchLiveObject,
|
|
4332
|
-
patchLiveObjectKey: patchLiveObjectKey
|
|
4333
|
-
};
|
|
4334
|
-
|
|
2461
|
+
exports.AbstractCrdt = AbstractCrdt;
|
|
4335
2462
|
exports.LiveList = LiveList;
|
|
4336
2463
|
exports.LiveMap = LiveMap;
|
|
4337
2464
|
exports.LiveObject = LiveObject;
|
|
4338
|
-
exports.
|
|
4339
|
-
exports.
|
|
2465
|
+
exports.LiveRegister = LiveRegister;
|
|
2466
|
+
exports._createForOfIteratorHelperLoose = _createForOfIteratorHelperLoose;
|
|
2467
|
+
exports._extends = _extends;
|
|
2468
|
+
exports._inheritsLoose = _inheritsLoose;
|
|
2469
|
+
exports._objectWithoutPropertiesLoose = _objectWithoutPropertiesLoose;
|
|
2470
|
+
exports._wrapNativeSuper = _wrapNativeSuper;
|
|
2471
|
+
exports.compact = compact;
|
|
2472
|
+
exports.compare = compare;
|
|
2473
|
+
exports.deprecate = deprecate;
|
|
2474
|
+
exports.deprecateIf = deprecateIf;
|
|
2475
|
+
exports.findNonSerializableValue = findNonSerializableValue;
|
|
2476
|
+
exports.getTreesDiffOperations = getTreesDiffOperations;
|
|
2477
|
+
exports.isJsonArray = isJsonArray;
|
|
2478
|
+
exports.isJsonObject = isJsonObject;
|
|
2479
|
+
exports.isSameNodeOrChildOf = isSameNodeOrChildOf;
|
|
2480
|
+
exports.isTokenValid = isTokenValid;
|
|
2481
|
+
exports.makePosition = makePosition;
|
|
2482
|
+
exports.max = max;
|
|
2483
|
+
exports.mergeStorageUpdates = mergeStorageUpdates;
|
|
2484
|
+
exports.min = min;
|
|
2485
|
+
exports.parseJson = parseJson;
|
|
2486
|
+
exports.pos = pos;
|
|
2487
|
+
exports.posCodes = posCodes;
|
|
2488
|
+
exports.remove = remove;
|