@sovereignbase/convergent-replicated-list 1.3.5 → 1.3.6

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/dist/index.cjs CHANGED
@@ -436,6 +436,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
436
436
  if (listValues.length === 0) return false;
437
437
  const change = {};
438
438
  const delta = { values: [], tombstones: [] };
439
+ let displacedEntry;
439
440
  for (const listValue of listValues) {
440
441
  const v7 = _uuid.v7.call(void 0, );
441
442
  const linkedListEntry = {
@@ -484,25 +485,37 @@ function __update(listIndex, listValues, crListReplica, mode) {
484
485
  linkedListEntry,
485
486
  entryToOverwrite.next
486
487
  );
487
- if (entryToOverwrite.next) {
488
- if (entryToOverwrite.next.predecessor === entryToOverwrite.uuidv7) {
489
- void moveEntryToPredecessor(
490
- crListReplica,
491
- entryToOverwrite.next,
492
- linkedListEntry.uuidv7,
493
- delta
494
- );
488
+ if (_optionalChain([entryToOverwrite, 'access', _48 => _48.next, 'optionalAccess', _49 => _49.predecessor]) === entryToOverwrite.uuidv7) {
489
+ const overwriteNext = entryToOverwrite.next;
490
+ const owSibs = crListReplica.childrenMap.get(
491
+ overwriteNext.predecessor
492
+ );
493
+ if (owSibs) {
494
+ const i = owSibs.indexOf(overwriteNext);
495
+ if (i !== -1) owSibs.splice(i, 1);
495
496
  }
497
+ overwriteNext.predecessor = linkedListEntry.uuidv7;
498
+ const newSibs = crListReplica.childrenMap.get(linkedListEntry.uuidv7);
499
+ if (newSibs) newSibs.push(overwriteNext);
500
+ else
501
+ crListReplica.childrenMap.set(linkedListEntry.uuidv7, [
502
+ overwriteNext
503
+ ]);
504
+ _optionalChain([delta, 'access', _50 => _50.values, 'optionalAccess', _51 => _51.push, 'call', _52 => _52({
505
+ uuidv7: overwriteNext.uuidv7,
506
+ value: overwriteNext.value,
507
+ predecessor: overwriteNext.predecessor
508
+ })]);
496
509
  }
497
510
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
498
511
  void crListReplica.tombstones.add(entryToOverwrite.uuidv7);
499
- void _optionalChain([delta, 'access', _48 => _48.tombstones, 'optionalAccess', _49 => _49.push, 'call', _50 => _50(entryToOverwrite.uuidv7)]);
512
+ void _optionalChain([delta, 'access', _53 => _53.tombstones, 'optionalAccess', _54 => _54.push, 'call', _55 => _55(entryToOverwrite.uuidv7)]);
500
513
  void detachEntryFromIndexes(crListReplica, entryToOverwrite);
501
514
  entryToOverwrite.next = void 0;
502
515
  entryToOverwrite.prev = void 0;
503
516
  crListReplica.cursor = linkedListEntry;
504
517
  crListReplica.cursorIndex = actualIndex;
505
- void _optionalChain([crListReplica, 'access', _51 => _51.index, 'optionalAccess', _52 => _52.set, 'call', _53 => _53(linkedListEntry.index, linkedListEntry)]);
518
+ void _optionalChain([crListReplica, 'access', _56 => _56.index, 'optionalAccess', _57 => _57.set, 'call', _58 => _58(linkedListEntry.index, linkedListEntry)]);
506
519
  change[actualIndex] = linkedListEntry.value;
507
520
  break;
508
521
  }
@@ -511,7 +524,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
511
524
  crListReplica.cursor = linkedListEntry;
512
525
  crListReplica.cursorIndex = linkedListEntry.index;
513
526
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
514
- void _optionalChain([crListReplica, 'access', _54 => _54.index, 'optionalAccess', _55 => _55.set, 'call', _56 => _56(linkedListEntry.index, linkedListEntry)]);
527
+ void _optionalChain([crListReplica, 'access', _59 => _59.index, 'optionalAccess', _60 => _60.set, 'call', _61 => _61(linkedListEntry.index, linkedListEntry)]);
515
528
  change[linkedListEntry.index] = linkedListEntry.value;
516
529
  break;
517
530
  }
@@ -526,21 +539,25 @@ function __update(listIndex, listValues, crListReplica, mode) {
526
539
  linkedListEntry.index = actualIndex + 1;
527
540
  linkedListEntry.predecessor = crListReplica.cursor.uuidv7;
528
541
  void linkEntryBetween(crListReplica.cursor, linkedListEntry, next);
529
- if (next) {
530
- if (next.predecessor === crListReplica.cursor.uuidv7) {
531
- void moveEntryToPredecessor(
532
- crListReplica,
533
- next,
534
- linkedListEntry.uuidv7,
535
- delta
536
- );
542
+ if (next && next.predecessor === crListReplica.cursor.uuidv7) {
543
+ if (!displacedEntry) {
544
+ displacedEntry = next;
545
+ const sibs = crListReplica.childrenMap.get(next.predecessor);
546
+ if (sibs) {
547
+ const i = sibs.indexOf(next);
548
+ if (i !== -1) sibs.splice(i, 1);
549
+ }
537
550
  }
551
+ displacedEntry.predecessor = linkedListEntry.uuidv7;
538
552
  }
539
553
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
540
554
  crListReplica.cursor = linkedListEntry;
541
555
  crListReplica.cursorIndex = linkedListEntry.index;
542
- if (next) crListReplica.index = /* @__PURE__ */ new Map();
543
- void _optionalChain([crListReplica, 'access', _57 => _57.index, 'optionalAccess', _58 => _58.set, 'call', _59 => _59(linkedListEntry.index, linkedListEntry)]);
556
+ if (next) {
557
+ if (crListReplica.index) crListReplica.index.clear();
558
+ else crListReplica.index = /* @__PURE__ */ new Map();
559
+ }
560
+ void _optionalChain([crListReplica, 'access', _62 => _62.index, 'optionalAccess', _63 => _63.set, 'call', _64 => _64(linkedListEntry.index, linkedListEntry)]);
544
561
  change[linkedListEntry.index] = linkedListEntry.value;
545
562
  break;
546
563
  }
@@ -549,7 +566,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
549
566
  crListReplica.cursor = linkedListEntry;
550
567
  crListReplica.cursorIndex = linkedListEntry.index;
551
568
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
552
- void _optionalChain([crListReplica, 'access', _60 => _60.index, 'optionalAccess', _61 => _61.set, 'call', _62 => _62(linkedListEntry.index, linkedListEntry)]);
569
+ void _optionalChain([crListReplica, 'access', _65 => _65.index, 'optionalAccess', _66 => _66.set, 'call', _67 => _67(linkedListEntry.index, linkedListEntry)]);
553
570
  change[linkedListEntry.index] = linkedListEntry.value;
554
571
  mode = "after";
555
572
  listIndex = linkedListEntry.index - 1;
@@ -560,21 +577,27 @@ function __update(listIndex, listValues, crListReplica, mode) {
560
577
  const actualIndex = _nullishCoalesce(crListReplica.cursorIndex, () => ( listIndex));
561
578
  const prev = crListReplica.cursor.prev;
562
579
  linkedListEntry.index = actualIndex;
563
- linkedListEntry.predecessor = _nullishCoalesce(_optionalChain([prev, 'optionalAccess', _63 => _63.uuidv7]), () => ( "\0"));
580
+ linkedListEntry.predecessor = _nullishCoalesce(_optionalChain([prev, 'optionalAccess', _68 => _68.uuidv7]), () => ( "\0"));
564
581
  void linkEntryBetween(prev, linkedListEntry, crListReplica.cursor);
565
582
  if (crListReplica.cursor.predecessor === linkedListEntry.predecessor) {
566
- void moveEntryToPredecessor(
567
- crListReplica,
568
- crListReplica.cursor,
569
- linkedListEntry.uuidv7,
570
- delta
571
- );
583
+ if (!displacedEntry) {
584
+ displacedEntry = crListReplica.cursor;
585
+ const sibs = crListReplica.childrenMap.get(
586
+ crListReplica.cursor.predecessor
587
+ );
588
+ if (sibs) {
589
+ const i = sibs.indexOf(crListReplica.cursor);
590
+ if (i !== -1) sibs.splice(i, 1);
591
+ }
592
+ }
593
+ displacedEntry.predecessor = linkedListEntry.uuidv7;
572
594
  }
573
595
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
574
596
  crListReplica.cursor = linkedListEntry;
575
597
  crListReplica.cursorIndex = actualIndex;
576
- crListReplica.index = /* @__PURE__ */ new Map();
577
- void _optionalChain([crListReplica, 'access', _64 => _64.index, 'optionalAccess', _65 => _65.set, 'call', _66 => _66(linkedListEntry.index, linkedListEntry)]);
598
+ if (crListReplica.index) crListReplica.index.clear();
599
+ else crListReplica.index = /* @__PURE__ */ new Map();
600
+ void _optionalChain([crListReplica, 'access', _69 => _69.index, 'optionalAccess', _70 => _70.set, 'call', _71 => _71(linkedListEntry.index, linkedListEntry)]);
578
601
  change[actualIndex] = linkedListEntry.value;
579
602
  mode = "after";
580
603
  listIndex = linkedListEntry.index - 1;
@@ -584,6 +607,19 @@ function __update(listIndex, listValues, crListReplica, mode) {
584
607
  crListReplica.size = crListReplica.parentMap.size;
585
608
  listIndex++;
586
609
  }
610
+ if (displacedEntry) {
611
+ const sibs = crListReplica.childrenMap.get(displacedEntry.predecessor);
612
+ if (sibs) sibs.push(displacedEntry);
613
+ else
614
+ crListReplica.childrenMap.set(displacedEntry.predecessor, [
615
+ displacedEntry
616
+ ]);
617
+ _optionalChain([delta, 'access', _72 => _72.values, 'optionalAccess', _73 => _73.push, 'call', _74 => _74({
618
+ uuidv7: displacedEntry.uuidv7,
619
+ value: displacedEntry.value,
620
+ predecessor: displacedEntry.predecessor
621
+ })]);
622
+ }
587
623
  return { change, delta };
588
624
  }
589
625
 
@@ -601,7 +637,7 @@ function __delete(crListReplica, startIndex, endIndex) {
601
637
  void seekCursorToIndex(listIndex, crListReplica);
602
638
  if (!crListReplica.cursor) return false;
603
639
  let current = crListReplica.cursor;
604
- const predecessor = _nullishCoalesce(_optionalChain([current, 'access', _67 => _67.prev, 'optionalAccess', _68 => _68.uuidv7]), () => ( "\0"));
640
+ const predecessor = _nullishCoalesce(_optionalChain([current, 'access', _75 => _75.prev, 'optionalAccess', _76 => _76.uuidv7]), () => ( "\0"));
605
641
  const deletedIds = /* @__PURE__ */ new Set();
606
642
  let deleted = 0;
607
643
  let currentIndex = _nullishCoalesce(crListReplica.cursorIndex, () => ( listIndex));
@@ -609,7 +645,7 @@ function __delete(crListReplica, startIndex, endIndex) {
609
645
  const next = current.next;
610
646
  change[currentIndex] = void 0;
611
647
  void deletedIds.add(current.uuidv7);
612
- void _optionalChain([crListReplica, 'access', _69 => _69.index, 'optionalAccess', _70 => _70.delete, 'call', _71 => _71(currentIndex)]);
648
+ void _optionalChain([crListReplica, 'access', _77 => _77.index, 'optionalAccess', _78 => _78.delete, 'call', _79 => _79(currentIndex)]);
613
649
  void deleteLiveEntry(crListReplica, current, delta);
614
650
  current = next;
615
651
  currentIndex++;
@@ -629,7 +665,7 @@ function __delete(crListReplica, startIndex, endIndex) {
629
665
  void deleteLiveEntry(crListReplica, current, delta);
630
666
  void linkEntryBetween(prev, replacement, next);
631
667
  void attachEntryToIndexes(crListReplica, replacement, delta);
632
- if (_optionalChain([next, 'optionalAccess', _72 => _72.predecessor]) === current.uuidv7)
668
+ if (_optionalChain([next, 'optionalAccess', _80 => _80.predecessor]) === current.uuidv7)
633
669
  void moveEntryToPredecessor(
634
670
  crListReplica,
635
671
  next,
@@ -675,7 +711,7 @@ function __merge(crListReplica, crListDelta) {
675
711
  crListReplica.cursorIndex = linkedListEntry.index;
676
712
  void attachEntryToIndexes(crListReplica, linkedListEntry);
677
713
  crListReplica.size = crListReplica.parentMap.size;
678
- void _optionalChain([crListReplica, 'access', _73 => _73.index, 'optionalAccess', _74 => _74.set, 'call', _75 => _75(linkedListEntry.index, linkedListEntry)]);
714
+ void _optionalChain([crListReplica, 'access', _81 => _81.index, 'optionalAccess', _82 => _82.set, 'call', _83 => _83(linkedListEntry.index, linkedListEntry)]);
679
715
  return { [linkedListEntry.index]: linkedListEntry.value };
680
716
  }
681
717
  }
@@ -689,7 +725,7 @@ function __merge(crListReplica, crListDelta) {
689
725
  const wasTail = linkedListEntry.next === void 0;
690
726
  const wasCursor = crListReplica.cursor === linkedListEntry;
691
727
  void newTombsIndices.push(linkedListEntry.index);
692
- void _optionalChain([crListReplica, 'access', _76 => _76.index, 'optionalAccess', _77 => _77.delete, 'call', _78 => _78(linkedListEntry.index)]);
728
+ void _optionalChain([crListReplica, 'access', _84 => _84.index, 'optionalAccess', _85 => _85.delete, 'call', _86 => _86(linkedListEntry.index)]);
693
729
  void deleteLiveEntry(crListReplica, linkedListEntry);
694
730
  tailTombstoneMovedCursor = wasTail && wasCursor;
695
731
  needsRelink = true;
@@ -701,7 +737,7 @@ function __merge(crListReplica, crListDelta) {
701
737
  if (newTombsIndices.length === 1 && tailTombstoneMovedCursor) {
702
738
  if (crListReplica.cursor) {
703
739
  crListReplica.cursorIndex = crListReplica.size - 1;
704
- void _optionalChain([crListReplica, 'access', _79 => _79.index, 'optionalAccess', _80 => _80.set, 'call', _81 => _81(
740
+ void _optionalChain([crListReplica, 'access', _87 => _87.index, 'optionalAccess', _88 => _88.set, 'call', _89 => _89(
705
741
  crListReplica.cursorIndex,
706
742
  crListReplica.cursor
707
743
  )]);
@@ -748,7 +784,7 @@ function __merge(crListReplica, crListDelta) {
748
784
  crListReplica.cursor = linkedListEntry;
749
785
  crListReplica.cursorIndex = linkedListEntry.index;
750
786
  crListReplica.size = crListReplica.parentMap.size;
751
- void _optionalChain([crListReplica, 'access', _82 => _82.index, 'optionalAccess', _83 => _83.set, 'call', _84 => _84(linkedListEntry.index, linkedListEntry)]);
787
+ void _optionalChain([crListReplica, 'access', _90 => _90.index, 'optionalAccess', _91 => _91.set, 'call', _92 => _92(linkedListEntry.index, linkedListEntry)]);
752
788
  } else {
753
789
  needsRelink = true;
754
790
  }
@@ -759,7 +795,7 @@ function __merge(crListReplica, crListDelta) {
759
795
  crListReplica.cursor = linkedListEntry;
760
796
  crListReplica.cursorIndex = linkedListEntry.index;
761
797
  crListReplica.size = crListReplica.parentMap.size;
762
- void _optionalChain([crListReplica, 'access', _85 => _85.index, 'optionalAccess', _86 => _86.set, 'call', _87 => _87(linkedListEntry.index, linkedListEntry)]);
798
+ void _optionalChain([crListReplica, 'access', _93 => _93.index, 'optionalAccess', _94 => _94.set, 'call', _95 => _95(linkedListEntry.index, linkedListEntry)]);
763
799
  } else {
764
800
  needsRelink = true;
765
801
  }
@@ -924,32 +960,32 @@ var CRList = class {
924
960
  return this.state.size;
925
961
  }
926
962
  /**
927
- * Inserts a value before an index.
963
+ * Inserts values before an index.
928
964
  *
929
- * If `beforeIndex` is omitted, the value is inserted at the start of the list.
965
+ * If `beforeIndex` is omitted, values are inserted at the start of the list.
930
966
  *
931
- * @param value - The value to insert.
967
+ * @param values - Values to insert.
932
968
  * @param beforeIndex - The index to insert before.
933
969
  */
934
- prepend(value, beforeIndex) {
935
- const result = __update(_nullishCoalesce(beforeIndex, () => ( 0)), [value], this.state, "before");
970
+ prepend(values, beforeIndex) {
971
+ const result = __update(_nullishCoalesce(beforeIndex, () => ( 0)), values, this.state, "before");
936
972
  if (!result) return;
937
973
  const { delta, change } = result;
938
974
  if (delta) void dispatchCRListEvent(this.eventTarget, "delta", delta);
939
975
  if (change) void dispatchCRListEvent(this.eventTarget, "change", change);
940
976
  }
941
977
  /**
942
- * Inserts a value after an index.
978
+ * Inserts values after an index.
943
979
  *
944
- * If `afterIndex` is omitted, the value is appended at the end of the list.
980
+ * If `afterIndex` is omitted, values are appended at the end of the list.
945
981
  *
946
- * @param value - The value to insert.
982
+ * @param values - Values to insert.
947
983
  * @param afterIndex - The index to insert after.
948
984
  */
949
- append(value, afterIndex) {
985
+ append(values, afterIndex) {
950
986
  const result = __update(
951
987
  _nullishCoalesce(afterIndex, () => ( this.state.size)),
952
- [value],
988
+ values,
953
989
  this.state,
954
990
  "after"
955
991
  );
@@ -958,6 +994,19 @@ var CRList = class {
958
994
  if (delta) void dispatchCRListEvent(this.eventTarget, "delta", delta);
959
995
  if (change) void dispatchCRListEvent(this.eventTarget, "change", change);
960
996
  }
997
+ /**
998
+ * Overwrites entries starting at an index.
999
+ *
1000
+ * @param index - The index to start overwriting at.
1001
+ * @param values - Values to write.
1002
+ */
1003
+ update(index, values) {
1004
+ const result = __update(index, values, this.state, "overwrite");
1005
+ if (!result) return;
1006
+ const { delta, change } = result;
1007
+ if (delta) void dispatchCRListEvent(this.eventTarget, "delta", delta);
1008
+ if (change) void dispatchCRListEvent(this.eventTarget, "change", change);
1009
+ }
961
1010
  /**
962
1011
  * Removes one or more entries starting at an index.
963
1012
  *
@@ -981,8 +1030,8 @@ var CRList = class {
981
1030
  * @param thisArg - Optional `this` value for the predicate.
982
1031
  */
983
1032
  find(predicate, thisArg) {
984
- let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _88 => _88.state, 'access', _89 => _89.index, 'optionalAccess', _90 => _90.get, 'call', _91 => _91(0)]), () => ( this.state.cursor));
985
- while (_optionalChain([linkedListEntry, 'optionalAccess', _92 => _92.prev])) linkedListEntry = linkedListEntry.prev;
1033
+ let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _96 => _96.state, 'access', _97 => _97.index, 'optionalAccess', _98 => _98.get, 'call', _99 => _99(0)]), () => ( this.state.cursor));
1034
+ while (_optionalChain([linkedListEntry, 'optionalAccess', _100 => _100.prev])) linkedListEntry = linkedListEntry.prev;
986
1035
  let index = 0;
987
1036
  while (linkedListEntry) {
988
1037
  if (predicate.call(thisArg, linkedListEntry.value, index, this))
@@ -1092,8 +1141,8 @@ var CRList = class {
1092
1141
  * Iterates over current live values in index order.
1093
1142
  */
1094
1143
  *[Symbol.iterator]() {
1095
- let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _93 => _93.state, 'access', _94 => _94.index, 'optionalAccess', _95 => _95.get, 'call', _96 => _96(0)]), () => ( this.state.cursor));
1096
- while (_optionalChain([linkedListEntry, 'optionalAccess', _97 => _97.prev])) linkedListEntry = linkedListEntry.prev;
1144
+ let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _101 => _101.state, 'access', _102 => _102.index, 'optionalAccess', _103 => _103.get, 'call', _104 => _104(0)]), () => ( this.state.cursor));
1145
+ while (_optionalChain([linkedListEntry, 'optionalAccess', _105 => _105.prev])) linkedListEntry = linkedListEntry.prev;
1097
1146
  while (linkedListEntry) {
1098
1147
  yield linkedListEntry.value;
1099
1148
  linkedListEntry = linkedListEntry.next;
@@ -1109,8 +1158,8 @@ var CRList = class {
1109
1158
  * @param thisArg - Optional `this` value for the callback.
1110
1159
  */
1111
1160
  forEach(callback, thisArg) {
1112
- let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _98 => _98.state, 'access', _99 => _99.index, 'optionalAccess', _100 => _100.get, 'call', _101 => _101(0)]), () => ( this.state.cursor));
1113
- while (_optionalChain([linkedListEntry, 'optionalAccess', _102 => _102.prev])) linkedListEntry = linkedListEntry.prev;
1161
+ let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _106 => _106.state, 'access', _107 => _107.index, 'optionalAccess', _108 => _108.get, 'call', _109 => _109(0)]), () => ( this.state.cursor));
1162
+ while (_optionalChain([linkedListEntry, 'optionalAccess', _110 => _110.prev])) linkedListEntry = linkedListEntry.prev;
1114
1163
  let index = 0;
1115
1164
  while (linkedListEntry) {
1116
1165
  void callback.call(thisArg, linkedListEntry.value, index, this);