@liveblocks/client 0.16.3 → 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.js CHANGED
@@ -2,2437 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- function _defineProperties(target, props) {
6
- for (var i = 0; i < props.length; i++) {
7
- var descriptor = props[i];
8
- descriptor.enumerable = descriptor.enumerable || false;
9
- descriptor.configurable = true;
10
- if ("value" in descriptor) descriptor.writable = true;
11
- Object.defineProperty(target, descriptor.key, descriptor);
12
- }
13
- }
14
-
15
- function _createClass(Constructor, protoProps, staticProps) {
16
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
- if (staticProps) _defineProperties(Constructor, staticProps);
18
- return Constructor;
19
- }
20
-
21
- function _extends() {
22
- _extends = Object.assign || function (target) {
23
- for (var i = 1; i < arguments.length; i++) {
24
- var source = arguments[i];
25
-
26
- for (var key in source) {
27
- if (Object.prototype.hasOwnProperty.call(source, key)) {
28
- target[key] = source[key];
29
- }
30
- }
31
- }
32
-
33
- return target;
34
- };
35
-
36
- return _extends.apply(this, arguments);
37
- }
38
-
39
- function _inheritsLoose(subClass, superClass) {
40
- subClass.prototype = Object.create(superClass.prototype);
41
- subClass.prototype.constructor = subClass;
42
-
43
- _setPrototypeOf(subClass, superClass);
44
- }
45
-
46
- function _getPrototypeOf(o) {
47
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
48
- return o.__proto__ || Object.getPrototypeOf(o);
49
- };
50
- return _getPrototypeOf(o);
51
- }
52
-
53
- function _setPrototypeOf(o, p) {
54
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
55
- o.__proto__ = p;
56
- return o;
57
- };
58
-
59
- return _setPrototypeOf(o, p);
60
- }
61
-
62
- function _isNativeReflectConstruct() {
63
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
64
- if (Reflect.construct.sham) return false;
65
- if (typeof Proxy === "function") return true;
66
-
67
- try {
68
- Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
69
- return true;
70
- } catch (e) {
71
- return false;
72
- }
73
- }
74
-
75
- function _construct(Parent, args, Class) {
76
- if (_isNativeReflectConstruct()) {
77
- _construct = Reflect.construct;
78
- } else {
79
- _construct = function _construct(Parent, args, Class) {
80
- var a = [null];
81
- a.push.apply(a, args);
82
- var Constructor = Function.bind.apply(Parent, a);
83
- var instance = new Constructor();
84
- if (Class) _setPrototypeOf(instance, Class.prototype);
85
- return instance;
86
- };
87
- }
88
-
89
- return _construct.apply(null, arguments);
90
- }
91
-
92
- function _isNativeFunction(fn) {
93
- return Function.toString.call(fn).indexOf("[native code]") !== -1;
94
- }
95
-
96
- function _wrapNativeSuper(Class) {
97
- var _cache = typeof Map === "function" ? new Map() : undefined;
98
-
99
- _wrapNativeSuper = function _wrapNativeSuper(Class) {
100
- if (Class === null || !_isNativeFunction(Class)) return Class;
101
-
102
- if (typeof Class !== "function") {
103
- throw new TypeError("Super expression must either be null or a function");
104
- }
105
-
106
- if (typeof _cache !== "undefined") {
107
- if (_cache.has(Class)) return _cache.get(Class);
108
-
109
- _cache.set(Class, Wrapper);
110
- }
111
-
112
- function Wrapper() {
113
- return _construct(Class, arguments, _getPrototypeOf(this).constructor);
114
- }
115
-
116
- Wrapper.prototype = Object.create(Class.prototype, {
117
- constructor: {
118
- value: Wrapper,
119
- enumerable: false,
120
- writable: true,
121
- configurable: true
122
- }
123
- });
124
- return _setPrototypeOf(Wrapper, Class);
125
- };
126
-
127
- return _wrapNativeSuper(Class);
128
- }
129
-
130
- function _objectWithoutPropertiesLoose(source, excluded) {
131
- if (source == null) return {};
132
- var target = {};
133
- var sourceKeys = Object.keys(source);
134
- var key, i;
135
-
136
- for (i = 0; i < sourceKeys.length; i++) {
137
- key = sourceKeys[i];
138
- if (excluded.indexOf(key) >= 0) continue;
139
- target[key] = source[key];
140
- }
141
-
142
- return target;
143
- }
144
-
145
- function _assertThisInitialized(self) {
146
- if (self === void 0) {
147
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
148
- }
149
-
150
- return self;
151
- }
152
-
153
- function _unsupportedIterableToArray(o, minLen) {
154
- if (!o) return;
155
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
156
- var n = Object.prototype.toString.call(o).slice(8, -1);
157
- if (n === "Object" && o.constructor) n = o.constructor.name;
158
- if (n === "Map" || n === "Set") return Array.from(o);
159
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
160
- }
161
-
162
- function _arrayLikeToArray(arr, len) {
163
- if (len == null || len > arr.length) len = arr.length;
164
-
165
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
166
-
167
- return arr2;
168
- }
169
-
170
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
171
- var it;
172
-
173
- if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
174
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
175
- if (it) o = it;
176
- var i = 0;
177
- return function () {
178
- if (i >= o.length) return {
179
- done: true
180
- };
181
- return {
182
- done: false,
183
- value: o[i++]
184
- };
185
- };
186
- }
187
-
188
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
189
- }
190
-
191
- it = o[Symbol.iterator]();
192
- return it.next.bind(it);
193
- }
194
-
195
- var ServerMessageType;
196
-
197
- (function (ServerMessageType) {
198
- ServerMessageType[ServerMessageType["UpdatePresence"] = 100] = "UpdatePresence";
199
- ServerMessageType[ServerMessageType["UserJoined"] = 101] = "UserJoined";
200
- ServerMessageType[ServerMessageType["UserLeft"] = 102] = "UserLeft";
201
- ServerMessageType[ServerMessageType["Event"] = 103] = "Event";
202
- ServerMessageType[ServerMessageType["RoomState"] = 104] = "RoomState";
203
- ServerMessageType[ServerMessageType["InitialStorageState"] = 200] = "InitialStorageState";
204
- ServerMessageType[ServerMessageType["UpdateStorage"] = 201] = "UpdateStorage";
205
- })(ServerMessageType || (ServerMessageType = {}));
206
-
207
- var ClientMessageType;
208
-
209
- (function (ClientMessageType) {
210
- ClientMessageType[ClientMessageType["UpdatePresence"] = 100] = "UpdatePresence";
211
- ClientMessageType[ClientMessageType["ClientEvent"] = 103] = "ClientEvent";
212
- ClientMessageType[ClientMessageType["FetchStorage"] = 200] = "FetchStorage";
213
- ClientMessageType[ClientMessageType["UpdateStorage"] = 201] = "UpdateStorage";
214
- })(ClientMessageType || (ClientMessageType = {}));
215
-
216
- var CrdtType;
217
-
218
- (function (CrdtType) {
219
- CrdtType[CrdtType["Object"] = 0] = "Object";
220
- CrdtType[CrdtType["List"] = 1] = "List";
221
- CrdtType[CrdtType["Map"] = 2] = "Map";
222
- CrdtType[CrdtType["Register"] = 3] = "Register";
223
- })(CrdtType || (CrdtType = {}));
224
-
225
- var OpType;
226
-
227
- (function (OpType) {
228
- OpType[OpType["Init"] = 0] = "Init";
229
- OpType[OpType["SetParentKey"] = 1] = "SetParentKey";
230
- OpType[OpType["CreateList"] = 2] = "CreateList";
231
- OpType[OpType["UpdateObject"] = 3] = "UpdateObject";
232
- OpType[OpType["CreateObject"] = 4] = "CreateObject";
233
- OpType[OpType["DeleteCrdt"] = 5] = "DeleteCrdt";
234
- OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey";
235
- OpType[OpType["CreateMap"] = 7] = "CreateMap";
236
- OpType[OpType["CreateRegister"] = 8] = "CreateRegister";
237
- })(OpType || (OpType = {}));
238
-
239
- var WebsocketCloseCodes;
240
-
241
- (function (WebsocketCloseCodes) {
242
- WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
243
- WebsocketCloseCodes[WebsocketCloseCodes["INVALID_MESSAGE_FORMAT"] = 4000] = "INVALID_MESSAGE_FORMAT";
244
- WebsocketCloseCodes[WebsocketCloseCodes["NOT_ALLOWED"] = 4001] = "NOT_ALLOWED";
245
- WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_SECONDS"] = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS";
246
- WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS"] = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS";
247
- WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
248
- WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
249
- WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_WITHOUT_RETRY"] = 4999] = "CLOSE_WITHOUT_RETRY";
250
- })(WebsocketCloseCodes || (WebsocketCloseCodes = {}));
251
-
252
- var AbstractCrdt = function () {
253
- function AbstractCrdt() {}
254
-
255
- var _proto = AbstractCrdt.prototype;
256
-
257
- _proto._apply = function _apply(op, _isLocal) {
258
- switch (op.type) {
259
- case OpType.DeleteCrdt:
260
- {
261
- if (this._parent != null && this._parentKey != null) {
262
- return this._parent._detachChild(this);
263
- }
264
-
265
- return {
266
- modified: false
267
- };
268
- }
269
- }
270
-
271
- return {
272
- modified: false
273
- };
274
- };
275
-
276
- _proto._setParentLink = function _setParentLink(parent, key) {
277
- if (this.__parent != null && this.__parent !== parent) {
278
- throw new Error("Cannot attach parent if it already exist");
279
- }
280
-
281
- this.__parentKey = key;
282
- this.__parent = parent;
283
- };
284
-
285
- _proto._attach = function _attach(id, doc) {
286
- if (this.__id || this.__doc) {
287
- throw new Error("Cannot attach if CRDT is already attached");
288
- }
289
-
290
- doc.addItem(id, this);
291
- this.__id = id;
292
- this.__doc = doc;
293
- };
294
-
295
- _proto._detach = function _detach() {
296
- if (this.__doc && this.__id) {
297
- this.__doc.deleteItem(this.__id);
298
- }
299
-
300
- this.__parent = undefined;
301
- this.__doc = undefined;
302
- };
303
-
304
- _createClass(AbstractCrdt, [{
305
- key: "_doc",
306
- get: function get() {
307
- return this.__doc;
308
- }
309
- }, {
310
- key: "roomId",
311
- get: function get() {
312
- return this.__doc ? this.__doc.roomId : null;
313
- }
314
- }, {
315
- key: "_id",
316
- get: function get() {
317
- return this.__id;
318
- }
319
- }, {
320
- key: "_parent",
321
- get: function get() {
322
- return this.__parent;
323
- }
324
- }, {
325
- key: "_parentKey",
326
- get: function get() {
327
- return this.__parentKey;
328
- }
329
- }]);
330
-
331
- return AbstractCrdt;
332
- }();
333
-
334
- var min = 32;
335
- var max = 126;
336
- function makePosition(before, after) {
337
- if (before == null && after == null) {
338
- return pos([min + 1]);
339
- }
340
-
341
- if (before != null && after == null) {
342
- return getNextPosition(before);
343
- }
344
-
345
- if (before == null && after != null) {
346
- return getPreviousPosition(after);
347
- }
348
-
349
- return pos(makePositionFromCodes(posCodes(before), posCodes(after)));
350
- }
351
-
352
- function getPreviousPosition(after) {
353
- var result = [];
354
- var afterCodes = posCodes(after);
355
-
356
- for (var i = 0; i < afterCodes.length; i++) {
357
- var code = afterCodes[i];
358
-
359
- if (code <= min + 1) {
360
- result.push(min);
361
-
362
- if (afterCodes.length - 1 === i) {
363
- result.push(max);
364
- break;
365
- }
366
- } else {
367
- result.push(code - 1);
368
- break;
369
- }
370
- }
371
-
372
- return pos(result);
373
- }
374
-
375
- function getNextPosition(before) {
376
- var result = [];
377
- var beforeCodes = posCodes(before);
378
-
379
- for (var i = 0; i < beforeCodes.length; i++) {
380
- var code = beforeCodes[i];
381
-
382
- if (code === max) {
383
- result.push(code);
384
-
385
- if (beforeCodes.length - 1 === i) {
386
- result.push(min + 1);
387
- break;
388
- }
389
- } else {
390
- result.push(code + 1);
391
- break;
392
- }
393
- }
394
-
395
- return pos(result);
396
- }
397
-
398
- function makePositionFromCodes(before, after) {
399
- var index = 0;
400
- var result = [];
401
-
402
- while (true) {
403
- var beforeDigit = before[index] || min;
404
- var afterDigit = after[index] || max;
405
-
406
- if (beforeDigit > afterDigit) {
407
- throw new Error("Impossible to generate position between " + before + " and " + after);
408
- }
409
-
410
- if (beforeDigit === afterDigit) {
411
- result.push(beforeDigit);
412
- index++;
413
- continue;
414
- }
415
-
416
- if (afterDigit - beforeDigit === 1) {
417
- result.push(beforeDigit);
418
- result.push.apply(result, makePositionFromCodes(before.slice(index + 1), []));
419
- break;
420
- }
421
-
422
- var mid = afterDigit + beforeDigit >> 1;
423
- result.push(mid);
424
- break;
425
- }
426
-
427
- return result;
428
- }
429
-
430
- function posCodes(str) {
431
- var codes = [];
432
-
433
- for (var i = 0; i < str.length; i++) {
434
- codes.push(str.charCodeAt(i));
435
- }
436
-
437
- return codes;
438
- }
439
- function pos(codes) {
440
- return String.fromCharCode.apply(String, codes);
441
- }
442
- function compare(posA, posB) {
443
- var aCodes = posCodes(posA);
444
- var bCodes = posCodes(posB);
445
- var maxLength = Math.max(aCodes.length, bCodes.length);
446
-
447
- for (var i = 0; i < maxLength; i++) {
448
- var a = aCodes[i] == null ? min : aCodes[i];
449
- var b = bCodes[i] == null ? min : bCodes[i];
450
-
451
- if (a === b) {
452
- continue;
453
- } else {
454
- return a - b;
455
- }
456
- }
457
-
458
- throw new Error("Impossible to compare similar position \"" + posA + "\" and \"" + posB + "\"");
459
- }
460
-
461
- var LiveRegister = function (_AbstractCrdt) {
462
- _inheritsLoose(LiveRegister, _AbstractCrdt);
463
-
464
- function LiveRegister(data) {
465
- var _this;
466
-
467
- _this = _AbstractCrdt.call(this) || this;
468
- _this._data = data;
469
- return _this;
470
- }
471
-
472
- LiveRegister._deserialize = function _deserialize(_ref, _parentToChildren, doc) {
473
- var id = _ref[0],
474
- item = _ref[1];
475
-
476
- if (item.type !== CrdtType.Register) {
477
- throw new Error("Tried to deserialize a map but item type is \"" + item.type + "\"");
478
- }
479
-
480
- var register = new LiveRegister(item.data);
481
-
482
- register._attach(id, doc);
483
-
484
- return register;
485
- };
486
-
487
- var _proto = LiveRegister.prototype;
488
-
489
- _proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
490
- if (this._id == null || parentId == null || parentKey == null) {
491
- throw new Error("Cannot serialize register if parentId or parentKey is undefined");
492
- }
493
-
494
- return [{
495
- type: OpType.CreateRegister,
496
- opId: doc == null ? void 0 : doc.generateOpId(),
497
- id: this._id,
498
- intent: intent,
499
- parentId: parentId,
500
- parentKey: parentKey,
501
- data: this.data
502
- }];
503
- };
504
-
505
- _proto._toSerializedCrdt = function _toSerializedCrdt() {
506
- var _this$_parent;
507
-
508
- return {
509
- type: CrdtType.Register,
510
- parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
511
- parentKey: this._parentKey,
512
- data: this.data
513
- };
514
- };
515
-
516
- _proto._attachChild = function _attachChild(_op, _isLocal) {
517
- throw new Error("Method not implemented.");
518
- };
519
-
520
- _proto._detachChild = function _detachChild(_crdt) {
521
- throw new Error("Method not implemented.");
522
- };
523
-
524
- _proto._apply = function _apply(op, isLocal) {
525
- return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
526
- };
527
-
528
- _createClass(LiveRegister, [{
529
- key: "data",
530
- get: function get() {
531
- return this._data;
532
- }
533
- }]);
534
-
535
- return LiveRegister;
536
- }(AbstractCrdt);
537
-
538
- var LiveList = function (_AbstractCrdt) {
539
- _inheritsLoose(LiveList, _AbstractCrdt);
540
-
541
- function LiveList(items) {
542
- var _this;
543
-
544
- if (items === void 0) {
545
- items = [];
546
- }
547
-
548
- _this = _AbstractCrdt.call(this) || this;
549
- _this._items = [];
550
- var position = undefined;
551
-
552
- for (var i = 0; i < items.length; i++) {
553
- var newPosition = makePosition(position);
554
-
555
- var _item = selfOrRegister(items[i]);
556
-
557
- _this._items.push([_item, newPosition]);
558
-
559
- position = newPosition;
560
- }
561
-
562
- return _this;
563
- }
564
-
565
- LiveList._deserialize = function _deserialize(_ref, parentToChildren, doc) {
566
- var id = _ref[0];
567
- var list = new LiveList([]);
568
-
569
- list._attach(id, doc);
570
-
571
- var children = parentToChildren.get(id);
572
-
573
- if (children == null) {
574
- return list;
575
- }
576
-
577
- for (var _iterator = _createForOfIteratorHelperLoose(children), _step; !(_step = _iterator()).done;) {
578
- var entry = _step.value;
579
- var child = deserialize(entry, parentToChildren, doc);
580
-
581
- child._setParentLink(list, entry[1].parentKey);
582
-
583
- list._items.push([child, entry[1].parentKey]);
584
-
585
- list._items.sort(function (itemA, itemB) {
586
- return compare(itemA[1], itemB[1]);
587
- });
588
- }
589
-
590
- return list;
591
- };
592
-
593
- var _proto = LiveList.prototype;
594
-
595
- _proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
596
- if (this._id == null) {
597
- throw new Error("Cannot serialize item is not attached");
598
- }
599
-
600
- if (parentId == null || parentKey == null) {
601
- throw new Error("Cannot serialize list if parentId or parentKey is undefined");
602
- }
603
-
604
- var ops = [];
605
- var op = {
606
- id: this._id,
607
- opId: doc == null ? void 0 : doc.generateOpId(),
608
- intent: intent,
609
- type: OpType.CreateList,
610
- parentId: parentId,
611
- parentKey: parentKey
612
- };
613
- ops.push(op);
614
-
615
- for (var _iterator2 = _createForOfIteratorHelperLoose(this._items), _step2; !(_step2 = _iterator2()).done;) {
616
- var _step2$value = _step2.value,
617
- _value = _step2$value[0],
618
- key = _step2$value[1];
619
- ops.push.apply(ops, _value._serialize(this._id, key, doc));
620
- }
621
-
622
- return ops;
623
- };
624
-
625
- _proto._indexOfPosition = function _indexOfPosition(position) {
626
- return this._items.findIndex(function (item) {
627
- return item[1] === position;
628
- });
629
- };
630
-
631
- _proto._attach = function _attach(id, doc) {
632
- _AbstractCrdt.prototype._attach.call(this, id, doc);
633
-
634
- for (var _iterator3 = _createForOfIteratorHelperLoose(this._items), _step3; !(_step3 = _iterator3()).done;) {
635
- var _step3$value = _step3.value,
636
- _item2 = _step3$value[0];
637
-
638
- _item2._attach(doc.generateId(), doc);
639
- }
640
- };
641
-
642
- _proto._detach = function _detach() {
643
- _AbstractCrdt.prototype._detach.call(this);
644
-
645
- for (var _iterator4 = _createForOfIteratorHelperLoose(this._items), _step4; !(_step4 = _iterator4()).done;) {
646
- var _step4$value = _step4.value,
647
- _value2 = _step4$value[0];
648
-
649
- _value2._detach();
650
- }
651
- };
652
-
653
- _proto._attachChild = function _attachChild(op, isLocal) {
654
- if (this._doc == null) {
655
- throw new Error("Can't attach child if doc is not present");
656
- }
657
-
658
- var id = op.id,
659
- parentKey = op.parentKey,
660
- intent = op.intent;
661
- var key = parentKey;
662
- var child = creationOpToLiveStructure(op);
663
-
664
- if (this._doc.getItem(id) !== undefined) {
665
- return {
666
- modified: false
667
- };
668
- }
669
-
670
- child._attach(id, this._doc);
671
-
672
- child._setParentLink(this, key);
673
-
674
- var index = this._items.findIndex(function (entry) {
675
- return entry[1] === key;
676
- });
677
-
678
- var newKey = key;
679
-
680
- if (index !== -1) {
681
- if (intent === "set") {
682
- var existingItem = this._items[index][0];
683
-
684
- existingItem._detach();
685
-
686
- var storageUpdate = {
687
- node: this,
688
- type: "LiveList",
689
- updates: [{
690
- index: index,
691
- type: "set",
692
- item: child instanceof LiveRegister ? child.data : child
693
- }]
694
- };
695
- this._items[index][0] = child;
696
- return {
697
- modified: storageUpdate,
698
- reverse: existingItem._serialize(this._id, key, this._doc, "set")
699
- };
700
- } else if (isLocal) {
701
- var before = this._items[index] ? this._items[index][1] : undefined;
702
- var after = this._items[index + 1] ? this._items[index + 1][1] : undefined;
703
- newKey = makePosition(before, after);
704
-
705
- child._setParentLink(this, newKey);
706
- } else {
707
- var _this$_items;
708
-
709
- this._items[index][1] = makePosition(key, (_this$_items = this._items[index + 1]) == null ? void 0 : _this$_items[1]);
710
- }
711
- }
712
-
713
- this._items.push([child, newKey]);
714
-
715
- this._items.sort(function (itemA, itemB) {
716
- return compare(itemA[1], itemB[1]);
717
- });
718
-
719
- var newIndex = this._items.findIndex(function (entry) {
720
- return entry[1] === newKey;
721
- });
722
-
723
- return {
724
- reverse: [{
725
- type: OpType.DeleteCrdt,
726
- id: id
727
- }],
728
- modified: {
729
- node: this,
730
- type: "LiveList",
731
- updates: [{
732
- index: newIndex,
733
- type: "insert",
734
- item: child instanceof LiveRegister ? child.data : child
735
- }]
736
- }
737
- };
738
- };
739
-
740
- _proto._detachChild = function _detachChild(child) {
741
- if (child) {
742
- var reverse = child._serialize(this._id, child._parentKey, this._doc);
743
-
744
- var indexToDelete = this._items.findIndex(function (item) {
745
- return item[0] === child;
746
- });
747
-
748
- this._items.splice(indexToDelete, 1);
749
-
750
- child._detach();
751
-
752
- var storageUpdate = {
753
- node: this,
754
- type: "LiveList",
755
- updates: [{
756
- index: indexToDelete,
757
- type: "delete"
758
- }]
759
- };
760
- return {
761
- modified: storageUpdate,
762
- reverse: reverse
763
- };
764
- }
765
-
766
- return {
767
- modified: false
768
- };
769
- };
770
-
771
- _proto._setChildKey = function _setChildKey(key, child, previousKey) {
772
- child._setParentLink(this, key);
773
-
774
- var previousIndex = this._items.findIndex(function (entry) {
775
- return entry[0]._id === child._id;
776
- });
777
-
778
- var index = this._items.findIndex(function (entry) {
779
- return entry[1] === key;
780
- });
781
-
782
- if (index !== -1) {
783
- var _this$_items2;
784
-
785
- this._items[index][1] = makePosition(key, (_this$_items2 = this._items[index + 1]) == null ? void 0 : _this$_items2[1]);
786
- }
787
-
788
- var item = this._items.find(function (item) {
789
- return item[0] === child;
790
- });
791
-
792
- if (item) {
793
- item[1] = key;
794
- }
795
-
796
- this._items.sort(function (itemA, itemB) {
797
- return compare(itemA[1], itemB[1]);
798
- });
799
-
800
- var newIndex = this._items.findIndex(function (entry) {
801
- return entry[0]._id === child._id;
802
- });
803
-
804
- var updatesDelta = newIndex === previousIndex ? [] : [{
805
- index: newIndex,
806
- item: child instanceof LiveRegister ? child.data : child,
807
- previousIndex: previousIndex,
808
- type: "move"
809
- }];
810
- return {
811
- modified: {
812
- node: this,
813
- type: "LiveList",
814
- updates: updatesDelta
815
- },
816
- reverse: [{
817
- type: OpType.SetParentKey,
818
- id: item == null ? void 0 : item[0]._id,
819
- parentKey: previousKey
820
- }]
821
- };
822
- };
823
-
824
- _proto._apply = function _apply(op, isLocal) {
825
- return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
826
- };
827
-
828
- _proto._toSerializedCrdt = function _toSerializedCrdt() {
829
- var _this$_parent;
830
-
831
- return {
832
- type: CrdtType.List,
833
- parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
834
- parentKey: this._parentKey
835
- };
836
- };
837
-
838
- _proto.push = function push(element) {
839
- return this.insert(element, this.length);
840
- };
841
-
842
- _proto.insert = function insert(element, index) {
843
- if (index < 0 || index > this._items.length) {
844
- throw new Error("Cannot insert list item at index \"\x1D" + index + "\". index should be between 0 and " + this._items.length);
845
- }
846
-
847
- var before = this._items[index - 1] ? this._items[index - 1][1] : undefined;
848
- var after = this._items[index] ? this._items[index][1] : undefined;
849
- var position = makePosition(before, after);
850
- var value = selfOrRegister(element);
851
-
852
- value._setParentLink(this, position);
853
-
854
- this._items.push([value, position]);
855
-
856
- this._items.sort(function (itemA, itemB) {
857
- return compare(itemA[1], itemB[1]);
858
- });
859
-
860
- var newIndex = this._items.findIndex(function (entry) {
861
- return entry[1] === position;
862
- });
863
-
864
- if (this._doc && this._id) {
865
- var _id = this._doc.generateId();
866
-
867
- value._attach(_id, this._doc);
868
-
869
- var storageUpdates = new Map();
870
- storageUpdates.set(this._id, {
871
- node: this,
872
- type: "LiveList",
873
- updates: [{
874
- index: newIndex,
875
- item: value instanceof LiveRegister ? value.data : value,
876
- type: "insert"
877
- }]
878
- });
879
-
880
- this._doc.dispatch(value._serialize(this._id, position, this._doc), [{
881
- type: OpType.DeleteCrdt,
882
- id: _id
883
- }], storageUpdates);
884
- }
885
- };
886
-
887
- _proto.move = function move(index, targetIndex) {
888
- if (targetIndex < 0) {
889
- throw new Error("targetIndex cannot be less than 0");
890
- }
891
-
892
- if (targetIndex >= this._items.length) {
893
- throw new Error("targetIndex cannot be greater or equal than the list length");
894
- }
895
-
896
- if (index < 0) {
897
- throw new Error("index cannot be less than 0");
898
- }
899
-
900
- if (index >= this._items.length) {
901
- throw new Error("index cannot be greater or equal than the list length");
902
- }
903
-
904
- var beforePosition = null;
905
- var afterPosition = null;
906
-
907
- if (index < targetIndex) {
908
- afterPosition = targetIndex === this._items.length - 1 ? undefined : this._items[targetIndex + 1][1];
909
- beforePosition = this._items[targetIndex][1];
910
- } else {
911
- afterPosition = this._items[targetIndex][1];
912
- beforePosition = targetIndex === 0 ? undefined : this._items[targetIndex - 1][1];
913
- }
914
-
915
- var position = makePosition(beforePosition, afterPosition);
916
- var item = this._items[index];
917
- var previousPosition = item[1];
918
- item[1] = position;
919
-
920
- item[0]._setParentLink(this, position);
921
-
922
- this._items.sort(function (itemA, itemB) {
923
- return compare(itemA[1], itemB[1]);
924
- });
925
-
926
- var newIndex = this._items.findIndex(function (entry) {
927
- return entry[1] === position;
928
- });
929
-
930
- if (this._doc && this._id) {
931
- var storageUpdates = new Map();
932
- storageUpdates.set(this._id, {
933
- node: this,
934
- type: "LiveList",
935
- updates: [{
936
- index: newIndex,
937
- previousIndex: index,
938
- item: item[0],
939
- type: "move"
940
- }]
941
- });
942
-
943
- this._doc.dispatch([{
944
- type: OpType.SetParentKey,
945
- id: item[0]._id,
946
- opId: this._doc.generateOpId(),
947
- parentKey: position
948
- }], [{
949
- type: OpType.SetParentKey,
950
- id: item[0]._id,
951
- parentKey: previousPosition
952
- }], storageUpdates);
953
- }
954
- };
955
-
956
- _proto.delete = function _delete(index) {
957
- if (index < 0 || index >= this._items.length) {
958
- throw new Error("Cannot delete list item at index \"\x1D" + index + "\". index should be between 0 and " + (this._items.length - 1));
959
- }
960
-
961
- var item = this._items[index];
962
-
963
- item[0]._detach();
964
-
965
- this._items.splice(index, 1);
966
-
967
- if (this._doc) {
968
- var childRecordId = item[0]._id;
969
-
970
- if (childRecordId) {
971
- var storageUpdates = new Map();
972
- storageUpdates.set(this._id, {
973
- node: this,
974
- type: "LiveList",
975
- updates: [{
976
- index: index,
977
- type: "delete"
978
- }]
979
- });
980
-
981
- this._doc.dispatch([{
982
- id: childRecordId,
983
- opId: this._doc.generateOpId(),
984
- type: OpType.DeleteCrdt
985
- }], item[0]._serialize(this._id, item[1]), storageUpdates);
986
- }
987
- }
988
- };
989
-
990
- _proto.clear = function clear() {
991
- if (this._doc) {
992
- var ops = [];
993
- var reverseOps = [];
994
- var updateDelta = [];
995
- var i = 0;
996
-
997
- for (var _iterator5 = _createForOfIteratorHelperLoose(this._items), _step5; !(_step5 = _iterator5()).done;) {
998
- var _item3 = _step5.value;
999
-
1000
- _item3[0]._detach();
1001
-
1002
- var childId = _item3[0]._id;
1003
-
1004
- if (childId) {
1005
- ops.push({
1006
- id: childId,
1007
- type: OpType.DeleteCrdt
1008
- });
1009
- reverseOps.push.apply(reverseOps, _item3[0]._serialize(this._id, _item3[1]));
1010
- updateDelta.push({
1011
- index: i,
1012
- type: "delete"
1013
- });
1014
- }
1015
-
1016
- i++;
1017
- }
1018
-
1019
- this._items = [];
1020
- var storageUpdates = new Map();
1021
- storageUpdates.set(this._id, {
1022
- node: this,
1023
- type: "LiveList",
1024
- updates: updateDelta
1025
- });
1026
-
1027
- this._doc.dispatch(ops, reverseOps, storageUpdates);
1028
- } else {
1029
- for (var _iterator6 = _createForOfIteratorHelperLoose(this._items), _step6; !(_step6 = _iterator6()).done;) {
1030
- var _item4 = _step6.value;
1031
-
1032
- _item4[0]._detach();
1033
- }
1034
-
1035
- this._items = [];
1036
- }
1037
- };
1038
-
1039
- _proto.set = function set(index, item) {
1040
- if (index < 0 || index >= this._items.length) {
1041
- throw new Error("Cannot set list item at index \"\x1D" + index + "\". index should be between 0 and " + (this._items.length - 1));
1042
- }
1043
-
1044
- var _this$_items$index = this._items[index],
1045
- existingItem = _this$_items$index[0],
1046
- position = _this$_items$index[1];
1047
-
1048
- existingItem._detach();
1049
-
1050
- var value = selfOrRegister(item);
1051
-
1052
- value._setParentLink(this, position);
1053
-
1054
- this._items[index][0] = value;
1055
-
1056
- if (this._doc && this._id) {
1057
- var _id2 = this._doc.generateId();
1058
-
1059
- value._attach(_id2, this._doc);
1060
-
1061
- var storageUpdates = new Map();
1062
- storageUpdates.set(this._id, {
1063
- node: this,
1064
- type: "LiveList",
1065
- updates: [{
1066
- index: index,
1067
- item: value instanceof LiveRegister ? value.data : value,
1068
- type: "set"
1069
- }]
1070
- });
1071
-
1072
- this._doc.dispatch(value._serialize(this._id, position, this._doc, "set"), existingItem._serialize(this._id, position, undefined, "set"), storageUpdates);
1073
- }
1074
- };
1075
-
1076
- _proto.toArray = function toArray() {
1077
- return this._items.map(function (entry) {
1078
- return selfOrRegisterValue(entry[0]);
1079
- });
1080
- };
1081
-
1082
- _proto.every = function every(predicate) {
1083
- return this.toArray().every(predicate);
1084
- };
1085
-
1086
- _proto.filter = function filter(predicate) {
1087
- return this.toArray().filter(predicate);
1088
- };
1089
-
1090
- _proto.find = function find(predicate) {
1091
- return this.toArray().find(predicate);
1092
- };
1093
-
1094
- _proto.findIndex = function findIndex(predicate) {
1095
- return this.toArray().findIndex(predicate);
1096
- };
1097
-
1098
- _proto.forEach = function forEach(callbackfn) {
1099
- return this.toArray().forEach(callbackfn);
1100
- };
1101
-
1102
- _proto.get = function get(index) {
1103
- if (index < 0 || index >= this._items.length) {
1104
- return undefined;
1105
- }
1106
-
1107
- return selfOrRegisterValue(this._items[index][0]);
1108
- };
1109
-
1110
- _proto.indexOf = function indexOf(searchElement, fromIndex) {
1111
- return this.toArray().indexOf(searchElement, fromIndex);
1112
- };
1113
-
1114
- _proto.lastIndexOf = function lastIndexOf(searchElement, fromIndex) {
1115
- return this.toArray().lastIndexOf(searchElement, fromIndex);
1116
- };
1117
-
1118
- _proto.map = function map(callback) {
1119
- return this._items.map(function (entry, i) {
1120
- return callback(selfOrRegisterValue(entry[0]), i);
1121
- });
1122
- };
1123
-
1124
- _proto.some = function some(predicate) {
1125
- return this.toArray().some(predicate);
1126
- };
1127
-
1128
- _proto[Symbol.iterator] = function () {
1129
- return new LiveListIterator(this._items);
1130
- };
1131
-
1132
- _createClass(LiveList, [{
1133
- key: "length",
1134
- get: function get() {
1135
- return this._items.length;
1136
- }
1137
- }]);
1138
-
1139
- return LiveList;
1140
- }(AbstractCrdt);
1141
-
1142
- var LiveListIterator = function () {
1143
- function LiveListIterator(items) {
1144
- this._innerIterator = items[Symbol.iterator]();
1145
- }
1146
-
1147
- var _proto2 = LiveListIterator.prototype;
1148
-
1149
- _proto2[Symbol.iterator] = function () {
1150
- return this;
1151
- };
1152
-
1153
- _proto2.next = function next() {
1154
- var result = this._innerIterator.next();
1155
-
1156
- if (result.done) {
1157
- return {
1158
- done: true,
1159
- value: undefined
1160
- };
1161
- }
1162
-
1163
- return {
1164
- value: selfOrRegisterValue(result.value[0])
1165
- };
1166
- };
1167
-
1168
- return LiveListIterator;
1169
- }();
1170
-
1171
- var LiveMap = function (_AbstractCrdt) {
1172
- _inheritsLoose(LiveMap, _AbstractCrdt);
1173
-
1174
- function LiveMap(entries) {
1175
- var _this;
1176
-
1177
- _this = _AbstractCrdt.call(this) || this;
1178
-
1179
- if (entries) {
1180
- var mappedEntries = [];
1181
-
1182
- for (var _iterator = _createForOfIteratorHelperLoose(entries), _step; !(_step = _iterator()).done;) {
1183
- var entry = _step.value;
1184
-
1185
- var _value = selfOrRegister(entry[1]);
1186
-
1187
- _value._setParentLink(_assertThisInitialized(_this), entry[0]);
1188
-
1189
- mappedEntries.push([entry[0], _value]);
1190
- }
1191
-
1192
- _this._map = new Map(mappedEntries);
1193
- } else {
1194
- _this._map = new Map();
1195
- }
1196
-
1197
- return _this;
1198
- }
1199
-
1200
- var _proto = LiveMap.prototype;
1201
-
1202
- _proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
1203
- if (this._id == null) {
1204
- throw new Error("Cannot serialize item is not attached");
1205
- }
1206
-
1207
- if (parentId == null || parentKey == null) {
1208
- throw new Error("Cannot serialize map if parentId or parentKey is undefined");
1209
- }
1210
-
1211
- var ops = [];
1212
- var op = {
1213
- id: this._id,
1214
- opId: doc == null ? void 0 : doc.generateOpId(),
1215
- type: OpType.CreateMap,
1216
- intent: intent,
1217
- parentId: parentId,
1218
- parentKey: parentKey
1219
- };
1220
- ops.push(op);
1221
-
1222
- for (var _iterator2 = _createForOfIteratorHelperLoose(this._map), _step2; !(_step2 = _iterator2()).done;) {
1223
- var _step2$value = _step2.value,
1224
- _key2 = _step2$value[0],
1225
- _value2 = _step2$value[1];
1226
- ops.push.apply(ops, _value2._serialize(this._id, _key2, doc));
1227
- }
1228
-
1229
- return ops;
1230
- };
1231
-
1232
- LiveMap._deserialize = function _deserialize(_ref, parentToChildren, doc) {
1233
- var id = _ref[0],
1234
- item = _ref[1];
1235
-
1236
- if (item.type !== CrdtType.Map) {
1237
- throw new Error("Tried to deserialize a map but item type is \"" + item.type + "\"");
1238
- }
1239
-
1240
- var map = new LiveMap();
1241
-
1242
- map._attach(id, doc);
1243
-
1244
- var children = parentToChildren.get(id);
1245
-
1246
- if (children == null) {
1247
- return map;
1248
- }
1249
-
1250
- for (var _iterator3 = _createForOfIteratorHelperLoose(children), _step3; !(_step3 = _iterator3()).done;) {
1251
- var entry = _step3.value;
1252
- var crdt = entry[1];
1253
-
1254
- if (crdt.parentKey == null) {
1255
- throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
1256
- }
1257
-
1258
- var child = deserialize(entry, parentToChildren, doc);
1259
-
1260
- child._setParentLink(map, crdt.parentKey);
1261
-
1262
- map._map.set(crdt.parentKey, child);
1263
- }
1264
-
1265
- return map;
1266
- };
1267
-
1268
- _proto._attach = function _attach(id, doc) {
1269
- _AbstractCrdt.prototype._attach.call(this, id, doc);
1270
-
1271
- for (var _iterator4 = _createForOfIteratorHelperLoose(this._map), _step4; !(_step4 = _iterator4()).done;) {
1272
- var _step4$value = _step4.value;
1273
- _step4$value[0];
1274
- var _value3 = _step4$value[1];
1275
-
1276
- if (isCrdt(_value3)) {
1277
- _value3._attach(doc.generateId(), doc);
1278
- }
1279
- }
1280
- };
1281
-
1282
- _proto._attachChild = function _attachChild(op, _isLocal) {
1283
- var _updates;
1284
-
1285
- if (this._doc == null) {
1286
- throw new Error("Can't attach child if doc is not present");
1287
- }
1288
-
1289
- var id = op.id,
1290
- parentKey = op.parentKey;
1291
- var key = parentKey;
1292
- var child = creationOpToLiveStructure(op);
1293
-
1294
- if (this._doc.getItem(id) !== undefined) {
1295
- return {
1296
- modified: false
1297
- };
1298
- }
1299
-
1300
- var previousValue = this._map.get(key);
1301
-
1302
- var reverse;
1303
-
1304
- if (previousValue) {
1305
- reverse = previousValue._serialize(this._id, key);
1306
-
1307
- previousValue._detach();
1308
- } else {
1309
- reverse = [{
1310
- type: OpType.DeleteCrdt,
1311
- id: id
1312
- }];
1313
- }
1314
-
1315
- child._setParentLink(this, key);
1316
-
1317
- child._attach(id, this._doc);
1318
-
1319
- this._map.set(key, child);
1320
-
1321
- return {
1322
- modified: {
1323
- node: this,
1324
- type: "LiveMap",
1325
- updates: (_updates = {}, _updates[key] = {
1326
- type: "update"
1327
- }, _updates)
1328
- },
1329
- reverse: reverse
1330
- };
1331
- };
1332
-
1333
- _proto._detach = function _detach() {
1334
- _AbstractCrdt.prototype._detach.call(this);
1335
-
1336
- for (var _iterator5 = _createForOfIteratorHelperLoose(this._map.values()), _step5; !(_step5 = _iterator5()).done;) {
1337
- var item = _step5.value;
1338
-
1339
- item._detach();
1340
- }
1341
- };
1342
-
1343
- _proto._detachChild = function _detachChild(child) {
1344
- var _updates2;
1345
-
1346
- var reverse = child._serialize(this._id, child._parentKey, this._doc);
1347
-
1348
- for (var _iterator6 = _createForOfIteratorHelperLoose(this._map), _step6; !(_step6 = _iterator6()).done;) {
1349
- var _step6$value = _step6.value,
1350
- _key3 = _step6$value[0],
1351
- _value4 = _step6$value[1];
1352
-
1353
- if (_value4 === child) {
1354
- this._map.delete(_key3);
1355
- }
1356
- }
1357
-
1358
- child._detach();
1359
-
1360
- var storageUpdate = {
1361
- node: this,
1362
- type: "LiveMap",
1363
- updates: (_updates2 = {}, _updates2[child._parentKey] = {
1364
- type: "delete"
1365
- }, _updates2)
1366
- };
1367
- return {
1368
- modified: storageUpdate,
1369
- reverse: reverse
1370
- };
1371
- };
1372
-
1373
- _proto._toSerializedCrdt = function _toSerializedCrdt() {
1374
- var _this$_parent;
1375
-
1376
- return {
1377
- type: CrdtType.Map,
1378
- parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
1379
- parentKey: this._parentKey
1380
- };
1381
- };
1382
-
1383
- _proto.get = function get(key) {
1384
- var value = this._map.get(key);
1385
-
1386
- if (value == undefined) {
1387
- return undefined;
1388
- }
1389
-
1390
- return selfOrRegisterValue(value);
1391
- };
1392
-
1393
- _proto.set = function set(key, value) {
1394
- var oldValue = this._map.get(key);
1395
-
1396
- if (oldValue) {
1397
- oldValue._detach();
1398
- }
1399
-
1400
- var item = selfOrRegister(value);
1401
-
1402
- item._setParentLink(this, key);
1403
-
1404
- this._map.set(key, item);
1405
-
1406
- if (this._doc && this._id) {
1407
- var _updates3;
1408
-
1409
- var id = this._doc.generateId();
1410
-
1411
- item._attach(id, this._doc);
1412
-
1413
- var storageUpdates = new Map();
1414
- storageUpdates.set(this._id, {
1415
- node: this,
1416
- type: "LiveMap",
1417
- updates: (_updates3 = {}, _updates3[key] = {
1418
- type: "update"
1419
- }, _updates3)
1420
- });
1421
-
1422
- this._doc.dispatch(item._serialize(this._id, key, this._doc), oldValue ? oldValue._serialize(this._id, key) : [{
1423
- type: OpType.DeleteCrdt,
1424
- id: id
1425
- }], storageUpdates);
1426
- }
1427
- };
1428
-
1429
- _proto.has = function has(key) {
1430
- return this._map.has(key);
1431
- };
1432
-
1433
- _proto.delete = function _delete(key) {
1434
- var item = this._map.get(key);
1435
-
1436
- if (item == null) {
1437
- return false;
1438
- }
1439
-
1440
- item._detach();
1441
-
1442
- this._map.delete(key);
1443
-
1444
- if (this._doc && item._id) {
1445
- var _updates4;
1446
-
1447
- var storageUpdates = new Map();
1448
- storageUpdates.set(this._id, {
1449
- node: this,
1450
- type: "LiveMap",
1451
- updates: (_updates4 = {}, _updates4[key] = {
1452
- type: "delete"
1453
- }, _updates4)
1454
- });
1455
-
1456
- this._doc.dispatch([{
1457
- type: OpType.DeleteCrdt,
1458
- id: item._id,
1459
- opId: this._doc.generateOpId()
1460
- }], item._serialize(this._id, key), storageUpdates);
1461
- }
1462
-
1463
- return true;
1464
- };
1465
-
1466
- _proto.entries = function entries() {
1467
- var _ref2;
1468
-
1469
- var innerIterator = this._map.entries();
1470
-
1471
- return _ref2 = {}, _ref2[Symbol.iterator] = function () {
1472
- return this;
1473
- }, _ref2.next = function next() {
1474
- var iteratorValue = innerIterator.next();
1475
-
1476
- if (iteratorValue.done) {
1477
- return {
1478
- done: true,
1479
- value: undefined
1480
- };
1481
- }
1482
-
1483
- var entry = iteratorValue.value;
1484
- return {
1485
- value: [entry[0], selfOrRegisterValue(iteratorValue.value[1])]
1486
- };
1487
- }, _ref2;
1488
- };
1489
-
1490
- _proto[Symbol.iterator] = function () {
1491
- return this.entries();
1492
- };
1493
-
1494
- _proto.keys = function keys() {
1495
- return this._map.keys();
1496
- };
1497
-
1498
- _proto.values = function values() {
1499
- var _ref3;
1500
-
1501
- var innerIterator = this._map.values();
1502
-
1503
- return _ref3 = {}, _ref3[Symbol.iterator] = function () {
1504
- return this;
1505
- }, _ref3.next = function next() {
1506
- var iteratorValue = innerIterator.next();
1507
-
1508
- if (iteratorValue.done) {
1509
- return {
1510
- done: true,
1511
- value: undefined
1512
- };
1513
- }
1514
-
1515
- return {
1516
- value: selfOrRegisterValue(iteratorValue.value)
1517
- };
1518
- }, _ref3;
1519
- };
1520
-
1521
- _proto.forEach = function forEach(callback) {
1522
- for (var _iterator7 = _createForOfIteratorHelperLoose(this), _step7; !(_step7 = _iterator7()).done;) {
1523
- var entry = _step7.value;
1524
- callback(entry[1], entry[0], this);
1525
- }
1526
- };
1527
-
1528
- _createClass(LiveMap, [{
1529
- key: "size",
1530
- get: function get() {
1531
- return this._map.size;
1532
- }
1533
- }]);
1534
-
1535
- return LiveMap;
1536
- }(AbstractCrdt);
1537
-
1538
- function parseJson(rawMessage) {
1539
- try {
1540
- return JSON.parse(rawMessage);
1541
- } catch (e) {
1542
- return undefined;
1543
- }
1544
- }
1545
- function isJsonArray(data) {
1546
- return Array.isArray(data);
1547
- }
1548
- function isJsonObject(data) {
1549
- return data !== null && typeof data === "object" && !isJsonArray(data);
1550
- }
1551
-
1552
- function remove(array, item) {
1553
- for (var i = 0; i < array.length; i++) {
1554
- if (array[i] === item) {
1555
- array.splice(i, 1);
1556
- break;
1557
- }
1558
- }
1559
- }
1560
- function compact(items) {
1561
- return items.filter(function (item) {
1562
- return item != null;
1563
- });
1564
- }
1565
- function creationOpToLiveStructure(op) {
1566
- switch (op.type) {
1567
- case OpType.CreateRegister:
1568
- return new LiveRegister(op.data);
1569
-
1570
- case OpType.CreateObject:
1571
- return new LiveObject(op.data);
1572
-
1573
- case OpType.CreateMap:
1574
- return new LiveMap();
1575
-
1576
- case OpType.CreateList:
1577
- return new LiveList();
1578
- }
1579
- }
1580
- function isSameNodeOrChildOf(node, parent) {
1581
- if (node === parent) {
1582
- return true;
1583
- }
1584
-
1585
- if (node._parent) {
1586
- return isSameNodeOrChildOf(node._parent, parent);
1587
- }
1588
-
1589
- return false;
1590
- }
1591
- function deserialize(entry, parentToChildren, doc) {
1592
- switch (entry[1].type) {
1593
- case CrdtType.Object:
1594
- {
1595
- return LiveObject._deserialize(entry, parentToChildren, doc);
1596
- }
1597
-
1598
- case CrdtType.List:
1599
- {
1600
- return LiveList._deserialize(entry, parentToChildren, doc);
1601
- }
1602
-
1603
- case CrdtType.Map:
1604
- {
1605
- return LiveMap._deserialize(entry, parentToChildren, doc);
1606
- }
1607
-
1608
- case CrdtType.Register:
1609
- {
1610
- return LiveRegister._deserialize(entry, parentToChildren, doc);
1611
- }
1612
-
1613
- default:
1614
- {
1615
- throw new Error("Unexpected CRDT type");
1616
- }
1617
- }
1618
- }
1619
- function isCrdt(obj) {
1620
- return obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList || obj instanceof LiveRegister;
1621
- }
1622
- function selfOrRegisterValue(obj) {
1623
- if (obj instanceof LiveRegister) {
1624
- return obj.data;
1625
- }
1626
-
1627
- return obj;
1628
- }
1629
- function selfOrRegister(obj) {
1630
- if (obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList) {
1631
- return obj;
1632
- } else if (obj instanceof LiveRegister) {
1633
- throw new Error("Internal error. LiveRegister should not be created from selfOrRegister");
1634
- } else {
1635
- return new LiveRegister(obj);
1636
- }
1637
- }
1638
- function getTreesDiffOperations(currentItems, newItems) {
1639
- var ops = [];
1640
- currentItems.forEach(function (_, id) {
1641
- if (!newItems.get(id)) {
1642
- ops.push({
1643
- type: OpType.DeleteCrdt,
1644
- id: id
1645
- });
1646
- }
1647
- });
1648
- newItems.forEach(function (crdt, id) {
1649
- var currentCrdt = currentItems.get(id);
1650
-
1651
- if (currentCrdt) {
1652
- if (crdt.type === CrdtType.Object) {
1653
- if (JSON.stringify(crdt.data) !== JSON.stringify(currentCrdt.data)) {
1654
- ops.push({
1655
- type: OpType.UpdateObject,
1656
- id: id,
1657
- data: crdt.data
1658
- });
1659
- }
1660
- }
1661
-
1662
- if (crdt.parentKey !== currentCrdt.parentKey) {
1663
- ops.push({
1664
- type: OpType.SetParentKey,
1665
- id: id,
1666
- parentKey: crdt.parentKey
1667
- });
1668
- }
1669
- } else {
1670
- switch (crdt.type) {
1671
- case CrdtType.Register:
1672
- ops.push({
1673
- type: OpType.CreateRegister,
1674
- id: id,
1675
- parentId: crdt.parentId,
1676
- parentKey: crdt.parentKey,
1677
- data: crdt.data
1678
- });
1679
- break;
1680
-
1681
- case CrdtType.List:
1682
- ops.push({
1683
- type: OpType.CreateList,
1684
- id: id,
1685
- parentId: crdt.parentId,
1686
- parentKey: crdt.parentKey
1687
- });
1688
- break;
1689
-
1690
- case CrdtType.Object:
1691
- ops.push({
1692
- type: OpType.CreateObject,
1693
- id: id,
1694
- parentId: crdt.parentId,
1695
- parentKey: crdt.parentKey,
1696
- data: crdt.data
1697
- });
1698
- break;
1699
-
1700
- case CrdtType.Map:
1701
- ops.push({
1702
- type: OpType.CreateMap,
1703
- id: id,
1704
- parentId: crdt.parentId,
1705
- parentKey: crdt.parentKey
1706
- });
1707
- break;
1708
- }
1709
- }
1710
- });
1711
- return ops;
1712
- }
1713
-
1714
- function mergeObjectStorageUpdates(first, second) {
1715
- var updates = first.updates;
1716
-
1717
- for (var _iterator = _createForOfIteratorHelperLoose(entries(second.updates)), _step; !(_step = _iterator()).done;) {
1718
- var _step$value = _step.value,
1719
- _key = _step$value[0],
1720
- value = _step$value[1];
1721
- updates[_key] = value;
1722
- }
1723
-
1724
- return _extends({}, second, {
1725
- updates: updates
1726
- });
1727
- }
1728
-
1729
- function mergeMapStorageUpdates(first, second) {
1730
- var updates = first.updates;
1731
-
1732
- for (var _iterator2 = _createForOfIteratorHelperLoose(entries(second.updates)), _step2; !(_step2 = _iterator2()).done;) {
1733
- var _step2$value = _step2.value,
1734
- _key2 = _step2$value[0],
1735
- value = _step2$value[1];
1736
- updates[_key2] = value;
1737
- }
1738
-
1739
- return _extends({}, second, {
1740
- updates: updates
1741
- });
1742
- }
1743
-
1744
- function mergeListStorageUpdates(first, second) {
1745
- var updates = first.updates;
1746
- return _extends({}, second, {
1747
- updates: updates.concat(second.updates)
1748
- });
1749
- }
1750
-
1751
- function mergeStorageUpdates(first, second) {
1752
- if (!first) {
1753
- return second;
1754
- }
1755
-
1756
- if (first.type === "LiveObject" && second.type === "LiveObject") {
1757
- return mergeObjectStorageUpdates(first, second);
1758
- } else if (first.type === "LiveMap" && second.type === "LiveMap") {
1759
- return mergeMapStorageUpdates(first, second);
1760
- } else if (first.type === "LiveList" && second.type === "LiveList") {
1761
- return mergeListStorageUpdates(first, second);
1762
- } else ;
1763
-
1764
- return second;
1765
- }
1766
-
1767
- function isPlain(value) {
1768
- var type = typeof value;
1769
- return type === "undefined" || value === null || type === "string" || type === "boolean" || type === "number" || Array.isArray(value) || isPlainObject$1(value);
1770
- }
1771
-
1772
- function isPlainObject$1(value) {
1773
- if (typeof value !== "object" || value === null) return false;
1774
- var proto = Object.getPrototypeOf(value);
1775
- if (proto === null) return true;
1776
- var baseProto = proto;
1777
-
1778
- while (Object.getPrototypeOf(baseProto) !== null) {
1779
- baseProto = Object.getPrototypeOf(baseProto);
1780
- }
1781
-
1782
- return proto === baseProto;
1783
- }
1784
-
1785
- function findNonSerializableValue(value, path) {
1786
- if (path === void 0) {
1787
- path = "";
1788
- }
1789
-
1790
- if (!isPlain) {
1791
- return {
1792
- path: path || "root",
1793
- value: value
1794
- };
1795
- }
1796
-
1797
- if (typeof value !== "object" || value === null) {
1798
- return false;
1799
- }
1800
-
1801
- for (var _i = 0, _Object$entries = Object.entries(value); _i < _Object$entries.length; _i++) {
1802
- var _Object$entries$_i = _Object$entries[_i],
1803
- _key3 = _Object$entries$_i[0],
1804
- nestedValue = _Object$entries$_i[1];
1805
- var nestedPath = path ? path + "." + _key3 : _key3;
1806
-
1807
- if (!isPlain(nestedValue)) {
1808
- return {
1809
- path: nestedPath,
1810
- value: nestedValue
1811
- };
1812
- }
1813
-
1814
- if (typeof nestedValue === "object") {
1815
- var nonSerializableNestedValue = findNonSerializableValue(nestedValue, nestedPath);
1816
-
1817
- if (nonSerializableNestedValue) {
1818
- return nonSerializableNestedValue;
1819
- }
1820
- }
1821
- }
1822
-
1823
- return false;
1824
- }
1825
- function isTokenValid(token) {
1826
- var tokenParts = token.split(".");
1827
-
1828
- if (tokenParts.length !== 3) {
1829
- return false;
1830
- }
1831
-
1832
- var data = parseJson(atob(tokenParts[1]));
1833
-
1834
- if (data === undefined || !isJsonObject(data) || typeof data.exp !== "number") {
1835
- return false;
1836
- }
1837
-
1838
- var now = Date.now();
1839
-
1840
- if (now / 1000 > data.exp - 300) {
1841
- return false;
1842
- }
1843
-
1844
- return true;
1845
- }
1846
- function entries(obj) {
1847
- return Object.entries(obj);
1848
- }
1849
-
1850
- var LiveObject = function (_AbstractCrdt) {
1851
- _inheritsLoose(LiveObject, _AbstractCrdt);
1852
-
1853
- function LiveObject(object) {
1854
- var _this;
1855
-
1856
- if (object === void 0) {
1857
- object = {};
1858
- }
1859
-
1860
- _this = _AbstractCrdt.call(this) || this;
1861
- _this._propToLastUpdate = new Map();
1862
-
1863
- for (var key in object) {
1864
- var value = object[key];
1865
-
1866
- if (value instanceof AbstractCrdt) {
1867
- value._setParentLink(_assertThisInitialized(_this), key);
1868
- }
1869
- }
1870
-
1871
- _this._map = new Map(Object.entries(object));
1872
- return _this;
1873
- }
1874
-
1875
- var _proto = LiveObject.prototype;
1876
-
1877
- _proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
1878
- if (this._id == null) {
1879
- throw new Error("Cannot serialize item is not attached");
1880
- }
1881
-
1882
- var ops = [];
1883
- var op = {
1884
- id: this._id,
1885
- opId: doc == null ? void 0 : doc.generateOpId(),
1886
- intent: intent,
1887
- type: OpType.CreateObject,
1888
- parentId: parentId,
1889
- parentKey: parentKey,
1890
- data: {}
1891
- };
1892
- ops.push(op);
1893
-
1894
- for (var _iterator = _createForOfIteratorHelperLoose(this._map), _step; !(_step = _iterator()).done;) {
1895
- var _step$value = _step.value,
1896
- key = _step$value[0],
1897
- value = _step$value[1];
1898
-
1899
- if (value instanceof AbstractCrdt) {
1900
- ops.push.apply(ops, value._serialize(this._id, key, doc));
1901
- } else {
1902
- op.data[key] = value;
1903
- }
1904
- }
1905
-
1906
- return ops;
1907
- };
1908
-
1909
- LiveObject._deserialize = function _deserialize(_ref, parentToChildren, doc) {
1910
- var id = _ref[0],
1911
- item = _ref[1];
1912
-
1913
- if (item.type !== CrdtType.Object) {
1914
- throw new Error("Tried to deserialize a record but item type is \"" + item.type + "\"");
1915
- }
1916
-
1917
- var object = new LiveObject(item.data);
1918
-
1919
- object._attach(id, doc);
1920
-
1921
- return this._deserializeChildren(object, parentToChildren, doc);
1922
- };
1923
-
1924
- LiveObject._deserializeChildren = function _deserializeChildren(object, parentToChildren, doc) {
1925
- var children = parentToChildren.get(object._id);
1926
-
1927
- if (children == null) {
1928
- return object;
1929
- }
1930
-
1931
- for (var _iterator2 = _createForOfIteratorHelperLoose(children), _step2; !(_step2 = _iterator2()).done;) {
1932
- var entry = _step2.value;
1933
- var crdt = entry[1];
1934
-
1935
- if (crdt.parentKey == null) {
1936
- throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
1937
- }
1938
-
1939
- var child = deserialize(entry, parentToChildren, doc);
1940
-
1941
- child._setParentLink(object, crdt.parentKey);
1942
-
1943
- object._map.set(crdt.parentKey, child);
1944
- }
1945
-
1946
- return object;
1947
- };
1948
-
1949
- _proto._attach = function _attach(id, doc) {
1950
- _AbstractCrdt.prototype._attach.call(this, id, doc);
1951
-
1952
- for (var _iterator3 = _createForOfIteratorHelperLoose(this._map), _step3; !(_step3 = _iterator3()).done;) {
1953
- var _step3$value = _step3.value;
1954
- _step3$value[0];
1955
- var value = _step3$value[1];
1956
-
1957
- if (value instanceof AbstractCrdt) {
1958
- value._attach(doc.generateId(), doc);
1959
- }
1960
- }
1961
- };
1962
-
1963
- _proto._attachChild = function _attachChild(op, isLocal) {
1964
- var _updates;
1965
-
1966
- if (this._doc == null) {
1967
- throw new Error("Can't attach child if doc is not present");
1968
- }
1969
-
1970
- var id = op.id,
1971
- parentKey = op.parentKey,
1972
- opId = op.opId;
1973
- var key = parentKey;
1974
- var child = creationOpToLiveStructure(op);
1975
-
1976
- if (this._doc.getItem(id) !== undefined) {
1977
- if (this._propToLastUpdate.get(key) === opId) {
1978
- this._propToLastUpdate.delete(key);
1979
- }
1980
-
1981
- return {
1982
- modified: false
1983
- };
1984
- }
1985
-
1986
- if (isLocal) {
1987
- this._propToLastUpdate.set(key, opId);
1988
- } else if (this._propToLastUpdate.get(key) === undefined) ; else if (this._propToLastUpdate.get(key) === opId) {
1989
- this._propToLastUpdate.delete(key);
1990
-
1991
- return {
1992
- modified: false
1993
- };
1994
- } else {
1995
- return {
1996
- modified: false
1997
- };
1998
- }
1999
-
2000
- var previousValue = this._map.get(key);
2001
-
2002
- var reverse;
2003
-
2004
- if (isCrdt(previousValue)) {
2005
- reverse = previousValue._serialize(this._id, key);
2006
-
2007
- previousValue._detach();
2008
- } else if (previousValue === undefined) {
2009
- reverse = [{
2010
- type: OpType.DeleteObjectKey,
2011
- id: this._id,
2012
- key: key
2013
- }];
2014
- } else {
2015
- var _data;
2016
-
2017
- reverse = [{
2018
- type: OpType.UpdateObject,
2019
- id: this._id,
2020
- data: (_data = {}, _data[key] = previousValue, _data)
2021
- }];
2022
- }
2023
-
2024
- this._map.set(key, child);
2025
-
2026
- child._setParentLink(this, key);
2027
-
2028
- child._attach(id, this._doc);
2029
-
2030
- return {
2031
- reverse: reverse,
2032
- modified: {
2033
- node: this,
2034
- type: "LiveObject",
2035
- updates: (_updates = {}, _updates[key] = {
2036
- type: "update"
2037
- }, _updates)
2038
- }
2039
- };
2040
- };
2041
-
2042
- _proto._detachChild = function _detachChild(child) {
2043
- if (child) {
2044
- var _updates2;
2045
-
2046
- var reverse = child._serialize(this._id, child._parentKey, this._doc);
2047
-
2048
- for (var _iterator4 = _createForOfIteratorHelperLoose(this._map), _step4; !(_step4 = _iterator4()).done;) {
2049
- var _step4$value = _step4.value,
2050
- key = _step4$value[0],
2051
- value = _step4$value[1];
2052
-
2053
- if (value === child) {
2054
- this._map.delete(key);
2055
- }
2056
- }
2057
-
2058
- child._detach();
2059
-
2060
- var storageUpdate = {
2061
- node: this,
2062
- type: "LiveObject",
2063
- updates: (_updates2 = {}, _updates2[child._parentKey] = {
2064
- type: "delete"
2065
- }, _updates2)
2066
- };
2067
- return {
2068
- modified: storageUpdate,
2069
- reverse: reverse
2070
- };
2071
- }
2072
-
2073
- return {
2074
- modified: false
2075
- };
2076
- };
2077
-
2078
- _proto._detachChildren = function _detachChildren() {
2079
- for (var _iterator5 = _createForOfIteratorHelperLoose(this._map), _step5; !(_step5 = _iterator5()).done;) {
2080
- var _step5$value = _step5.value,
2081
- key = _step5$value[0],
2082
- value = _step5$value[1];
2083
-
2084
- this._map.delete(key);
2085
-
2086
- value._detach();
2087
- }
2088
- };
2089
-
2090
- _proto._detach = function _detach() {
2091
- _AbstractCrdt.prototype._detach.call(this);
2092
-
2093
- for (var _iterator6 = _createForOfIteratorHelperLoose(this._map.values()), _step6; !(_step6 = _iterator6()).done;) {
2094
- var value = _step6.value;
2095
-
2096
- if (isCrdt(value)) {
2097
- value._detach();
2098
- }
2099
- }
2100
- };
2101
-
2102
- _proto._apply = function _apply(op, isLocal) {
2103
- if (op.type === OpType.UpdateObject) {
2104
- return this._applyUpdate(op, isLocal);
2105
- } else if (op.type === OpType.DeleteObjectKey) {
2106
- return this._applyDeleteObjectKey(op);
2107
- }
2108
-
2109
- return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
2110
- };
2111
-
2112
- _proto._toSerializedCrdt = function _toSerializedCrdt() {
2113
- var _this$_parent;
2114
-
2115
- var data = {};
2116
-
2117
- for (var _iterator7 = _createForOfIteratorHelperLoose(this._map), _step7; !(_step7 = _iterator7()).done;) {
2118
- var _step7$value = _step7.value,
2119
- key = _step7$value[0],
2120
- value = _step7$value[1];
2121
-
2122
- if (value instanceof AbstractCrdt === false) {
2123
- data[key] = value;
2124
- }
2125
- }
2126
-
2127
- return {
2128
- type: CrdtType.Object,
2129
- parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
2130
- parentKey: this._parentKey,
2131
- data: data
2132
- };
2133
- };
2134
-
2135
- _proto._applyUpdate = function _applyUpdate(op, isLocal) {
2136
- var isModified = false;
2137
- var reverse = [];
2138
- var reverseUpdate = {
2139
- type: OpType.UpdateObject,
2140
- id: this._id,
2141
- data: {}
2142
- };
2143
- reverse.push(reverseUpdate);
2144
-
2145
- for (var key in op.data) {
2146
- var oldValue = this._map.get(key);
2147
-
2148
- if (oldValue instanceof AbstractCrdt) {
2149
- reverse.push.apply(reverse, oldValue._serialize(this._id, key));
2150
-
2151
- oldValue._detach();
2152
- } else if (oldValue !== undefined) {
2153
- reverseUpdate.data[key] = oldValue;
2154
- } else if (oldValue === undefined) {
2155
- reverse.push({
2156
- type: OpType.DeleteObjectKey,
2157
- id: this._id,
2158
- key: key
2159
- });
2160
- }
2161
- }
2162
-
2163
- var updateDelta = {};
2164
-
2165
- for (var _key2 in op.data) {
2166
- if (isLocal) {
2167
- this._propToLastUpdate.set(_key2, op.opId);
2168
- } else if (this._propToLastUpdate.get(_key2) == null) {
2169
- isModified = true;
2170
- } else if (this._propToLastUpdate.get(_key2) === op.opId) {
2171
- this._propToLastUpdate.delete(_key2);
2172
-
2173
- continue;
2174
- } else {
2175
- continue;
2176
- }
2177
-
2178
- var _oldValue = this._map.get(_key2);
2179
-
2180
- if (isCrdt(_oldValue)) {
2181
- _oldValue._detach();
2182
- }
2183
-
2184
- isModified = true;
2185
- updateDelta[_key2] = {
2186
- type: "update"
2187
- };
2188
-
2189
- this._map.set(_key2, op.data[_key2]);
2190
- }
2191
-
2192
- if (Object.keys(reverseUpdate.data).length !== 0) {
2193
- reverse.unshift(reverseUpdate);
2194
- }
2195
-
2196
- return isModified ? {
2197
- modified: {
2198
- node: this,
2199
- type: "LiveObject",
2200
- updates: updateDelta
2201
- },
2202
- reverse: reverse
2203
- } : {
2204
- modified: false
2205
- };
2206
- };
2207
-
2208
- _proto._applyDeleteObjectKey = function _applyDeleteObjectKey(op) {
2209
- var _updates3;
2210
-
2211
- var key = op.key;
2212
-
2213
- if (this._map.has(key) === false) {
2214
- return {
2215
- modified: false
2216
- };
2217
- }
2218
-
2219
- if (this._propToLastUpdate.get(key) !== undefined) {
2220
- return {
2221
- modified: false
2222
- };
2223
- }
2224
-
2225
- var oldValue = this._map.get(key);
2226
-
2227
- var reverse = [];
2228
-
2229
- if (isCrdt(oldValue)) {
2230
- reverse = oldValue._serialize(this._id, op.key);
2231
-
2232
- oldValue._detach();
2233
- } else if (oldValue !== undefined) {
2234
- var _data2;
2235
-
2236
- reverse = [{
2237
- type: OpType.UpdateObject,
2238
- id: this._id,
2239
- data: (_data2 = {}, _data2[key] = oldValue, _data2)
2240
- }];
2241
- }
2242
-
2243
- this._map.delete(key);
2244
-
2245
- return {
2246
- modified: {
2247
- node: this,
2248
- type: "LiveObject",
2249
- updates: (_updates3 = {}, _updates3[op.key] = {
2250
- type: "delete"
2251
- }, _updates3)
2252
- },
2253
- reverse: reverse
2254
- };
2255
- };
2256
-
2257
- _proto.toObject = function toObject() {
2258
- return Object.fromEntries(this._map);
2259
- };
2260
-
2261
- _proto.set = function set(key, value) {
2262
- var _this$update;
2263
-
2264
- this.update((_this$update = {}, _this$update[key] = value, _this$update));
2265
- };
2266
-
2267
- _proto.get = function get(key) {
2268
- return this._map.get(key);
2269
- };
2270
-
2271
- _proto.delete = function _delete(key) {
2272
- var _updates4;
2273
-
2274
- var keyAsString = key;
2275
-
2276
- var oldValue = this._map.get(keyAsString);
2277
-
2278
- if (oldValue === undefined) {
2279
- return;
2280
- }
2281
-
2282
- if (this._doc == null || this._id == null) {
2283
- if (oldValue instanceof AbstractCrdt) {
2284
- oldValue._detach();
2285
- }
2286
-
2287
- this._map.delete(keyAsString);
2288
-
2289
- return;
2290
- }
2291
-
2292
- var reverse;
2293
-
2294
- if (oldValue instanceof AbstractCrdt) {
2295
- oldValue._detach();
2296
-
2297
- reverse = oldValue._serialize(this._id, keyAsString);
2298
- } else {
2299
- var _data3;
2300
-
2301
- reverse = [{
2302
- type: OpType.UpdateObject,
2303
- data: (_data3 = {}, _data3[keyAsString] = oldValue, _data3),
2304
- id: this._id
2305
- }];
2306
- }
2307
-
2308
- this._map.delete(keyAsString);
2309
-
2310
- var storageUpdates = new Map();
2311
- storageUpdates.set(this._id, {
2312
- node: this,
2313
- type: "LiveObject",
2314
- updates: (_updates4 = {}, _updates4[key] = {
2315
- type: "delete"
2316
- }, _updates4)
2317
- });
2318
-
2319
- this._doc.dispatch([{
2320
- type: OpType.DeleteObjectKey,
2321
- key: keyAsString,
2322
- id: this._id,
2323
- opId: this._doc.generateOpId()
2324
- }], reverse, storageUpdates);
2325
- };
2326
-
2327
- _proto.update = function update(overrides) {
2328
- var _this2 = this;
2329
-
2330
- if (this._doc == null || this._id == null) {
2331
- for (var key in overrides) {
2332
- var oldValue = this._map.get(key);
2333
-
2334
- if (oldValue instanceof AbstractCrdt) {
2335
- oldValue._detach();
2336
- }
2337
-
2338
- var newValue = overrides[key];
2339
-
2340
- if (newValue instanceof AbstractCrdt) {
2341
- newValue._setParentLink(this, key);
2342
- }
2343
-
2344
- this._map.set(key, newValue);
2345
- }
2346
-
2347
- return;
2348
- }
2349
-
2350
- var ops = [];
2351
- var reverseOps = [];
2352
-
2353
- var opId = this._doc.generateOpId();
2354
-
2355
- var updatedProps = {};
2356
- var reverseUpdateOp = {
2357
- id: this._id,
2358
- type: OpType.UpdateObject,
2359
- data: {}
2360
- };
2361
- var updateDelta = {};
2362
-
2363
- for (var _key3 in overrides) {
2364
- var _oldValue2 = this._map.get(_key3);
2365
-
2366
- if (_oldValue2 instanceof AbstractCrdt) {
2367
- reverseOps.push.apply(reverseOps, _oldValue2._serialize(this._id, _key3));
2368
-
2369
- _oldValue2._detach();
2370
- } else if (_oldValue2 === undefined) {
2371
- reverseOps.push({
2372
- type: OpType.DeleteObjectKey,
2373
- id: this._id,
2374
- key: _key3
2375
- });
2376
- } else {
2377
- reverseUpdateOp.data[_key3] = _oldValue2;
2378
- }
2379
-
2380
- var _newValue = overrides[_key3];
2381
-
2382
- if (_newValue instanceof AbstractCrdt) {
2383
- _newValue._setParentLink(this, _key3);
2384
-
2385
- _newValue._attach(this._doc.generateId(), this._doc);
2386
-
2387
- var newAttachChildOps = _newValue._serialize(this._id, _key3, this._doc);
2388
-
2389
- var createCrdtOp = newAttachChildOps.find(function (op) {
2390
- return op.parentId === _this2._id;
2391
- });
2392
-
2393
- if (createCrdtOp) {
2394
- this._propToLastUpdate.set(_key3, createCrdtOp.opId);
2395
- }
2396
-
2397
- ops.push.apply(ops, newAttachChildOps);
2398
- } else {
2399
- updatedProps[_key3] = _newValue;
2400
-
2401
- this._propToLastUpdate.set(_key3, opId);
2402
- }
2403
-
2404
- this._map.set(_key3, _newValue);
2405
-
2406
- updateDelta[_key3] = {
2407
- type: "update"
2408
- };
2409
- }
2410
-
2411
- if (Object.keys(reverseUpdateOp.data).length !== 0) {
2412
- reverseOps.unshift(reverseUpdateOp);
2413
- }
2414
-
2415
- if (Object.keys(updatedProps).length !== 0) {
2416
- ops.unshift({
2417
- opId: opId,
2418
- id: this._id,
2419
- type: OpType.UpdateObject,
2420
- data: updatedProps
2421
- });
2422
- }
2423
-
2424
- var storageUpdates = new Map();
2425
- storageUpdates.set(this._id, {
2426
- node: this,
2427
- type: "LiveObject",
2428
- updates: updateDelta
2429
- });
2430
-
2431
- this._doc.dispatch(ops, reverseOps, storageUpdates);
2432
- };
2433
-
2434
- return LiveObject;
2435
- }(AbstractCrdt);
5
+ var LiveObject = require('./shared.js');
2436
6
 
2437
7
  var BACKOFF_RETRY_DELAYS = [250, 500, 1000, 2000, 4000, 8000, 10000];
2438
8
  var BACKOFF_RETRY_DELAYS_SLOW = [2000, 30000, 60000, 300000];
@@ -2455,7 +25,7 @@ function makeOthers(userMap) {
2455
25
 
2456
26
  var users = Object.values(userMap).map(function (user) {
2457
27
  user._hasReceivedInitialPresence;
2458
- var publicKeys = _objectWithoutPropertiesLoose(user, ["_hasReceivedInitialPresence"]);
28
+ var publicKeys = LiveObject._objectWithoutPropertiesLoose(user, ["_hasReceivedInitialPresence"]);
2459
29
 
2460
30
  return publicKeys;
2461
31
  });
@@ -2478,7 +48,7 @@ function makeStateMachine(state, context, mockedEffects) {
2478
48
  authenticate: function authenticate(auth, createWebSocket) {
2479
49
  var token = state.token;
2480
50
 
2481
- if (token && isTokenValid(token)) {
51
+ if (token && LiveObject.isTokenValid(token)) {
2482
52
  var parsedToken = parseToken(token);
2483
53
  var socket = createWebSocket(token);
2484
54
  authenticationSuccess(parsedToken, socket);
@@ -2523,7 +93,7 @@ function makeStateMachine(state, context, mockedEffects) {
2523
93
  function genericSubscribe(callback) {
2524
94
  state.listeners.storage.push(callback);
2525
95
  return function () {
2526
- return remove(state.listeners.storage, callback);
96
+ return LiveObject.remove(state.listeners.storage, callback);
2527
97
  };
2528
98
  }
2529
99
 
@@ -2531,10 +101,10 @@ function makeStateMachine(state, context, mockedEffects) {
2531
101
  var cb = function cb(updates) {
2532
102
  var relatedUpdates = [];
2533
103
 
2534
- for (var _iterator = _createForOfIteratorHelperLoose(updates), _step; !(_step = _iterator()).done;) {
104
+ for (var _iterator = LiveObject._createForOfIteratorHelperLoose(updates), _step; !(_step = _iterator()).done;) {
2535
105
  var update = _step.value;
2536
106
 
2537
- if (options != null && options.isDeep && isSameNodeOrChildOf(update.node, crdt)) {
107
+ if (options != null && options.isDeep && LiveObject.isSameNodeOrChildOf(update.node, crdt)) {
2538
108
  relatedUpdates.push(update);
2539
109
  } else if (update.node._id === crdt._id) {
2540
110
  innerCallback(update.node);
@@ -2571,7 +141,7 @@ function makeStateMachine(state, context, mockedEffects) {
2571
141
  var parentToChildren = new Map();
2572
142
  var root = null;
2573
143
 
2574
- for (var _iterator2 = _createForOfIteratorHelperLoose(items), _step2; !(_step2 = _iterator2()).done;) {
144
+ for (var _iterator2 = LiveObject._createForOfIteratorHelperLoose(items), _step2; !(_step2 = _iterator2()).done;) {
2575
145
  var tuple = _step2.value;
2576
146
  var parentId = tuple[1].parentId;
2577
147
 
@@ -2604,7 +174,7 @@ function makeStateMachine(state, context, mockedEffects) {
2604
174
  state.items.forEach(function (liveCrdt, id) {
2605
175
  currentItems.set(id, liveCrdt._toSerializedCrdt());
2606
176
  });
2607
- var ops = getTreesDiffOperations(currentItems, new Map(items));
177
+ var ops = LiveObject.getTreesDiffOperations(currentItems, new Map(items));
2608
178
  var result = apply(ops, false);
2609
179
  notify(result.updates);
2610
180
  }
@@ -2614,7 +184,7 @@ function makeStateMachine(state, context, mockedEffects) {
2614
184
  root = _buildRootAndParentTo[0],
2615
185
  parentToChildren = _buildRootAndParentTo[1];
2616
186
 
2617
- return LiveObject._deserialize(root, parentToChildren, {
187
+ return LiveObject.LiveObject._deserialize(root, parentToChildren, {
2618
188
  getItem: getItem,
2619
189
  addItem: addItem,
2620
190
  deleteItem: deleteItem,
@@ -2658,7 +228,7 @@ function makeStateMachine(state, context, mockedEffects) {
2658
228
  (_state$batch$ops = state.batch.ops).push.apply(_state$batch$ops, ops);
2659
229
 
2660
230
  storageUpdates.forEach(function (value, key) {
2661
- state.batch.updates.storageUpdates.set(key, mergeStorageUpdates(state.batch.updates.storageUpdates.get(key), value));
231
+ state.batch.updates.storageUpdates.set(key, LiveObject.mergeStorageUpdates(state.batch.updates.storageUpdates.get(key), value));
2662
232
  });
2663
233
 
2664
234
  (_state$batch$reverseO = state.batch.reverseOps).push.apply(_state$batch$reverseO, reverse);
@@ -2683,10 +253,10 @@ function makeStateMachine(state, context, mockedEffects) {
2683
253
  if (otherEvents.length > 0) {
2684
254
  state.others = makeOthers(state.users);
2685
255
 
2686
- for (var _iterator3 = _createForOfIteratorHelperLoose(otherEvents), _step3; !(_step3 = _iterator3()).done;) {
256
+ for (var _iterator3 = LiveObject._createForOfIteratorHelperLoose(otherEvents), _step3; !(_step3 = _iterator3()).done;) {
2687
257
  var event = _step3.value;
2688
258
 
2689
- for (var _iterator4 = _createForOfIteratorHelperLoose(state.listeners.others), _step4; !(_step4 = _iterator4()).done;) {
259
+ for (var _iterator4 = LiveObject._createForOfIteratorHelperLoose(state.listeners.others), _step4; !(_step4 = _iterator4()).done;) {
2690
260
  var _listener = _step4.value;
2691
261
 
2692
262
  _listener(state.others, event);
@@ -2695,7 +265,7 @@ function makeStateMachine(state, context, mockedEffects) {
2695
265
  }
2696
266
 
2697
267
  if (presence) {
2698
- for (var _iterator5 = _createForOfIteratorHelperLoose(state.listeners["my-presence"]), _step5; !(_step5 = _iterator5()).done;) {
268
+ for (var _iterator5 = LiveObject._createForOfIteratorHelperLoose(state.listeners["my-presence"]), _step5; !(_step5 = _iterator5()).done;) {
2699
269
  var _listener2 = _step5.value;
2700
270
 
2701
271
  _listener2(state.me);
@@ -2703,7 +273,7 @@ function makeStateMachine(state, context, mockedEffects) {
2703
273
  }
2704
274
 
2705
275
  if (storageUpdates.size > 0) {
2706
- for (var _iterator6 = _createForOfIteratorHelperLoose(state.listeners.storage), _step6; !(_step6 = _iterator6()).done;) {
276
+ for (var _iterator6 = LiveObject._createForOfIteratorHelperLoose(state.listeners.storage), _step6; !(_step6 = _iterator6()).done;) {
2707
277
  var subscriber = _step6.value;
2708
278
  subscriber(Array.from(storageUpdates.values()));
2709
279
  }
@@ -2738,7 +308,7 @@ function makeStateMachine(state, context, mockedEffects) {
2738
308
  };
2739
309
  var createdNodeIds = new Set();
2740
310
 
2741
- for (var _iterator7 = _createForOfIteratorHelperLoose(item), _step7; !(_step7 = _iterator7()).done;) {
311
+ for (var _iterator7 = LiveObject._createForOfIteratorHelperLoose(item), _step7; !(_step7 = _iterator7()).done;) {
2742
312
  var op = _step7.value;
2743
313
 
2744
314
  if (op.type === "presence") {
@@ -2751,7 +321,7 @@ function makeStateMachine(state, context, mockedEffects) {
2751
321
  reverse.data[_key2] = state.me[_key2];
2752
322
  }
2753
323
 
2754
- state.me = _extends({}, state.me, op.data);
324
+ state.me = LiveObject._extends({}, state.me, op.data);
2755
325
 
2756
326
  if (state.buffer.presence == null) {
2757
327
  state.buffer.presence = op.data;
@@ -2778,12 +348,12 @@ function makeStateMachine(state, context, mockedEffects) {
2778
348
  if (!createdNodeIds.has(parentId)) {
2779
349
  var _result$reverse;
2780
350
 
2781
- result.updates.storageUpdates.set(applyOpResult.modified.node._id, mergeStorageUpdates(result.updates.storageUpdates.get(applyOpResult.modified.node._id), applyOpResult.modified));
351
+ result.updates.storageUpdates.set(applyOpResult.modified.node._id, LiveObject.mergeStorageUpdates(result.updates.storageUpdates.get(applyOpResult.modified.node._id), applyOpResult.modified));
2782
352
 
2783
353
  (_result$reverse = result.reverse).unshift.apply(_result$reverse, applyOpResult.reverse);
2784
354
  }
2785
355
 
2786
- if (op.type === OpType.CreateList || op.type === OpType.CreateMap || op.type === OpType.CreateObject) {
356
+ if (op.type === LiveObject.OpType.CreateList || op.type === LiveObject.OpType.CreateMap || op.type === LiveObject.OpType.CreateObject) {
2787
357
  createdNodeIds.add(applyOpResult.modified.node._id);
2788
358
  }
2789
359
  }
@@ -2799,9 +369,9 @@ function makeStateMachine(state, context, mockedEffects) {
2799
369
  }
2800
370
 
2801
371
  switch (op.type) {
2802
- case OpType.DeleteObjectKey:
2803
- case OpType.UpdateObject:
2804
- case OpType.DeleteCrdt:
372
+ case LiveObject.OpType.DeleteObjectKey:
373
+ case LiveObject.OpType.UpdateObject:
374
+ case LiveObject.OpType.DeleteCrdt:
2805
375
  {
2806
376
  var item = state.items.get(op.id);
2807
377
 
@@ -2814,7 +384,7 @@ function makeStateMachine(state, context, mockedEffects) {
2814
384
  return item._apply(op, isLocal);
2815
385
  }
2816
386
 
2817
- case OpType.SetParentKey:
387
+ case LiveObject.OpType.SetParentKey:
2818
388
  {
2819
389
  var _item = state.items.get(op.id);
2820
390
 
@@ -2824,7 +394,7 @@ function makeStateMachine(state, context, mockedEffects) {
2824
394
  };
2825
395
  }
2826
396
 
2827
- if (_item._parent instanceof LiveList) {
397
+ if (_item._parent instanceof LiveObject.LiveList) {
2828
398
  var previousKey = _item._parentKey;
2829
399
 
2830
400
  if (previousKey === op.parentKey) {
@@ -2841,10 +411,10 @@ function makeStateMachine(state, context, mockedEffects) {
2841
411
  };
2842
412
  }
2843
413
 
2844
- case OpType.CreateObject:
2845
- case OpType.CreateList:
2846
- case OpType.CreateMap:
2847
- case OpType.CreateRegister:
414
+ case LiveObject.OpType.CreateObject:
415
+ case LiveObject.OpType.CreateList:
416
+ case LiveObject.OpType.CreateMap:
417
+ case LiveObject.OpType.CreateRegister:
2848
418
  {
2849
419
  var parent = state.items.get(op.parentId);
2850
420
 
@@ -2857,14 +427,10 @@ function makeStateMachine(state, context, mockedEffects) {
2857
427
  return parent._attachChild(op, isLocal);
2858
428
  }
2859
429
  }
2860
-
2861
- return {
2862
- modified: false
2863
- };
2864
430
  }
2865
431
 
2866
432
  function subscribe(firstParam, listener, options) {
2867
- if (firstParam instanceof AbstractCrdt) {
433
+ if (firstParam instanceof LiveObject.AbstractCrdt) {
2868
434
  return crdtSubscribe(firstParam, listener, options);
2869
435
  } else if (typeof firstParam === "function") {
2870
436
  return genericSubscribe(firstParam);
@@ -2875,7 +441,7 @@ function makeStateMachine(state, context, mockedEffects) {
2875
441
  state.listeners[firstParam].push(listener);
2876
442
  return function () {
2877
443
  var callbacks = state.listeners[firstParam];
2878
- remove(callbacks, listener);
444
+ LiveObject.remove(callbacks, listener);
2879
445
  };
2880
446
  }
2881
447
 
@@ -2887,7 +453,7 @@ function makeStateMachine(state, context, mockedEffects) {
2887
453
  }
2888
454
 
2889
455
  var callbacks = state.listeners[event];
2890
- remove(callbacks, callback);
456
+ LiveObject.remove(callbacks, callback);
2891
457
  }
2892
458
 
2893
459
  function getConnectionState() {
@@ -2928,7 +494,7 @@ function makeStateMachine(state, context, mockedEffects) {
2928
494
  oldValues[_key4] = state.me[_key4];
2929
495
  }
2930
496
 
2931
- state.me = _extends({}, state.me, overrides);
497
+ state.me = LiveObject._extends({}, state.me, overrides);
2932
498
 
2933
499
  if (state.isBatching) {
2934
500
  if (options != null && options.addToHistory) {
@@ -3007,7 +573,7 @@ function makeStateMachine(state, context, mockedEffects) {
3007
573
  id: user.id,
3008
574
  info: user.info,
3009
575
  connectionId: message.actor,
3010
- presence: _extends({}, user.presence, message.data),
576
+ presence: LiveObject._extends({}, user.presence, message.data),
3011
577
  _hasReceivedInitialPresence: true
3012
578
  };
3013
579
  }
@@ -3061,7 +627,7 @@ function makeStateMachine(state, context, mockedEffects) {
3061
627
  }
3062
628
 
3063
629
  function onEvent(message) {
3064
- for (var _iterator8 = _createForOfIteratorHelperLoose(state.listeners.event), _step8; !(_step8 = _iterator8()).done;) {
630
+ for (var _iterator8 = LiveObject._createForOfIteratorHelperLoose(state.listeners.event), _step8; !(_step8 = _iterator8()).done;) {
3065
631
  var _listener3 = _step8.value;
3066
632
 
3067
633
  _listener3({
@@ -3081,7 +647,7 @@ function makeStateMachine(state, context, mockedEffects) {
3081
647
 
3082
648
  if (state.me) {
3083
649
  state.buffer.messages.push({
3084
- type: ClientMessageType.UpdatePresence,
650
+ type: LiveObject.ClientMessageType.UpdatePresence,
3085
651
  data: state.me,
3086
652
  targetActor: message.actor
3087
653
  });
@@ -3095,7 +661,7 @@ function makeStateMachine(state, context, mockedEffects) {
3095
661
  }
3096
662
 
3097
663
  function parseServerMessage(data) {
3098
- if (!isJsonObject(data)) {
664
+ if (!LiveObject.isJsonObject(data)) {
3099
665
  return null;
3100
666
  }
3101
667
 
@@ -3103,16 +669,16 @@ function makeStateMachine(state, context, mockedEffects) {
3103
669
  }
3104
670
 
3105
671
  function parseServerMessages(text) {
3106
- var data = parseJson(text);
672
+ var data = LiveObject.parseJson(text);
3107
673
 
3108
674
  if (data === undefined) {
3109
675
  return null;
3110
- } else if (isJsonArray(data)) {
3111
- return compact(data.map(function (item) {
676
+ } else if (LiveObject.isJsonArray(data)) {
677
+ return LiveObject.compact(data.map(function (item) {
3112
678
  return parseServerMessage(item);
3113
679
  }));
3114
680
  } else {
3115
- return compact([parseServerMessage(data)]);
681
+ return LiveObject.compact([parseServerMessage(data)]);
3116
682
  }
3117
683
  }
3118
684
 
@@ -3133,17 +699,17 @@ function makeStateMachine(state, context, mockedEffects) {
3133
699
  others: []
3134
700
  };
3135
701
 
3136
- for (var _iterator9 = _createForOfIteratorHelperLoose(messages), _step9; !(_step9 = _iterator9()).done;) {
702
+ for (var _iterator9 = LiveObject._createForOfIteratorHelperLoose(messages), _step9; !(_step9 = _iterator9()).done;) {
3137
703
  var message = _step9.value;
3138
704
 
3139
705
  switch (message.type) {
3140
- case ServerMessageType.UserJoined:
706
+ case LiveObject.ServerMessageType.UserJoined:
3141
707
  {
3142
708
  updates.others.push(onUserJoinedMessage(message));
3143
709
  break;
3144
710
  }
3145
711
 
3146
- case ServerMessageType.UpdatePresence:
712
+ case LiveObject.ServerMessageType.UpdatePresence:
3147
713
  {
3148
714
  var othersPresenceUpdate = onUpdatePresenceMessage(message);
3149
715
 
@@ -3154,13 +720,13 @@ function makeStateMachine(state, context, mockedEffects) {
3154
720
  break;
3155
721
  }
3156
722
 
3157
- case ServerMessageType.Event:
723
+ case LiveObject.ServerMessageType.Event:
3158
724
  {
3159
725
  onEvent(message);
3160
726
  break;
3161
727
  }
3162
728
 
3163
- case ServerMessageType.UserLeft:
729
+ case LiveObject.ServerMessageType.UserLeft:
3164
730
  {
3165
731
  var _event = onUserLeftMessage(message);
3166
732
 
@@ -3171,13 +737,13 @@ function makeStateMachine(state, context, mockedEffects) {
3171
737
  break;
3172
738
  }
3173
739
 
3174
- case ServerMessageType.RoomState:
740
+ case LiveObject.ServerMessageType.RoomState:
3175
741
  {
3176
742
  updates.others.push(onRoomStateMessage(message));
3177
743
  break;
3178
744
  }
3179
745
 
3180
- case ServerMessageType.InitialStorageState:
746
+ case LiveObject.ServerMessageType.InitialStorageState:
3181
747
  {
3182
748
  var offlineOps = new Map(state.offlineOperations);
3183
749
  createOrUpdateRootFromMessage(message);
@@ -3186,11 +752,11 @@ function makeStateMachine(state, context, mockedEffects) {
3186
752
  break;
3187
753
  }
3188
754
 
3189
- case ServerMessageType.UpdateStorage:
755
+ case LiveObject.ServerMessageType.UpdateStorage:
3190
756
  {
3191
757
  var applyResult = apply(message.ops, false);
3192
758
  applyResult.updates.storageUpdates.forEach(function (value, key) {
3193
- updates.storageUpdates.set(key, mergeStorageUpdates(updates.storageUpdates.get(key), value));
759
+ updates.storageUpdates.set(key, LiveObject.mergeStorageUpdates(updates.storageUpdates.get(key), value));
3194
760
  });
3195
761
  break;
3196
762
  }
@@ -3223,7 +789,7 @@ function makeStateMachine(state, context, mockedEffects) {
3223
789
  });
3224
790
  var error = new LiveblocksError(event.reason, event.code);
3225
791
 
3226
- for (var _iterator10 = _createForOfIteratorHelperLoose(state.listeners.error), _step10; !(_step10 = _iterator10()).done;) {
792
+ for (var _iterator10 = LiveObject._createForOfIteratorHelperLoose(state.listeners.error), _step10; !(_step10 = _iterator10()).done;) {
3227
793
  var _listener4 = _step10.value;
3228
794
 
3229
795
  _listener4(error);
@@ -3241,7 +807,7 @@ function makeStateMachine(state, context, mockedEffects) {
3241
807
  state: "unavailable"
3242
808
  });
3243
809
  state.timeoutHandles.reconnect = effects.scheduleReconnect(_delay);
3244
- } else if (event.code === WebsocketCloseCodes.CLOSE_WITHOUT_RETRY) {
810
+ } else if (event.code === LiveObject.WebsocketCloseCodes.CLOSE_WITHOUT_RETRY) {
3245
811
  updateConnection({
3246
812
  state: "closed"
3247
813
  });
@@ -3264,7 +830,7 @@ function makeStateMachine(state, context, mockedEffects) {
3264
830
  function updateConnection(connection) {
3265
831
  state.connection = connection;
3266
832
 
3267
- for (var _iterator11 = _createForOfIteratorHelperLoose(state.listeners.connection), _step11; !(_step11 = _iterator11()).done;) {
833
+ for (var _iterator11 = LiveObject._createForOfIteratorHelperLoose(state.listeners.connection), _step11; !(_step11 = _iterator11()).done;) {
3268
834
  var _listener5 = _step11.value;
3269
835
 
3270
836
  _listener5(connection.state);
@@ -3290,7 +856,7 @@ function makeStateMachine(state, context, mockedEffects) {
3290
856
  state.intervalHandles.heartbeat = effects.startHeartbeatInterval();
3291
857
 
3292
858
  if (state.connection.state === "connecting") {
3293
- updateConnection(_extends({}, state.connection, {
859
+ updateConnection(LiveObject._extends({}, state.connection, {
3294
860
  state: "open"
3295
861
  }));
3296
862
  state.numberOfRetry = 0;
@@ -3304,7 +870,7 @@ function makeStateMachine(state, context, mockedEffects) {
3304
870
 
3305
871
  if (state.root) {
3306
872
  state.buffer.messages.push({
3307
- type: ClientMessageType.FetchStorage
873
+ type: LiveObject.ClientMessageType.FetchStorage
3308
874
  });
3309
875
  }
3310
876
 
@@ -3362,7 +928,7 @@ function makeStateMachine(state, context, mockedEffects) {
3362
928
  var ops = Array.from(offlineOps.values());
3363
929
  var result = apply(ops, true);
3364
930
  messages.push({
3365
- type: ClientMessageType.UpdateStorage,
931
+ type: LiveObject.ClientMessageType.UpdateStorage,
3366
932
  ops: ops
3367
933
  });
3368
934
  notify(result.updates);
@@ -3414,19 +980,19 @@ function makeStateMachine(state, context, mockedEffects) {
3414
980
 
3415
981
  if (state.buffer.presence) {
3416
982
  messages.push({
3417
- type: ClientMessageType.UpdatePresence,
983
+ type: LiveObject.ClientMessageType.UpdatePresence,
3418
984
  data: state.buffer.presence
3419
985
  });
3420
986
  }
3421
987
 
3422
- for (var _iterator12 = _createForOfIteratorHelperLoose(state.buffer.messages), _step12; !(_step12 = _iterator12()).done;) {
988
+ for (var _iterator12 = LiveObject._createForOfIteratorHelperLoose(state.buffer.messages), _step12; !(_step12 = _iterator12()).done;) {
3423
989
  var event = _step12.value;
3424
990
  messages.push(event);
3425
991
  }
3426
992
 
3427
993
  if (state.buffer.storageOperations.length > 0) {
3428
994
  messages.push({
3429
- type: ClientMessageType.UpdateStorage,
995
+ type: LiveObject.ClientMessageType.UpdateStorage,
3430
996
  ops: state.buffer.storageOperations
3431
997
  });
3432
998
  }
@@ -3490,7 +1056,7 @@ function makeStateMachine(state, context, mockedEffects) {
3490
1056
  }
3491
1057
 
3492
1058
  state.buffer.messages.push({
3493
- type: ClientMessageType.ClientEvent,
1059
+ type: LiveObject.ClientMessageType.ClientEvent,
3494
1060
  event: event
3495
1061
  });
3496
1062
  tryFlushing();
@@ -3518,7 +1084,7 @@ function makeStateMachine(state, context, mockedEffects) {
3518
1084
 
3519
1085
  if (_getInitialStatePromise == null) {
3520
1086
  state.buffer.messages.push({
3521
- type: ClientMessageType.FetchStorage
1087
+ type: LiveObject.ClientMessageType.FetchStorage
3522
1088
  });
3523
1089
  tryFlushing();
3524
1090
  _getInitialStatePromise = new Promise(function (resolve) {
@@ -3549,7 +1115,7 @@ function makeStateMachine(state, context, mockedEffects) {
3549
1115
  notify(result.updates);
3550
1116
  state.redoStack.push(result.reverse);
3551
1117
 
3552
- for (var _iterator13 = _createForOfIteratorHelperLoose(historyItem), _step13; !(_step13 = _iterator13()).done;) {
1118
+ for (var _iterator13 = LiveObject._createForOfIteratorHelperLoose(historyItem), _step13; !(_step13 = _iterator13()).done;) {
3553
1119
  var op = _step13.value;
3554
1120
 
3555
1121
  if (op.type !== "presence") {
@@ -3576,7 +1142,7 @@ function makeStateMachine(state, context, mockedEffects) {
3576
1142
  notify(result.updates);
3577
1143
  state.undoStack.push(result.reverse);
3578
1144
 
3579
- for (var _iterator14 = _createForOfIteratorHelperLoose(historyItem), _step14; !(_step14 = _iterator14()).done;) {
1145
+ for (var _iterator14 = LiveObject._createForOfIteratorHelperLoose(historyItem), _step14; !(_step14 = _iterator14()).done;) {
3580
1146
  var op = _step14.value;
3581
1147
 
3582
1148
  if (op.type !== "presence") {
@@ -3780,7 +1346,7 @@ function createRoom(options, context) {
3780
1346
  }
3781
1347
 
3782
1348
  var LiveblocksError = function (_Error) {
3783
- _inheritsLoose(LiveblocksError, _Error);
1349
+ LiveObject._inheritsLoose(LiveblocksError, _Error);
3784
1350
 
3785
1351
  function LiveblocksError(message, code) {
3786
1352
  var _this;
@@ -3791,7 +1357,7 @@ var LiveblocksError = function (_Error) {
3791
1357
  }
3792
1358
 
3793
1359
  return LiveblocksError;
3794
- }(_wrapNativeSuper(Error));
1360
+ }(LiveObject._wrapNativeSuper(Error));
3795
1361
 
3796
1362
  function parseToken(token) {
3797
1363
  var tokenParts = token.split(".");
@@ -3800,9 +1366,9 @@ function parseToken(token) {
3800
1366
  throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
3801
1367
  }
3802
1368
 
3803
- var data = parseJson(atob(tokenParts[1]));
1369
+ var data = LiveObject.parseJson(atob(tokenParts[1]));
3804
1370
 
3805
- if (data !== undefined && isJsonObject(data) && typeof data.actor === "number" && (data.id === undefined || typeof data.id === "string")) {
1371
+ if (data !== undefined && LiveObject.isJsonObject(data) && typeof data.actor === "number" && (data.id === undefined || typeof data.id === "string")) {
3806
1372
  return {
3807
1373
  actor: data.actor,
3808
1374
  id: data.id,
@@ -3882,14 +1448,14 @@ function fetchAuthEndpoint(fetch, endpoint, body) {
3882
1448
  }
3883
1449
 
3884
1450
  var AuthenticationError = function (_Error2) {
3885
- _inheritsLoose(AuthenticationError, _Error2);
1451
+ LiveObject._inheritsLoose(AuthenticationError, _Error2);
3886
1452
 
3887
1453
  function AuthenticationError(message) {
3888
1454
  return _Error2.call(this, message) || this;
3889
1455
  }
3890
1456
 
3891
1457
  return AuthenticationError;
3892
- }(_wrapNativeSuper(Error));
1458
+ }(LiveObject._wrapNativeSuper(Error));
3893
1459
 
3894
1460
  function createClient(options) {
3895
1461
  var clientOptions = options;
@@ -3943,7 +1509,7 @@ function createClient(options) {
3943
1509
 
3944
1510
  if (typeof window !== "undefined") {
3945
1511
  window.addEventListener("online", function () {
3946
- for (var _iterator = _createForOfIteratorHelperLoose(rooms), _step; !(_step = _iterator()).done;) {
1512
+ for (var _iterator = LiveObject._createForOfIteratorHelperLoose(rooms), _step; !(_step = _iterator()).done;) {
3947
1513
  var _step$value = _step.value,
3948
1514
  room = _step$value[1];
3949
1515
  room.onNavigatorOnline();
@@ -3953,7 +1519,7 @@ function createClient(options) {
3953
1519
 
3954
1520
  if (typeof document !== "undefined") {
3955
1521
  document.addEventListener("visibilitychange", function () {
3956
- for (var _iterator2 = _createForOfIteratorHelperLoose(rooms), _step2; !(_step2 = _iterator2()).done;) {
1522
+ for (var _iterator2 = LiveObject._createForOfIteratorHelperLoose(rooms), _step2; !(_step2 = _iterator2()).done;) {
3957
1523
  var _step2$value = _step2.value,
3958
1524
  room = _step2$value[1];
3959
1525
  room.onVisibilityChange(document.visibilityState);
@@ -4023,7 +1589,7 @@ function liveObjectToJson(liveObject) {
4023
1589
  function liveMapToJson(map) {
4024
1590
  var result = {};
4025
1591
 
4026
- for (var _iterator = _createForOfIteratorHelperLoose(map.entries()), _step; !(_step = _iterator()).done;) {
1592
+ for (var _iterator = LiveObject._createForOfIteratorHelperLoose(map.entries()), _step; !(_step = _iterator()).done;) {
4027
1593
  var _step$value = _step.value,
4028
1594
  _key2 = _step$value[0],
4029
1595
  value = _step$value[1];
@@ -4042,15 +1608,15 @@ function liveListToJson(value) {
4042
1608
  }
4043
1609
 
4044
1610
  function lsonToJson(value) {
4045
- if (value instanceof LiveObject) {
1611
+ if (value instanceof LiveObject.LiveObject) {
4046
1612
  return liveObjectToJson(value);
4047
- } else if (value instanceof LiveList) {
1613
+ } else if (value instanceof LiveObject.LiveList) {
4048
1614
  return liveListToJson(value);
4049
- } else if (value instanceof LiveMap) {
1615
+ } else if (value instanceof LiveObject.LiveMap) {
4050
1616
  return liveMapToJson(value);
4051
- } else if (value instanceof LiveRegister) {
1617
+ } else if (value instanceof LiveObject.LiveRegister) {
4052
1618
  return value.data;
4053
- } else if (value instanceof AbstractCrdt) {
1619
+ } else if (value instanceof LiveObject.AbstractCrdt) {
4054
1620
  throw new Error("Unhandled subclass of AbstractCrdt encountered");
4055
1621
  }
4056
1622
 
@@ -4073,7 +1639,7 @@ function anyToCrdt(obj) {
4073
1639
  }
4074
1640
 
4075
1641
  if (Array.isArray(obj)) {
4076
- return new LiveList(obj.map(anyToCrdt));
1642
+ return new LiveObject.LiveList(obj.map(anyToCrdt));
4077
1643
  }
4078
1644
 
4079
1645
  if (isPlainObject(obj)) {
@@ -4083,7 +1649,7 @@ function anyToCrdt(obj) {
4083
1649
  init[_key3] = anyToCrdt(obj[_key3]);
4084
1650
  }
4085
1651
 
4086
- return new LiveObject(init);
1652
+ return new LiveObject.LiveObject(init);
4087
1653
  }
4088
1654
 
4089
1655
  return obj;
@@ -4144,7 +1710,7 @@ function patchLiveList(liveList, prev, next) {
4144
1710
  nextNode = next[i];
4145
1711
  var liveListNode = liveList.get(i);
4146
1712
 
4147
- if (liveListNode instanceof LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
1713
+ if (liveListNode instanceof LiveObject.LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
4148
1714
  patchLiveObject(liveListNode, prevNode, nextNode);
4149
1715
  } else {
4150
1716
  liveList.set(i, anyToCrdt(nextNode));
@@ -4168,7 +1734,7 @@ function patchLiveList(liveList, prev, next) {
4168
1734
  }
4169
1735
  function patchLiveObjectKey(liveObject, key, prev, next) {
4170
1736
  if (process.env.NODE_ENV !== "production") {
4171
- var nonSerializableValue = findNonSerializableValue(next);
1737
+ var nonSerializableValue = LiveObject.findNonSerializableValue(next);
4172
1738
 
4173
1739
  if (nonSerializableValue) {
4174
1740
  console.error("New state path: '" + nonSerializableValue.path + "' value: '" + nonSerializableValue.value + "' is not serializable.\nOnly serializable value can be synced with Liveblocks.");
@@ -4184,9 +1750,9 @@ function patchLiveObjectKey(liveObject, key, prev, next) {
4184
1750
  liveObject.set(key, anyToCrdt(next));
4185
1751
  } else if (prev === next) {
4186
1752
  return;
4187
- } else if (value instanceof LiveList && Array.isArray(prev) && Array.isArray(next)) {
1753
+ } else if (value instanceof LiveObject.LiveList && Array.isArray(prev) && Array.isArray(next)) {
4188
1754
  patchLiveList(value, prev, next);
4189
- } else if (value instanceof LiveObject && isPlainObject(prev) && isPlainObject(next)) {
1755
+ } else if (value instanceof LiveObject.LiveObject && isPlainObject(prev) && isPlainObject(next)) {
4190
1756
  patchLiveObject(value, prev, next);
4191
1757
  } else {
4192
1758
  liveObject.set(key, anyToCrdt(next));
@@ -4214,7 +1780,7 @@ function getParentsPath(node) {
4214
1780
  var path = [];
4215
1781
 
4216
1782
  while (node._parentKey != null && node._parent != null) {
4217
- if (node._parent instanceof LiveList) {
1783
+ if (node._parent instanceof LiveObject.LiveList) {
4218
1784
  path.push(node._parent._indexOfPosition(node._parentKey));
4219
1785
  } else {
4220
1786
  path.push(node._parentKey);
@@ -4301,7 +1867,7 @@ function patchImmutableNode(state, path, update) {
4301
1867
  }
4302
1868
  };
4303
1869
 
4304
- for (var _iterator2 = _createForOfIteratorHelperLoose(update.updates), _step2; !(_step2 = _iterator2()).done;) {
1870
+ for (var _iterator2 = LiveObject._createForOfIteratorHelperLoose(update.updates), _step2; !(_step2 = _iterator2()).done;) {
4305
1871
  _loop();
4306
1872
  }
4307
1873
 
@@ -4338,7 +1904,7 @@ function patchImmutableNode(state, path, update) {
4338
1904
  } else {
4339
1905
  var _extends2;
4340
1906
 
4341
- return _extends({}, state, (_extends2 = {}, _extends2[pathItem] = patchImmutableNode(state[pathItem], path, update), _extends2));
1907
+ return LiveObject._extends({}, state, (_extends2 = {}, _extends2[pathItem] = patchImmutableNode(state[pathItem], path, update), _extends2));
4342
1908
  }
4343
1909
  }
4344
1910
 
@@ -4351,8 +1917,8 @@ var internals = {
4351
1917
  patchLiveObjectKey: patchLiveObjectKey
4352
1918
  };
4353
1919
 
4354
- exports.LiveList = LiveList;
4355
- exports.LiveMap = LiveMap;
4356
- exports.LiveObject = LiveObject;
1920
+ exports.LiveList = LiveObject.LiveList;
1921
+ exports.LiveMap = LiveObject.LiveMap;
1922
+ exports.LiveObject = LiveObject.LiveObject;
4357
1923
  exports.createClient = createClient;
4358
1924
  exports.internals = internals;