@liveblocks/client 0.17.7 → 0.17.10-debug

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.
@@ -0,0 +1,3592 @@
1
+ "use strict";
2
+ function _arrayLikeToArray(arr, len) {
3
+ if (len == null || len > arr.length) len = arr.length;
4
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
5
+ return arr2;
6
+ }
7
+ function _arrayWithHoles(arr) {
8
+ if (Array.isArray(arr)) return arr;
9
+ }
10
+ function _arrayWithoutHoles(arr) {
11
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
12
+ }
13
+ function _assertThisInitialized(self) {
14
+ if (self === void 0) {
15
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
16
+ }
17
+ return self;
18
+ }
19
+ function _classCallCheck(instance, Constructor) {
20
+ if (!(instance instanceof Constructor)) {
21
+ throw new TypeError("Cannot call a class as a function");
22
+ }
23
+ }
24
+ function _defineProperties(target, props) {
25
+ for(var i = 0; i < props.length; i++){
26
+ var descriptor = props[i];
27
+ descriptor.enumerable = descriptor.enumerable || false;
28
+ descriptor.configurable = true;
29
+ if ("value" in descriptor) descriptor.writable = true;
30
+ Object.defineProperty(target, descriptor.key, descriptor);
31
+ }
32
+ }
33
+ function _createClass(Constructor, protoProps, staticProps) {
34
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
35
+ if (staticProps) _defineProperties(Constructor, staticProps);
36
+ return Constructor;
37
+ }
38
+ function _defineProperty(obj, key, value) {
39
+ if (key in obj) {
40
+ Object.defineProperty(obj, key, {
41
+ value: value,
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true
45
+ });
46
+ } else {
47
+ obj[key] = value;
48
+ }
49
+ return obj;
50
+ }
51
+ function _get(target, property, receiver) {
52
+ if (typeof Reflect !== "undefined" && Reflect.get) {
53
+ _get = Reflect.get;
54
+ } else {
55
+ _get = function _get(target, property, receiver) {
56
+ var base = _superPropBase(target, property);
57
+ if (!base) return;
58
+ var desc = Object.getOwnPropertyDescriptor(base, property);
59
+ if (desc.get) {
60
+ return desc.get.call(receiver);
61
+ }
62
+ return desc.value;
63
+ };
64
+ }
65
+ return _get(target, property, receiver || target);
66
+ }
67
+ function _getPrototypeOf(o) {
68
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
69
+ return o.__proto__ || Object.getPrototypeOf(o);
70
+ };
71
+ return _getPrototypeOf(o);
72
+ }
73
+ function _inherits(subClass, superClass) {
74
+ if (typeof superClass !== "function" && superClass !== null) {
75
+ throw new TypeError("Super expression must either be null or a function");
76
+ }
77
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
78
+ constructor: {
79
+ value: subClass,
80
+ writable: true,
81
+ configurable: true
82
+ }
83
+ });
84
+ if (superClass) _setPrototypeOf(subClass, superClass);
85
+ }
86
+ function _instanceof(left, right) {
87
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
88
+ return !!right[Symbol.hasInstance](left);
89
+ } else {
90
+ return left instanceof right;
91
+ }
92
+ }
93
+ function _iterableToArray(iter) {
94
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
95
+ }
96
+ function _iterableToArrayLimit(arr, i) {
97
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
98
+ if (_i == null) return;
99
+ var _arr = [];
100
+ var _n = true;
101
+ var _d = false;
102
+ var _s, _e;
103
+ try {
104
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
105
+ _arr.push(_s.value);
106
+ if (i && _arr.length === i) break;
107
+ }
108
+ } catch (err) {
109
+ _d = true;
110
+ _e = err;
111
+ } finally{
112
+ try {
113
+ if (!_n && _i["return"] != null) _i["return"]();
114
+ } finally{
115
+ if (_d) throw _e;
116
+ }
117
+ }
118
+ return _arr;
119
+ }
120
+ function _nonIterableRest() {
121
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
122
+ }
123
+ function _nonIterableSpread() {
124
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
125
+ }
126
+ function _objectSpread(target) {
127
+ for(var i = 1; i < arguments.length; i++){
128
+ var source = arguments[i] != null ? arguments[i] : {};
129
+ var ownKeys = Object.keys(source);
130
+ if (typeof Object.getOwnPropertySymbols === "function") {
131
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
132
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
133
+ }));
134
+ }
135
+ ownKeys.forEach(function(key) {
136
+ _defineProperty(target, key, source[key]);
137
+ });
138
+ }
139
+ return target;
140
+ }
141
+ function ownKeys(object, enumerableOnly) {
142
+ var keys = Object.keys(object);
143
+ if (Object.getOwnPropertySymbols) {
144
+ var symbols = Object.getOwnPropertySymbols(object);
145
+ if (enumerableOnly) {
146
+ symbols = symbols.filter(function(sym) {
147
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
148
+ });
149
+ }
150
+ keys.push.apply(keys, symbols);
151
+ }
152
+ return keys;
153
+ }
154
+ function _objectSpreadProps(target, source) {
155
+ source = source != null ? source : {};
156
+ if (Object.getOwnPropertyDescriptors) {
157
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
158
+ } else {
159
+ ownKeys(Object(source)).forEach(function(key) {
160
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
161
+ });
162
+ }
163
+ return target;
164
+ }
165
+ function _objectWithoutProperties(source, excluded) {
166
+ if (source == null) return {};
167
+ var target = _objectWithoutPropertiesLoose(source, excluded);
168
+ var key, i;
169
+ if (Object.getOwnPropertySymbols) {
170
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
171
+ for(i = 0; i < sourceSymbolKeys.length; i++){
172
+ key = sourceSymbolKeys[i];
173
+ if (excluded.indexOf(key) >= 0) continue;
174
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
175
+ target[key] = source[key];
176
+ }
177
+ }
178
+ return target;
179
+ }
180
+ function _objectWithoutPropertiesLoose(source, excluded) {
181
+ if (source == null) return {};
182
+ var target = {};
183
+ var sourceKeys = Object.keys(source);
184
+ var key, i;
185
+ for(i = 0; i < sourceKeys.length; i++){
186
+ key = sourceKeys[i];
187
+ if (excluded.indexOf(key) >= 0) continue;
188
+ target[key] = source[key];
189
+ }
190
+ return target;
191
+ }
192
+ function _possibleConstructorReturn(self, call) {
193
+ if (call && (_typeof(call) === "object" || typeof call === "function")) {
194
+ return call;
195
+ }
196
+ return _assertThisInitialized(self);
197
+ }
198
+ function _setPrototypeOf(o, p) {
199
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
200
+ o.__proto__ = p;
201
+ return o;
202
+ };
203
+ return _setPrototypeOf(o, p);
204
+ }
205
+ function _slicedToArray(arr, i) {
206
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
207
+ }
208
+ function _superPropBase(object, property) {
209
+ while(!Object.prototype.hasOwnProperty.call(object, property)){
210
+ object = _getPrototypeOf(object);
211
+ if (object === null) break;
212
+ }
213
+ return object;
214
+ }
215
+ function _toConsumableArray(arr) {
216
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
217
+ }
218
+ var _typeof = function(obj) {
219
+ "@swc/helpers - typeof";
220
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
221
+ };
222
+ function _unsupportedIterableToArray(o, minLen) {
223
+ if (!o) return;
224
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
225
+ var n = Object.prototype.toString.call(o).slice(8, -1);
226
+ if (n === "Object" && o.constructor) n = o.constructor.name;
227
+ if (n === "Map" || n === "Set") return Array.from(n);
228
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
229
+ }
230
+ function _isNativeReflectConstruct() {
231
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
232
+ if (Reflect.construct.sham) return false;
233
+ if (typeof Proxy === "function") return true;
234
+ try {
235
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
236
+ return true;
237
+ } catch (e) {
238
+ return false;
239
+ }
240
+ }
241
+ function _createSuper(Derived) {
242
+ var hasNativeReflectConstruct = _isNativeReflectConstruct();
243
+ return function _createSuperInternal() {
244
+ var Super = _getPrototypeOf(Derived), result;
245
+ if (hasNativeReflectConstruct) {
246
+ var NewTarget = _getPrototypeOf(this).constructor;
247
+ result = Reflect.construct(Super, arguments, NewTarget);
248
+ } else {
249
+ result = Super.apply(this, arguments);
250
+ }
251
+ return _possibleConstructorReturn(this, result);
252
+ };
253
+ }
254
+ Object.defineProperty(exports, "__esModule", {
255
+ value: true
256
+ });
257
+ function _optionalChain(ops) {
258
+ var lastAccessLHS = undefined;
259
+ var value = ops[0];
260
+ var i = 1;
261
+ while(i < ops.length){
262
+ var op = ops[i];
263
+ var fn = ops[i + 1];
264
+ i += 2;
265
+ if ((op === "optionalAccess" || op === "optionalCall") && value == null) {
266
+ return undefined;
267
+ }
268
+ if (op === "access" || op === "optionalAccess") {
269
+ lastAccessLHS = value;
270
+ value = fn(value);
271
+ } else if (op === "call" || op === "optionalCall") {
272
+ var _value;
273
+ value = fn(function() {
274
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
275
+ args[_key] = arguments[_key];
276
+ }
277
+ return (_value = value).call.apply(_value, [
278
+ lastAccessLHS
279
+ ].concat(_toConsumableArray(args)));
280
+ });
281
+ lastAccessLHS = undefined;
282
+ }
283
+ }
284
+ return value;
285
+ } // src/deprecation.ts
286
+ var _emittedDeprecationWarnings = /* @__PURE__ */ new Set();
287
+ function deprecate(message) {
288
+ var key = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : message;
289
+ if (process.env.NODE_ENV !== "production") {
290
+ if (!_emittedDeprecationWarnings.has(key)) {
291
+ _emittedDeprecationWarnings.add(key);
292
+ console.error("DEPRECATION WARNING: ".concat(message));
293
+ }
294
+ }
295
+ }
296
+ function deprecateIf(condition, message) {
297
+ var key = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : message;
298
+ if (process.env.NODE_ENV !== "production") {
299
+ if (condition) {
300
+ deprecate(message, key);
301
+ }
302
+ }
303
+ }
304
+ function throwUsageError(message) {
305
+ if (process.env.NODE_ENV !== "production") {
306
+ var usageError = new Error(message);
307
+ usageError.name = "Usage error";
308
+ throw usageError;
309
+ }
310
+ }
311
+ function errorIf(condition, message) {
312
+ if (process.env.NODE_ENV !== "production") {
313
+ if (condition) {
314
+ throwUsageError(message);
315
+ }
316
+ }
317
+ }
318
+ // src/assert.ts
319
+ function assertNever(_value, errmsg) {
320
+ throw new Error(errmsg);
321
+ }
322
+ function assert(condition, errmsg) {
323
+ if (process.env.NODE_ENV !== "production" && !condition) {
324
+ var err = new Error(errmsg);
325
+ err.name = "Assertion failure";
326
+ throw err;
327
+ }
328
+ }
329
+ function nn(value) {
330
+ var errmsg = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "Expected value to be non-nullable";
331
+ assert(value != null, errmsg);
332
+ return value;
333
+ }
334
+ // src/types/ClientMsg.ts
335
+ var ClientMsgCode = /* @__PURE__ */ function(ClientMsgCode2) {
336
+ ClientMsgCode2[ClientMsgCode2["UPDATE_PRESENCE"] = 100] = "UPDATE_PRESENCE";
337
+ ClientMsgCode2[ClientMsgCode2["BROADCAST_EVENT"] = 103] = "BROADCAST_EVENT";
338
+ ClientMsgCode2[ClientMsgCode2["FETCH_STORAGE"] = 200] = "FETCH_STORAGE";
339
+ ClientMsgCode2[ClientMsgCode2["UPDATE_STORAGE"] = 201] = "UPDATE_STORAGE";
340
+ return ClientMsgCode2;
341
+ }(ClientMsgCode || {});
342
+ // src/types/Op.ts
343
+ var OpCode = /* @__PURE__ */ function(OpCode2) {
344
+ OpCode2[OpCode2["INIT"] = 0] = "INIT";
345
+ OpCode2[OpCode2["SET_PARENT_KEY"] = 1] = "SET_PARENT_KEY";
346
+ OpCode2[OpCode2["CREATE_LIST"] = 2] = "CREATE_LIST";
347
+ OpCode2[OpCode2["UPDATE_OBJECT"] = 3] = "UPDATE_OBJECT";
348
+ OpCode2[OpCode2["CREATE_OBJECT"] = 4] = "CREATE_OBJECT";
349
+ OpCode2[OpCode2["DELETE_CRDT"] = 5] = "DELETE_CRDT";
350
+ OpCode2[OpCode2["DELETE_OBJECT_KEY"] = 6] = "DELETE_OBJECT_KEY";
351
+ OpCode2[OpCode2["CREATE_MAP"] = 7] = "CREATE_MAP";
352
+ OpCode2[OpCode2["CREATE_REGISTER"] = 8] = "CREATE_REGISTER";
353
+ return OpCode2;
354
+ }(OpCode || {});
355
+ // src/types/SerializedCrdt.ts
356
+ var CrdtType = /* @__PURE__ */ function(CrdtType2) {
357
+ CrdtType2[CrdtType2["OBJECT"] = 0] = "OBJECT";
358
+ CrdtType2[CrdtType2["LIST"] = 1] = "LIST";
359
+ CrdtType2[CrdtType2["MAP"] = 2] = "MAP";
360
+ CrdtType2[CrdtType2["REGISTER"] = 3] = "REGISTER";
361
+ return CrdtType2;
362
+ }(CrdtType || {});
363
+ function isRootCrdt(crdt) {
364
+ return crdt.type === 0 /* OBJECT */ && !isChildCrdt(crdt);
365
+ }
366
+ function isChildCrdt(crdt) {
367
+ return crdt.parentId !== void 0 && crdt.parentKey !== void 0;
368
+ }
369
+ // src/types/ServerMsg.ts
370
+ var ServerMsgCode = /* @__PURE__ */ function(ServerMsgCode2) {
371
+ ServerMsgCode2[ServerMsgCode2["UPDATE_PRESENCE"] = 100] = "UPDATE_PRESENCE";
372
+ ServerMsgCode2[ServerMsgCode2["USER_JOINED"] = 101] = "USER_JOINED";
373
+ ServerMsgCode2[ServerMsgCode2["USER_LEFT"] = 102] = "USER_LEFT";
374
+ ServerMsgCode2[ServerMsgCode2["BROADCASTED_EVENT"] = 103] = "BROADCASTED_EVENT";
375
+ ServerMsgCode2[ServerMsgCode2["ROOM_STATE"] = 104] = "ROOM_STATE";
376
+ ServerMsgCode2[ServerMsgCode2["INITIAL_STORAGE_STATE"] = 200] = "INITIAL_STORAGE_STATE";
377
+ ServerMsgCode2[ServerMsgCode2["UPDATE_STORAGE"] = 201] = "UPDATE_STORAGE";
378
+ return ServerMsgCode2;
379
+ }(ServerMsgCode || {});
380
+ // src/types/index.ts
381
+ function isRoomEventName(value) {
382
+ return value === "my-presence" || value === "others" || value === "event" || value === "error" || value === "connection" || value === "history";
383
+ }
384
+ var WebsocketCloseCodes = /* @__PURE__ */ function(WebsocketCloseCodes2) {
385
+ WebsocketCloseCodes2[WebsocketCloseCodes2["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
386
+ WebsocketCloseCodes2[WebsocketCloseCodes2["INVALID_MESSAGE_FORMAT"] = 4e3] = "INVALID_MESSAGE_FORMAT";
387
+ WebsocketCloseCodes2[WebsocketCloseCodes2["NOT_ALLOWED"] = 4001] = "NOT_ALLOWED";
388
+ WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_MESSAGES_PER_SECONDS"] = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS";
389
+ WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS"] = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS";
390
+ WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
391
+ WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
392
+ WebsocketCloseCodes2[WebsocketCloseCodes2["CLOSE_WITHOUT_RETRY"] = 4999] = "CLOSE_WITHOUT_RETRY";
393
+ return WebsocketCloseCodes2;
394
+ }(WebsocketCloseCodes || {});
395
+ // src/position.ts
396
+ var min = 32;
397
+ var max = 126;
398
+ function makePosition(before, after) {
399
+ if (before != null && after != null) {
400
+ return pos(makePositionFromCodes(posCodes(before), posCodes(after)));
401
+ } else if (before != null) {
402
+ return getNextPosition(before);
403
+ } else if (after != null) {
404
+ return getPreviousPosition(after);
405
+ }
406
+ return pos([
407
+ min + 1
408
+ ]);
409
+ }
410
+ function getPreviousPosition(after) {
411
+ var result = [];
412
+ var afterCodes = posCodes(after);
413
+ for(var i = 0; i < afterCodes.length; i++){
414
+ var code = afterCodes[i];
415
+ if (code <= min + 1) {
416
+ result.push(min);
417
+ if (afterCodes.length - 1 === i) {
418
+ result.push(max);
419
+ break;
420
+ }
421
+ } else {
422
+ result.push(code - 1);
423
+ break;
424
+ }
425
+ }
426
+ return pos(result);
427
+ }
428
+ function getNextPosition(before) {
429
+ var result = [];
430
+ var beforeCodes = posCodes(before);
431
+ for(var i = 0; i < beforeCodes.length; i++){
432
+ var code = beforeCodes[i];
433
+ if (code === max) {
434
+ result.push(code);
435
+ if (beforeCodes.length - 1 === i) {
436
+ result.push(min + 1);
437
+ break;
438
+ }
439
+ } else {
440
+ result.push(code + 1);
441
+ break;
442
+ }
443
+ }
444
+ return pos(result);
445
+ }
446
+ function makePositionFromCodes(before, after) {
447
+ var index = 0;
448
+ var result = [];
449
+ while(true){
450
+ var beforeDigit = before[index] || min;
451
+ var afterDigit = after[index] || max;
452
+ if (beforeDigit > afterDigit) {
453
+ throw new Error("Impossible to generate position between ".concat(before, " and ").concat(after));
454
+ }
455
+ if (beforeDigit === afterDigit) {
456
+ result.push(beforeDigit);
457
+ index++;
458
+ continue;
459
+ }
460
+ if (afterDigit - beforeDigit === 1) {
461
+ var _result;
462
+ result.push(beforeDigit);
463
+ (_result = result).push.apply(_result, _toConsumableArray(makePositionFromCodes(before.slice(index + 1), [])));
464
+ break;
465
+ }
466
+ var mid = afterDigit + beforeDigit >> 1;
467
+ result.push(mid);
468
+ break;
469
+ }
470
+ return result;
471
+ }
472
+ function posCodes(str) {
473
+ var codes = [];
474
+ for(var i = 0; i < str.length; i++){
475
+ codes.push(str.charCodeAt(i));
476
+ }
477
+ return codes;
478
+ }
479
+ function pos(codes) {
480
+ var _String;
481
+ return (_String = String).fromCharCode.apply(_String, _toConsumableArray(codes));
482
+ }
483
+ function comparePosition(posA, posB) {
484
+ var aCodes = posCodes(posA);
485
+ var bCodes = posCodes(posB);
486
+ var maxLength = Math.max(aCodes.length, bCodes.length);
487
+ for(var i = 0; i < maxLength; i++){
488
+ var a = aCodes[i] == null ? min : aCodes[i];
489
+ var b = bCodes[i] == null ? min : bCodes[i];
490
+ if (a === b) {
491
+ continue;
492
+ } else {
493
+ return a - b;
494
+ }
495
+ }
496
+ throw new Error('Impossible to compare similar position "'.concat(posA, '" and "').concat(posB, '"'));
497
+ }
498
+ // src/AbstractCrdt.ts
499
+ function crdtAsLiveNode(value) {
500
+ return value;
501
+ }
502
+ function HasParent(node, key) {
503
+ return Object.freeze({
504
+ type: "HasParent",
505
+ node: node,
506
+ key: key
507
+ });
508
+ }
509
+ var NoParent = Object.freeze({
510
+ type: "NoParent"
511
+ });
512
+ function Orphaned(oldKey) {
513
+ return Object.freeze({
514
+ type: "Orphaned",
515
+ oldKey: oldKey
516
+ });
517
+ }
518
+ var AbstractCrdt = /*#__PURE__*/ function() {
519
+ function AbstractCrdt() {
520
+ _classCallCheck(this, AbstractCrdt);
521
+ this._parent = NoParent;
522
+ }
523
+ _createClass(AbstractCrdt, [
524
+ {
525
+ key: "_getParentKeyOrThrow",
526
+ value: function _getParentKeyOrThrow() {
527
+ switch(this.parent.type){
528
+ case "HasParent":
529
+ return this.parent.key;
530
+ case "NoParent":
531
+ throw new Error("Parent key is missing");
532
+ case "Orphaned":
533
+ return this.parent.oldKey;
534
+ default:
535
+ return assertNever(this.parent, "Unknown state");
536
+ }
537
+ }
538
+ },
539
+ {
540
+ key: "_doc",
541
+ get: function get() {
542
+ return this.__doc;
543
+ }
544
+ },
545
+ {
546
+ key: "roomId",
547
+ get: function get() {
548
+ return this.__doc ? this.__doc.roomId : null;
549
+ }
550
+ },
551
+ {
552
+ key: "_id",
553
+ get: function get() {
554
+ return this.__id;
555
+ }
556
+ },
557
+ {
558
+ key: "parent",
559
+ get: function get() {
560
+ return this._parent;
561
+ }
562
+ },
563
+ {
564
+ key: "_parentNode",
565
+ get: function get() {
566
+ switch(this.parent.type){
567
+ case "HasParent":
568
+ return this.parent.node;
569
+ case "NoParent":
570
+ return null;
571
+ case "Orphaned":
572
+ return null;
573
+ default:
574
+ return assertNever(this.parent, "Unknown state");
575
+ }
576
+ }
577
+ },
578
+ {
579
+ key: "_parentKey",
580
+ get: function get() {
581
+ switch(this.parent.type){
582
+ case "HasParent":
583
+ return this.parent.key;
584
+ case "NoParent":
585
+ return null;
586
+ case "Orphaned":
587
+ return this.parent.oldKey;
588
+ default:
589
+ return assertNever(this.parent, "Unknown state");
590
+ }
591
+ }
592
+ },
593
+ {
594
+ key: "_apply",
595
+ value: function _apply(op, _isLocal) {
596
+ switch(op.type){
597
+ case 5 /* DELETE_CRDT */ :
598
+ {
599
+ if (this.parent.type === "HasParent") {
600
+ return this.parent.node._detachChild(crdtAsLiveNode(this));
601
+ }
602
+ return {
603
+ modified: false
604
+ };
605
+ }
606
+ }
607
+ return {
608
+ modified: false
609
+ };
610
+ }
611
+ },
612
+ {
613
+ key: "_setParentLink",
614
+ value: function _setParentLink(newParentNode, newParentKey) {
615
+ switch(this.parent.type){
616
+ case "HasParent":
617
+ if (this.parent.node !== newParentNode) {
618
+ throw new Error("Cannot attach parent if it already exist");
619
+ } else {
620
+ this._parent = HasParent(newParentNode, newParentKey);
621
+ return;
622
+ }
623
+ case "Orphaned":
624
+ case "NoParent":
625
+ {
626
+ this._parent = HasParent(newParentNode, newParentKey);
627
+ return;
628
+ }
629
+ default:
630
+ return assertNever(this.parent, "Unknown state");
631
+ }
632
+ }
633
+ },
634
+ {
635
+ key: "_attach",
636
+ value: function _attach(id, doc) {
637
+ if (this.__id || this.__doc) {
638
+ throw new Error("Cannot attach if CRDT is already attached");
639
+ }
640
+ doc.addItem(id, crdtAsLiveNode(this));
641
+ this.__id = id;
642
+ this.__doc = doc;
643
+ }
644
+ },
645
+ {
646
+ key: "_detach",
647
+ value: function _detach() {
648
+ if (this.__doc && this.__id) {
649
+ this.__doc.deleteItem(this.__id);
650
+ }
651
+ switch(this.parent.type){
652
+ case "HasParent":
653
+ {
654
+ this._parent = Orphaned(this.parent.key);
655
+ break;
656
+ }
657
+ case "NoParent":
658
+ {
659
+ this._parent = NoParent;
660
+ break;
661
+ }
662
+ case "Orphaned":
663
+ {
664
+ this._parent = Orphaned(this.parent.oldKey);
665
+ break;
666
+ }
667
+ default:
668
+ assertNever(this.parent, "Unknown state");
669
+ }
670
+ this.__doc = void 0;
671
+ }
672
+ }
673
+ ]);
674
+ return AbstractCrdt;
675
+ }();
676
+ // src/LiveRegister.ts
677
+ var LiveRegister = /*#__PURE__*/ function(AbstractCrdt) {
678
+ _inherits(LiveRegister, AbstractCrdt);
679
+ var _super = _createSuper(LiveRegister);
680
+ function LiveRegister(data) {
681
+ _classCallCheck(this, LiveRegister);
682
+ var _this;
683
+ _this = _super.call(this);
684
+ _this._data = data;
685
+ return _this;
686
+ }
687
+ _createClass(LiveRegister, [
688
+ {
689
+ key: "data",
690
+ get: function get() {
691
+ return this._data;
692
+ }
693
+ },
694
+ {
695
+ key: "_serialize",
696
+ value: function _serialize(parentId, parentKey, doc) {
697
+ if (this._id == null || parentId == null || parentKey == null) {
698
+ throw new Error("Cannot serialize register if parentId or parentKey is undefined");
699
+ }
700
+ return [
701
+ {
702
+ type: 8 /* CREATE_REGISTER */ ,
703
+ opId: _optionalChain([
704
+ doc,
705
+ "optionalAccess",
706
+ function(_2) {
707
+ return _2.generateOpId;
708
+ },
709
+ "call",
710
+ function(_3) {
711
+ return _3();
712
+ }
713
+ ]),
714
+ id: this._id,
715
+ parentId: parentId,
716
+ parentKey: parentKey,
717
+ data: this.data
718
+ }
719
+ ];
720
+ }
721
+ },
722
+ {
723
+ key: "_toSerializedCrdt",
724
+ value: function _toSerializedCrdt() {
725
+ if (this.parent.type !== "HasParent") {
726
+ throw new Error("Cannot serialize LiveRegister if parent is missing");
727
+ }
728
+ return {
729
+ type: 3 /* REGISTER */ ,
730
+ parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
731
+ parentKey: this.parent.key,
732
+ data: this.data
733
+ };
734
+ }
735
+ },
736
+ {
737
+ key: "_attachChild",
738
+ value: function _attachChild(_op) {
739
+ throw new Error("Method not implemented.");
740
+ }
741
+ },
742
+ {
743
+ key: "_detachChild",
744
+ value: function _detachChild(_crdt) {
745
+ throw new Error("Method not implemented.");
746
+ }
747
+ },
748
+ {
749
+ key: "_apply",
750
+ value: function _apply(op, isLocal) {
751
+ return _get(_getPrototypeOf(LiveRegister.prototype), "_apply", this).call(this, op, isLocal);
752
+ }
753
+ }
754
+ ], [
755
+ {
756
+ key: "_deserialize",
757
+ value: function _deserialize(param, _parentToChildren, doc) {
758
+ var _param = _slicedToArray(param, 2), id = _param[0], item = _param[1];
759
+ var register = new LiveRegister(item.data);
760
+ register._attach(id, doc);
761
+ return register;
762
+ }
763
+ }
764
+ ]);
765
+ return LiveRegister;
766
+ }(AbstractCrdt);
767
+ // src/LiveMap.ts
768
+ var LiveMap = /*#__PURE__*/ function(AbstractCrdt) {
769
+ _inherits(LiveMap, AbstractCrdt);
770
+ var _super = _createSuper(LiveMap);
771
+ function LiveMap(entries2) {
772
+ _classCallCheck(this, LiveMap);
773
+ var _this;
774
+ _this = _super.call(this);
775
+ errorIf(entries2 === null, "Support for calling `new LiveMap(null)` will be removed in @liveblocks/client 0.18. Please call as `new LiveMap()`, or `new LiveMap([])`.");
776
+ _this.unacknowledgedSet = /* @__PURE__ */ new Map();
777
+ if (entries2) {
778
+ var mappedEntries = [];
779
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
780
+ try {
781
+ for(var _iterator = entries2[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
782
+ var entry = _step.value;
783
+ var value = lsonToLiveNode(entry[1]);
784
+ value._setParentLink(_assertThisInitialized(_this), entry[0]);
785
+ mappedEntries.push([
786
+ entry[0],
787
+ value
788
+ ]);
789
+ }
790
+ } catch (err) {
791
+ _didIteratorError = true;
792
+ _iteratorError = err;
793
+ } finally{
794
+ try {
795
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
796
+ _iterator.return();
797
+ }
798
+ } finally{
799
+ if (_didIteratorError) {
800
+ throw _iteratorError;
801
+ }
802
+ }
803
+ }
804
+ _this._map = new Map(mappedEntries);
805
+ } else {
806
+ _this._map = /* @__PURE__ */ new Map();
807
+ }
808
+ return _this;
809
+ }
810
+ _createClass(LiveMap, [
811
+ {
812
+ key: "_serialize",
813
+ value: function _serialize(parentId, parentKey, doc) {
814
+ if (this._id == null) {
815
+ throw new Error("Cannot serialize item is not attached");
816
+ }
817
+ var ops = [];
818
+ var op = {
819
+ id: this._id,
820
+ opId: _optionalChain([
821
+ doc,
822
+ "optionalAccess",
823
+ function(_4) {
824
+ return _4.generateOpId;
825
+ },
826
+ "call",
827
+ function(_5) {
828
+ return _5();
829
+ }
830
+ ]),
831
+ type: 7 /* CREATE_MAP */ ,
832
+ parentId: parentId,
833
+ parentKey: parentKey
834
+ };
835
+ ops.push(op);
836
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
837
+ try {
838
+ for(var _iterator = this._map[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
839
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
840
+ var _ops;
841
+ (_ops = ops).push.apply(_ops, _toConsumableArray(value._serialize(this._id, key, doc)));
842
+ }
843
+ } catch (err) {
844
+ _didIteratorError = true;
845
+ _iteratorError = err;
846
+ } finally{
847
+ try {
848
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
849
+ _iterator.return();
850
+ }
851
+ } finally{
852
+ if (_didIteratorError) {
853
+ throw _iteratorError;
854
+ }
855
+ }
856
+ }
857
+ return ops;
858
+ }
859
+ },
860
+ {
861
+ key: "_attach",
862
+ value: function _attach(id, doc) {
863
+ _get(_getPrototypeOf(LiveMap.prototype), "_attach", this).call(this, id, doc);
864
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
865
+ try {
866
+ for(var _iterator = this._map[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
867
+ var _value = _slicedToArray(_step.value, 2), _key = _value[0], value = _value[1];
868
+ if (isLiveNode(value)) {
869
+ value._attach(doc.generateId(), doc);
870
+ }
871
+ }
872
+ } catch (err) {
873
+ _didIteratorError = true;
874
+ _iteratorError = err;
875
+ } finally{
876
+ try {
877
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
878
+ _iterator.return();
879
+ }
880
+ } finally{
881
+ if (_didIteratorError) {
882
+ throw _iteratorError;
883
+ }
884
+ }
885
+ }
886
+ }
887
+ },
888
+ {
889
+ key: "_attachChild",
890
+ value: function _attachChild(op, source) {
891
+ if (this._doc == null) {
892
+ throw new Error("Can't attach child if doc is not present");
893
+ }
894
+ var id = op.id, parentKey = op.parentKey, opId = op.opId;
895
+ var key = parentKey;
896
+ var child = creationOpToLiveNode(op);
897
+ if (this._doc.getItem(id) !== void 0) {
898
+ return {
899
+ modified: false
900
+ };
901
+ }
902
+ if (source === 2 /* ACK */ ) {
903
+ var lastUpdateOpId = this.unacknowledgedSet.get(key);
904
+ if (lastUpdateOpId === opId) {
905
+ this.unacknowledgedSet.delete(key);
906
+ return {
907
+ modified: false
908
+ };
909
+ } else if (lastUpdateOpId != null) {
910
+ return {
911
+ modified: false
912
+ };
913
+ }
914
+ } else if (source === 1 /* REMOTE */ ) {
915
+ this.unacknowledgedSet.delete(key);
916
+ }
917
+ var previousValue = this._map.get(key);
918
+ var reverse;
919
+ if (previousValue) {
920
+ var thisId = nn(this._id);
921
+ reverse = previousValue._serialize(thisId, key);
922
+ previousValue._detach();
923
+ } else {
924
+ reverse = [
925
+ {
926
+ type: 5 /* DELETE_CRDT */ ,
927
+ id: id
928
+ }
929
+ ];
930
+ }
931
+ child._setParentLink(this, key);
932
+ child._attach(id, this._doc);
933
+ this._map.set(key, child);
934
+ return {
935
+ modified: {
936
+ node: this,
937
+ type: "LiveMap",
938
+ updates: _defineProperty({}, key, {
939
+ type: "update"
940
+ })
941
+ },
942
+ reverse: reverse
943
+ };
944
+ }
945
+ },
946
+ {
947
+ key: "_detach",
948
+ value: function _detach() {
949
+ _get(_getPrototypeOf(LiveMap.prototype), "_detach", this).call(this);
950
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
951
+ try {
952
+ for(var _iterator = this._map.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
953
+ var item = _step.value;
954
+ item._detach();
955
+ }
956
+ } catch (err) {
957
+ _didIteratorError = true;
958
+ _iteratorError = err;
959
+ } finally{
960
+ try {
961
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
962
+ _iterator.return();
963
+ }
964
+ } finally{
965
+ if (_didIteratorError) {
966
+ throw _iteratorError;
967
+ }
968
+ }
969
+ }
970
+ }
971
+ },
972
+ {
973
+ key: "_detachChild",
974
+ value: function _detachChild(child) {
975
+ var id = nn(this._id);
976
+ var parentKey = nn(child._parentKey);
977
+ var reverse = child._serialize(id, parentKey, this._doc);
978
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
979
+ try {
980
+ for(var _iterator = this._map[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
981
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
982
+ if (value === child) {
983
+ this._map.delete(key);
984
+ }
985
+ }
986
+ } catch (err) {
987
+ _didIteratorError = true;
988
+ _iteratorError = err;
989
+ } finally{
990
+ try {
991
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
992
+ _iterator.return();
993
+ }
994
+ } finally{
995
+ if (_didIteratorError) {
996
+ throw _iteratorError;
997
+ }
998
+ }
999
+ }
1000
+ child._detach();
1001
+ var storageUpdate = {
1002
+ node: this,
1003
+ type: "LiveMap",
1004
+ updates: _defineProperty({}, parentKey, {
1005
+ type: "delete"
1006
+ })
1007
+ };
1008
+ return {
1009
+ modified: storageUpdate,
1010
+ reverse: reverse
1011
+ };
1012
+ }
1013
+ },
1014
+ {
1015
+ key: "_toSerializedCrdt",
1016
+ value: function _toSerializedCrdt() {
1017
+ if (this.parent.type !== "HasParent") {
1018
+ throw new Error("Cannot serialize LiveMap if parent is missing");
1019
+ }
1020
+ return {
1021
+ type: 2 /* MAP */ ,
1022
+ parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
1023
+ parentKey: this.parent.key
1024
+ };
1025
+ }
1026
+ },
1027
+ {
1028
+ key: "get",
1029
+ value: function get(key) {
1030
+ var value = this._map.get(key);
1031
+ if (value == void 0) {
1032
+ return void 0;
1033
+ }
1034
+ return liveNodeToLson(value);
1035
+ }
1036
+ },
1037
+ {
1038
+ key: "set",
1039
+ value: function set(key, value) {
1040
+ var oldValue = this._map.get(key);
1041
+ if (oldValue) {
1042
+ oldValue._detach();
1043
+ }
1044
+ var item = lsonToLiveNode(value);
1045
+ item._setParentLink(this, key);
1046
+ this._map.set(key, item);
1047
+ if (this._doc && this._id) {
1048
+ var id = this._doc.generateId();
1049
+ item._attach(id, this._doc);
1050
+ var storageUpdates = /* @__PURE__ */ new Map();
1051
+ storageUpdates.set(this._id, {
1052
+ node: this,
1053
+ type: "LiveMap",
1054
+ updates: _defineProperty({}, key, {
1055
+ type: "update"
1056
+ })
1057
+ });
1058
+ var ops = item._serialize(this._id, key, this._doc);
1059
+ this.unacknowledgedSet.set(key, nn(ops[0].opId));
1060
+ this._doc.dispatch(item._serialize(this._id, key, this._doc), oldValue ? oldValue._serialize(this._id, key) : [
1061
+ {
1062
+ type: 5 /* DELETE_CRDT */ ,
1063
+ id: id
1064
+ }
1065
+ ], storageUpdates);
1066
+ }
1067
+ }
1068
+ },
1069
+ {
1070
+ key: "size",
1071
+ get: function get() {
1072
+ return this._map.size;
1073
+ }
1074
+ },
1075
+ {
1076
+ key: "has",
1077
+ value: function has(key) {
1078
+ return this._map.has(key);
1079
+ }
1080
+ },
1081
+ {
1082
+ key: "delete",
1083
+ value: function _delete(key) {
1084
+ var item = this._map.get(key);
1085
+ if (item == null) {
1086
+ return false;
1087
+ }
1088
+ item._detach();
1089
+ this._map.delete(key);
1090
+ if (this._doc && item._id) {
1091
+ var thisId = nn(this._id);
1092
+ var storageUpdates = /* @__PURE__ */ new Map();
1093
+ storageUpdates.set(thisId, {
1094
+ node: this,
1095
+ type: "LiveMap",
1096
+ updates: _defineProperty({}, key, {
1097
+ type: "delete"
1098
+ })
1099
+ });
1100
+ this._doc.dispatch([
1101
+ {
1102
+ type: 5 /* DELETE_CRDT */ ,
1103
+ id: item._id,
1104
+ opId: this._doc.generateOpId()
1105
+ }
1106
+ ], item._serialize(thisId, key), storageUpdates);
1107
+ }
1108
+ return true;
1109
+ }
1110
+ },
1111
+ {
1112
+ key: "entries",
1113
+ value: function entries() {
1114
+ var innerIterator = this._map.entries();
1115
+ var _obj;
1116
+ return _obj = {}, _defineProperty(_obj, Symbol.iterator, function() {
1117
+ return this;
1118
+ }), _defineProperty(_obj, "next", function next() {
1119
+ var iteratorValue = innerIterator.next();
1120
+ if (iteratorValue.done) {
1121
+ return {
1122
+ done: true,
1123
+ value: void 0
1124
+ };
1125
+ }
1126
+ var entry = iteratorValue.value;
1127
+ var key = entry[0];
1128
+ var value = liveNodeToLson(iteratorValue.value[1]);
1129
+ return {
1130
+ value: [
1131
+ key,
1132
+ value
1133
+ ]
1134
+ };
1135
+ }), _obj;
1136
+ }
1137
+ },
1138
+ {
1139
+ key: Symbol.iterator,
1140
+ value: function value() {
1141
+ return this.entries();
1142
+ }
1143
+ },
1144
+ {
1145
+ key: "keys",
1146
+ value: function keys() {
1147
+ return this._map.keys();
1148
+ }
1149
+ },
1150
+ {
1151
+ key: "values",
1152
+ value: function values() {
1153
+ var innerIterator = this._map.values();
1154
+ var _obj;
1155
+ return _obj = {}, _defineProperty(_obj, Symbol.iterator, function() {
1156
+ return this;
1157
+ }), _defineProperty(_obj, "next", function next() {
1158
+ var iteratorValue = innerIterator.next();
1159
+ if (iteratorValue.done) {
1160
+ return {
1161
+ done: true,
1162
+ value: void 0
1163
+ };
1164
+ }
1165
+ var value = liveNodeToLson(iteratorValue.value);
1166
+ return {
1167
+ value: value
1168
+ };
1169
+ }), _obj;
1170
+ }
1171
+ },
1172
+ {
1173
+ key: "forEach",
1174
+ value: function forEach(callback) {
1175
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1176
+ try {
1177
+ for(var _iterator = this[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1178
+ var entry = _step.value;
1179
+ callback(entry[1], entry[0], this);
1180
+ }
1181
+ } catch (err) {
1182
+ _didIteratorError = true;
1183
+ _iteratorError = err;
1184
+ } finally{
1185
+ try {
1186
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1187
+ _iterator.return();
1188
+ }
1189
+ } finally{
1190
+ if (_didIteratorError) {
1191
+ throw _iteratorError;
1192
+ }
1193
+ }
1194
+ }
1195
+ }
1196
+ }
1197
+ ], [
1198
+ {
1199
+ key: "_deserialize",
1200
+ value: function _deserialize(param, parentToChildren, doc) {
1201
+ var _param = _slicedToArray(param, 2), id = _param[0], _item = _param[1];
1202
+ var map = new LiveMap();
1203
+ map._attach(id, doc);
1204
+ var children = parentToChildren.get(id);
1205
+ if (children == null) {
1206
+ return map;
1207
+ }
1208
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1209
+ try {
1210
+ for(var _iterator = children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1211
+ var _value = _slicedToArray(_step.value, 2), id2 = _value[0], crdt = _value[1];
1212
+ var child = deserialize([
1213
+ id2,
1214
+ crdt
1215
+ ], parentToChildren, doc);
1216
+ child._setParentLink(map, crdt.parentKey);
1217
+ map._map.set(crdt.parentKey, child);
1218
+ }
1219
+ } catch (err) {
1220
+ _didIteratorError = true;
1221
+ _iteratorError = err;
1222
+ } finally{
1223
+ try {
1224
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1225
+ _iterator.return();
1226
+ }
1227
+ } finally{
1228
+ if (_didIteratorError) {
1229
+ throw _iteratorError;
1230
+ }
1231
+ }
1232
+ }
1233
+ return map;
1234
+ }
1235
+ }
1236
+ ]);
1237
+ return LiveMap;
1238
+ }(AbstractCrdt);
1239
+ // src/LiveObject.ts
1240
+ var LiveObject = /*#__PURE__*/ function(AbstractCrdt) {
1241
+ _inherits(LiveObject, AbstractCrdt);
1242
+ var _super = _createSuper(LiveObject);
1243
+ function LiveObject() {
1244
+ var obj = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
1245
+ _classCallCheck(this, LiveObject);
1246
+ var _this;
1247
+ _this = _super.call(this);
1248
+ _this._propToLastUpdate = /* @__PURE__ */ new Map();
1249
+ for(var key in obj){
1250
+ var value = obj[key];
1251
+ if (value === void 0) {
1252
+ continue;
1253
+ } else if (isLiveNode(value)) {
1254
+ value._setParentLink(_assertThisInitialized(_this), key);
1255
+ }
1256
+ }
1257
+ _this._map = new Map(Object.entries(obj));
1258
+ return _this;
1259
+ }
1260
+ _createClass(LiveObject, [
1261
+ {
1262
+ key: "_serialize",
1263
+ value: function _serialize(parentId, parentKey, doc) {
1264
+ if (this._id == null) {
1265
+ throw new Error("Cannot serialize item is not attached");
1266
+ }
1267
+ var opId = _optionalChain([
1268
+ doc,
1269
+ "optionalAccess",
1270
+ function(_6) {
1271
+ return _6.generateOpId;
1272
+ },
1273
+ "call",
1274
+ function(_7) {
1275
+ return _7();
1276
+ }
1277
+ ]);
1278
+ var ops = [];
1279
+ var op = parentId !== void 0 && parentKey !== void 0 ? {
1280
+ type: 4 /* CREATE_OBJECT */ ,
1281
+ id: this._id,
1282
+ opId: opId,
1283
+ parentId: parentId,
1284
+ parentKey: parentKey,
1285
+ data: {}
1286
+ } : {
1287
+ type: 4 /* CREATE_OBJECT */ ,
1288
+ id: this._id,
1289
+ opId: opId,
1290
+ data: {}
1291
+ };
1292
+ ops.push(op);
1293
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1294
+ try {
1295
+ for(var _iterator = this._map[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1296
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
1297
+ if (isLiveNode(value)) {
1298
+ var _ops;
1299
+ (_ops = ops).push.apply(_ops, _toConsumableArray(value._serialize(this._id, key, doc)));
1300
+ } else {
1301
+ op.data[key] = value;
1302
+ }
1303
+ }
1304
+ } catch (err) {
1305
+ _didIteratorError = true;
1306
+ _iteratorError = err;
1307
+ } finally{
1308
+ try {
1309
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1310
+ _iterator.return();
1311
+ }
1312
+ } finally{
1313
+ if (_didIteratorError) {
1314
+ throw _iteratorError;
1315
+ }
1316
+ }
1317
+ }
1318
+ return ops;
1319
+ }
1320
+ },
1321
+ {
1322
+ key: "_attach",
1323
+ value: function _attach(id, doc) {
1324
+ _get(_getPrototypeOf(LiveObject.prototype), "_attach", this).call(this, id, doc);
1325
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1326
+ try {
1327
+ for(var _iterator = this._map[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1328
+ var _value = _slicedToArray(_step.value, 2), _key = _value[0], value = _value[1];
1329
+ if (isLiveNode(value)) {
1330
+ value._attach(doc.generateId(), doc);
1331
+ }
1332
+ }
1333
+ } catch (err) {
1334
+ _didIteratorError = true;
1335
+ _iteratorError = err;
1336
+ } finally{
1337
+ try {
1338
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1339
+ _iterator.return();
1340
+ }
1341
+ } finally{
1342
+ if (_didIteratorError) {
1343
+ throw _iteratorError;
1344
+ }
1345
+ }
1346
+ }
1347
+ }
1348
+ },
1349
+ {
1350
+ key: "_attachChild",
1351
+ value: function _attachChild(op, source) {
1352
+ if (this._doc == null) {
1353
+ throw new Error("Can't attach child if doc is not present");
1354
+ }
1355
+ var id = op.id, opId = op.opId, key = op.parentKey;
1356
+ var child = creationOpToLson(op);
1357
+ if (this._doc.getItem(id) !== void 0) {
1358
+ if (this._propToLastUpdate.get(key) === opId) {
1359
+ this._propToLastUpdate.delete(key);
1360
+ }
1361
+ return {
1362
+ modified: false
1363
+ };
1364
+ }
1365
+ if (source === 0 /* UNDOREDO_RECONNECT */ ) {
1366
+ this._propToLastUpdate.set(key, nn(opId));
1367
+ } else if (this._propToLastUpdate.get(key) === void 0) {} else if (this._propToLastUpdate.get(key) === opId) {
1368
+ this._propToLastUpdate.delete(key);
1369
+ return {
1370
+ modified: false
1371
+ };
1372
+ } else {
1373
+ return {
1374
+ modified: false
1375
+ };
1376
+ }
1377
+ var thisId = nn(this._id);
1378
+ var previousValue = this._map.get(key);
1379
+ var reverse;
1380
+ if (isLiveNode(previousValue)) {
1381
+ reverse = previousValue._serialize(thisId, key);
1382
+ previousValue._detach();
1383
+ } else if (previousValue === void 0) {
1384
+ reverse = [
1385
+ {
1386
+ type: 6 /* DELETE_OBJECT_KEY */ ,
1387
+ id: thisId,
1388
+ key: key
1389
+ }
1390
+ ];
1391
+ } else {
1392
+ reverse = [
1393
+ {
1394
+ type: 3 /* UPDATE_OBJECT */ ,
1395
+ id: thisId,
1396
+ data: _defineProperty({}, key, previousValue)
1397
+ }
1398
+ ];
1399
+ }
1400
+ this._map.set(key, child);
1401
+ if (isLiveStructure(child)) {
1402
+ child._setParentLink(this, key);
1403
+ child._attach(id, this._doc);
1404
+ }
1405
+ return {
1406
+ reverse: reverse,
1407
+ modified: {
1408
+ node: this,
1409
+ type: "LiveObject",
1410
+ updates: _defineProperty({}, key, {
1411
+ type: "update"
1412
+ })
1413
+ }
1414
+ };
1415
+ }
1416
+ },
1417
+ {
1418
+ key: "_detachChild",
1419
+ value: function _detachChild(child) {
1420
+ if (child) {
1421
+ var id = nn(this._id);
1422
+ var parentKey = nn(child._parentKey);
1423
+ var reverse = child._serialize(id, parentKey, this._doc);
1424
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1425
+ try {
1426
+ for(var _iterator = this._map[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1427
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
1428
+ if (value === child) {
1429
+ this._map.delete(key);
1430
+ }
1431
+ }
1432
+ } catch (err) {
1433
+ _didIteratorError = true;
1434
+ _iteratorError = err;
1435
+ } finally{
1436
+ try {
1437
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1438
+ _iterator.return();
1439
+ }
1440
+ } finally{
1441
+ if (_didIteratorError) {
1442
+ throw _iteratorError;
1443
+ }
1444
+ }
1445
+ }
1446
+ child._detach();
1447
+ var storageUpdate = {
1448
+ node: this,
1449
+ type: "LiveObject",
1450
+ updates: _defineProperty({}, parentKey, {
1451
+ type: "delete"
1452
+ })
1453
+ };
1454
+ return {
1455
+ modified: storageUpdate,
1456
+ reverse: reverse
1457
+ };
1458
+ }
1459
+ return {
1460
+ modified: false
1461
+ };
1462
+ }
1463
+ },
1464
+ {
1465
+ key: "_detach",
1466
+ value: function _detach() {
1467
+ _get(_getPrototypeOf(LiveObject.prototype), "_detach", this).call(this);
1468
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1469
+ try {
1470
+ for(var _iterator = this._map.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1471
+ var value = _step.value;
1472
+ if (isLiveNode(value)) {
1473
+ value._detach();
1474
+ }
1475
+ }
1476
+ } catch (err) {
1477
+ _didIteratorError = true;
1478
+ _iteratorError = err;
1479
+ } finally{
1480
+ try {
1481
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1482
+ _iterator.return();
1483
+ }
1484
+ } finally{
1485
+ if (_didIteratorError) {
1486
+ throw _iteratorError;
1487
+ }
1488
+ }
1489
+ }
1490
+ }
1491
+ },
1492
+ {
1493
+ key: "_apply",
1494
+ value: function _apply(op, isLocal) {
1495
+ if (op.type === 3 /* UPDATE_OBJECT */ ) {
1496
+ return this._applyUpdate(op, isLocal);
1497
+ } else if (op.type === 6 /* DELETE_OBJECT_KEY */ ) {
1498
+ return this._applyDeleteObjectKey(op);
1499
+ }
1500
+ return _get(_getPrototypeOf(LiveObject.prototype), "_apply", this).call(this, op, isLocal);
1501
+ }
1502
+ },
1503
+ {
1504
+ key: "_toSerializedCrdt",
1505
+ value: function _toSerializedCrdt() {
1506
+ var data = {};
1507
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1508
+ try {
1509
+ for(var _iterator = this._map[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1510
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
1511
+ if (!isLiveNode(value)) {
1512
+ data[key] = value;
1513
+ }
1514
+ }
1515
+ } catch (err) {
1516
+ _didIteratorError = true;
1517
+ _iteratorError = err;
1518
+ } finally{
1519
+ try {
1520
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1521
+ _iterator.return();
1522
+ }
1523
+ } finally{
1524
+ if (_didIteratorError) {
1525
+ throw _iteratorError;
1526
+ }
1527
+ }
1528
+ }
1529
+ if (this.parent.type === "HasParent" && this.parent.node._id) {
1530
+ return {
1531
+ type: 0 /* OBJECT */ ,
1532
+ parentId: this.parent.node._id,
1533
+ parentKey: this.parent.key,
1534
+ data: data
1535
+ };
1536
+ } else {
1537
+ return {
1538
+ type: 0 /* OBJECT */ ,
1539
+ data: data
1540
+ };
1541
+ }
1542
+ }
1543
+ },
1544
+ {
1545
+ key: "_applyUpdate",
1546
+ value: function _applyUpdate(op, isLocal) {
1547
+ var isModified = false;
1548
+ var id = nn(this._id);
1549
+ var reverse = [];
1550
+ var reverseUpdate = {
1551
+ type: 3 /* UPDATE_OBJECT */ ,
1552
+ id: id,
1553
+ data: {}
1554
+ };
1555
+ reverse.push(reverseUpdate);
1556
+ for(var key in op.data){
1557
+ var oldValue = this._map.get(key);
1558
+ if (isLiveNode(oldValue)) {
1559
+ var _reverse;
1560
+ (_reverse = reverse).push.apply(_reverse, _toConsumableArray(oldValue._serialize(id, key)));
1561
+ oldValue._detach();
1562
+ } else if (oldValue !== void 0) {
1563
+ reverseUpdate.data[key] = oldValue;
1564
+ } else if (oldValue === void 0) {
1565
+ reverse.push({
1566
+ type: 6 /* DELETE_OBJECT_KEY */ ,
1567
+ id: id,
1568
+ key: key
1569
+ });
1570
+ }
1571
+ }
1572
+ var updateDelta = {};
1573
+ for(var key1 in op.data){
1574
+ var value = op.data[key1];
1575
+ if (value === void 0) {
1576
+ continue;
1577
+ }
1578
+ if (isLocal) {
1579
+ this._propToLastUpdate.set(key1, nn(op.opId));
1580
+ } else if (this._propToLastUpdate.get(key1) == null) {
1581
+ isModified = true;
1582
+ } else if (this._propToLastUpdate.get(key1) === op.opId) {
1583
+ this._propToLastUpdate.delete(key1);
1584
+ continue;
1585
+ } else {
1586
+ continue;
1587
+ }
1588
+ var oldValue1 = this._map.get(key1);
1589
+ if (isLiveNode(oldValue1)) {
1590
+ oldValue1._detach();
1591
+ }
1592
+ isModified = true;
1593
+ updateDelta[key1] = {
1594
+ type: "update"
1595
+ };
1596
+ this._map.set(key1, value);
1597
+ }
1598
+ if (Object.keys(reverseUpdate.data).length !== 0) {
1599
+ reverse.unshift(reverseUpdate);
1600
+ }
1601
+ return isModified ? {
1602
+ modified: {
1603
+ node: this,
1604
+ type: "LiveObject",
1605
+ updates: updateDelta
1606
+ },
1607
+ reverse: reverse
1608
+ } : {
1609
+ modified: false
1610
+ };
1611
+ }
1612
+ },
1613
+ {
1614
+ key: "_applyDeleteObjectKey",
1615
+ value: function _applyDeleteObjectKey(op) {
1616
+ var key = op.key;
1617
+ if (this._map.has(key) === false) {
1618
+ return {
1619
+ modified: false
1620
+ };
1621
+ }
1622
+ if (this._propToLastUpdate.get(key) !== void 0) {
1623
+ return {
1624
+ modified: false
1625
+ };
1626
+ }
1627
+ var oldValue = this._map.get(key);
1628
+ var id = nn(this._id);
1629
+ var reverse = [];
1630
+ if (isLiveNode(oldValue)) {
1631
+ reverse = oldValue._serialize(id, op.key);
1632
+ oldValue._detach();
1633
+ } else if (oldValue !== void 0) {
1634
+ reverse = [
1635
+ {
1636
+ type: 3 /* UPDATE_OBJECT */ ,
1637
+ id: id,
1638
+ data: _defineProperty({}, key, oldValue)
1639
+ }
1640
+ ];
1641
+ }
1642
+ this._map.delete(key);
1643
+ return {
1644
+ modified: {
1645
+ node: this,
1646
+ type: "LiveObject",
1647
+ updates: _defineProperty({}, op.key, {
1648
+ type: "delete"
1649
+ })
1650
+ },
1651
+ reverse: reverse
1652
+ };
1653
+ }
1654
+ },
1655
+ {
1656
+ key: "toObject",
1657
+ value: function toObject() {
1658
+ return fromEntries(this._map);
1659
+ }
1660
+ },
1661
+ {
1662
+ key: "set",
1663
+ value: function set(key, value) {
1664
+ this.update(_defineProperty({}, key, value));
1665
+ }
1666
+ },
1667
+ {
1668
+ key: "get",
1669
+ value: function get(key) {
1670
+ return this._map.get(key);
1671
+ }
1672
+ },
1673
+ {
1674
+ key: "delete",
1675
+ value: function _delete(key) {
1676
+ var keyAsString = key;
1677
+ var oldValue = this._map.get(keyAsString);
1678
+ if (oldValue === void 0) {
1679
+ return;
1680
+ }
1681
+ if (this._doc == null || this._id == null) {
1682
+ if (isLiveNode(oldValue)) {
1683
+ oldValue._detach();
1684
+ }
1685
+ this._map.delete(keyAsString);
1686
+ return;
1687
+ }
1688
+ var reverse;
1689
+ if (isLiveNode(oldValue)) {
1690
+ oldValue._detach();
1691
+ reverse = oldValue._serialize(this._id, keyAsString);
1692
+ } else {
1693
+ reverse = [
1694
+ {
1695
+ type: 3 /* UPDATE_OBJECT */ ,
1696
+ data: _defineProperty({}, keyAsString, oldValue),
1697
+ id: this._id
1698
+ }
1699
+ ];
1700
+ }
1701
+ this._map.delete(keyAsString);
1702
+ var storageUpdates = /* @__PURE__ */ new Map();
1703
+ storageUpdates.set(this._id, {
1704
+ node: this,
1705
+ type: "LiveObject",
1706
+ updates: _defineProperty({}, key, {
1707
+ type: "delete"
1708
+ })
1709
+ });
1710
+ this._doc.dispatch([
1711
+ {
1712
+ type: 6 /* DELETE_OBJECT_KEY */ ,
1713
+ key: keyAsString,
1714
+ id: this._id,
1715
+ opId: this._doc.generateOpId()
1716
+ }
1717
+ ], reverse, storageUpdates);
1718
+ }
1719
+ },
1720
+ {
1721
+ key: "update",
1722
+ value: function update(overrides) {
1723
+ var _this = this;
1724
+ if (this._doc == null || this._id == null) {
1725
+ for(var key in overrides){
1726
+ var newValue = overrides[key];
1727
+ if (newValue === void 0) {
1728
+ continue;
1729
+ }
1730
+ var oldValue = this._map.get(key);
1731
+ if (isLiveNode(oldValue)) {
1732
+ oldValue._detach();
1733
+ }
1734
+ if (isLiveNode(newValue)) {
1735
+ newValue._setParentLink(this, key);
1736
+ }
1737
+ this._map.set(key, newValue);
1738
+ }
1739
+ return;
1740
+ }
1741
+ var ops = [];
1742
+ var reverseOps = [];
1743
+ var opId = this._doc.generateOpId();
1744
+ var updatedProps = {};
1745
+ var reverseUpdateOp = {
1746
+ id: this._id,
1747
+ type: 3 /* UPDATE_OBJECT */ ,
1748
+ data: {}
1749
+ };
1750
+ var updateDelta = {};
1751
+ for(var key1 in overrides){
1752
+ var newValue1 = overrides[key1];
1753
+ if (newValue1 === void 0) {
1754
+ continue;
1755
+ }
1756
+ var oldValue1 = this._map.get(key1);
1757
+ if (isLiveNode(oldValue1)) {
1758
+ var _reverseOps;
1759
+ (_reverseOps = reverseOps).push.apply(_reverseOps, _toConsumableArray(oldValue1._serialize(this._id, key1)));
1760
+ oldValue1._detach();
1761
+ } else if (oldValue1 === void 0) {
1762
+ reverseOps.push({
1763
+ type: 6 /* DELETE_OBJECT_KEY */ ,
1764
+ id: this._id,
1765
+ key: key1
1766
+ });
1767
+ } else {
1768
+ reverseUpdateOp.data[key1] = oldValue1;
1769
+ }
1770
+ if (isLiveNode(newValue1)) {
1771
+ var _ops;
1772
+ newValue1._setParentLink(this, key1);
1773
+ newValue1._attach(this._doc.generateId(), this._doc);
1774
+ var newAttachChildOps = newValue1._serialize(this._id, key1, this._doc);
1775
+ var createCrdtOp = newAttachChildOps.find(function(op) {
1776
+ return op.parentId === _this._id;
1777
+ });
1778
+ if (createCrdtOp) {
1779
+ this._propToLastUpdate.set(key1, nn(createCrdtOp.opId));
1780
+ }
1781
+ (_ops = ops).push.apply(_ops, _toConsumableArray(newAttachChildOps));
1782
+ } else {
1783
+ updatedProps[key1] = newValue1;
1784
+ this._propToLastUpdate.set(key1, opId);
1785
+ }
1786
+ this._map.set(key1, newValue1);
1787
+ updateDelta[key1] = {
1788
+ type: "update"
1789
+ };
1790
+ }
1791
+ if (Object.keys(reverseUpdateOp.data).length !== 0) {
1792
+ reverseOps.unshift(reverseUpdateOp);
1793
+ }
1794
+ if (Object.keys(updatedProps).length !== 0) {
1795
+ ops.unshift({
1796
+ opId: opId,
1797
+ id: this._id,
1798
+ type: 3 /* UPDATE_OBJECT */ ,
1799
+ data: updatedProps
1800
+ });
1801
+ }
1802
+ var storageUpdates = /* @__PURE__ */ new Map();
1803
+ storageUpdates.set(this._id, {
1804
+ node: this,
1805
+ type: "LiveObject",
1806
+ updates: updateDelta
1807
+ });
1808
+ this._doc.dispatch(ops, reverseOps, storageUpdates);
1809
+ }
1810
+ }
1811
+ ], [
1812
+ {
1813
+ key: "_deserialize",
1814
+ value: function _deserialize(param, parentToChildren, doc) {
1815
+ var _param = _slicedToArray(param, 2), id = _param[0], item = _param[1];
1816
+ var liveObj = new LiveObject(item.data);
1817
+ liveObj._attach(id, doc);
1818
+ return this._deserializeChildren(liveObj, parentToChildren, doc);
1819
+ }
1820
+ },
1821
+ {
1822
+ key: "_deserializeChildren",
1823
+ value: function _deserializeChildren(liveObj, parentToChildren, doc) {
1824
+ var children = parentToChildren.get(nn(liveObj._id));
1825
+ if (children == null) {
1826
+ return liveObj;
1827
+ }
1828
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1829
+ try {
1830
+ for(var _iterator = children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1831
+ var _value = _slicedToArray(_step.value, 2), id = _value[0], crdt = _value[1];
1832
+ var child = deserializeToLson([
1833
+ id,
1834
+ crdt
1835
+ ], parentToChildren, doc);
1836
+ if (isLiveStructure(child)) {
1837
+ child._setParentLink(liveObj, crdt.parentKey);
1838
+ }
1839
+ liveObj._map.set(crdt.parentKey, child);
1840
+ }
1841
+ } catch (err) {
1842
+ _didIteratorError = true;
1843
+ _iteratorError = err;
1844
+ } finally{
1845
+ try {
1846
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1847
+ _iterator.return();
1848
+ }
1849
+ } finally{
1850
+ if (_didIteratorError) {
1851
+ throw _iteratorError;
1852
+ }
1853
+ }
1854
+ }
1855
+ return liveObj;
1856
+ }
1857
+ }
1858
+ ]);
1859
+ return LiveObject;
1860
+ }(AbstractCrdt);
1861
+ // src/utils.ts
1862
+ function remove(array, item) {
1863
+ for(var i = 0; i < array.length; i++){
1864
+ if (array[i] === item) {
1865
+ array.splice(i, 1);
1866
+ break;
1867
+ }
1868
+ }
1869
+ }
1870
+ function compact(items) {
1871
+ return items.filter(function(item) {
1872
+ return item != null;
1873
+ });
1874
+ }
1875
+ function creationOpToLiveNode(op) {
1876
+ return lsonToLiveNode(creationOpToLson(op));
1877
+ }
1878
+ function creationOpToLson(op) {
1879
+ switch(op.type){
1880
+ case 8 /* CREATE_REGISTER */ :
1881
+ return op.data;
1882
+ case 4 /* CREATE_OBJECT */ :
1883
+ return new LiveObject(op.data);
1884
+ case 7 /* CREATE_MAP */ :
1885
+ return new LiveMap();
1886
+ case 2 /* CREATE_LIST */ :
1887
+ return new LiveList();
1888
+ default:
1889
+ return assertNever(op, "Unknown creation Op");
1890
+ }
1891
+ }
1892
+ function isSameNodeOrChildOf(node, parent) {
1893
+ if (node === parent) {
1894
+ return true;
1895
+ }
1896
+ if (node.parent.type === "HasParent") {
1897
+ return isSameNodeOrChildOf(node.parent.node, parent);
1898
+ }
1899
+ return false;
1900
+ }
1901
+ function deserialize(param, parentToChildren, doc) {
1902
+ var _param = _slicedToArray(param, 2), id = _param[0], crdt = _param[1];
1903
+ switch(crdt.type){
1904
+ case 0 /* OBJECT */ :
1905
+ {
1906
+ return LiveObject._deserialize([
1907
+ id,
1908
+ crdt
1909
+ ], parentToChildren, doc);
1910
+ }
1911
+ case 1 /* LIST */ :
1912
+ {
1913
+ return LiveList._deserialize([
1914
+ id,
1915
+ crdt
1916
+ ], parentToChildren, doc);
1917
+ }
1918
+ case 2 /* MAP */ :
1919
+ {
1920
+ return LiveMap._deserialize([
1921
+ id,
1922
+ crdt
1923
+ ], parentToChildren, doc);
1924
+ }
1925
+ case 3 /* REGISTER */ :
1926
+ {
1927
+ return LiveRegister._deserialize([
1928
+ id,
1929
+ crdt
1930
+ ], parentToChildren, doc);
1931
+ }
1932
+ default:
1933
+ {
1934
+ throw new Error("Unexpected CRDT type");
1935
+ }
1936
+ }
1937
+ }
1938
+ function deserializeToLson(param, parentToChildren, doc) {
1939
+ var _param = _slicedToArray(param, 2), id = _param[0], crdt = _param[1];
1940
+ switch(crdt.type){
1941
+ case 0 /* OBJECT */ :
1942
+ {
1943
+ return LiveObject._deserialize([
1944
+ id,
1945
+ crdt
1946
+ ], parentToChildren, doc);
1947
+ }
1948
+ case 1 /* LIST */ :
1949
+ {
1950
+ return LiveList._deserialize([
1951
+ id,
1952
+ crdt
1953
+ ], parentToChildren, doc);
1954
+ }
1955
+ case 2 /* MAP */ :
1956
+ {
1957
+ return LiveMap._deserialize([
1958
+ id,
1959
+ crdt
1960
+ ], parentToChildren, doc);
1961
+ }
1962
+ case 3 /* REGISTER */ :
1963
+ {
1964
+ return crdt.data;
1965
+ }
1966
+ default:
1967
+ {
1968
+ throw new Error("Unexpected CRDT type");
1969
+ }
1970
+ }
1971
+ }
1972
+ function isLiveStructure(value) {
1973
+ return isLiveList(value) || isLiveMap(value) || isLiveObject(value);
1974
+ }
1975
+ function isLiveNode(value) {
1976
+ return isLiveStructure(value) || isLiveRegister(value);
1977
+ }
1978
+ function isLiveList(value) {
1979
+ return _instanceof(value, LiveList);
1980
+ }
1981
+ function isLiveMap(value) {
1982
+ return _instanceof(value, LiveMap);
1983
+ }
1984
+ function isLiveObject(value) {
1985
+ return _instanceof(value, LiveObject);
1986
+ }
1987
+ function isLiveRegister(value) {
1988
+ return _instanceof(value, LiveRegister);
1989
+ }
1990
+ function liveNodeToLson(obj) {
1991
+ if (_instanceof(obj, LiveRegister)) {
1992
+ return obj.data;
1993
+ } else if (_instanceof(obj, LiveList) || _instanceof(obj, LiveMap) || _instanceof(obj, LiveObject)) {
1994
+ return obj;
1995
+ } else {
1996
+ return assertNever(obj, "Unknown AbstractCrdt");
1997
+ }
1998
+ }
1999
+ function lsonToLiveNode(value) {
2000
+ if (_instanceof(value, LiveObject) || _instanceof(value, LiveMap) || _instanceof(value, LiveList)) {
2001
+ return value;
2002
+ } else {
2003
+ return new LiveRegister(value);
2004
+ }
2005
+ }
2006
+ function getTreesDiffOperations(currentItems, newItems) {
2007
+ var ops = [];
2008
+ currentItems.forEach(function(_, id) {
2009
+ if (!newItems.get(id)) {
2010
+ ops.push({
2011
+ type: 5 /* DELETE_CRDT */ ,
2012
+ id: id
2013
+ });
2014
+ }
2015
+ });
2016
+ newItems.forEach(function(crdt, id) {
2017
+ var currentCrdt = currentItems.get(id);
2018
+ if (currentCrdt) {
2019
+ if (crdt.type === 0 /* OBJECT */ ) {
2020
+ if (currentCrdt.type !== 0 /* OBJECT */ || JSON.stringify(crdt.data) !== JSON.stringify(currentCrdt.data)) {
2021
+ ops.push({
2022
+ type: 3 /* UPDATE_OBJECT */ ,
2023
+ id: id,
2024
+ data: crdt.data
2025
+ });
2026
+ }
2027
+ }
2028
+ if (crdt.parentKey !== currentCrdt.parentKey) {
2029
+ ops.push({
2030
+ type: 1 /* SET_PARENT_KEY */ ,
2031
+ id: id,
2032
+ parentKey: nn(crdt.parentKey, "Parent key must not be missing")
2033
+ });
2034
+ }
2035
+ } else {
2036
+ switch(crdt.type){
2037
+ case 3 /* REGISTER */ :
2038
+ ops.push({
2039
+ type: 8 /* CREATE_REGISTER */ ,
2040
+ id: id,
2041
+ parentId: crdt.parentId,
2042
+ parentKey: crdt.parentKey,
2043
+ data: crdt.data
2044
+ });
2045
+ break;
2046
+ case 1 /* LIST */ :
2047
+ ops.push({
2048
+ type: 2 /* CREATE_LIST */ ,
2049
+ id: id,
2050
+ parentId: crdt.parentId,
2051
+ parentKey: crdt.parentKey
2052
+ });
2053
+ break;
2054
+ case 0 /* OBJECT */ :
2055
+ ops.push(crdt.parentId ? {
2056
+ type: 4 /* CREATE_OBJECT */ ,
2057
+ id: id,
2058
+ parentId: crdt.parentId,
2059
+ parentKey: crdt.parentKey,
2060
+ data: crdt.data
2061
+ } : {
2062
+ type: 4 /* CREATE_OBJECT */ ,
2063
+ id: id,
2064
+ data: crdt.data
2065
+ });
2066
+ break;
2067
+ case 2 /* MAP */ :
2068
+ ops.push({
2069
+ type: 7 /* CREATE_MAP */ ,
2070
+ id: id,
2071
+ parentId: crdt.parentId,
2072
+ parentKey: crdt.parentKey
2073
+ });
2074
+ break;
2075
+ }
2076
+ }
2077
+ });
2078
+ return ops;
2079
+ }
2080
+ function mergeObjectStorageUpdates(first, second) {
2081
+ var updates = first.updates;
2082
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2083
+ try {
2084
+ for(var _iterator = entries(second.updates)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2085
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
2086
+ updates[key] = value;
2087
+ }
2088
+ } catch (err) {
2089
+ _didIteratorError = true;
2090
+ _iteratorError = err;
2091
+ } finally{
2092
+ try {
2093
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2094
+ _iterator.return();
2095
+ }
2096
+ } finally{
2097
+ if (_didIteratorError) {
2098
+ throw _iteratorError;
2099
+ }
2100
+ }
2101
+ }
2102
+ return _objectSpreadProps(_objectSpread({}, second), {
2103
+ updates: updates
2104
+ });
2105
+ }
2106
+ function mergeMapStorageUpdates(first, second) {
2107
+ var updates = first.updates;
2108
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2109
+ try {
2110
+ for(var _iterator = entries(second.updates)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2111
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
2112
+ updates[key] = value;
2113
+ }
2114
+ } catch (err) {
2115
+ _didIteratorError = true;
2116
+ _iteratorError = err;
2117
+ } finally{
2118
+ try {
2119
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2120
+ _iterator.return();
2121
+ }
2122
+ } finally{
2123
+ if (_didIteratorError) {
2124
+ throw _iteratorError;
2125
+ }
2126
+ }
2127
+ }
2128
+ return _objectSpreadProps(_objectSpread({}, second), {
2129
+ updates: updates
2130
+ });
2131
+ }
2132
+ function mergeListStorageUpdates(first, second) {
2133
+ var updates = first.updates;
2134
+ return _objectSpreadProps(_objectSpread({}, second), {
2135
+ updates: updates.concat(second.updates)
2136
+ });
2137
+ }
2138
+ function mergeStorageUpdates(first, second) {
2139
+ if (!first) {
2140
+ return second;
2141
+ }
2142
+ if (first.type === "LiveObject" && second.type === "LiveObject") {
2143
+ return mergeObjectStorageUpdates(first, second);
2144
+ } else if (first.type === "LiveMap" && second.type === "LiveMap") {
2145
+ return mergeMapStorageUpdates(first, second);
2146
+ } else if (first.type === "LiveList" && second.type === "LiveList") {
2147
+ return mergeListStorageUpdates(first, second);
2148
+ } else {}
2149
+ return second;
2150
+ }
2151
+ function isPlain(value) {
2152
+ var type = typeof value === "undefined" ? "undefined" : _typeof(value);
2153
+ return value === void 0 || value === null || type === "string" || type === "boolean" || type === "number" || Array.isArray(value) || isPlainObject(value);
2154
+ }
2155
+ function isPlainObject(blob) {
2156
+ return blob !== null && typeof blob === "object" && Object.prototype.toString.call(blob) === "[object Object]";
2157
+ }
2158
+ function findNonSerializableValue(value) {
2159
+ var path = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "";
2160
+ if (!isPlain) {
2161
+ return {
2162
+ path: path || "root",
2163
+ value: value
2164
+ };
2165
+ }
2166
+ if (typeof value !== "object" || value === null) {
2167
+ return false;
2168
+ }
2169
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2170
+ try {
2171
+ for(var _iterator = Object.entries(value)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2172
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], nestedValue = _value[1];
2173
+ var nestedPath = path ? path + "." + key : key;
2174
+ if (!isPlain(nestedValue)) {
2175
+ return {
2176
+ path: nestedPath,
2177
+ value: nestedValue
2178
+ };
2179
+ }
2180
+ if (typeof nestedValue === "object") {
2181
+ var nonSerializableNestedValue = findNonSerializableValue(nestedValue, nestedPath);
2182
+ if (nonSerializableNestedValue) {
2183
+ return nonSerializableNestedValue;
2184
+ }
2185
+ }
2186
+ }
2187
+ } catch (err) {
2188
+ _didIteratorError = true;
2189
+ _iteratorError = err;
2190
+ } finally{
2191
+ try {
2192
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2193
+ _iterator.return();
2194
+ }
2195
+ } finally{
2196
+ if (_didIteratorError) {
2197
+ throw _iteratorError;
2198
+ }
2199
+ }
2200
+ }
2201
+ return false;
2202
+ }
2203
+ function fromEntries(iterable) {
2204
+ var obj = {};
2205
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2206
+ try {
2207
+ for(var _iterator = iterable[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2208
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], val = _value[1];
2209
+ obj[key] = val;
2210
+ }
2211
+ } catch (err) {
2212
+ _didIteratorError = true;
2213
+ _iteratorError = err;
2214
+ } finally{
2215
+ try {
2216
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2217
+ _iterator.return();
2218
+ }
2219
+ } finally{
2220
+ if (_didIteratorError) {
2221
+ throw _iteratorError;
2222
+ }
2223
+ }
2224
+ }
2225
+ return obj;
2226
+ }
2227
+ function entries(obj) {
2228
+ return Object.entries(obj);
2229
+ }
2230
+ function tryParseJson(rawMessage) {
2231
+ try {
2232
+ return JSON.parse(rawMessage);
2233
+ } catch (e) {
2234
+ return void 0;
2235
+ }
2236
+ }
2237
+ function b64decode(b64value) {
2238
+ try {
2239
+ var formattedValue = b64value.replace(/-/g, "+").replace(/_/g, "/");
2240
+ var decodedValue = decodeURIComponent(atob(formattedValue).split("").map(function(c) {
2241
+ return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
2242
+ }).join(""));
2243
+ return decodedValue;
2244
+ } catch (err) {
2245
+ return atob(b64value);
2246
+ }
2247
+ }
2248
+ // src/LiveList.ts
2249
+ function compareNodePosition(itemA, itemB) {
2250
+ return comparePosition(itemA._getParentKeyOrThrow(), itemB._getParentKeyOrThrow());
2251
+ }
2252
+ var LiveList = /*#__PURE__*/ function(AbstractCrdt) {
2253
+ _inherits(LiveList, AbstractCrdt);
2254
+ var _super = _createSuper(LiveList);
2255
+ function LiveList() {
2256
+ var items = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
2257
+ _classCallCheck(this, LiveList);
2258
+ var _this;
2259
+ _this = _super.call(this);
2260
+ _this._items = [];
2261
+ _this._implicitlyDeletedItems = /* @__PURE__ */ new Set();
2262
+ _this._unacknowledgedSets = /* @__PURE__ */ new Map();
2263
+ var position = void 0;
2264
+ for(var i = 0; i < items.length; i++){
2265
+ var newPosition = makePosition(position);
2266
+ var item = lsonToLiveNode(items[i]);
2267
+ item._setParentLink(_assertThisInitialized(_this), newPosition);
2268
+ _this._items.push(item);
2269
+ position = newPosition;
2270
+ }
2271
+ return _this;
2272
+ }
2273
+ _createClass(LiveList, [
2274
+ {
2275
+ key: "_serialize",
2276
+ value: function _serialize(parentId, parentKey, doc) {
2277
+ if (this._id == null) {
2278
+ throw new Error("Cannot serialize item is not attached");
2279
+ }
2280
+ var ops = [];
2281
+ var op = {
2282
+ id: this._id,
2283
+ opId: _optionalChain([
2284
+ doc,
2285
+ "optionalAccess",
2286
+ function(_8) {
2287
+ return _8.generateOpId;
2288
+ },
2289
+ "call",
2290
+ function(_9) {
2291
+ return _9();
2292
+ }
2293
+ ]),
2294
+ type: 2 /* CREATE_LIST */ ,
2295
+ parentId: parentId,
2296
+ parentKey: parentKey
2297
+ };
2298
+ ops.push(op);
2299
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2300
+ try {
2301
+ for(var _iterator = this._items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2302
+ var item = _step.value;
2303
+ var _ops;
2304
+ (_ops = ops).push.apply(_ops, _toConsumableArray(item._serialize(this._id, item._getParentKeyOrThrow(), doc)));
2305
+ }
2306
+ } catch (err) {
2307
+ _didIteratorError = true;
2308
+ _iteratorError = err;
2309
+ } finally{
2310
+ try {
2311
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2312
+ _iterator.return();
2313
+ }
2314
+ } finally{
2315
+ if (_didIteratorError) {
2316
+ throw _iteratorError;
2317
+ }
2318
+ }
2319
+ }
2320
+ return ops;
2321
+ }
2322
+ },
2323
+ {
2324
+ key: "_insertAndSort",
2325
+ value: function _insertAndSort(item) {
2326
+ this._items.push(item);
2327
+ this._sortItems();
2328
+ }
2329
+ },
2330
+ {
2331
+ key: "_sortItems",
2332
+ value: function _sortItems() {
2333
+ this._items.sort(compareNodePosition);
2334
+ }
2335
+ },
2336
+ {
2337
+ key: "_indexOfPosition",
2338
+ value: function _indexOfPosition(position) {
2339
+ return this._items.findIndex(function(item) {
2340
+ return item._getParentKeyOrThrow() === position;
2341
+ });
2342
+ }
2343
+ },
2344
+ {
2345
+ key: "_attach",
2346
+ value: function _attach(id, doc) {
2347
+ _get(_getPrototypeOf(LiveList.prototype), "_attach", this).call(this, id, doc);
2348
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2349
+ try {
2350
+ for(var _iterator = this._items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2351
+ var item = _step.value;
2352
+ item._attach(doc.generateId(), doc);
2353
+ }
2354
+ } catch (err) {
2355
+ _didIteratorError = true;
2356
+ _iteratorError = err;
2357
+ } finally{
2358
+ try {
2359
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2360
+ _iterator.return();
2361
+ }
2362
+ } finally{
2363
+ if (_didIteratorError) {
2364
+ throw _iteratorError;
2365
+ }
2366
+ }
2367
+ }
2368
+ }
2369
+ },
2370
+ {
2371
+ key: "_detach",
2372
+ value: function _detach() {
2373
+ _get(_getPrototypeOf(LiveList.prototype), "_detach", this).call(this);
2374
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2375
+ try {
2376
+ for(var _iterator = this._items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2377
+ var item = _step.value;
2378
+ item._detach();
2379
+ }
2380
+ } catch (err) {
2381
+ _didIteratorError = true;
2382
+ _iteratorError = err;
2383
+ } finally{
2384
+ try {
2385
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2386
+ _iterator.return();
2387
+ }
2388
+ } finally{
2389
+ if (_didIteratorError) {
2390
+ throw _iteratorError;
2391
+ }
2392
+ }
2393
+ }
2394
+ }
2395
+ },
2396
+ {
2397
+ key: "_applySetRemote",
2398
+ value: function _applySetRemote(op) {
2399
+ if (this._doc == null) {
2400
+ throw new Error("Can't attach child if doc is not present");
2401
+ }
2402
+ var id = op.id, key = op.parentKey;
2403
+ var child = creationOpToLiveNode(op);
2404
+ child._attach(id, this._doc);
2405
+ child._setParentLink(this, key);
2406
+ var deletedId = op.deletedId;
2407
+ var indexOfItemWithSamePosition = this._indexOfPosition(key);
2408
+ if (indexOfItemWithSamePosition !== -1) {
2409
+ var itemWithSamePosition = this._items[indexOfItemWithSamePosition];
2410
+ if (itemWithSamePosition._id === deletedId) {
2411
+ itemWithSamePosition._detach();
2412
+ this._items[indexOfItemWithSamePosition] = child;
2413
+ return {
2414
+ modified: makeUpdate(this, [
2415
+ setDelta(indexOfItemWithSamePosition, child)
2416
+ ]),
2417
+ reverse: []
2418
+ };
2419
+ } else {
2420
+ this._implicitlyDeletedItems.add(itemWithSamePosition);
2421
+ this._items[indexOfItemWithSamePosition] = child;
2422
+ var delta = [
2423
+ setDelta(indexOfItemWithSamePosition, child)
2424
+ ];
2425
+ var deleteDelta2 = this._detachItemAssociatedToSetOperation(op.deletedId);
2426
+ if (deleteDelta2) {
2427
+ delta.push(deleteDelta2);
2428
+ }
2429
+ return {
2430
+ modified: makeUpdate(this, delta),
2431
+ reverse: []
2432
+ };
2433
+ }
2434
+ } else {
2435
+ var updates = [];
2436
+ var deleteDelta21 = this._detachItemAssociatedToSetOperation(op.deletedId);
2437
+ if (deleteDelta21) {
2438
+ updates.push(deleteDelta21);
2439
+ }
2440
+ this._insertAndSort(child);
2441
+ updates.push(insertDelta(this._indexOfPosition(key), child));
2442
+ return {
2443
+ reverse: [],
2444
+ modified: makeUpdate(this, updates)
2445
+ };
2446
+ }
2447
+ }
2448
+ },
2449
+ {
2450
+ key: "_applySetAck",
2451
+ value: function _applySetAck(op) {
2452
+ if (this._doc == null) {
2453
+ throw new Error("Can't attach child if doc is not present");
2454
+ }
2455
+ var delta = [];
2456
+ var deletedDelta = this._detachItemAssociatedToSetOperation(op.deletedId);
2457
+ if (deletedDelta) {
2458
+ delta.push(deletedDelta);
2459
+ }
2460
+ var unacknowledgedOpId = this._unacknowledgedSets.get(op.parentKey);
2461
+ if (unacknowledgedOpId != null) {
2462
+ if (unacknowledgedOpId !== op.opId) {
2463
+ return delta.length === 0 ? {
2464
+ modified: false
2465
+ } : {
2466
+ modified: makeUpdate(this, delta),
2467
+ reverse: []
2468
+ };
2469
+ } else {
2470
+ this._unacknowledgedSets.delete(op.parentKey);
2471
+ }
2472
+ }
2473
+ var indexOfItemWithSamePosition = this._indexOfPosition(op.parentKey);
2474
+ var existingItem = this._items.find(function(item) {
2475
+ return item._id === op.id;
2476
+ });
2477
+ if (existingItem != null) {
2478
+ if (existingItem._parentKey === op.parentKey) {
2479
+ return {
2480
+ modified: delta.length > 0 ? makeUpdate(this, delta) : false,
2481
+ reverse: []
2482
+ };
2483
+ }
2484
+ if (indexOfItemWithSamePosition !== -1) {
2485
+ this._implicitlyDeletedItems.add(this._items[indexOfItemWithSamePosition]);
2486
+ this._items.splice(indexOfItemWithSamePosition, 1);
2487
+ delta.push(deleteDelta(indexOfItemWithSamePosition));
2488
+ }
2489
+ var previousIndex = this._items.indexOf(existingItem);
2490
+ existingItem._setParentLink(this, op.parentKey);
2491
+ this._sortItems();
2492
+ var newIndex = this._items.indexOf(existingItem);
2493
+ if (newIndex !== previousIndex) {
2494
+ delta.push(moveDelta(previousIndex, newIndex, existingItem));
2495
+ }
2496
+ return {
2497
+ modified: delta.length > 0 ? makeUpdate(this, delta) : false,
2498
+ reverse: []
2499
+ };
2500
+ } else {
2501
+ var orphan = this._doc.getItem(op.id);
2502
+ if (orphan && this._implicitlyDeletedItems.has(orphan)) {
2503
+ orphan._setParentLink(this, op.parentKey);
2504
+ this._implicitlyDeletedItems.delete(orphan);
2505
+ this._insertAndSort(orphan);
2506
+ var recreatedItemIndex = this._items.indexOf(orphan);
2507
+ return {
2508
+ modified: makeUpdate(this, [
2509
+ indexOfItemWithSamePosition === -1 ? insertDelta(recreatedItemIndex, orphan) : setDelta(recreatedItemIndex, orphan)
2510
+ ].concat(_toConsumableArray(delta))),
2511
+ reverse: []
2512
+ };
2513
+ } else {
2514
+ if (indexOfItemWithSamePosition !== -1) {
2515
+ this._items.splice(indexOfItemWithSamePosition, 1);
2516
+ }
2517
+ var ref = this._createAttachItemAndSort(op, op.parentKey), newItem = ref.newItem, newIndex1 = ref.newIndex;
2518
+ return {
2519
+ modified: makeUpdate(this, [
2520
+ indexOfItemWithSamePosition === -1 ? insertDelta(newIndex1, newItem) : setDelta(newIndex1, newItem)
2521
+ ].concat(_toConsumableArray(delta))),
2522
+ reverse: []
2523
+ };
2524
+ }
2525
+ }
2526
+ }
2527
+ },
2528
+ {
2529
+ key: "_detachItemAssociatedToSetOperation",
2530
+ value: function _detachItemAssociatedToSetOperation(deletedId) {
2531
+ if (deletedId == null || this._doc == null) {
2532
+ return null;
2533
+ }
2534
+ var deletedItem = this._doc.getItem(deletedId);
2535
+ if (deletedItem == null) {
2536
+ return null;
2537
+ }
2538
+ var result = this._detachChild(deletedItem);
2539
+ if (result.modified === false) {
2540
+ return null;
2541
+ }
2542
+ return result.modified.updates[0];
2543
+ }
2544
+ },
2545
+ {
2546
+ key: "_applyRemoteInsert",
2547
+ value: function _applyRemoteInsert(op) {
2548
+ if (this._doc == null) {
2549
+ throw new Error("Can't attach child if doc is not present");
2550
+ }
2551
+ var key = op.parentKey;
2552
+ var existingItemIndex = this._indexOfPosition(key);
2553
+ if (existingItemIndex !== -1) {
2554
+ this._shiftItemPosition(existingItemIndex, key);
2555
+ }
2556
+ var ref = this._createAttachItemAndSort(op, key), newItem = ref.newItem, newIndex = ref.newIndex;
2557
+ return {
2558
+ modified: makeUpdate(this, [
2559
+ insertDelta(newIndex, newItem)
2560
+ ]),
2561
+ reverse: []
2562
+ };
2563
+ }
2564
+ },
2565
+ {
2566
+ key: "_applyInsertAck",
2567
+ value: function _applyInsertAck(op) {
2568
+ var existingItem = this._items.find(function(item) {
2569
+ return item._id === op.id;
2570
+ });
2571
+ var key = op.parentKey;
2572
+ var itemIndexAtPosition = this._indexOfPosition(key);
2573
+ if (existingItem) {
2574
+ if (existingItem._parentKey === key) {
2575
+ return {
2576
+ modified: false
2577
+ };
2578
+ } else {
2579
+ var oldPositionIndex = this._items.indexOf(existingItem);
2580
+ if (itemIndexAtPosition !== -1) {
2581
+ this._shiftItemPosition(itemIndexAtPosition, key);
2582
+ }
2583
+ existingItem._setParentLink(this, key);
2584
+ this._sortItems();
2585
+ var newIndex = this._indexOfPosition(key);
2586
+ if (newIndex === oldPositionIndex) {
2587
+ return {
2588
+ modified: false
2589
+ };
2590
+ }
2591
+ return {
2592
+ modified: makeUpdate(this, [
2593
+ moveDelta(oldPositionIndex, newIndex, existingItem)
2594
+ ]),
2595
+ reverse: []
2596
+ };
2597
+ }
2598
+ } else {
2599
+ var orphan = nn(this._doc).getItem(op.id);
2600
+ if (orphan && this._implicitlyDeletedItems.has(orphan)) {
2601
+ orphan._setParentLink(this, key);
2602
+ this._implicitlyDeletedItems.delete(orphan);
2603
+ this._insertAndSort(orphan);
2604
+ var newIndex1 = this._indexOfPosition(key);
2605
+ return {
2606
+ modified: makeUpdate(this, [
2607
+ insertDelta(newIndex1, orphan)
2608
+ ]),
2609
+ reverse: []
2610
+ };
2611
+ } else {
2612
+ if (itemIndexAtPosition !== -1) {
2613
+ this._shiftItemPosition(itemIndexAtPosition, key);
2614
+ }
2615
+ var ref = this._createAttachItemAndSort(op, key), newItem = ref.newItem, newIndex2 = ref.newIndex;
2616
+ return {
2617
+ modified: makeUpdate(this, [
2618
+ insertDelta(newIndex2, newItem)
2619
+ ]),
2620
+ reverse: []
2621
+ };
2622
+ }
2623
+ }
2624
+ }
2625
+ },
2626
+ {
2627
+ key: "_applyInsertUndoRedo",
2628
+ value: function _applyInsertUndoRedo(op) {
2629
+ var id = op.id, key = op.parentKey;
2630
+ var child = creationOpToLiveNode(op);
2631
+ if (_optionalChain([
2632
+ this,
2633
+ "access",
2634
+ function(_10) {
2635
+ return _10._doc;
2636
+ },
2637
+ "optionalAccess",
2638
+ function(_11) {
2639
+ return _11.getItem;
2640
+ },
2641
+ "call",
2642
+ function(_12) {
2643
+ return _12(id);
2644
+ }
2645
+ ]) !== void 0) {
2646
+ return {
2647
+ modified: false
2648
+ };
2649
+ }
2650
+ child._attach(id, nn(this._doc));
2651
+ child._setParentLink(this, key);
2652
+ var existingItemIndex = this._indexOfPosition(key);
2653
+ var newKey = key;
2654
+ if (existingItemIndex !== -1) {
2655
+ var before = this._items[existingItemIndex] ? this._items[existingItemIndex]._getParentKeyOrThrow() : void 0;
2656
+ var after = this._items[existingItemIndex + 1] ? this._items[existingItemIndex + 1]._getParentKeyOrThrow() : void 0;
2657
+ newKey = makePosition(before, after);
2658
+ child._setParentLink(this, newKey);
2659
+ }
2660
+ this._insertAndSort(child);
2661
+ var newIndex = this._indexOfPosition(newKey);
2662
+ return {
2663
+ modified: makeUpdate(this, [
2664
+ insertDelta(newIndex, child)
2665
+ ]),
2666
+ reverse: [
2667
+ {
2668
+ type: 5 /* DELETE_CRDT */ ,
2669
+ id: id
2670
+ }
2671
+ ]
2672
+ };
2673
+ }
2674
+ },
2675
+ {
2676
+ key: "_applySetUndoRedo",
2677
+ value: function _applySetUndoRedo(op) {
2678
+ var id = op.id, key = op.parentKey;
2679
+ var child = creationOpToLiveNode(op);
2680
+ if (_optionalChain([
2681
+ this,
2682
+ "access",
2683
+ function(_13) {
2684
+ return _13._doc;
2685
+ },
2686
+ "optionalAccess",
2687
+ function(_14) {
2688
+ return _14.getItem;
2689
+ },
2690
+ "call",
2691
+ function(_15) {
2692
+ return _15(id);
2693
+ }
2694
+ ]) !== void 0) {
2695
+ return {
2696
+ modified: false
2697
+ };
2698
+ }
2699
+ this._unacknowledgedSets.set(key, nn(op.opId));
2700
+ var indexOfItemWithSameKey = this._indexOfPosition(key);
2701
+ child._attach(id, nn(this._doc));
2702
+ child._setParentLink(this, key);
2703
+ var newKey = key;
2704
+ if (indexOfItemWithSameKey !== -1) {
2705
+ var existingItem = this._items[indexOfItemWithSameKey];
2706
+ existingItem._detach();
2707
+ this._items[indexOfItemWithSameKey] = child;
2708
+ var reverse = existingItem._serialize(nn(this._id), key, this._doc);
2709
+ addIntentAndDeletedIdToOperation(reverse, op.id);
2710
+ var delta = [
2711
+ setDelta(indexOfItemWithSameKey, child)
2712
+ ];
2713
+ var deletedDelta = this._detachItemAssociatedToSetOperation(op.deletedId);
2714
+ if (deletedDelta) {
2715
+ delta.push(deletedDelta);
2716
+ }
2717
+ return {
2718
+ modified: makeUpdate(this, delta),
2719
+ reverse: reverse
2720
+ };
2721
+ } else {
2722
+ this._insertAndSort(child);
2723
+ this._detachItemAssociatedToSetOperation(op.deletedId);
2724
+ var newIndex = this._indexOfPosition(newKey);
2725
+ return {
2726
+ reverse: [
2727
+ {
2728
+ type: 5 /* DELETE_CRDT */ ,
2729
+ id: id
2730
+ }
2731
+ ],
2732
+ modified: makeUpdate(this, [
2733
+ insertDelta(newIndex, child)
2734
+ ])
2735
+ };
2736
+ }
2737
+ }
2738
+ },
2739
+ {
2740
+ key: "_attachChild",
2741
+ value: function _attachChild(op, source) {
2742
+ if (this._doc == null) {
2743
+ throw new Error("Can't attach child if doc is not present");
2744
+ }
2745
+ if (op.intent === "set") {
2746
+ if (source === 1 /* REMOTE */ ) {
2747
+ return this._applySetRemote(op);
2748
+ }
2749
+ if (source === 0 /* UNDOREDO_RECONNECT */ ) {
2750
+ return this._applySetUndoRedo(op);
2751
+ }
2752
+ if (source === 2 /* ACK */ ) {
2753
+ return this._applySetAck(op);
2754
+ }
2755
+ }
2756
+ if (source === 1 /* REMOTE */ ) {
2757
+ return this._applyRemoteInsert(op);
2758
+ } else if (source === 2 /* ACK */ ) {
2759
+ return this._applyInsertAck(op);
2760
+ } else {
2761
+ return this._applyInsertUndoRedo(op);
2762
+ }
2763
+ }
2764
+ },
2765
+ {
2766
+ key: "_detachChild",
2767
+ value: function _detachChild(child) {
2768
+ if (child) {
2769
+ var parentKey = nn(child._parentKey);
2770
+ var reverse = child._serialize(nn(this._id), parentKey, this._doc);
2771
+ var indexToDelete = this._items.indexOf(child);
2772
+ if (indexToDelete === -1) {
2773
+ return {
2774
+ modified: false
2775
+ };
2776
+ }
2777
+ this._items.splice(indexToDelete, 1);
2778
+ child._detach();
2779
+ return {
2780
+ modified: makeUpdate(this, [
2781
+ deleteDelta(indexToDelete)
2782
+ ]),
2783
+ reverse: reverse
2784
+ };
2785
+ }
2786
+ return {
2787
+ modified: false
2788
+ };
2789
+ }
2790
+ },
2791
+ {
2792
+ key: "_applySetChildKeyRemote",
2793
+ value: function _applySetChildKeyRemote(newKey, child) {
2794
+ if (this._implicitlyDeletedItems.has(child)) {
2795
+ this._implicitlyDeletedItems.delete(child);
2796
+ child._setParentLink(this, newKey);
2797
+ this._insertAndSort(child);
2798
+ var newIndex = this._items.indexOf(child);
2799
+ return {
2800
+ modified: makeUpdate(this, [
2801
+ insertDelta(newIndex, child)
2802
+ ]),
2803
+ reverse: []
2804
+ };
2805
+ }
2806
+ var previousKey = child._parentKey;
2807
+ if (newKey === previousKey) {
2808
+ return {
2809
+ modified: false
2810
+ };
2811
+ }
2812
+ var existingItemIndex = this._indexOfPosition(newKey);
2813
+ if (existingItemIndex === -1) {
2814
+ var previousIndex = this._items.indexOf(child);
2815
+ child._setParentLink(this, newKey);
2816
+ this._sortItems();
2817
+ var newIndex1 = this._items.indexOf(child);
2818
+ if (newIndex1 === previousIndex) {
2819
+ return {
2820
+ modified: false
2821
+ };
2822
+ }
2823
+ return {
2824
+ modified: makeUpdate(this, [
2825
+ moveDelta(previousIndex, newIndex1, child)
2826
+ ]),
2827
+ reverse: []
2828
+ };
2829
+ } else {
2830
+ this._items[existingItemIndex]._setParentLink(this, makePosition(newKey, _optionalChain([
2831
+ this,
2832
+ "access",
2833
+ function(_16) {
2834
+ return _16._items;
2835
+ },
2836
+ "access",
2837
+ function(_17) {
2838
+ return _17[existingItemIndex + 1];
2839
+ },
2840
+ "optionalAccess",
2841
+ function(_18) {
2842
+ return _18._getParentKeyOrThrow;
2843
+ },
2844
+ "call",
2845
+ function(_19) {
2846
+ return _19();
2847
+ }
2848
+ ])));
2849
+ var previousIndex1 = this._items.indexOf(child);
2850
+ child._setParentLink(this, newKey);
2851
+ this._sortItems();
2852
+ var newIndex2 = this._items.indexOf(child);
2853
+ if (newIndex2 === previousIndex1) {
2854
+ return {
2855
+ modified: false
2856
+ };
2857
+ }
2858
+ return {
2859
+ modified: makeUpdate(this, [
2860
+ moveDelta(previousIndex1, newIndex2, child)
2861
+ ]),
2862
+ reverse: []
2863
+ };
2864
+ }
2865
+ }
2866
+ },
2867
+ {
2868
+ key: "_applySetChildKeyAck",
2869
+ value: function _applySetChildKeyAck(newKey, child) {
2870
+ var previousKey = nn(child._parentKey);
2871
+ if (this._implicitlyDeletedItems.has(child)) {
2872
+ var existingItemIndex = this._indexOfPosition(newKey);
2873
+ this._implicitlyDeletedItems.delete(child);
2874
+ if (existingItemIndex !== -1) {
2875
+ this._items[existingItemIndex]._setParentLink(this, makePosition(newKey, _optionalChain([
2876
+ this,
2877
+ "access",
2878
+ function(_20) {
2879
+ return _20._items;
2880
+ },
2881
+ "access",
2882
+ function(_21) {
2883
+ return _21[existingItemIndex + 1];
2884
+ },
2885
+ "optionalAccess",
2886
+ function(_22) {
2887
+ return _22._getParentKeyOrThrow;
2888
+ },
2889
+ "call",
2890
+ function(_23) {
2891
+ return _23();
2892
+ }
2893
+ ])));
2894
+ }
2895
+ child._setParentLink(this, newKey);
2896
+ this._insertAndSort(child);
2897
+ return {
2898
+ modified: false
2899
+ };
2900
+ } else {
2901
+ if (newKey === previousKey) {
2902
+ return {
2903
+ modified: false
2904
+ };
2905
+ }
2906
+ var previousIndex = this._items.indexOf(child);
2907
+ var existingItemIndex1 = this._indexOfPosition(newKey);
2908
+ if (existingItemIndex1 !== -1) {
2909
+ this._items[existingItemIndex1]._setParentLink(this, makePosition(newKey, _optionalChain([
2910
+ this,
2911
+ "access",
2912
+ function(_24) {
2913
+ return _24._items;
2914
+ },
2915
+ "access",
2916
+ function(_25) {
2917
+ return _25[existingItemIndex1 + 1];
2918
+ },
2919
+ "optionalAccess",
2920
+ function(_26) {
2921
+ return _26._getParentKeyOrThrow;
2922
+ },
2923
+ "call",
2924
+ function(_27) {
2925
+ return _27();
2926
+ }
2927
+ ])));
2928
+ }
2929
+ child._setParentLink(this, newKey);
2930
+ this._sortItems();
2931
+ var newIndex = this._items.indexOf(child);
2932
+ if (previousIndex === newIndex) {
2933
+ return {
2934
+ modified: false
2935
+ };
2936
+ } else {
2937
+ return {
2938
+ modified: makeUpdate(this, [
2939
+ moveDelta(previousIndex, newIndex, child)
2940
+ ]),
2941
+ reverse: []
2942
+ };
2943
+ }
2944
+ }
2945
+ }
2946
+ },
2947
+ {
2948
+ key: "_applySetChildKeyUndoRedo",
2949
+ value: function _applySetChildKeyUndoRedo(newKey, child) {
2950
+ var previousKey = nn(child._parentKey);
2951
+ var previousIndex = this._items.indexOf(child);
2952
+ var existingItemIndex = this._indexOfPosition(newKey);
2953
+ if (existingItemIndex !== -1) {
2954
+ this._items[existingItemIndex]._setParentLink(this, makePosition(newKey, _optionalChain([
2955
+ this,
2956
+ "access",
2957
+ function(_28) {
2958
+ return _28._items;
2959
+ },
2960
+ "access",
2961
+ function(_29) {
2962
+ return _29[existingItemIndex + 1];
2963
+ },
2964
+ "optionalAccess",
2965
+ function(_30) {
2966
+ return _30._getParentKeyOrThrow;
2967
+ },
2968
+ "call",
2969
+ function(_31) {
2970
+ return _31();
2971
+ }
2972
+ ])));
2973
+ }
2974
+ child._setParentLink(this, newKey);
2975
+ this._sortItems();
2976
+ var newIndex = this._items.indexOf(child);
2977
+ if (previousIndex === newIndex) {
2978
+ return {
2979
+ modified: false
2980
+ };
2981
+ }
2982
+ return {
2983
+ modified: makeUpdate(this, [
2984
+ moveDelta(previousIndex, newIndex, child)
2985
+ ]),
2986
+ reverse: [
2987
+ {
2988
+ type: 1 /* SET_PARENT_KEY */ ,
2989
+ id: nn(child._id),
2990
+ parentKey: previousKey
2991
+ }
2992
+ ]
2993
+ };
2994
+ }
2995
+ },
2996
+ {
2997
+ key: "_setChildKey",
2998
+ value: function _setChildKey(newKey, child, source) {
2999
+ if (source === 1 /* REMOTE */ ) {
3000
+ return this._applySetChildKeyRemote(newKey, child);
3001
+ } else if (source === 2 /* ACK */ ) {
3002
+ return this._applySetChildKeyAck(newKey, child);
3003
+ } else {
3004
+ return this._applySetChildKeyUndoRedo(newKey, child);
3005
+ }
3006
+ }
3007
+ },
3008
+ {
3009
+ key: "_apply",
3010
+ value: function _apply(op, isLocal) {
3011
+ return _get(_getPrototypeOf(LiveList.prototype), "_apply", this).call(this, op, isLocal);
3012
+ }
3013
+ },
3014
+ {
3015
+ key: "_toSerializedCrdt",
3016
+ value: function _toSerializedCrdt() {
3017
+ if (this.parent.type !== "HasParent") {
3018
+ throw new Error("Cannot serialize LiveList if parent is missing");
3019
+ }
3020
+ return {
3021
+ type: 1 /* LIST */ ,
3022
+ parentId: nn(this.parent.node._id, "Parent node expected to have ID"),
3023
+ parentKey: this.parent.key
3024
+ };
3025
+ }
3026
+ },
3027
+ {
3028
+ key: "length",
3029
+ get: function get() {
3030
+ return this._items.length;
3031
+ }
3032
+ },
3033
+ {
3034
+ key: "push",
3035
+ value: function push(element) {
3036
+ return this.insert(element, this.length);
3037
+ }
3038
+ },
3039
+ {
3040
+ key: "insert",
3041
+ value: function insert(element, index) {
3042
+ if (index < 0 || index > this._items.length) {
3043
+ throw new Error('Cannot insert list item at index "\x1d'.concat(index, '". index should be between 0 and ').concat(this._items.length));
3044
+ }
3045
+ var before = this._items[index - 1] ? this._items[index - 1]._getParentKeyOrThrow() : void 0;
3046
+ var after = this._items[index] ? this._items[index]._getParentKeyOrThrow() : void 0;
3047
+ var position = makePosition(before, after);
3048
+ var value = lsonToLiveNode(element);
3049
+ value._setParentLink(this, position);
3050
+ this._insertAndSort(value);
3051
+ if (this._doc && this._id) {
3052
+ var id = this._doc.generateId();
3053
+ value._attach(id, this._doc);
3054
+ this._doc.dispatch(value._serialize(this._id, position, this._doc), [
3055
+ {
3056
+ type: 5 /* DELETE_CRDT */ ,
3057
+ id: id
3058
+ }
3059
+ ], /* @__PURE__ */ new Map([
3060
+ [
3061
+ this._id,
3062
+ makeUpdate(this, [
3063
+ insertDelta(index, value)
3064
+ ])
3065
+ ]
3066
+ ]));
3067
+ }
3068
+ }
3069
+ },
3070
+ {
3071
+ key: "move",
3072
+ value: function move(index, targetIndex) {
3073
+ if (targetIndex < 0) {
3074
+ throw new Error("targetIndex cannot be less than 0");
3075
+ }
3076
+ if (targetIndex >= this._items.length) {
3077
+ throw new Error("targetIndex cannot be greater or equal than the list length");
3078
+ }
3079
+ if (index < 0) {
3080
+ throw new Error("index cannot be less than 0");
3081
+ }
3082
+ if (index >= this._items.length) {
3083
+ throw new Error("index cannot be greater or equal than the list length");
3084
+ }
3085
+ var beforePosition = null;
3086
+ var afterPosition = null;
3087
+ if (index < targetIndex) {
3088
+ afterPosition = targetIndex === this._items.length - 1 ? void 0 : this._items[targetIndex + 1]._getParentKeyOrThrow();
3089
+ beforePosition = this._items[targetIndex]._getParentKeyOrThrow();
3090
+ } else {
3091
+ afterPosition = this._items[targetIndex]._getParentKeyOrThrow();
3092
+ beforePosition = targetIndex === 0 ? void 0 : this._items[targetIndex - 1]._getParentKeyOrThrow();
3093
+ }
3094
+ var position = makePosition(beforePosition, afterPosition);
3095
+ var item = this._items[index];
3096
+ var previousPosition = item._getParentKeyOrThrow();
3097
+ item._setParentLink(this, position);
3098
+ this._sortItems();
3099
+ if (this._doc && this._id) {
3100
+ var storageUpdates = /* @__PURE__ */ new Map([
3101
+ [
3102
+ this._id,
3103
+ makeUpdate(this, [
3104
+ moveDelta(index, targetIndex, item)
3105
+ ])
3106
+ ]
3107
+ ]);
3108
+ this._doc.dispatch([
3109
+ {
3110
+ type: 1 /* SET_PARENT_KEY */ ,
3111
+ id: nn(item._id),
3112
+ opId: this._doc.generateOpId(),
3113
+ parentKey: position
3114
+ }
3115
+ ], [
3116
+ {
3117
+ type: 1 /* SET_PARENT_KEY */ ,
3118
+ id: nn(item._id),
3119
+ parentKey: previousPosition
3120
+ }
3121
+ ], storageUpdates);
3122
+ }
3123
+ }
3124
+ },
3125
+ {
3126
+ key: "delete",
3127
+ value: function _delete(index) {
3128
+ if (index < 0 || index >= this._items.length) {
3129
+ throw new Error('Cannot delete list item at index "\x1d'.concat(index, '". index should be between 0 and ').concat(this._items.length - 1));
3130
+ }
3131
+ var item = this._items[index];
3132
+ item._detach();
3133
+ this._items.splice(index, 1);
3134
+ if (this._doc) {
3135
+ var childRecordId = item._id;
3136
+ if (childRecordId) {
3137
+ var storageUpdates = /* @__PURE__ */ new Map();
3138
+ storageUpdates.set(nn(this._id), makeUpdate(this, [
3139
+ deleteDelta(index)
3140
+ ]));
3141
+ this._doc.dispatch([
3142
+ {
3143
+ id: childRecordId,
3144
+ opId: this._doc.generateOpId(),
3145
+ type: 5 /* DELETE_CRDT */
3146
+ }
3147
+ ], item._serialize(nn(this._id), item._getParentKeyOrThrow()), storageUpdates);
3148
+ }
3149
+ }
3150
+ }
3151
+ },
3152
+ {
3153
+ key: "clear",
3154
+ value: function clear() {
3155
+ if (this._doc) {
3156
+ var ops = [];
3157
+ var reverseOps = [];
3158
+ var updateDelta = [];
3159
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3160
+ try {
3161
+ for(var _iterator = this._items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3162
+ var item = _step.value;
3163
+ item._detach();
3164
+ var childId = item._id;
3165
+ if (childId) {
3166
+ var _reverseOps;
3167
+ ops.push({
3168
+ type: 5 /* DELETE_CRDT */ ,
3169
+ id: childId,
3170
+ opId: this._doc.generateOpId()
3171
+ });
3172
+ (_reverseOps = reverseOps).push.apply(_reverseOps, _toConsumableArray(item._serialize(nn(this._id), item._getParentKeyOrThrow())));
3173
+ updateDelta.push(deleteDelta(0));
3174
+ }
3175
+ }
3176
+ } catch (err) {
3177
+ _didIteratorError = true;
3178
+ _iteratorError = err;
3179
+ } finally{
3180
+ try {
3181
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
3182
+ _iterator.return();
3183
+ }
3184
+ } finally{
3185
+ if (_didIteratorError) {
3186
+ throw _iteratorError;
3187
+ }
3188
+ }
3189
+ }
3190
+ this._items = [];
3191
+ var storageUpdates = /* @__PURE__ */ new Map();
3192
+ storageUpdates.set(nn(this._id), makeUpdate(this, updateDelta));
3193
+ this._doc.dispatch(ops, reverseOps, storageUpdates);
3194
+ } else {
3195
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
3196
+ try {
3197
+ for(var _iterator1 = this._items[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
3198
+ var item1 = _step1.value;
3199
+ item1._detach();
3200
+ }
3201
+ } catch (err) {
3202
+ _didIteratorError1 = true;
3203
+ _iteratorError1 = err;
3204
+ } finally{
3205
+ try {
3206
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
3207
+ _iterator1.return();
3208
+ }
3209
+ } finally{
3210
+ if (_didIteratorError1) {
3211
+ throw _iteratorError1;
3212
+ }
3213
+ }
3214
+ }
3215
+ this._items = [];
3216
+ }
3217
+ }
3218
+ },
3219
+ {
3220
+ key: "set",
3221
+ value: function set(index, item) {
3222
+ if (index < 0 || index >= this._items.length) {
3223
+ throw new Error('Cannot set list item at index "\x1d'.concat(index, '". index should be between 0 and ').concat(this._items.length - 1));
3224
+ }
3225
+ var existingItem = this._items[index];
3226
+ var position = existingItem._getParentKeyOrThrow();
3227
+ var existingId = existingItem._id;
3228
+ existingItem._detach();
3229
+ var value = lsonToLiveNode(item);
3230
+ value._setParentLink(this, position);
3231
+ this._items[index] = value;
3232
+ if (this._doc && this._id) {
3233
+ var id = this._doc.generateId();
3234
+ value._attach(id, this._doc);
3235
+ var storageUpdates = /* @__PURE__ */ new Map();
3236
+ storageUpdates.set(this._id, makeUpdate(this, [
3237
+ setDelta(index, value)
3238
+ ]));
3239
+ var ops = value._serialize(this._id, position, this._doc);
3240
+ addIntentAndDeletedIdToOperation(ops, existingId);
3241
+ this._unacknowledgedSets.set(position, nn(ops[0].opId));
3242
+ var reverseOps = existingItem._serialize(this._id, position, void 0);
3243
+ addIntentAndDeletedIdToOperation(reverseOps, id);
3244
+ this._doc.dispatch(ops, reverseOps, storageUpdates);
3245
+ }
3246
+ }
3247
+ },
3248
+ {
3249
+ key: "toArray",
3250
+ value: function toArray() {
3251
+ return this._items.map(function(entry) {
3252
+ return liveNodeToLson(entry);
3253
+ });
3254
+ }
3255
+ },
3256
+ {
3257
+ key: "every",
3258
+ value: function every(predicate) {
3259
+ return this.toArray().every(predicate);
3260
+ }
3261
+ },
3262
+ {
3263
+ key: "filter",
3264
+ value: function filter(predicate) {
3265
+ return this.toArray().filter(predicate);
3266
+ }
3267
+ },
3268
+ {
3269
+ key: "find",
3270
+ value: function find(predicate) {
3271
+ return this.toArray().find(predicate);
3272
+ }
3273
+ },
3274
+ {
3275
+ key: "findIndex",
3276
+ value: function findIndex(predicate) {
3277
+ return this.toArray().findIndex(predicate);
3278
+ }
3279
+ },
3280
+ {
3281
+ key: "forEach",
3282
+ value: function forEach(callbackfn) {
3283
+ return this.toArray().forEach(callbackfn);
3284
+ }
3285
+ },
3286
+ {
3287
+ key: "get",
3288
+ value: function get(index) {
3289
+ if (index < 0 || index >= this._items.length) {
3290
+ return void 0;
3291
+ }
3292
+ return liveNodeToLson(this._items[index]);
3293
+ }
3294
+ },
3295
+ {
3296
+ key: "indexOf",
3297
+ value: function indexOf(searchElement, fromIndex) {
3298
+ return this.toArray().indexOf(searchElement, fromIndex);
3299
+ }
3300
+ },
3301
+ {
3302
+ key: "lastIndexOf",
3303
+ value: function lastIndexOf(searchElement, fromIndex) {
3304
+ return this.toArray().lastIndexOf(searchElement, fromIndex);
3305
+ }
3306
+ },
3307
+ {
3308
+ key: "map",
3309
+ value: function map(callback) {
3310
+ return this._items.map(function(entry, i) {
3311
+ return callback(liveNodeToLson(entry), i);
3312
+ });
3313
+ }
3314
+ },
3315
+ {
3316
+ key: "some",
3317
+ value: function some(predicate) {
3318
+ return this.toArray().some(predicate);
3319
+ }
3320
+ },
3321
+ {
3322
+ key: Symbol.iterator,
3323
+ value: function value() {
3324
+ return new LiveListIterator(this._items);
3325
+ }
3326
+ },
3327
+ {
3328
+ key: "_createAttachItemAndSort",
3329
+ value: function _createAttachItemAndSort(op, key) {
3330
+ var newItem = creationOpToLiveNode(op);
3331
+ newItem._attach(op.id, nn(this._doc));
3332
+ newItem._setParentLink(this, key);
3333
+ this._insertAndSort(newItem);
3334
+ var newIndex = this._indexOfPosition(key);
3335
+ return {
3336
+ newItem: newItem,
3337
+ newIndex: newIndex
3338
+ };
3339
+ }
3340
+ },
3341
+ {
3342
+ key: "_shiftItemPosition",
3343
+ value: function _shiftItemPosition(index, key) {
3344
+ var shiftedPosition = makePosition(key, this._items.length > index + 1 ? _optionalChain([
3345
+ this,
3346
+ "access",
3347
+ function(_32) {
3348
+ return _32._items;
3349
+ },
3350
+ "access",
3351
+ function(_33) {
3352
+ return _33[index + 1];
3353
+ },
3354
+ "optionalAccess",
3355
+ function(_34) {
3356
+ return _34._getParentKeyOrThrow;
3357
+ },
3358
+ "call",
3359
+ function(_35) {
3360
+ return _35();
3361
+ }
3362
+ ]) : void 0);
3363
+ this._items[index]._setParentLink(this, shiftedPosition);
3364
+ }
3365
+ }
3366
+ ], [
3367
+ {
3368
+ key: "_deserialize",
3369
+ value: function _deserialize(param, parentToChildren, doc) {
3370
+ var _param = _slicedToArray(param, 1), id = _param[0];
3371
+ var list = new LiveList();
3372
+ list._attach(id, doc);
3373
+ var children = parentToChildren.get(id);
3374
+ if (children == null) {
3375
+ return list;
3376
+ }
3377
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3378
+ try {
3379
+ for(var _iterator = children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3380
+ var _value = _slicedToArray(_step.value, 2), id2 = _value[0], crdt = _value[1];
3381
+ var child = deserialize([
3382
+ id2,
3383
+ crdt
3384
+ ], parentToChildren, doc);
3385
+ child._setParentLink(list, crdt.parentKey);
3386
+ list._insertAndSort(child);
3387
+ }
3388
+ } catch (err) {
3389
+ _didIteratorError = true;
3390
+ _iteratorError = err;
3391
+ } finally{
3392
+ try {
3393
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
3394
+ _iterator.return();
3395
+ }
3396
+ } finally{
3397
+ if (_didIteratorError) {
3398
+ throw _iteratorError;
3399
+ }
3400
+ }
3401
+ }
3402
+ return list;
3403
+ }
3404
+ }
3405
+ ]);
3406
+ return LiveList;
3407
+ }(AbstractCrdt);
3408
+ var LiveListIterator = /*#__PURE__*/ function() {
3409
+ function LiveListIterator(items) {
3410
+ _classCallCheck(this, LiveListIterator);
3411
+ this._innerIterator = items[Symbol.iterator]();
3412
+ }
3413
+ _createClass(LiveListIterator, [
3414
+ {
3415
+ key: Symbol.iterator,
3416
+ value: function value() {
3417
+ return this;
3418
+ }
3419
+ },
3420
+ {
3421
+ key: "next",
3422
+ value: function next() {
3423
+ var result = this._innerIterator.next();
3424
+ if (result.done) {
3425
+ return {
3426
+ done: true,
3427
+ value: void 0
3428
+ };
3429
+ }
3430
+ var value = liveNodeToLson(result.value);
3431
+ return {
3432
+ value: value
3433
+ };
3434
+ }
3435
+ }
3436
+ ]);
3437
+ return LiveListIterator;
3438
+ }();
3439
+ function makeUpdate(liveList, deltaUpdates) {
3440
+ return {
3441
+ node: liveList,
3442
+ type: "LiveList",
3443
+ updates: deltaUpdates
3444
+ };
3445
+ }
3446
+ function setDelta(index, item) {
3447
+ return {
3448
+ index: index,
3449
+ type: "set",
3450
+ item: _instanceof(item, LiveRegister) ? item.data : item
3451
+ };
3452
+ }
3453
+ function deleteDelta(index) {
3454
+ return {
3455
+ index: index,
3456
+ type: "delete"
3457
+ };
3458
+ }
3459
+ function insertDelta(index, item) {
3460
+ return {
3461
+ index: index,
3462
+ type: "insert",
3463
+ item: _instanceof(item, LiveRegister) ? item.data : item
3464
+ };
3465
+ }
3466
+ function moveDelta(previousIndex, index, item) {
3467
+ return {
3468
+ index: index,
3469
+ type: "move",
3470
+ previousIndex: previousIndex,
3471
+ item: _instanceof(item, LiveRegister) ? item.data : item
3472
+ };
3473
+ }
3474
+ function addIntentAndDeletedIdToOperation(ops, deletedId) {
3475
+ if (ops.length === 0) {
3476
+ throw new Error("Internal error. Serialized LiveStructure should have at least 1 operation");
3477
+ }
3478
+ var firstOp = ops[0];
3479
+ firstOp.intent = "set";
3480
+ firstOp.deletedId = deletedId;
3481
+ }
3482
+ // src/AuthToken.ts
3483
+ var SCOPES = [
3484
+ "websocket:presence",
3485
+ "websocket:storage",
3486
+ "room:read",
3487
+ "room:write",
3488
+ "rooms:read",
3489
+ "rooms:write"
3490
+ ];
3491
+ function hasJwtMeta(data) {
3492
+ if (!isPlainObject(data)) {
3493
+ return false;
3494
+ }
3495
+ var iat = data.iat, exp = data.exp;
3496
+ return typeof iat === "number" && typeof exp === "number";
3497
+ }
3498
+ function isTokenExpired(token) {
3499
+ var now = Date.now() / 1e3;
3500
+ return now > token.exp - 300 || now < token.iat + 300;
3501
+ }
3502
+ function isScope(value) {
3503
+ return SCOPES.includes(value);
3504
+ }
3505
+ function isStringList(value) {
3506
+ return Array.isArray(value) && value.every(function(i) {
3507
+ return typeof i === "string";
3508
+ });
3509
+ }
3510
+ function isAppOnlyAuthToken(data) {
3511
+ return typeof data.appId === "string" && data.roomId === void 0 && isStringList(data.scopes);
3512
+ }
3513
+ function isRoomAuthToken(data) {
3514
+ return typeof data.appId === "string" && typeof data.roomId === "string" && typeof data.actor === "number" && (data.id === void 0 || typeof data.id === "string") && isStringList(data.scopes) && (data.maxConnectionsPerRoom === void 0 || typeof data.maxConnectionsPerRoom === "number");
3515
+ }
3516
+ function isAuthToken(data) {
3517
+ return isAppOnlyAuthToken(data) || isRoomAuthToken(data);
3518
+ }
3519
+ function parseJwtToken(token) {
3520
+ var tokenParts = token.split(".");
3521
+ if (tokenParts.length !== 3) {
3522
+ throw new Error("Authentication error: invalid JWT token");
3523
+ }
3524
+ var data = tryParseJson(b64decode(tokenParts[1]));
3525
+ if (data && hasJwtMeta(data)) {
3526
+ return data;
3527
+ } else {
3528
+ throw new Error("Authentication error: missing JWT metadata");
3529
+ }
3530
+ }
3531
+ function parseRoomAuthToken(tokenString) {
3532
+ var data = parseJwtToken(tokenString);
3533
+ if (data && isRoomAuthToken(data)) {
3534
+ var _legacyField = data.maxConnections, token = _objectWithoutProperties(data, [
3535
+ "maxConnections"
3536
+ ]);
3537
+ return token;
3538
+ } else {
3539
+ throw new Error("Authentication error: we expected a room token but did not get one. Hint: if you are using a callback, ensure the room is passed when creating the token. For more information: https://liveblocks.io/docs/api-reference/liveblocks-client#createClientCallback");
3540
+ }
3541
+ }
3542
+ // src/types/Json.ts
3543
+ function isJsonScalar(data) {
3544
+ return data === null || typeof data === "string" || typeof data === "number" || typeof data === "boolean";
3545
+ }
3546
+ function isJsonArray(data) {
3547
+ return Array.isArray(data);
3548
+ }
3549
+ function isJsonObject(data) {
3550
+ return !isJsonScalar(data) && !isJsonArray(data);
3551
+ }
3552
+ exports.deprecate = deprecate;
3553
+ exports.deprecateIf = deprecateIf;
3554
+ exports.throwUsageError = throwUsageError;
3555
+ exports.errorIf = errorIf;
3556
+ exports.assertNever = assertNever;
3557
+ exports.nn = nn;
3558
+ exports.ClientMsgCode = ClientMsgCode;
3559
+ exports.OpCode = OpCode;
3560
+ exports.CrdtType = CrdtType;
3561
+ exports.isRootCrdt = isRootCrdt;
3562
+ exports.isChildCrdt = isChildCrdt;
3563
+ exports.ServerMsgCode = ServerMsgCode;
3564
+ exports.isRoomEventName = isRoomEventName;
3565
+ exports.WebsocketCloseCodes = WebsocketCloseCodes;
3566
+ exports.LiveRegister = LiveRegister;
3567
+ exports.makePosition = makePosition;
3568
+ exports.comparePosition = comparePosition;
3569
+ exports.LiveList = LiveList;
3570
+ exports.LiveMap = LiveMap;
3571
+ exports.LiveObject = LiveObject;
3572
+ exports.remove = remove;
3573
+ exports.compact = compact;
3574
+ exports.isSameNodeOrChildOf = isSameNodeOrChildOf;
3575
+ exports.isLiveNode = isLiveNode;
3576
+ exports.isLiveList = isLiveList;
3577
+ exports.isLiveObject = isLiveObject;
3578
+ exports.getTreesDiffOperations = getTreesDiffOperations;
3579
+ exports.mergeStorageUpdates = mergeStorageUpdates;
3580
+ exports.isPlainObject = isPlainObject;
3581
+ exports.findNonSerializableValue = findNonSerializableValue;
3582
+ exports.tryParseJson = tryParseJson;
3583
+ exports.b64decode = b64decode;
3584
+ exports.isTokenExpired = isTokenExpired;
3585
+ exports.isScope = isScope;
3586
+ exports.isAppOnlyAuthToken = isAppOnlyAuthToken;
3587
+ exports.isRoomAuthToken = isRoomAuthToken;
3588
+ exports.isAuthToken = isAuthToken;
3589
+ exports.parseRoomAuthToken = parseRoomAuthToken;
3590
+ exports.isJsonScalar = isJsonScalar;
3591
+ exports.isJsonArray = isJsonArray;
3592
+ exports.isJsonObject = isJsonObject;