@liveblocks/client 0.15.11-test.1 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/esm/index.mjs CHANGED
@@ -46,46 +46,21 @@ var WebsocketCloseCodes = /* @__PURE__ */ ((WebsocketCloseCodes2) => {
46
46
  return WebsocketCloseCodes2;
47
47
  })(WebsocketCloseCodes || {});
48
48
 
49
- var __accessCheck$4 = (obj, member, msg) => {
50
- if (!member.has(obj))
51
- throw TypeError("Cannot " + msg);
52
- };
53
- var __privateGet$4 = (obj, member, getter) => {
54
- __accessCheck$4(obj, member, "read from private field");
55
- return getter ? getter.call(obj) : member.get(obj);
56
- };
57
- var __privateAdd$4 = (obj, member, value) => {
58
- if (member.has(obj))
59
- throw TypeError("Cannot add the same private member more than once");
60
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
61
- };
62
- var __privateSet$4 = (obj, member, value, setter) => {
63
- __accessCheck$4(obj, member, "write to private field");
64
- setter ? setter.call(obj, value) : member.set(obj, value);
65
- return value;
66
- };
67
- var _parent, _doc, _id, _parentKey;
68
49
  class AbstractCrdt {
69
- constructor() {
70
- __privateAdd$4(this, _parent, void 0);
71
- __privateAdd$4(this, _doc, void 0);
72
- __privateAdd$4(this, _id, void 0);
73
- __privateAdd$4(this, _parentKey, void 0);
74
- }
75
50
  get _doc() {
76
- return __privateGet$4(this, _doc);
51
+ return this.__doc;
77
52
  }
78
53
  get roomId() {
79
- return __privateGet$4(this, _doc) ? __privateGet$4(this, _doc).roomId : null;
54
+ return this.__doc ? this.__doc.roomId : null;
80
55
  }
81
56
  get _id() {
82
- return __privateGet$4(this, _id);
57
+ return this.__id;
83
58
  }
84
59
  get _parent() {
85
- return __privateGet$4(this, _parent);
60
+ return this.__parent;
86
61
  }
87
62
  get _parentKey() {
88
- return __privateGet$4(this, _parentKey);
63
+ return this.__parentKey;
89
64
  }
90
65
  _apply(op, _isLocal) {
91
66
  switch (op.type) {
@@ -99,32 +74,28 @@ class AbstractCrdt {
99
74
  return { modified: false };
100
75
  }
101
76
  _setParentLink(parent, key) {
102
- if (__privateGet$4(this, _parent) != null && __privateGet$4(this, _parent) !== parent) {
77
+ if (this.__parent != null && this.__parent !== parent) {
103
78
  throw new Error("Cannot attach parent if it already exist");
104
79
  }
105
- __privateSet$4(this, _parentKey, key);
106
- __privateSet$4(this, _parent, parent);
80
+ this.__parentKey = key;
81
+ this.__parent = parent;
107
82
  }
108
83
  _attach(id, doc) {
109
- if (__privateGet$4(this, _id) || __privateGet$4(this, _doc)) {
84
+ if (this.__id || this.__doc) {
110
85
  throw new Error("Cannot attach if CRDT is already attached");
111
86
  }
112
87
  doc.addItem(id, this);
113
- __privateSet$4(this, _id, id);
114
- __privateSet$4(this, _doc, doc);
88
+ this.__id = id;
89
+ this.__doc = doc;
115
90
  }
116
91
  _detach() {
117
- if (__privateGet$4(this, _doc) && __privateGet$4(this, _id)) {
118
- __privateGet$4(this, _doc).deleteItem(__privateGet$4(this, _id));
92
+ if (this.__doc && this.__id) {
93
+ this.__doc.deleteItem(this.__id);
119
94
  }
120
- __privateSet$4(this, _parent, void 0);
121
- __privateSet$4(this, _doc, void 0);
95
+ this.__parent = void 0;
96
+ this.__doc = void 0;
122
97
  }
123
98
  }
124
- _parent = new WeakMap();
125
- _doc = new WeakMap();
126
- _id = new WeakMap();
127
- _parentKey = new WeakMap();
128
99
 
129
100
  const min = 32;
130
101
  const max = 126;
@@ -227,43 +198,23 @@ function compare(posA, posB) {
227
198
  throw new Error(`Impossible to compare similar position "${posA}" and "${posB}"`);
228
199
  }
229
200
 
230
- var __accessCheck$3 = (obj, member, msg) => {
231
- if (!member.has(obj))
232
- throw TypeError("Cannot " + msg);
233
- };
234
- var __privateGet$3 = (obj, member, getter) => {
235
- __accessCheck$3(obj, member, "read from private field");
236
- return getter ? getter.call(obj) : member.get(obj);
237
- };
238
- var __privateAdd$3 = (obj, member, value) => {
239
- if (member.has(obj))
240
- throw TypeError("Cannot add the same private member more than once");
241
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
242
- };
243
- var __privateSet$3 = (obj, member, value, setter) => {
244
- __accessCheck$3(obj, member, "write to private field");
245
- setter ? setter.call(obj, value) : member.set(obj, value);
246
- return value;
247
- };
248
- var _data;
249
- const _LiveRegister = class extends AbstractCrdt {
201
+ class LiveRegister extends AbstractCrdt {
250
202
  constructor(data) {
251
203
  super();
252
- __privateAdd$3(this, _data, void 0);
253
- __privateSet$3(this, _data, data);
204
+ this._data = data;
254
205
  }
255
206
  get data() {
256
- return __privateGet$3(this, _data);
207
+ return this._data;
257
208
  }
258
- static _deserialize([id, item], parentToChildren, doc) {
209
+ static _deserialize([id, item], _parentToChildren, doc) {
259
210
  if (item.type !== CrdtType.Register) {
260
211
  throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
261
212
  }
262
- const register = new _LiveRegister(item.data);
213
+ const register = new LiveRegister(item.data);
263
214
  register._attach(id, doc);
264
215
  return register;
265
216
  }
266
- _serialize(parentId, parentKey, doc) {
217
+ _serialize(parentId, parentKey, doc, intent) {
267
218
  if (this._id == null || parentId == null || parentKey == null) {
268
219
  throw new Error("Cannot serialize register if parentId or parentKey is undefined");
269
220
  }
@@ -272,6 +223,7 @@ const _LiveRegister = class extends AbstractCrdt {
272
223
  type: OpType.CreateRegister,
273
224
  opId: doc == null ? void 0 : doc.generateOpId(),
274
225
  id: this._id,
226
+ intent,
275
227
  parentId,
276
228
  parentKey,
277
229
  data: this.data
@@ -287,7 +239,7 @@ const _LiveRegister = class extends AbstractCrdt {
287
239
  data: this.data
288
240
  };
289
241
  }
290
- _attachChild(_id, _key, _crdt, _opId, _isLocal) {
242
+ _attachChild(_op, _isLocal) {
291
243
  throw new Error("Method not implemented.");
292
244
  }
293
245
  _detachChild(_crdt) {
@@ -296,43 +248,22 @@ const _LiveRegister = class extends AbstractCrdt {
296
248
  _apply(op, isLocal) {
297
249
  return super._apply(op, isLocal);
298
250
  }
299
- };
300
- let LiveRegister = _LiveRegister;
301
- _data = new WeakMap();
251
+ }
302
252
 
303
- var __accessCheck$2 = (obj, member, msg) => {
304
- if (!member.has(obj))
305
- throw TypeError("Cannot " + msg);
306
- };
307
- var __privateGet$2 = (obj, member, getter) => {
308
- __accessCheck$2(obj, member, "read from private field");
309
- return getter ? getter.call(obj) : member.get(obj);
310
- };
311
- var __privateAdd$2 = (obj, member, value) => {
312
- if (member.has(obj))
313
- throw TypeError("Cannot add the same private member more than once");
314
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
315
- };
316
- var __privateSet$2 = (obj, member, value, setter) => {
317
- __accessCheck$2(obj, member, "write to private field");
318
- setter ? setter.call(obj, value) : member.set(obj, value);
319
- return value;
320
- };
321
- var _items, _innerIterator;
322
- const _LiveList = class extends AbstractCrdt {
253
+ class LiveList extends AbstractCrdt {
323
254
  constructor(items = []) {
324
255
  super();
325
- __privateAdd$2(this, _items, []);
256
+ this._items = [];
326
257
  let position = void 0;
327
258
  for (let i = 0; i < items.length; i++) {
328
259
  const newPosition = makePosition(position);
329
260
  const item = selfOrRegister(items[i]);
330
- __privateGet$2(this, _items).push([item, newPosition]);
261
+ this._items.push([item, newPosition]);
331
262
  position = newPosition;
332
263
  }
333
264
  }
334
265
  static _deserialize([id], parentToChildren, doc) {
335
- const list = new _LiveList([]);
266
+ const list = new LiveList([]);
336
267
  list._attach(id, doc);
337
268
  const children = parentToChildren.get(id);
338
269
  if (children == null) {
@@ -341,12 +272,12 @@ const _LiveList = class extends AbstractCrdt {
341
272
  for (const entry of children) {
342
273
  const child = deserialize(entry, parentToChildren, doc);
343
274
  child._setParentLink(list, entry[1].parentKey);
344
- __privateGet$2(list, _items).push([child, entry[1].parentKey]);
345
- __privateGet$2(list, _items).sort((itemA, itemB) => compare(itemA[1], itemB[1]));
275
+ list._items.push([child, entry[1].parentKey]);
276
+ list._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
346
277
  }
347
278
  return list;
348
279
  }
349
- _serialize(parentId, parentKey, doc) {
280
+ _serialize(parentId, parentKey, doc, intent) {
350
281
  if (this._id == null) {
351
282
  throw new Error("Cannot serialize item is not attached");
352
283
  }
@@ -357,56 +288,79 @@ const _LiveList = class extends AbstractCrdt {
357
288
  const op = {
358
289
  id: this._id,
359
290
  opId: doc == null ? void 0 : doc.generateOpId(),
291
+ intent,
360
292
  type: OpType.CreateList,
361
293
  parentId,
362
294
  parentKey
363
295
  };
364
296
  ops.push(op);
365
- for (const [value, key] of __privateGet$2(this, _items)) {
297
+ for (const [value, key] of this._items) {
366
298
  ops.push(...value._serialize(this._id, key, doc));
367
299
  }
368
300
  return ops;
369
301
  }
370
302
  _indexOfPosition(position) {
371
- return __privateGet$2(this, _items).findIndex((item) => item[1] === position);
303
+ return this._items.findIndex((item) => item[1] === position);
372
304
  }
373
305
  _attach(id, doc) {
374
306
  super._attach(id, doc);
375
- for (const [item] of __privateGet$2(this, _items)) {
307
+ for (const [item] of this._items) {
376
308
  item._attach(doc.generateId(), doc);
377
309
  }
378
310
  }
379
311
  _detach() {
380
312
  super._detach();
381
- for (const [value] of __privateGet$2(this, _items)) {
313
+ for (const [value] of this._items) {
382
314
  value._detach();
383
315
  }
384
316
  }
385
- _attachChild(id, key, child, _opId, isLocal) {
317
+ _attachChild(op, isLocal) {
386
318
  var _a;
387
319
  if (this._doc == null) {
388
320
  throw new Error("Can't attach child if doc is not present");
389
321
  }
322
+ const { id, parentKey, intent } = op;
323
+ const key = parentKey;
324
+ const child = creationOpToLiveStructure(op);
390
325
  if (this._doc.getItem(id) !== void 0) {
391
326
  return { modified: false };
392
327
  }
393
328
  child._attach(id, this._doc);
394
329
  child._setParentLink(this, key);
395
- const index = __privateGet$2(this, _items).findIndex((entry) => entry[1] === key);
330
+ const index = this._items.findIndex((entry) => entry[1] === key);
396
331
  let newKey = key;
397
332
  if (index !== -1) {
398
- if (isLocal) {
399
- const before = __privateGet$2(this, _items)[index] ? __privateGet$2(this, _items)[index][1] : void 0;
400
- const after = __privateGet$2(this, _items)[index + 1] ? __privateGet$2(this, _items)[index + 1][1] : void 0;
333
+ if (intent === "set") {
334
+ const existingItem = this._items[index][0];
335
+ existingItem._detach();
336
+ const storageUpdate = {
337
+ node: this,
338
+ type: "LiveList",
339
+ updates: [
340
+ {
341
+ index,
342
+ type: "set",
343
+ item: child instanceof LiveRegister ? child.data : child
344
+ }
345
+ ]
346
+ };
347
+ this._items[index][0] = child;
348
+ return {
349
+ modified: storageUpdate,
350
+ reverse: existingItem._serialize(this._id, key, this._doc, "set")
351
+ };
352
+ } else if (isLocal) {
353
+ const before = this._items[index] ? this._items[index][1] : void 0;
354
+ const after = this._items[index + 1] ? this._items[index + 1][1] : void 0;
401
355
  newKey = makePosition(before, after);
402
356
  child._setParentLink(this, newKey);
403
357
  } else {
404
- __privateGet$2(this, _items)[index][1] = makePosition(key, (_a = __privateGet$2(this, _items)[index + 1]) == null ? void 0 : _a[1]);
358
+ this._items[index][1] = makePosition(key, (_a = this._items[index + 1]) == null ? void 0 : _a[1]);
405
359
  }
406
360
  }
407
- __privateGet$2(this, _items).push([child, newKey]);
408
- __privateGet$2(this, _items).sort((itemA, itemB) => compare(itemA[1], itemB[1]));
409
- const newIndex = __privateGet$2(this, _items).findIndex((entry) => entry[1] === newKey);
361
+ this._items.push([child, newKey]);
362
+ this._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
363
+ const newIndex = this._items.findIndex((entry) => entry[1] === newKey);
410
364
  return {
411
365
  reverse: [{ type: OpType.DeleteCrdt, id }],
412
366
  modified: {
@@ -425,8 +379,8 @@ const _LiveList = class extends AbstractCrdt {
425
379
  _detachChild(child) {
426
380
  if (child) {
427
381
  const reverse = child._serialize(this._id, child._parentKey, this._doc);
428
- const indexToDelete = __privateGet$2(this, _items).findIndex((item) => item[0] === child);
429
- __privateGet$2(this, _items).splice(indexToDelete, 1);
382
+ const indexToDelete = this._items.findIndex((item) => item[0] === child);
383
+ this._items.splice(indexToDelete, 1);
430
384
  child._detach();
431
385
  const storageUpdate = {
432
386
  node: this,
@@ -440,17 +394,17 @@ const _LiveList = class extends AbstractCrdt {
440
394
  _setChildKey(key, child, previousKey) {
441
395
  var _a;
442
396
  child._setParentLink(this, key);
443
- const previousIndex = __privateGet$2(this, _items).findIndex((entry) => entry[0]._id === child._id);
444
- const index = __privateGet$2(this, _items).findIndex((entry) => entry[1] === key);
397
+ const previousIndex = this._items.findIndex((entry) => entry[0]._id === child._id);
398
+ const index = this._items.findIndex((entry) => entry[1] === key);
445
399
  if (index !== -1) {
446
- __privateGet$2(this, _items)[index][1] = makePosition(key, (_a = __privateGet$2(this, _items)[index + 1]) == null ? void 0 : _a[1]);
400
+ this._items[index][1] = makePosition(key, (_a = this._items[index + 1]) == null ? void 0 : _a[1]);
447
401
  }
448
- const item = __privateGet$2(this, _items).find((item2) => item2[0] === child);
402
+ const item = this._items.find((item2) => item2[0] === child);
449
403
  if (item) {
450
404
  item[1] = key;
451
405
  }
452
- __privateGet$2(this, _items).sort((itemA, itemB) => compare(itemA[1], itemB[1]));
453
- const newIndex = __privateGet$2(this, _items).findIndex((entry) => entry[0]._id === child._id);
406
+ this._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
407
+ const newIndex = this._items.findIndex((entry) => entry[0]._id === child._id);
454
408
  const updatesDelta = newIndex === previousIndex ? [] : [
455
409
  {
456
410
  index: newIndex,
@@ -486,23 +440,23 @@ const _LiveList = class extends AbstractCrdt {
486
440
  };
487
441
  }
488
442
  get length() {
489
- return __privateGet$2(this, _items).length;
443
+ return this._items.length;
490
444
  }
491
445
  push(element) {
492
446
  return this.insert(element, this.length);
493
447
  }
494
448
  insert(element, index) {
495
- if (index < 0 || index > __privateGet$2(this, _items).length) {
496
- throw new Error(`Cannot insert list item at index "${index}". index should be between 0 and ${__privateGet$2(this, _items).length}`);
449
+ if (index < 0 || index > this._items.length) {
450
+ throw new Error(`Cannot insert list item at index "${index}". index should be between 0 and ${this._items.length}`);
497
451
  }
498
- const before = __privateGet$2(this, _items)[index - 1] ? __privateGet$2(this, _items)[index - 1][1] : void 0;
499
- const after = __privateGet$2(this, _items)[index] ? __privateGet$2(this, _items)[index][1] : void 0;
452
+ const before = this._items[index - 1] ? this._items[index - 1][1] : void 0;
453
+ const after = this._items[index] ? this._items[index][1] : void 0;
500
454
  const position = makePosition(before, after);
501
455
  const value = selfOrRegister(element);
502
456
  value._setParentLink(this, position);
503
- __privateGet$2(this, _items).push([value, position]);
504
- __privateGet$2(this, _items).sort((itemA, itemB) => compare(itemA[1], itemB[1]));
505
- const newIndex = __privateGet$2(this, _items).findIndex((entry) => entry[1] === position);
457
+ this._items.push([value, position]);
458
+ this._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
459
+ const newIndex = this._items.findIndex((entry) => entry[1] === position);
506
460
  if (this._doc && this._id) {
507
461
  const id = this._doc.generateId();
508
462
  value._attach(id, this._doc);
@@ -525,31 +479,31 @@ const _LiveList = class extends AbstractCrdt {
525
479
  if (targetIndex < 0) {
526
480
  throw new Error("targetIndex cannot be less than 0");
527
481
  }
528
- if (targetIndex >= __privateGet$2(this, _items).length) {
482
+ if (targetIndex >= this._items.length) {
529
483
  throw new Error("targetIndex cannot be greater or equal than the list length");
530
484
  }
531
485
  if (index < 0) {
532
486
  throw new Error("index cannot be less than 0");
533
487
  }
534
- if (index >= __privateGet$2(this, _items).length) {
488
+ if (index >= this._items.length) {
535
489
  throw new Error("index cannot be greater or equal than the list length");
536
490
  }
537
491
  let beforePosition = null;
538
492
  let afterPosition = null;
539
493
  if (index < targetIndex) {
540
- afterPosition = targetIndex === __privateGet$2(this, _items).length - 1 ? void 0 : __privateGet$2(this, _items)[targetIndex + 1][1];
541
- beforePosition = __privateGet$2(this, _items)[targetIndex][1];
494
+ afterPosition = targetIndex === this._items.length - 1 ? void 0 : this._items[targetIndex + 1][1];
495
+ beforePosition = this._items[targetIndex][1];
542
496
  } else {
543
- afterPosition = __privateGet$2(this, _items)[targetIndex][1];
544
- beforePosition = targetIndex === 0 ? void 0 : __privateGet$2(this, _items)[targetIndex - 1][1];
497
+ afterPosition = this._items[targetIndex][1];
498
+ beforePosition = targetIndex === 0 ? void 0 : this._items[targetIndex - 1][1];
545
499
  }
546
500
  const position = makePosition(beforePosition, afterPosition);
547
- const item = __privateGet$2(this, _items)[index];
501
+ const item = this._items[index];
548
502
  const previousPosition = item[1];
549
503
  item[1] = position;
550
504
  item[0]._setParentLink(this, position);
551
- __privateGet$2(this, _items).sort((itemA, itemB) => compare(itemA[1], itemB[1]));
552
- const newIndex = __privateGet$2(this, _items).findIndex((entry) => entry[1] === position);
505
+ this._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
506
+ const newIndex = this._items.findIndex((entry) => entry[1] === position);
553
507
  if (this._doc && this._id) {
554
508
  const storageUpdates = /* @__PURE__ */ new Map();
555
509
  storageUpdates.set(this._id, {
@@ -581,12 +535,12 @@ const _LiveList = class extends AbstractCrdt {
581
535
  }
582
536
  }
583
537
  delete(index) {
584
- if (index < 0 || index >= __privateGet$2(this, _items).length) {
585
- throw new Error(`Cannot delete list item at index "${index}". index should be between 0 and ${__privateGet$2(this, _items).length - 1}`);
538
+ if (index < 0 || index >= this._items.length) {
539
+ throw new Error(`Cannot delete list item at index "${index}". index should be between 0 and ${this._items.length - 1}`);
586
540
  }
587
- const item = __privateGet$2(this, _items)[index];
541
+ const item = this._items[index];
588
542
  item[0]._detach();
589
- __privateGet$2(this, _items).splice(index, 1);
543
+ this._items.splice(index, 1);
590
544
  if (this._doc) {
591
545
  const childRecordId = item[0]._id;
592
546
  if (childRecordId) {
@@ -612,7 +566,7 @@ const _LiveList = class extends AbstractCrdt {
612
566
  const reverseOps = [];
613
567
  const updateDelta = [];
614
568
  let i = 0;
615
- for (const item of __privateGet$2(this, _items)) {
569
+ for (const item of this._items) {
616
570
  item[0]._detach();
617
571
  const childId = item[0]._id;
618
572
  if (childId) {
@@ -622,7 +576,7 @@ const _LiveList = class extends AbstractCrdt {
622
576
  }
623
577
  i++;
624
578
  }
625
- __privateSet$2(this, _items, []);
579
+ this._items = [];
626
580
  const storageUpdates = /* @__PURE__ */ new Map();
627
581
  storageUpdates.set(this._id, {
628
582
  node: this,
@@ -631,14 +585,41 @@ const _LiveList = class extends AbstractCrdt {
631
585
  });
632
586
  this._doc.dispatch(ops, reverseOps, storageUpdates);
633
587
  } else {
634
- for (const item of __privateGet$2(this, _items)) {
588
+ for (const item of this._items) {
635
589
  item[0]._detach();
636
590
  }
637
- __privateSet$2(this, _items, []);
591
+ this._items = [];
592
+ }
593
+ }
594
+ set(index, item) {
595
+ if (index < 0 || index >= this._items.length) {
596
+ throw new Error(`Cannot set list item at index "${index}". index should be between 0 and ${this._items.length - 1}`);
597
+ }
598
+ const [existingItem, position] = this._items[index];
599
+ existingItem._detach();
600
+ const value = selfOrRegister(item);
601
+ value._setParentLink(this, position);
602
+ this._items[index][0] = value;
603
+ if (this._doc && this._id) {
604
+ const id = this._doc.generateId();
605
+ value._attach(id, this._doc);
606
+ const storageUpdates = /* @__PURE__ */ new Map();
607
+ storageUpdates.set(this._id, {
608
+ node: this,
609
+ type: "LiveList",
610
+ updates: [
611
+ {
612
+ index,
613
+ item: value instanceof LiveRegister ? value.data : value,
614
+ type: "set"
615
+ }
616
+ ]
617
+ });
618
+ this._doc.dispatch(value._serialize(this._id, position, this._doc, "set"), existingItem._serialize(this._id, position, void 0, "set"), storageUpdates);
638
619
  }
639
620
  }
640
621
  toArray() {
641
- return __privateGet$2(this, _items).map((entry) => selfOrRegisterValue(entry[0]));
622
+ return this._items.map((entry) => selfOrRegisterValue(entry[0]));
642
623
  }
643
624
  every(predicate) {
644
625
  return this.toArray().every(predicate);
@@ -656,10 +637,10 @@ const _LiveList = class extends AbstractCrdt {
656
637
  return this.toArray().forEach(callbackfn);
657
638
  }
658
639
  get(index) {
659
- if (index < 0 || index >= __privateGet$2(this, _items).length) {
640
+ if (index < 0 || index >= this._items.length) {
660
641
  return void 0;
661
642
  }
662
- return selfOrRegisterValue(__privateGet$2(this, _items)[index][0]);
643
+ return selfOrRegisterValue(this._items[index][0]);
663
644
  }
664
645
  indexOf(searchElement, fromIndex) {
665
646
  return this.toArray().indexOf(searchElement, fromIndex);
@@ -668,27 +649,24 @@ const _LiveList = class extends AbstractCrdt {
668
649
  return this.toArray().lastIndexOf(searchElement, fromIndex);
669
650
  }
670
651
  map(callback) {
671
- return __privateGet$2(this, _items).map((entry, i) => callback(selfOrRegisterValue(entry[0]), i));
652
+ return this._items.map((entry, i) => callback(selfOrRegisterValue(entry[0]), i));
672
653
  }
673
654
  some(predicate) {
674
655
  return this.toArray().some(predicate);
675
656
  }
676
657
  [Symbol.iterator]() {
677
- return new LiveListIterator(__privateGet$2(this, _items));
658
+ return new LiveListIterator(this._items);
678
659
  }
679
- };
680
- let LiveList = _LiveList;
681
- _items = new WeakMap();
660
+ }
682
661
  class LiveListIterator {
683
662
  constructor(items) {
684
- __privateAdd$2(this, _innerIterator, void 0);
685
- __privateSet$2(this, _innerIterator, items[Symbol.iterator]());
663
+ this._innerIterator = items[Symbol.iterator]();
686
664
  }
687
665
  [Symbol.iterator]() {
688
666
  return this;
689
667
  }
690
668
  next() {
691
- const result = __privateGet$2(this, _innerIterator).next();
669
+ const result = this._innerIterator.next();
692
670
  if (result.done) {
693
671
  return {
694
672
  done: true,
@@ -700,31 +678,10 @@ class LiveListIterator {
700
678
  };
701
679
  }
702
680
  }
703
- _innerIterator = new WeakMap();
704
681
 
705
- var __accessCheck$1 = (obj, member, msg) => {
706
- if (!member.has(obj))
707
- throw TypeError("Cannot " + msg);
708
- };
709
- var __privateGet$1 = (obj, member, getter) => {
710
- __accessCheck$1(obj, member, "read from private field");
711
- return getter ? getter.call(obj) : member.get(obj);
712
- };
713
- var __privateAdd$1 = (obj, member, value) => {
714
- if (member.has(obj))
715
- throw TypeError("Cannot add the same private member more than once");
716
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
717
- };
718
- var __privateSet$1 = (obj, member, value, setter) => {
719
- __accessCheck$1(obj, member, "write to private field");
720
- setter ? setter.call(obj, value) : member.set(obj, value);
721
- return value;
722
- };
723
- var _map$1;
724
- const _LiveMap = class extends AbstractCrdt {
682
+ class LiveMap extends AbstractCrdt {
725
683
  constructor(entries) {
726
684
  super();
727
- __privateAdd$1(this, _map$1, void 0);
728
685
  if (entries) {
729
686
  const mappedEntries = [];
730
687
  for (const entry of entries) {
@@ -732,12 +689,12 @@ const _LiveMap = class extends AbstractCrdt {
732
689
  value._setParentLink(this, entry[0]);
733
690
  mappedEntries.push([entry[0], value]);
734
691
  }
735
- __privateSet$1(this, _map$1, new Map(mappedEntries));
692
+ this._map = new Map(mappedEntries);
736
693
  } else {
737
- __privateSet$1(this, _map$1, /* @__PURE__ */ new Map());
694
+ this._map = /* @__PURE__ */ new Map();
738
695
  }
739
696
  }
740
- _serialize(parentId, parentKey, doc) {
697
+ _serialize(parentId, parentKey, doc, intent) {
741
698
  if (this._id == null) {
742
699
  throw new Error("Cannot serialize item is not attached");
743
700
  }
@@ -749,11 +706,12 @@ const _LiveMap = class extends AbstractCrdt {
749
706
  id: this._id,
750
707
  opId: doc == null ? void 0 : doc.generateOpId(),
751
708
  type: OpType.CreateMap,
709
+ intent,
752
710
  parentId,
753
711
  parentKey
754
712
  };
755
713
  ops.push(op);
756
- for (const [key, value] of __privateGet$1(this, _map$1)) {
714
+ for (const [key, value] of this._map) {
757
715
  ops.push(...value._serialize(this._id, key, doc));
758
716
  }
759
717
  return ops;
@@ -762,7 +720,7 @@ const _LiveMap = class extends AbstractCrdt {
762
720
  if (item.type !== CrdtType.Map) {
763
721
  throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
764
722
  }
765
- const map = new _LiveMap();
723
+ const map = new LiveMap();
766
724
  map._attach(id, doc);
767
725
  const children = parentToChildren.get(id);
768
726
  if (children == null) {
@@ -775,26 +733,29 @@ const _LiveMap = class extends AbstractCrdt {
775
733
  }
776
734
  const child = deserialize(entry, parentToChildren, doc);
777
735
  child._setParentLink(map, crdt.parentKey);
778
- __privateGet$1(map, _map$1).set(crdt.parentKey, child);
736
+ map._map.set(crdt.parentKey, child);
779
737
  }
780
738
  return map;
781
739
  }
782
740
  _attach(id, doc) {
783
741
  super._attach(id, doc);
784
- for (const [_key, value] of __privateGet$1(this, _map$1)) {
742
+ for (const [_key, value] of this._map) {
785
743
  if (isCrdt(value)) {
786
744
  value._attach(doc.generateId(), doc);
787
745
  }
788
746
  }
789
747
  }
790
- _attachChild(id, key, child, _opId, _isLocal) {
748
+ _attachChild(op, _isLocal) {
791
749
  if (this._doc == null) {
792
750
  throw new Error("Can't attach child if doc is not present");
793
751
  }
752
+ const { id, parentKey } = op;
753
+ const key = parentKey;
754
+ const child = creationOpToLiveStructure(op);
794
755
  if (this._doc.getItem(id) !== void 0) {
795
756
  return { modified: false };
796
757
  }
797
- const previousValue = __privateGet$1(this, _map$1).get(key);
758
+ const previousValue = this._map.get(key);
798
759
  let reverse;
799
760
  if (previousValue) {
800
761
  reverse = previousValue._serialize(this._id, key);
@@ -804,7 +765,7 @@ const _LiveMap = class extends AbstractCrdt {
804
765
  }
805
766
  child._setParentLink(this, key);
806
767
  child._attach(id, this._doc);
807
- __privateGet$1(this, _map$1).set(key, child);
768
+ this._map.set(key, child);
808
769
  return {
809
770
  modified: {
810
771
  node: this,
@@ -816,15 +777,15 @@ const _LiveMap = class extends AbstractCrdt {
816
777
  }
817
778
  _detach() {
818
779
  super._detach();
819
- for (const item of __privateGet$1(this, _map$1).values()) {
780
+ for (const item of this._map.values()) {
820
781
  item._detach();
821
782
  }
822
783
  }
823
784
  _detachChild(child) {
824
785
  const reverse = child._serialize(this._id, child._parentKey, this._doc);
825
- for (const [key, value] of __privateGet$1(this, _map$1)) {
786
+ for (const [key, value] of this._map) {
826
787
  if (value === child) {
827
- __privateGet$1(this, _map$1).delete(key);
788
+ this._map.delete(key);
828
789
  }
829
790
  }
830
791
  child._detach();
@@ -844,20 +805,20 @@ const _LiveMap = class extends AbstractCrdt {
844
805
  };
845
806
  }
846
807
  get(key) {
847
- const value = __privateGet$1(this, _map$1).get(key);
808
+ const value = this._map.get(key);
848
809
  if (value == void 0) {
849
810
  return void 0;
850
811
  }
851
812
  return selfOrRegisterValue(value);
852
813
  }
853
814
  set(key, value) {
854
- const oldValue = __privateGet$1(this, _map$1).get(key);
815
+ const oldValue = this._map.get(key);
855
816
  if (oldValue) {
856
817
  oldValue._detach();
857
818
  }
858
819
  const item = selfOrRegister(value);
859
820
  item._setParentLink(this, key);
860
- __privateGet$1(this, _map$1).set(key, item);
821
+ this._map.set(key, item);
861
822
  if (this._doc && this._id) {
862
823
  const id = this._doc.generateId();
863
824
  item._attach(id, this._doc);
@@ -871,18 +832,18 @@ const _LiveMap = class extends AbstractCrdt {
871
832
  }
872
833
  }
873
834
  get size() {
874
- return __privateGet$1(this, _map$1).size;
835
+ return this._map.size;
875
836
  }
876
837
  has(key) {
877
- return __privateGet$1(this, _map$1).has(key);
838
+ return this._map.has(key);
878
839
  }
879
840
  delete(key) {
880
- const item = __privateGet$1(this, _map$1).get(key);
841
+ const item = this._map.get(key);
881
842
  if (item == null) {
882
843
  return false;
883
844
  }
884
845
  item._detach();
885
- __privateGet$1(this, _map$1).delete(key);
846
+ this._map.delete(key);
886
847
  if (this._doc && item._id) {
887
848
  const storageUpdates = /* @__PURE__ */ new Map();
888
849
  storageUpdates.set(this._id, {
@@ -901,7 +862,7 @@ const _LiveMap = class extends AbstractCrdt {
901
862
  return true;
902
863
  }
903
864
  entries() {
904
- const innerIterator = __privateGet$1(this, _map$1).entries();
865
+ const innerIterator = this._map.entries();
905
866
  return {
906
867
  [Symbol.iterator]: function() {
907
868
  return this;
@@ -925,10 +886,10 @@ const _LiveMap = class extends AbstractCrdt {
925
886
  return this.entries();
926
887
  }
927
888
  keys() {
928
- return __privateGet$1(this, _map$1).keys();
889
+ return this._map.keys();
929
890
  }
930
891
  values() {
931
- const innerIterator = __privateGet$1(this, _map$1).values();
892
+ const innerIterator = this._map.values();
932
893
  return {
933
894
  [Symbol.iterator]: function() {
934
895
  return this;
@@ -952,9 +913,21 @@ const _LiveMap = class extends AbstractCrdt {
952
913
  callback(entry[1], entry[0], this);
953
914
  }
954
915
  }
955
- };
956
- let LiveMap = _LiveMap;
957
- _map$1 = new WeakMap();
916
+ }
917
+
918
+ function parseJson(rawMessage) {
919
+ try {
920
+ return JSON.parse(rawMessage);
921
+ } catch (e) {
922
+ return void 0;
923
+ }
924
+ }
925
+ function isJsonArray(data) {
926
+ return Array.isArray(data);
927
+ }
928
+ function isJsonObject(data) {
929
+ return data !== null && typeof data === "object" && !isJsonArray(data);
930
+ }
958
931
 
959
932
  var __defProp$2 = Object.defineProperty;
960
933
  var __defProps$2 = Object.defineProperties;
@@ -983,6 +956,21 @@ function remove(array, item) {
983
956
  }
984
957
  }
985
958
  }
959
+ function compact(items) {
960
+ return items.filter((item) => item != null);
961
+ }
962
+ function creationOpToLiveStructure(op) {
963
+ switch (op.type) {
964
+ case OpType.CreateRegister:
965
+ return new LiveRegister(op.data);
966
+ case OpType.CreateObject:
967
+ return new LiveObject(op.data);
968
+ case OpType.CreateMap:
969
+ return new LiveMap();
970
+ case OpType.CreateList:
971
+ return new LiveList();
972
+ }
973
+ }
986
974
  function isSameNodeOrChildOf(node, parent) {
987
975
  if (node === parent) {
988
976
  return true;
@@ -1099,32 +1087,41 @@ function getTreesDiffOperations(currentItems, newItems) {
1099
1087
  });
1100
1088
  return ops;
1101
1089
  }
1090
+ function mergeObjectStorageUpdates(first, second) {
1091
+ const updates = first.updates;
1092
+ for (const [key, value] of entries(second.updates)) {
1093
+ updates[key] = value;
1094
+ }
1095
+ return __spreadProps$2(__spreadValues$2({}, second), {
1096
+ updates
1097
+ });
1098
+ }
1099
+ function mergeMapStorageUpdates(first, second) {
1100
+ const updates = first.updates;
1101
+ for (const [key, value] of entries(second.updates)) {
1102
+ updates[key] = value;
1103
+ }
1104
+ return __spreadProps$2(__spreadValues$2({}, second), {
1105
+ updates
1106
+ });
1107
+ }
1108
+ function mergeListStorageUpdates(first, second) {
1109
+ const updates = first.updates;
1110
+ return __spreadProps$2(__spreadValues$2({}, second), {
1111
+ updates: updates.concat(second.updates)
1112
+ });
1113
+ }
1102
1114
  function mergeStorageUpdates(first, second) {
1103
1115
  if (!first) {
1104
1116
  return second;
1105
1117
  }
1106
- if (second.type === "LiveObject") {
1107
- const updates = first.updates;
1108
- for (const [key, value] of Object.entries(second.updates)) {
1109
- updates[key] = value;
1110
- }
1111
- return __spreadProps$2(__spreadValues$2({}, second), {
1112
- updates
1113
- });
1114
- } else if (second.type === "LiveMap") {
1115
- const updates = first.updates;
1116
- for (const [key, value] of Object.entries(second.updates)) {
1117
- updates[key] = value;
1118
- }
1119
- return __spreadProps$2(__spreadValues$2({}, second), {
1120
- updates
1121
- });
1122
- } else if (second.type === "LiveList") {
1123
- const updates = first.updates;
1124
- return __spreadProps$2(__spreadValues$2({}, second), {
1125
- updates: updates.concat(second.updates)
1126
- });
1127
- }
1118
+ if (first.type === "LiveObject" && second.type === "LiveObject") {
1119
+ return mergeObjectStorageUpdates(first, second);
1120
+ } else if (first.type === "LiveMap" && second.type === "LiveMap") {
1121
+ return mergeMapStorageUpdates(first, second);
1122
+ } else if (first.type === "LiveList" && second.type === "LiveList") {
1123
+ return mergeListStorageUpdates(first, second);
1124
+ } else ;
1128
1125
  return second;
1129
1126
  }
1130
1127
  function isPlain(value) {
@@ -1170,46 +1167,38 @@ function findNonSerializableValue(value, path = "") {
1170
1167
  }
1171
1168
  return false;
1172
1169
  }
1170
+ function isTokenValid(token) {
1171
+ const tokenParts = token.split(".");
1172
+ if (tokenParts.length !== 3) {
1173
+ return false;
1174
+ }
1175
+ const data = parseJson(atob(tokenParts[1]));
1176
+ if (data === void 0 || !isJsonObject(data) || typeof data.exp !== "number") {
1177
+ return false;
1178
+ }
1179
+ const now = Date.now();
1180
+ if (now / 1e3 > data.exp - 300) {
1181
+ return false;
1182
+ }
1183
+ return true;
1184
+ }
1185
+ function entries(obj) {
1186
+ return Object.entries(obj);
1187
+ }
1173
1188
 
1174
- var __accessCheck = (obj, member, msg) => {
1175
- if (!member.has(obj))
1176
- throw TypeError("Cannot " + msg);
1177
- };
1178
- var __privateGet = (obj, member, getter) => {
1179
- __accessCheck(obj, member, "read from private field");
1180
- return getter ? getter.call(obj) : member.get(obj);
1181
- };
1182
- var __privateAdd = (obj, member, value) => {
1183
- if (member.has(obj))
1184
- throw TypeError("Cannot add the same private member more than once");
1185
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1186
- };
1187
- var __privateSet = (obj, member, value, setter) => {
1188
- __accessCheck(obj, member, "write to private field");
1189
- setter ? setter.call(obj, value) : member.set(obj, value);
1190
- return value;
1191
- };
1192
- var __privateMethod = (obj, member, method) => {
1193
- __accessCheck(obj, member, "access private method");
1194
- return method;
1195
- };
1196
- var _map, _propToLastUpdate, _applyUpdate, applyUpdate_fn, _applyDeleteObjectKey, applyDeleteObjectKey_fn;
1197
- const _LiveObject = class extends AbstractCrdt {
1189
+ class LiveObject extends AbstractCrdt {
1198
1190
  constructor(object = {}) {
1199
1191
  super();
1200
- __privateAdd(this, _applyUpdate);
1201
- __privateAdd(this, _applyDeleteObjectKey);
1202
- __privateAdd(this, _map, void 0);
1203
- __privateAdd(this, _propToLastUpdate, /* @__PURE__ */ new Map());
1192
+ this._propToLastUpdate = /* @__PURE__ */ new Map();
1204
1193
  for (const key in object) {
1205
1194
  const value = object[key];
1206
1195
  if (value instanceof AbstractCrdt) {
1207
1196
  value._setParentLink(this, key);
1208
1197
  }
1209
1198
  }
1210
- __privateSet(this, _map, new Map(Object.entries(object)));
1199
+ this._map = new Map(Object.entries(object));
1211
1200
  }
1212
- _serialize(parentId, parentKey, doc) {
1201
+ _serialize(parentId, parentKey, doc, intent) {
1213
1202
  if (this._id == null) {
1214
1203
  throw new Error("Cannot serialize item is not attached");
1215
1204
  }
@@ -1217,13 +1206,14 @@ const _LiveObject = class extends AbstractCrdt {
1217
1206
  const op = {
1218
1207
  id: this._id,
1219
1208
  opId: doc == null ? void 0 : doc.generateOpId(),
1209
+ intent,
1220
1210
  type: OpType.CreateObject,
1221
1211
  parentId,
1222
1212
  parentKey,
1223
1213
  data: {}
1224
1214
  };
1225
1215
  ops.push(op);
1226
- for (const [key, value] of __privateGet(this, _map)) {
1216
+ for (const [key, value] of this._map) {
1227
1217
  if (value instanceof AbstractCrdt) {
1228
1218
  ops.push(...value._serialize(this._id, key, doc));
1229
1219
  } else {
@@ -1236,7 +1226,7 @@ const _LiveObject = class extends AbstractCrdt {
1236
1226
  if (item.type !== CrdtType.Object) {
1237
1227
  throw new Error(`Tried to deserialize a record but item type is "${item.type}"`);
1238
1228
  }
1239
- const object = new _LiveObject(item.data);
1229
+ const object = new LiveObject(item.data);
1240
1230
  object._attach(id, doc);
1241
1231
  return this._deserializeChildren(object, parentToChildren, doc);
1242
1232
  }
@@ -1252,37 +1242,40 @@ const _LiveObject = class extends AbstractCrdt {
1252
1242
  }
1253
1243
  const child = deserialize(entry, parentToChildren, doc);
1254
1244
  child._setParentLink(object, crdt.parentKey);
1255
- __privateGet(object, _map).set(crdt.parentKey, child);
1245
+ object._map.set(crdt.parentKey, child);
1256
1246
  }
1257
1247
  return object;
1258
1248
  }
1259
1249
  _attach(id, doc) {
1260
1250
  super._attach(id, doc);
1261
- for (const [_key, value] of __privateGet(this, _map)) {
1251
+ for (const [_key, value] of this._map) {
1262
1252
  if (value instanceof AbstractCrdt) {
1263
1253
  value._attach(doc.generateId(), doc);
1264
1254
  }
1265
1255
  }
1266
1256
  }
1267
- _attachChild(id, key, child, opId, isLocal) {
1257
+ _attachChild(op, isLocal) {
1268
1258
  if (this._doc == null) {
1269
1259
  throw new Error("Can't attach child if doc is not present");
1270
1260
  }
1261
+ const { id, parentKey, opId } = op;
1262
+ const key = parentKey;
1263
+ const child = creationOpToLiveStructure(op);
1271
1264
  if (this._doc.getItem(id) !== void 0) {
1272
- if (__privateGet(this, _propToLastUpdate).get(key) === opId) {
1273
- __privateGet(this, _propToLastUpdate).delete(key);
1265
+ if (this._propToLastUpdate.get(key) === opId) {
1266
+ this._propToLastUpdate.delete(key);
1274
1267
  }
1275
1268
  return { modified: false };
1276
1269
  }
1277
1270
  if (isLocal) {
1278
- __privateGet(this, _propToLastUpdate).set(key, opId);
1279
- } else if (__privateGet(this, _propToLastUpdate).get(key) === void 0) ; else if (__privateGet(this, _propToLastUpdate).get(key) === opId) {
1280
- __privateGet(this, _propToLastUpdate).delete(key);
1271
+ this._propToLastUpdate.set(key, opId);
1272
+ } else if (this._propToLastUpdate.get(key) === void 0) ; else if (this._propToLastUpdate.get(key) === opId) {
1273
+ this._propToLastUpdate.delete(key);
1281
1274
  return { modified: false };
1282
1275
  } else {
1283
1276
  return { modified: false };
1284
1277
  }
1285
- const previousValue = __privateGet(this, _map).get(key);
1278
+ const previousValue = this._map.get(key);
1286
1279
  let reverse;
1287
1280
  if (isCrdt(previousValue)) {
1288
1281
  reverse = previousValue._serialize(this._id, key);
@@ -1300,7 +1293,7 @@ const _LiveObject = class extends AbstractCrdt {
1300
1293
  }
1301
1294
  ];
1302
1295
  }
1303
- __privateGet(this, _map).set(key, child);
1296
+ this._map.set(key, child);
1304
1297
  child._setParentLink(this, key);
1305
1298
  child._attach(id, this._doc);
1306
1299
  return {
@@ -1315,9 +1308,9 @@ const _LiveObject = class extends AbstractCrdt {
1315
1308
  _detachChild(child) {
1316
1309
  if (child) {
1317
1310
  const reverse = child._serialize(this._id, child._parentKey, this._doc);
1318
- for (const [key, value] of __privateGet(this, _map)) {
1311
+ for (const [key, value] of this._map) {
1319
1312
  if (value === child) {
1320
- __privateGet(this, _map).delete(key);
1313
+ this._map.delete(key);
1321
1314
  }
1322
1315
  }
1323
1316
  child._detach();
@@ -1333,14 +1326,14 @@ const _LiveObject = class extends AbstractCrdt {
1333
1326
  return { modified: false };
1334
1327
  }
1335
1328
  _detachChildren() {
1336
- for (const [key, value] of __privateGet(this, _map)) {
1337
- __privateGet(this, _map).delete(key);
1329
+ for (const [key, value] of this._map) {
1330
+ this._map.delete(key);
1338
1331
  value._detach();
1339
1332
  }
1340
1333
  }
1341
1334
  _detach() {
1342
1335
  super._detach();
1343
- for (const value of __privateGet(this, _map).values()) {
1336
+ for (const value of this._map.values()) {
1344
1337
  if (isCrdt(value)) {
1345
1338
  value._detach();
1346
1339
  }
@@ -1348,33 +1341,123 @@ const _LiveObject = class extends AbstractCrdt {
1348
1341
  }
1349
1342
  _apply(op, isLocal) {
1350
1343
  if (op.type === OpType.UpdateObject) {
1351
- return __privateMethod(this, _applyUpdate, applyUpdate_fn).call(this, op, isLocal);
1344
+ return this._applyUpdate(op, isLocal);
1352
1345
  } else if (op.type === OpType.DeleteObjectKey) {
1353
- return __privateMethod(this, _applyDeleteObjectKey, applyDeleteObjectKey_fn).call(this, op);
1346
+ return this._applyDeleteObjectKey(op);
1354
1347
  }
1355
1348
  return super._apply(op, isLocal);
1356
1349
  }
1357
1350
  _toSerializedCrdt() {
1358
1351
  var _a;
1352
+ const data = {};
1353
+ for (const [key, value] of this._map) {
1354
+ if (value instanceof AbstractCrdt === false) {
1355
+ data[key] = value;
1356
+ }
1357
+ }
1359
1358
  return {
1360
1359
  type: CrdtType.Object,
1361
1360
  parentId: (_a = this._parent) == null ? void 0 : _a._id,
1362
1361
  parentKey: this._parentKey,
1363
- data: this.toObject()
1362
+ data
1363
+ };
1364
+ }
1365
+ _applyUpdate(op, isLocal) {
1366
+ let isModified = false;
1367
+ const reverse = [];
1368
+ const reverseUpdate = {
1369
+ type: OpType.UpdateObject,
1370
+ id: this._id,
1371
+ data: {}
1372
+ };
1373
+ reverse.push(reverseUpdate);
1374
+ for (const key in op.data) {
1375
+ const oldValue = this._map.get(key);
1376
+ if (oldValue instanceof AbstractCrdt) {
1377
+ reverse.push(...oldValue._serialize(this._id, key));
1378
+ oldValue._detach();
1379
+ } else if (oldValue !== void 0) {
1380
+ reverseUpdate.data[key] = oldValue;
1381
+ } else if (oldValue === void 0) {
1382
+ reverse.push({ type: OpType.DeleteObjectKey, id: this._id, key });
1383
+ }
1384
+ }
1385
+ const updateDelta = {};
1386
+ for (const key in op.data) {
1387
+ if (isLocal) {
1388
+ this._propToLastUpdate.set(key, op.opId);
1389
+ } else if (this._propToLastUpdate.get(key) == null) {
1390
+ isModified = true;
1391
+ } else if (this._propToLastUpdate.get(key) === op.opId) {
1392
+ this._propToLastUpdate.delete(key);
1393
+ continue;
1394
+ } else {
1395
+ continue;
1396
+ }
1397
+ const oldValue = this._map.get(key);
1398
+ if (isCrdt(oldValue)) {
1399
+ oldValue._detach();
1400
+ }
1401
+ isModified = true;
1402
+ updateDelta[key] = { type: "update" };
1403
+ this._map.set(key, op.data[key]);
1404
+ }
1405
+ if (Object.keys(reverseUpdate.data).length !== 0) {
1406
+ reverse.unshift(reverseUpdate);
1407
+ }
1408
+ return isModified ? {
1409
+ modified: {
1410
+ node: this,
1411
+ type: "LiveObject",
1412
+ updates: updateDelta
1413
+ },
1414
+ reverse
1415
+ } : { modified: false };
1416
+ }
1417
+ _applyDeleteObjectKey(op) {
1418
+ const key = op.key;
1419
+ if (this._map.has(key) === false) {
1420
+ return { modified: false };
1421
+ }
1422
+ if (this._propToLastUpdate.get(key) !== void 0) {
1423
+ return { modified: false };
1424
+ }
1425
+ const oldValue = this._map.get(key);
1426
+ let reverse = [];
1427
+ if (isCrdt(oldValue)) {
1428
+ reverse = oldValue._serialize(this._id, op.key);
1429
+ oldValue._detach();
1430
+ } else if (oldValue !== void 0) {
1431
+ reverse = [
1432
+ {
1433
+ type: OpType.UpdateObject,
1434
+ id: this._id,
1435
+ data: { [key]: oldValue }
1436
+ }
1437
+ ];
1438
+ }
1439
+ this._map.delete(key);
1440
+ return {
1441
+ modified: {
1442
+ node: this,
1443
+ type: "LiveObject",
1444
+ updates: { [op.key]: { type: "delete" } }
1445
+ },
1446
+ reverse
1364
1447
  };
1365
1448
  }
1366
1449
  toObject() {
1367
- return Object.fromEntries(__privateGet(this, _map));
1450
+ return Object.fromEntries(this._map);
1368
1451
  }
1369
1452
  set(key, value) {
1370
1453
  this.update({ [key]: value });
1371
1454
  }
1372
1455
  get(key) {
1373
- return __privateGet(this, _map).get(key);
1456
+ return this._map.get(key);
1374
1457
  }
1375
1458
  delete(key) {
1376
1459
  const keyAsString = key;
1377
- const oldValue = __privateGet(this, _map).get(keyAsString);
1460
+ const oldValue = this._map.get(keyAsString);
1378
1461
  if (oldValue === void 0) {
1379
1462
  return;
1380
1463
  }
@@ -1382,7 +1465,7 @@ const _LiveObject = class extends AbstractCrdt {
1382
1465
  if (oldValue instanceof AbstractCrdt) {
1383
1466
  oldValue._detach();
1384
1467
  }
1385
- __privateGet(this, _map).delete(keyAsString);
1468
+ this._map.delete(keyAsString);
1386
1469
  return;
1387
1470
  }
1388
1471
  let reverse;
@@ -1398,7 +1481,7 @@ const _LiveObject = class extends AbstractCrdt {
1398
1481
  }
1399
1482
  ];
1400
1483
  }
1401
- __privateGet(this, _map).delete(keyAsString);
1484
+ this._map.delete(keyAsString);
1402
1485
  const storageUpdates = /* @__PURE__ */ new Map();
1403
1486
  storageUpdates.set(this._id, {
1404
1487
  node: this,
@@ -1417,7 +1500,7 @@ const _LiveObject = class extends AbstractCrdt {
1417
1500
  update(overrides) {
1418
1501
  if (this._doc == null || this._id == null) {
1419
1502
  for (const key in overrides) {
1420
- const oldValue = __privateGet(this, _map).get(key);
1503
+ const oldValue = this._map.get(key);
1421
1504
  if (oldValue instanceof AbstractCrdt) {
1422
1505
  oldValue._detach();
1423
1506
  }
@@ -1425,7 +1508,7 @@ const _LiveObject = class extends AbstractCrdt {
1425
1508
  if (newValue instanceof AbstractCrdt) {
1426
1509
  newValue._setParentLink(this, key);
1427
1510
  }
1428
- __privateGet(this, _map).set(key, newValue);
1511
+ this._map.set(key, newValue);
1429
1512
  }
1430
1513
  return;
1431
1514
  }
@@ -1440,7 +1523,7 @@ const _LiveObject = class extends AbstractCrdt {
1440
1523
  };
1441
1524
  const updateDelta = {};
1442
1525
  for (const key in overrides) {
1443
- const oldValue = __privateGet(this, _map).get(key);
1526
+ const oldValue = this._map.get(key);
1444
1527
  if (oldValue instanceof AbstractCrdt) {
1445
1528
  reverseOps.push(...oldValue._serialize(this._id, key));
1446
1529
  oldValue._detach();
@@ -1456,14 +1539,14 @@ const _LiveObject = class extends AbstractCrdt {
1456
1539
  const newAttachChildOps = newValue._serialize(this._id, key, this._doc);
1457
1540
  const createCrdtOp = newAttachChildOps.find((op) => op.parentId === this._id);
1458
1541
  if (createCrdtOp) {
1459
- __privateGet(this, _propToLastUpdate).set(key, createCrdtOp.opId);
1542
+ this._propToLastUpdate.set(key, createCrdtOp.opId);
1460
1543
  }
1461
1544
  ops.push(...newAttachChildOps);
1462
1545
  } else {
1463
1546
  updatedProps[key] = newValue;
1464
- __privateGet(this, _propToLastUpdate).set(key, opId);
1547
+ this._propToLastUpdate.set(key, opId);
1465
1548
  }
1466
- __privateGet(this, _map).set(key, newValue);
1549
+ this._map.set(key, newValue);
1467
1550
  updateDelta[key] = { type: "update" };
1468
1551
  }
1469
1552
  if (Object.keys(reverseUpdateOp.data).length !== 0) {
@@ -1485,96 +1568,7 @@ const _LiveObject = class extends AbstractCrdt {
1485
1568
  });
1486
1569
  this._doc.dispatch(ops, reverseOps, storageUpdates);
1487
1570
  }
1488
- };
1489
- let LiveObject = _LiveObject;
1490
- _map = new WeakMap();
1491
- _propToLastUpdate = new WeakMap();
1492
- _applyUpdate = new WeakSet();
1493
- applyUpdate_fn = function(op, isLocal) {
1494
- let isModified = false;
1495
- const reverse = [];
1496
- const reverseUpdate = {
1497
- type: OpType.UpdateObject,
1498
- id: this._id,
1499
- data: {}
1500
- };
1501
- reverse.push(reverseUpdate);
1502
- for (const key in op.data) {
1503
- const oldValue = __privateGet(this, _map).get(key);
1504
- if (oldValue instanceof AbstractCrdt) {
1505
- reverse.push(...oldValue._serialize(this._id, key));
1506
- oldValue._detach();
1507
- } else if (oldValue !== void 0) {
1508
- reverseUpdate.data[key] = oldValue;
1509
- } else if (oldValue === void 0) {
1510
- reverse.push({ type: OpType.DeleteObjectKey, id: this._id, key });
1511
- }
1512
- }
1513
- const updateDelta = {};
1514
- for (const key in op.data) {
1515
- if (isLocal) {
1516
- __privateGet(this, _propToLastUpdate).set(key, op.opId);
1517
- } else if (__privateGet(this, _propToLastUpdate).get(key) == null) {
1518
- isModified = true;
1519
- } else if (__privateGet(this, _propToLastUpdate).get(key) === op.opId) {
1520
- __privateGet(this, _propToLastUpdate).delete(key);
1521
- continue;
1522
- } else {
1523
- continue;
1524
- }
1525
- const oldValue = __privateGet(this, _map).get(key);
1526
- if (isCrdt(oldValue)) {
1527
- oldValue._detach();
1528
- }
1529
- isModified = true;
1530
- updateDelta[key] = { type: "update" };
1531
- __privateGet(this, _map).set(key, op.data[key]);
1532
- }
1533
- if (Object.keys(reverseUpdate.data).length !== 0) {
1534
- reverse.unshift(reverseUpdate);
1535
- }
1536
- return isModified ? {
1537
- modified: {
1538
- node: this,
1539
- type: "LiveObject",
1540
- updates: updateDelta
1541
- },
1542
- reverse
1543
- } : { modified: false };
1544
- };
1545
- _applyDeleteObjectKey = new WeakSet();
1546
- applyDeleteObjectKey_fn = function(op) {
1547
- const key = op.key;
1548
- if (__privateGet(this, _map).has(key) === false) {
1549
- return { modified: false };
1550
- }
1551
- if (__privateGet(this, _propToLastUpdate).get(key) !== void 0) {
1552
- return { modified: false };
1553
- }
1554
- const oldValue = __privateGet(this, _map).get(key);
1555
- let reverse = [];
1556
- if (isCrdt(oldValue)) {
1557
- reverse = oldValue._serialize(this._id, op.key);
1558
- oldValue._detach();
1559
- } else if (oldValue !== void 0) {
1560
- reverse = [
1561
- {
1562
- type: OpType.UpdateObject,
1563
- id: this._id,
1564
- data: { [key]: oldValue }
1565
- }
1566
- ];
1567
- }
1568
- __privateGet(this, _map).delete(key);
1569
- return {
1570
- modified: {
1571
- node: this,
1572
- type: "LiveObject",
1573
- updates: { [op.key]: { type: "delete" } }
1574
- },
1575
- reverse
1576
- };
1577
- };
1571
+ }
1578
1572
 
1579
1573
  var __defProp$1 = Object.defineProperty;
1580
1574
  var __defProps$1 = Object.defineProperties;
@@ -1641,14 +1635,22 @@ function makeOthers(userMap) {
1641
1635
  function makeStateMachine(state, context, mockedEffects) {
1642
1636
  const effects = mockedEffects || {
1643
1637
  authenticate(auth, createWebSocket) {
1644
- return auth(context.room).then(({ token }) => {
1645
- if (state.connection.state !== "authenticating") {
1646
- return;
1647
- }
1638
+ const token = state.token;
1639
+ if (token && isTokenValid(token)) {
1648
1640
  const parsedToken = parseToken(token);
1649
1641
  const socket = createWebSocket(token);
1650
1642
  authenticationSuccess(parsedToken, socket);
1651
- }).catch((er) => authenticationFailure(er));
1643
+ } else {
1644
+ return auth(context.roomId).then(({ token: token2 }) => {
1645
+ if (state.connection.state !== "authenticating") {
1646
+ return;
1647
+ }
1648
+ const parsedToken = parseToken(token2);
1649
+ const socket = createWebSocket(token2);
1650
+ authenticationSuccess(parsedToken, socket);
1651
+ state.token = token2;
1652
+ }).catch((er) => authenticationFailure(er));
1653
+ }
1652
1654
  },
1653
1655
  send(messageOrMessages) {
1654
1656
  if (state.socket == null) {
@@ -1746,7 +1748,7 @@ function makeStateMachine(state, context, mockedEffects) {
1746
1748
  generateId,
1747
1749
  generateOpId,
1748
1750
  dispatch: storageDispatch,
1749
- roomId: context.room
1751
+ roomId: context.roomId
1750
1752
  });
1751
1753
  }
1752
1754
  function addItem(id, item) {
@@ -1889,33 +1891,15 @@ function makeStateMachine(state, context, mockedEffects) {
1889
1891
  }
1890
1892
  return { modified: false };
1891
1893
  }
1892
- case OpType.CreateObject: {
1893
- const parent = state.items.get(op.parentId);
1894
- if (parent == null) {
1895
- return { modified: false };
1896
- }
1897
- return parent._attachChild(op.id, op.parentKey, new LiveObject(op.data), op.opId, isLocal);
1898
- }
1899
- case OpType.CreateList: {
1900
- const parent = state.items.get(op.parentId);
1901
- if (parent == null) {
1902
- return { modified: false };
1903
- }
1904
- return parent._attachChild(op.id, op.parentKey, new LiveList(), op.opId, isLocal);
1905
- }
1894
+ case OpType.CreateObject:
1895
+ case OpType.CreateList:
1896
+ case OpType.CreateMap:
1906
1897
  case OpType.CreateRegister: {
1907
1898
  const parent = state.items.get(op.parentId);
1908
1899
  if (parent == null) {
1909
1900
  return { modified: false };
1910
1901
  }
1911
- return parent._attachChild(op.id, op.parentKey, new LiveRegister(op.data), op.opId, isLocal);
1912
- }
1913
- case OpType.CreateMap: {
1914
- const parent = state.items.get(op.parentId);
1915
- if (parent == null) {
1916
- return { modified: false };
1917
- }
1918
- return parent._attachChild(op.id, op.parentKey, new LiveMap(), op.opId, isLocal);
1902
+ return parent._attachChild(op, isLocal);
1919
1903
  }
1920
1904
  }
1921
1905
  return { modified: false };
@@ -2006,6 +1990,7 @@ See v0.13 release notes for more information.
2006
1990
  if (process.env.NODE_ENV !== "production") {
2007
1991
  console.error("Call to authentication endpoint failed", error);
2008
1992
  }
1993
+ state.token = null;
2009
1994
  updateConnection({ state: "unavailable" });
2010
1995
  state.numberOfRetry++;
2011
1996
  state.timeoutHandles.reconnect = effects.scheduleReconnect(getRetryDelay());
@@ -2091,58 +2076,72 @@ See v0.13 release notes for more information.
2091
2076
  }
2092
2077
  return { type: "enter", user: state.users[message.actor] };
2093
2078
  }
2079
+ function parseServerMessage(data) {
2080
+ if (!isJsonObject(data)) {
2081
+ return null;
2082
+ }
2083
+ return data;
2084
+ }
2085
+ function parseServerMessages(text) {
2086
+ const data = parseJson(text);
2087
+ if (data === void 0) {
2088
+ return null;
2089
+ } else if (isJsonArray(data)) {
2090
+ return compact(data.map((item) => parseServerMessage(item)));
2091
+ } else {
2092
+ return compact([parseServerMessage(data)]);
2093
+ }
2094
+ }
2094
2095
  function onMessage(event) {
2095
2096
  if (event.data === "pong") {
2096
2097
  clearTimeout(state.timeoutHandles.pongTimeout);
2097
2098
  return;
2098
2099
  }
2099
- const message = JSON.parse(event.data);
2100
- let subMessages = [];
2101
- if (Array.isArray(message)) {
2102
- subMessages = message;
2103
- } else {
2104
- subMessages.push(message);
2100
+ const messages = parseServerMessages(event.data);
2101
+ if (messages === null || messages.length === 0) {
2102
+ return;
2105
2103
  }
2106
2104
  const updates = {
2107
2105
  storageUpdates: /* @__PURE__ */ new Map(),
2108
2106
  others: []
2109
2107
  };
2110
- for (const subMessage of subMessages) {
2111
- switch (subMessage.type) {
2108
+ for (const message of messages) {
2109
+ switch (message.type) {
2112
2110
  case ServerMessageType.UserJoined: {
2113
2111
  updates.others.push(onUserJoinedMessage(message));
2114
2112
  break;
2115
2113
  }
2116
2114
  case ServerMessageType.UpdatePresence: {
2117
- const othersPresenceUpdate = onUpdatePresenceMessage(subMessage);
2115
+ const othersPresenceUpdate = onUpdatePresenceMessage(message);
2118
2116
  if (othersPresenceUpdate) {
2119
2117
  updates.others.push(othersPresenceUpdate);
2120
2118
  }
2121
2119
  break;
2122
2120
  }
2123
2121
  case ServerMessageType.Event: {
2124
- onEvent(subMessage);
2122
+ onEvent(message);
2125
2123
  break;
2126
2124
  }
2127
2125
  case ServerMessageType.UserLeft: {
2128
- const event2 = onUserLeftMessage(subMessage);
2126
+ const event2 = onUserLeftMessage(message);
2129
2127
  if (event2) {
2130
2128
  updates.others.push(event2);
2131
2129
  }
2132
2130
  break;
2133
2131
  }
2134
2132
  case ServerMessageType.RoomState: {
2135
- updates.others.push(onRoomStateMessage(subMessage));
2133
+ updates.others.push(onRoomStateMessage(message));
2136
2134
  break;
2137
2135
  }
2138
2136
  case ServerMessageType.InitialStorageState: {
2139
- createOrUpdateRootFromMessage(subMessage);
2140
- applyAndSendOfflineOps();
2137
+ const offlineOps = new Map(state.offlineOperations);
2138
+ createOrUpdateRootFromMessage(message);
2139
+ applyAndSendOfflineOps(offlineOps);
2141
2140
  _getInitialStateResolver == null ? void 0 : _getInitialStateResolver();
2142
2141
  break;
2143
2142
  }
2144
2143
  case ServerMessageType.UpdateStorage: {
2145
- const applyResult = apply(subMessage.ops, false);
2144
+ const applyResult = apply(message.ops, false);
2146
2145
  applyResult.updates.storageUpdates.forEach((value, key) => {
2147
2146
  updates.storageUpdates.set(key, mergeStorageUpdates(updates.storageUpdates.get(key), value));
2148
2147
  });
@@ -2249,12 +2248,12 @@ See v0.13 release notes for more information.
2249
2248
  clearInterval(state.intervalHandles.heartbeat);
2250
2249
  connect();
2251
2250
  }
2252
- function applyAndSendOfflineOps() {
2253
- if (state.offlineOperations.size === 0) {
2251
+ function applyAndSendOfflineOps(offlineOps) {
2252
+ if (offlineOps.size === 0) {
2254
2253
  return;
2255
2254
  }
2256
2255
  const messages = [];
2257
- const ops = Array.from(state.offlineOperations.values());
2256
+ const ops = Array.from(offlineOps.values());
2258
2257
  const result = apply(ops, true);
2259
2258
  messages.push({
2260
2259
  type: ClientMessageType.UpdateStorage,
@@ -2504,6 +2503,7 @@ See v0.13 release notes for more information.
2504
2503
  function defaultState(me, defaultStorageRoot) {
2505
2504
  return {
2506
2505
  connection: { state: "closed" },
2506
+ token: null,
2507
2507
  lastConnectionId: null,
2508
2508
  socket: null,
2509
2509
  listeners: {
@@ -2559,7 +2559,7 @@ function createRoom(options, context) {
2559
2559
  const state = defaultState(options.defaultPresence, options.defaultStorageRoot);
2560
2560
  const machine = makeStateMachine(state, context);
2561
2561
  const room = {
2562
- id: context.room,
2562
+ id: context.roomId,
2563
2563
  getConnectionState: machine.selectors.getConnectionState,
2564
2564
  getSelf: machine.selectors.getSelf,
2565
2565
  subscribe: machine.subscribe,
@@ -2600,11 +2600,15 @@ function parseToken(token) {
2600
2600
  if (tokenParts.length !== 3) {
2601
2601
  throw new Error(`Authentication error. Liveblocks could not parse the response of your authentication endpoint`);
2602
2602
  }
2603
- const data = JSON.parse(atob(tokenParts[1]));
2604
- if (typeof data.actor !== "number") {
2605
- throw new Error(`Authentication error. Liveblocks could not parse the response of your authentication endpoint`);
2603
+ const data = parseJson(atob(tokenParts[1]));
2604
+ if (data !== void 0 && isJsonObject(data) && typeof data.actor === "number" && (data.id === void 0 || typeof data.id === "string")) {
2605
+ return {
2606
+ actor: data.actor,
2607
+ id: data.id,
2608
+ info: data.info
2609
+ };
2606
2610
  }
2607
- return data;
2611
+ throw new Error(`Authentication error. Liveblocks could not parse the response of your authentication endpoint`);
2608
2612
  }
2609
2613
  function prepareCreateWebSocket(liveblocksServer, WebSocketPolyfill) {
2610
2614
  if (typeof window === "undefined" && WebSocketPolyfill == null) {
@@ -2682,7 +2686,7 @@ function createClient(options) {
2682
2686
  defaultPresence: options2.defaultPresence,
2683
2687
  defaultStorageRoot: options2.defaultStorageRoot
2684
2688
  }, {
2685
- room: roomId,
2689
+ roomId,
2686
2690
  throttleDelay,
2687
2691
  WebSocketPolyfill: clientOptions.WebSocketPolyfill,
2688
2692
  fetchPolyfill: clientOptions.fetchPolyfill,
@@ -2771,26 +2775,30 @@ var __spreadValues = (a, b) => {
2771
2775
  return a;
2772
2776
  };
2773
2777
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
2774
- function liveObjectToJson(liveObject) {
2778
+ function lsonObjectToJson(obj) {
2775
2779
  const result = {};
2776
- const obj = liveObject.toObject();
2777
2780
  for (const key in obj) {
2778
- result[key] = liveNodeToJson(obj[key]);
2781
+ result[key] = lsonToJson(obj[key]);
2779
2782
  }
2780
2783
  return result;
2781
2784
  }
2785
+ function liveObjectToJson(liveObject) {
2786
+ return lsonObjectToJson(liveObject.toObject());
2787
+ }
2782
2788
  function liveMapToJson(map) {
2783
2789
  const result = {};
2784
- const obj = Object.fromEntries(map);
2785
- for (const key in obj) {
2786
- result[key] = liveNodeToJson(obj[key]);
2790
+ for (const [key, value] of map.entries()) {
2791
+ result[key] = lsonToJson(value);
2787
2792
  }
2788
2793
  return result;
2789
2794
  }
2795
+ function lsonListToJson(value) {
2796
+ return value.map(lsonToJson);
2797
+ }
2790
2798
  function liveListToJson(value) {
2791
- return value.toArray().map(liveNodeToJson);
2799
+ return lsonListToJson(value.toArray());
2792
2800
  }
2793
- function liveNodeToJson(value) {
2801
+ function lsonToJson(value) {
2794
2802
  if (value instanceof LiveObject) {
2795
2803
  return liveObjectToJson(value);
2796
2804
  } else if (value instanceof LiveList) {
@@ -2799,11 +2807,18 @@ function liveNodeToJson(value) {
2799
2807
  return liveMapToJson(value);
2800
2808
  } else if (value instanceof LiveRegister) {
2801
2809
  return value.data;
2810
+ } else if (value instanceof AbstractCrdt) {
2811
+ throw new Error("Unhandled subclass of AbstractCrdt encountered");
2812
+ }
2813
+ if (Array.isArray(value)) {
2814
+ return lsonListToJson(value);
2815
+ } else if (isPlainObject(value)) {
2816
+ return lsonObjectToJson(value);
2802
2817
  }
2803
2818
  return value;
2804
2819
  }
2805
2820
  function isPlainObject(obj) {
2806
- return Object.prototype.toString.call(obj) === "[object Object]";
2821
+ return obj !== null && Object.prototype.toString.call(obj) === "[object Object]";
2807
2822
  }
2808
2823
  function anyToCrdt(obj) {
2809
2824
  if (obj == null) {
@@ -2869,8 +2884,7 @@ function patchLiveList(liveList, prev, next) {
2869
2884
  if (liveListNode instanceof LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
2870
2885
  patchLiveObject(liveListNode, prevNode, nextNode);
2871
2886
  } else {
2872
- liveList.delete(i);
2873
- liveList.insert(anyToCrdt(nextNode), i);
2887
+ liveList.set(i, anyToCrdt(nextNode));
2874
2888
  }
2875
2889
  i++;
2876
2890
  }
@@ -2878,9 +2892,10 @@ function patchLiveList(liveList, prev, next) {
2878
2892
  liveList.insert(anyToCrdt(next[i]), i);
2879
2893
  i++;
2880
2894
  }
2881
- while (i <= prevEnd) {
2895
+ let localI = i;
2896
+ while (localI <= prevEnd) {
2882
2897
  liveList.delete(i);
2883
- i++;
2898
+ localI++;
2884
2899
  }
2885
2900
  }
2886
2901
  }
@@ -2953,7 +2968,7 @@ function patchImmutableNode(state, path, update) {
2953
2968
  const newState = Object.assign({}, state);
2954
2969
  for (const key in update.updates) {
2955
2970
  if (((_a = update.updates[key]) == null ? void 0 : _a.type) === "update") {
2956
- newState[key] = liveNodeToJson(update.node.get(key));
2971
+ newState[key] = lsonToJson(update.node.get(key));
2957
2972
  } else if (((_b = update.updates[key]) == null ? void 0 : _b.type) === "delete") {
2958
2973
  delete newState[key];
2959
2974
  }
@@ -2966,13 +2981,15 @@ function patchImmutableNode(state, path, update) {
2966
2981
  }
2967
2982
  let newState = state.map((x) => x);
2968
2983
  for (const listUpdate of update.updates) {
2969
- if (listUpdate.type === "insert") {
2984
+ if (listUpdate.type === "set") {
2985
+ newState = newState.map((item, index) => index === listUpdate.index ? listUpdate.item : item);
2986
+ } else if (listUpdate.type === "insert") {
2970
2987
  if (listUpdate.index === newState.length) {
2971
- newState.push(liveNodeToJson(listUpdate.item));
2988
+ newState.push(lsonToJson(listUpdate.item));
2972
2989
  } else {
2973
2990
  newState = [
2974
2991
  ...newState.slice(0, listUpdate.index),
2975
- liveNodeToJson(listUpdate.item),
2992
+ lsonToJson(listUpdate.item),
2976
2993
  ...newState.slice(listUpdate.index)
2977
2994
  ];
2978
2995
  }
@@ -2982,7 +2999,7 @@ function patchImmutableNode(state, path, update) {
2982
2999
  if (listUpdate.previousIndex > listUpdate.index) {
2983
3000
  newState = [
2984
3001
  ...newState.slice(0, listUpdate.index),
2985
- liveNodeToJson(listUpdate.item),
3002
+ lsonToJson(listUpdate.item),
2986
3003
  ...newState.slice(listUpdate.index, listUpdate.previousIndex),
2987
3004
  ...newState.slice(listUpdate.previousIndex + 1)
2988
3005
  ];
@@ -2990,7 +3007,7 @@ function patchImmutableNode(state, path, update) {
2990
3007
  newState = [
2991
3008
  ...newState.slice(0, listUpdate.previousIndex),
2992
3009
  ...newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1),
2993
- liveNodeToJson(listUpdate.item),
3010
+ lsonToJson(listUpdate.item),
2994
3011
  ...newState.slice(listUpdate.index + 1)
2995
3012
  ];
2996
3013
  }
@@ -3005,7 +3022,7 @@ function patchImmutableNode(state, path, update) {
3005
3022
  const newState = Object.assign({}, state);
3006
3023
  for (const key in update.updates) {
3007
3024
  if (((_c = update.updates[key]) == null ? void 0 : _c.type) === "update") {
3008
- newState[key] = liveNodeToJson(update.node.get(key));
3025
+ newState[key] = lsonToJson(update.node.get(key));
3009
3026
  } else if (((_d = update.updates[key]) == null ? void 0 : _d.type) === "delete") {
3010
3027
  delete newState[key];
3011
3028
  }
@@ -3027,7 +3044,7 @@ function patchImmutableNode(state, path, update) {
3027
3044
 
3028
3045
  const internals = {
3029
3046
  liveObjectToJson,
3030
- liveNodeToJson,
3047
+ lsonToJson,
3031
3048
  patchLiveList,
3032
3049
  patchImmutableObject,
3033
3050
  patchLiveObject,