@sovereignbase/convergent-replicated-list 1.3.5 → 1.3.7

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
@@ -112,9 +112,19 @@ function rebuildLiveProjection(crListReplica) {
112
112
  while (stack.length > 0) {
113
113
  const frame = stack[stack.length - 1];
114
114
  if (!frame.siblings) {
115
- frame.siblings = crListReplica.childrenMap.get(
115
+ const childrenMapSibs = crListReplica.childrenMap.get(
116
116
  frame.predecessorIdentifier
117
117
  );
118
+ const runNextEntry = _optionalChain([crListReplica, 'access', _14 => _14.runNext, 'optionalAccess', _15 => _15.get, 'call', _16 => _16(
119
+ frame.predecessorIdentifier
120
+ )]);
121
+ if (childrenMapSibs && runNextEntry) {
122
+ frame.siblings = [...childrenMapSibs, runNextEntry];
123
+ } else if (runNextEntry) {
124
+ frame.siblings = [runNextEntry];
125
+ } else {
126
+ frame.siblings = childrenMapSibs;
127
+ }
118
128
  if (!frame.siblings) {
119
129
  void stack.pop();
120
130
  continue;
@@ -186,7 +196,7 @@ function attachEntryToIndexes(crListReplica, linkedListEntry, deltaBuf) {
186
196
  ]);
187
197
  }
188
198
  if (deltaBuf && !Array.isArray(deltaBuf.values)) deltaBuf.values = [];
189
- if (_optionalChain([deltaBuf, 'optionalAccess', _14 => _14.values]))
199
+ if (_optionalChain([deltaBuf, 'optionalAccess', _17 => _17.values]))
190
200
  void deltaBuf.values.push({
191
201
  uuidv7: linkedListEntry.uuidv7,
192
202
  value: linkedListEntry.value,
@@ -198,9 +208,21 @@ function attachEntryToIndexes(crListReplica, linkedListEntry, deltaBuf) {
198
208
  function detachEntryFromIndexes(crListReplica, linkedListEntry) {
199
209
  void crListReplica.parentMap.delete(linkedListEntry.uuidv7);
200
210
  const siblings = crListReplica.childrenMap.get(linkedListEntry.predecessor);
201
- if (!siblings) return;
202
- const index = siblings.indexOf(linkedListEntry);
203
- if (index !== -1) void siblings.splice(index, 1);
211
+ if (siblings) {
212
+ const index = siblings.indexOf(linkedListEntry);
213
+ if (index !== -1) void siblings.splice(index, 1);
214
+ }
215
+ if (crListReplica.runNext) {
216
+ if (crListReplica.runNext.get(linkedListEntry.predecessor) === linkedListEntry)
217
+ crListReplica.runNext.delete(linkedListEntry.predecessor);
218
+ const runSuccessor = crListReplica.runNext.get(linkedListEntry.uuidv7);
219
+ if (runSuccessor) {
220
+ crListReplica.runNext.delete(linkedListEntry.uuidv7);
221
+ const sibs = crListReplica.childrenMap.get(linkedListEntry.uuidv7);
222
+ if (sibs) sibs.push(runSuccessor);
223
+ else crListReplica.childrenMap.set(linkedListEntry.uuidv7, [runSuccessor]);
224
+ }
225
+ }
204
226
  }
205
227
 
206
228
  // src/.helpers/deleteLiveEntry/index.ts
@@ -209,7 +231,7 @@ function deleteLiveEntry(crListReplica, linkedListEntry, deltaBuf) {
209
231
  const next = linkedListEntry.next;
210
232
  void crListReplica.tombstones.add(linkedListEntry.uuidv7);
211
233
  if (deltaBuf && !Array.isArray(deltaBuf.tombstones)) deltaBuf.tombstones = [];
212
- void _optionalChain([deltaBuf, 'optionalAccess', _15 => _15.tombstones, 'optionalAccess', _16 => _16.push, 'call', _17 => _17(linkedListEntry.uuidv7)]);
234
+ void _optionalChain([deltaBuf, 'optionalAccess', _18 => _18.tombstones, 'optionalAccess', _19 => _19.push, 'call', _20 => _20(linkedListEntry.uuidv7)]);
213
235
  if (prev) prev.next = next;
214
236
  if (next) {
215
237
  next.prev = prev;
@@ -254,7 +276,7 @@ function trySpliceInsertedParent(crListReplica, insertedEntries, reparentedEntri
254
276
  return false;
255
277
  const siblings = crListReplica.childrenMap.get(inserted.predecessor);
256
278
  const children = crListReplica.childrenMap.get(inserted.uuidv7);
257
- if (_optionalChain([siblings, 'optionalAccess', _18 => _18.length]) !== 1 || siblings[0] !== inserted || _optionalChain([children, 'optionalAccess', _19 => _19.length]) !== 1 || children[0] !== moved)
279
+ if (_optionalChain([siblings, 'optionalAccess', _21 => _21.length]) !== 1 || siblings[0] !== inserted || _optionalChain([children, 'optionalAccess', _22 => _22.length]) !== 1 || children[0] !== moved)
258
280
  return false;
259
281
  const predecessor = inserted.predecessor === "\0" ? void 0 : crListReplica.parentMap.get(inserted.predecessor);
260
282
  if (inserted.predecessor !== "\0" && !predecessor) return false;
@@ -284,14 +306,14 @@ function trySpliceReplacement(crListReplica, insertedEntries, reparentedEntries,
284
306
  const predecessor = inserted.predecessor === "\0" ? void 0 : crListReplica.parentMap.get(inserted.predecessor);
285
307
  if (inserted.predecessor !== "\0" && !predecessor) return false;
286
308
  const siblings = crListReplica.childrenMap.get(inserted.predecessor);
287
- if (_optionalChain([siblings, 'optionalAccess', _20 => _20.length]) !== 1 || siblings[0] !== inserted) return false;
309
+ if (_optionalChain([siblings, 'optionalAccess', _23 => _23.length]) !== 1 || siblings[0] !== inserted) return false;
288
310
  const reparented = reparentedEntries[0];
289
- const next = _optionalChain([reparented, 'optionalAccess', _21 => _21.entry]);
311
+ const next = _optionalChain([reparented, 'optionalAccess', _24 => _24.entry]);
290
312
  if (next) {
291
313
  const children = crListReplica.childrenMap.get(inserted.uuidv7);
292
- if (next.predecessor !== inserted.uuidv7 || !crListReplica.tombstones.has(reparented.previousPredecessor) || _optionalChain([children, 'optionalAccess', _22 => _22.length]) !== 1 || children[0] !== next || next.prev !== predecessor)
314
+ if (next.predecessor !== inserted.uuidv7 || !crListReplica.tombstones.has(reparented.previousPredecessor) || _optionalChain([children, 'optionalAccess', _25 => _25.length]) !== 1 || children[0] !== next || next.prev !== predecessor)
293
315
  return false;
294
- } else if (_optionalChain([crListReplica, 'access', _23 => _23.childrenMap, 'access', _24 => _24.get, 'call', _25 => _25(inserted.uuidv7), 'optionalAccess', _26 => _26.length])) {
316
+ } else if (_optionalChain([crListReplica, 'access', _26 => _26.childrenMap, 'access', _27 => _27.get, 'call', _28 => _28(inserted.uuidv7), 'optionalAccess', _29 => _29.length])) {
295
317
  return false;
296
318
  }
297
319
  if (predecessor) {
@@ -327,7 +349,7 @@ function trySpliceSiblingInsert(crListReplica, insertedEntries, reparentedEntrie
327
349
  return false;
328
350
  const inserted = insertedEntries[0];
329
351
  if (inserted.predecessor === "\0") return false;
330
- if (_optionalChain([crListReplica, 'access', _27 => _27.childrenMap, 'access', _28 => _28.get, 'call', _29 => _29(inserted.uuidv7), 'optionalAccess', _30 => _30.length])) return false;
352
+ if (_optionalChain([crListReplica, 'access', _30 => _30.childrenMap, 'access', _31 => _31.get, 'call', _32 => _32(inserted.uuidv7), 'optionalAccess', _33 => _33.length])) return false;
331
353
  const predecessor = crListReplica.parentMap.get(inserted.predecessor);
332
354
  const siblings = crListReplica.childrenMap.get(inserted.predecessor);
333
355
  if (!predecessor || !siblings || siblings.length < 2) return false;
@@ -338,8 +360,8 @@ function trySpliceSiblingInsert(crListReplica, insertedEntries, reparentedEntrie
338
360
  if (lastSibling !== inserted && lastSibling.next) return false;
339
361
  const previousSibling = siblings[siblingIndex - 1];
340
362
  const nextSibling = siblings[siblingIndex + 1];
341
- if (_optionalChain([previousSibling, 'optionalAccess', _31 => _31.uuidv7])) {
342
- if (_optionalChain([crListReplica, 'access', _32 => _32.childrenMap, 'access', _33 => _33.get, 'call', _34 => _34(previousSibling.uuidv7), 'optionalAccess', _35 => _35.length]))
363
+ if (_optionalChain([previousSibling, 'optionalAccess', _34 => _34.uuidv7])) {
364
+ if (_optionalChain([crListReplica, 'access', _35 => _35.childrenMap, 'access', _36 => _36.get, 'call', _37 => _37(previousSibling.uuidv7), 'optionalAccess', _38 => _38.length]))
343
365
  return false;
344
366
  if (previousSibling.next !== nextSibling) return false;
345
367
  } else if (predecessor.next !== nextSibling) {
@@ -363,6 +385,13 @@ function trySpliceSiblingInsert(crListReplica, insertedEntries, reparentedEntrie
363
385
  return true;
364
386
  }
365
387
 
388
+ // src/.helpers/deriveRunUuid/index.ts
389
+ function deriveRunUuid(startUuid, offset) {
390
+ const randA = parseInt(startUuid.slice(15, 18), 16);
391
+ const newRandA = randA + offset & 4095;
392
+ return startUuid.slice(0, 15) + newRandA.toString(16).padStart(3, "0") + startUuid.slice(18);
393
+ }
394
+
366
395
  // src/core/crud/create/index.ts
367
396
 
368
397
  function __create(snapshot) {
@@ -394,11 +423,11 @@ function __create(snapshot) {
394
423
  );
395
424
  if (!linkedListEntry) continue;
396
425
  void attachEntryToIndexes(crListReplica, linkedListEntry);
397
- if (canUseLinearProjection && linkedListEntry.predecessor === (_nullishCoalesce(_optionalChain([previous, 'optionalAccess', _36 => _36.uuidv7]), () => ( "\0")))) {
426
+ if (canUseLinearProjection && linkedListEntry.predecessor === (_nullishCoalesce(_optionalChain([previous, 'optionalAccess', _39 => _39.uuidv7]), () => ( "\0")))) {
398
427
  linkedListEntry.index = crListReplica.parentMap.size - 1;
399
428
  void linkEntryBetween(previous, linkedListEntry, void 0);
400
429
  previous = linkedListEntry;
401
- void _optionalChain([crListReplica, 'access', _37 => _37.index, 'optionalAccess', _38 => _38.set, 'call', _39 => _39(linkedListEntry.index, linkedListEntry)]);
430
+ void _optionalChain([crListReplica, 'access', _40 => _40.index, 'optionalAccess', _41 => _41.set, 'call', _42 => _42(linkedListEntry.index, linkedListEntry)]);
402
431
  continue;
403
432
  }
404
433
  canUseLinearProjection = false;
@@ -417,8 +446,8 @@ function __create(snapshot) {
417
446
  function __read(targetIndex, crListReplica) {
418
447
  try {
419
448
  void seekCursorToIndex(targetIndex, crListReplica);
420
- return _optionalChain([crListReplica, 'access', _40 => _40.cursor, 'optionalAccess', _41 => _41.value]);
421
- } catch (e) {
449
+ return _optionalChain([crListReplica, 'access', _43 => _43.cursor, 'optionalAccess', _44 => _44.value]);
450
+ } catch (e2) {
422
451
  return void 0;
423
452
  }
424
453
  }
@@ -436,6 +465,8 @@ function __update(listIndex, listValues, crListReplica, mode) {
436
465
  if (listValues.length === 0) return false;
437
466
  const change = {};
438
467
  const delta = { values: [], tombstones: [] };
468
+ let displacedEntry;
469
+ const batchEntries = [];
439
470
  for (const listValue of listValues) {
440
471
  const v7 = _uuid.v7.call(void 0, );
441
472
  const linkedListEntry = {
@@ -453,7 +484,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
453
484
  crListReplica.cursor = linkedListEntry;
454
485
  crListReplica.cursorIndex = linkedListEntry.index;
455
486
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
456
- _optionalChain([crListReplica, 'access', _42 => _42.index, 'optionalAccess', _43 => _43.set, 'call', _44 => _44(linkedListEntry.index, linkedListEntry)]);
487
+ _optionalChain([crListReplica, 'access', _45 => _45.index, 'optionalAccess', _46 => _46.set, 'call', _47 => _47(linkedListEntry.index, linkedListEntry)]);
457
488
  change[linkedListEntry.index] = linkedListEntry.value;
458
489
  break;
459
490
  }
@@ -469,7 +500,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
469
500
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
470
501
  crListReplica.cursor = linkedListEntry;
471
502
  crListReplica.cursorIndex = linkedListEntry.index;
472
- void _optionalChain([crListReplica, 'access', _45 => _45.index, 'optionalAccess', _46 => _46.set, 'call', _47 => _47(linkedListEntry.index, linkedListEntry)]);
503
+ void _optionalChain([crListReplica, 'access', _48 => _48.index, 'optionalAccess', _49 => _49.set, 'call', _50 => _50(linkedListEntry.index, linkedListEntry)]);
473
504
  change[linkedListEntry.index] = linkedListEntry.value;
474
505
  break;
475
506
  }
@@ -484,25 +515,37 @@ function __update(listIndex, listValues, crListReplica, mode) {
484
515
  linkedListEntry,
485
516
  entryToOverwrite.next
486
517
  );
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
- );
518
+ if (_optionalChain([entryToOverwrite, 'access', _51 => _51.next, 'optionalAccess', _52 => _52.predecessor]) === entryToOverwrite.uuidv7) {
519
+ const overwriteNext = entryToOverwrite.next;
520
+ const owSibs = crListReplica.childrenMap.get(
521
+ overwriteNext.predecessor
522
+ );
523
+ if (owSibs) {
524
+ const i = owSibs.indexOf(overwriteNext);
525
+ if (i !== -1) owSibs.splice(i, 1);
495
526
  }
527
+ overwriteNext.predecessor = linkedListEntry.uuidv7;
528
+ const newSibs = crListReplica.childrenMap.get(linkedListEntry.uuidv7);
529
+ if (newSibs) newSibs.push(overwriteNext);
530
+ else
531
+ crListReplica.childrenMap.set(linkedListEntry.uuidv7, [
532
+ overwriteNext
533
+ ]);
534
+ _optionalChain([delta, 'access', _53 => _53.values, 'optionalAccess', _54 => _54.push, 'call', _55 => _55({
535
+ uuidv7: overwriteNext.uuidv7,
536
+ value: overwriteNext.value,
537
+ predecessor: overwriteNext.predecessor
538
+ })]);
496
539
  }
497
540
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
498
541
  void crListReplica.tombstones.add(entryToOverwrite.uuidv7);
499
- void _optionalChain([delta, 'access', _48 => _48.tombstones, 'optionalAccess', _49 => _49.push, 'call', _50 => _50(entryToOverwrite.uuidv7)]);
542
+ void _optionalChain([delta, 'access', _56 => _56.tombstones, 'optionalAccess', _57 => _57.push, 'call', _58 => _58(entryToOverwrite.uuidv7)]);
500
543
  void detachEntryFromIndexes(crListReplica, entryToOverwrite);
501
544
  entryToOverwrite.next = void 0;
502
545
  entryToOverwrite.prev = void 0;
503
546
  crListReplica.cursor = linkedListEntry;
504
547
  crListReplica.cursorIndex = actualIndex;
505
- void _optionalChain([crListReplica, 'access', _51 => _51.index, 'optionalAccess', _52 => _52.set, 'call', _53 => _53(linkedListEntry.index, linkedListEntry)]);
548
+ void _optionalChain([crListReplica, 'access', _59 => _59.index, 'optionalAccess', _60 => _60.set, 'call', _61 => _61(linkedListEntry.index, linkedListEntry)]);
506
549
  change[actualIndex] = linkedListEntry.value;
507
550
  break;
508
551
  }
@@ -511,7 +554,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
511
554
  crListReplica.cursor = linkedListEntry;
512
555
  crListReplica.cursorIndex = linkedListEntry.index;
513
556
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
514
- void _optionalChain([crListReplica, 'access', _54 => _54.index, 'optionalAccess', _55 => _55.set, 'call', _56 => _56(linkedListEntry.index, linkedListEntry)]);
557
+ void _optionalChain([crListReplica, 'access', _62 => _62.index, 'optionalAccess', _63 => _63.set, 'call', _64 => _64(linkedListEntry.index, linkedListEntry)]);
515
558
  change[linkedListEntry.index] = linkedListEntry.value;
516
559
  break;
517
560
  }
@@ -526,21 +569,25 @@ function __update(listIndex, listValues, crListReplica, mode) {
526
569
  linkedListEntry.index = actualIndex + 1;
527
570
  linkedListEntry.predecessor = crListReplica.cursor.uuidv7;
528
571
  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
- );
572
+ if (next && next.predecessor === crListReplica.cursor.uuidv7) {
573
+ if (!displacedEntry) {
574
+ displacedEntry = next;
575
+ const sibs = crListReplica.childrenMap.get(next.predecessor);
576
+ if (sibs) {
577
+ const i = sibs.indexOf(next);
578
+ if (i !== -1) sibs.splice(i, 1);
579
+ }
537
580
  }
581
+ displacedEntry.predecessor = linkedListEntry.uuidv7;
538
582
  }
539
583
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
540
584
  crListReplica.cursor = linkedListEntry;
541
585
  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)]);
586
+ if (next) {
587
+ if (crListReplica.index) crListReplica.index.clear();
588
+ else crListReplica.index = /* @__PURE__ */ new Map();
589
+ }
590
+ void _optionalChain([crListReplica, 'access', _65 => _65.index, 'optionalAccess', _66 => _66.set, 'call', _67 => _67(linkedListEntry.index, linkedListEntry)]);
544
591
  change[linkedListEntry.index] = linkedListEntry.value;
545
592
  break;
546
593
  }
@@ -549,7 +596,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
549
596
  crListReplica.cursor = linkedListEntry;
550
597
  crListReplica.cursorIndex = linkedListEntry.index;
551
598
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
552
- void _optionalChain([crListReplica, 'access', _60 => _60.index, 'optionalAccess', _61 => _61.set, 'call', _62 => _62(linkedListEntry.index, linkedListEntry)]);
599
+ void _optionalChain([crListReplica, 'access', _68 => _68.index, 'optionalAccess', _69 => _69.set, 'call', _70 => _70(linkedListEntry.index, linkedListEntry)]);
553
600
  change[linkedListEntry.index] = linkedListEntry.value;
554
601
  mode = "after";
555
602
  listIndex = linkedListEntry.index - 1;
@@ -560,30 +607,76 @@ function __update(listIndex, listValues, crListReplica, mode) {
560
607
  const actualIndex = _nullishCoalesce(crListReplica.cursorIndex, () => ( listIndex));
561
608
  const prev = crListReplica.cursor.prev;
562
609
  linkedListEntry.index = actualIndex;
563
- linkedListEntry.predecessor = _nullishCoalesce(_optionalChain([prev, 'optionalAccess', _63 => _63.uuidv7]), () => ( "\0"));
610
+ linkedListEntry.predecessor = _nullishCoalesce(_optionalChain([prev, 'optionalAccess', _71 => _71.uuidv7]), () => ( "\0"));
564
611
  void linkEntryBetween(prev, linkedListEntry, crListReplica.cursor);
565
612
  if (crListReplica.cursor.predecessor === linkedListEntry.predecessor) {
566
- void moveEntryToPredecessor(
567
- crListReplica,
568
- crListReplica.cursor,
569
- linkedListEntry.uuidv7,
570
- delta
571
- );
613
+ if (!displacedEntry) {
614
+ displacedEntry = crListReplica.cursor;
615
+ const sibs = crListReplica.childrenMap.get(
616
+ crListReplica.cursor.predecessor
617
+ );
618
+ if (sibs) {
619
+ const i = sibs.indexOf(crListReplica.cursor);
620
+ if (i !== -1) sibs.splice(i, 1);
621
+ }
622
+ }
623
+ displacedEntry.predecessor = linkedListEntry.uuidv7;
572
624
  }
573
625
  void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
574
626
  crListReplica.cursor = linkedListEntry;
575
627
  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)]);
628
+ if (crListReplica.index) crListReplica.index.clear();
629
+ else crListReplica.index = /* @__PURE__ */ new Map();
630
+ void _optionalChain([crListReplica, 'access', _72 => _72.index, 'optionalAccess', _73 => _73.set, 'call', _74 => _74(linkedListEntry.index, linkedListEntry)]);
578
631
  change[actualIndex] = linkedListEntry.value;
579
632
  mode = "after";
580
633
  listIndex = linkedListEntry.index - 1;
581
634
  break;
582
635
  }
583
636
  }
637
+ batchEntries.push(linkedListEntry);
584
638
  crListReplica.size = crListReplica.parentMap.size;
585
639
  listIndex++;
586
640
  }
641
+ if (batchEntries.length > 1) {
642
+ for (let k = 0; k + 1 < batchEntries.length; k++) {
643
+ const curr = batchEntries[k];
644
+ const next = batchEntries[k + 1];
645
+ if (next.predecessor !== curr.uuidv7) continue;
646
+ crListReplica.childrenMap.delete(curr.uuidv7);
647
+ if (!crListReplica.runNext) crListReplica.runNext = /* @__PURE__ */ new Map();
648
+ crListReplica.runNext.set(curr.uuidv7, next);
649
+ }
650
+ }
651
+ if (batchEntries.length > 1 && delta.values && delta.values.length >= batchEntries.length) {
652
+ const first = batchEntries[0];
653
+ const last = batchEntries[batchEntries.length - 1];
654
+ if (deriveRunUuid(first.uuidv7, batchEntries.length - 1) === last.uuidv7) {
655
+ delta.values.splice(
656
+ delta.values.length - batchEntries.length,
657
+ batchEntries.length,
658
+ {
659
+ uuidv7: first.uuidv7,
660
+ value: first.value,
661
+ predecessor: first.predecessor,
662
+ tail: batchEntries.slice(1).map((e) => e.value)
663
+ }
664
+ );
665
+ }
666
+ }
667
+ if (displacedEntry) {
668
+ const sibs = crListReplica.childrenMap.get(displacedEntry.predecessor);
669
+ if (sibs) sibs.push(displacedEntry);
670
+ else
671
+ crListReplica.childrenMap.set(displacedEntry.predecessor, [
672
+ displacedEntry
673
+ ]);
674
+ _optionalChain([delta, 'access', _75 => _75.values, 'optionalAccess', _76 => _76.push, 'call', _77 => _77({
675
+ uuidv7: displacedEntry.uuidv7,
676
+ value: displacedEntry.value,
677
+ predecessor: displacedEntry.predecessor
678
+ })]);
679
+ }
587
680
  return { change, delta };
588
681
  }
589
682
 
@@ -601,7 +694,7 @@ function __delete(crListReplica, startIndex, endIndex) {
601
694
  void seekCursorToIndex(listIndex, crListReplica);
602
695
  if (!crListReplica.cursor) return false;
603
696
  let current = crListReplica.cursor;
604
- const predecessor = _nullishCoalesce(_optionalChain([current, 'access', _67 => _67.prev, 'optionalAccess', _68 => _68.uuidv7]), () => ( "\0"));
697
+ const predecessor = _nullishCoalesce(_optionalChain([current, 'access', _78 => _78.prev, 'optionalAccess', _79 => _79.uuidv7]), () => ( "\0"));
605
698
  const deletedIds = /* @__PURE__ */ new Set();
606
699
  let deleted = 0;
607
700
  let currentIndex = _nullishCoalesce(crListReplica.cursorIndex, () => ( listIndex));
@@ -609,7 +702,7 @@ function __delete(crListReplica, startIndex, endIndex) {
609
702
  const next = current.next;
610
703
  change[currentIndex] = void 0;
611
704
  void deletedIds.add(current.uuidv7);
612
- void _optionalChain([crListReplica, 'access', _69 => _69.index, 'optionalAccess', _70 => _70.delete, 'call', _71 => _71(currentIndex)]);
705
+ void _optionalChain([crListReplica, 'access', _80 => _80.index, 'optionalAccess', _81 => _81.delete, 'call', _82 => _82(currentIndex)]);
613
706
  void deleteLiveEntry(crListReplica, current, delta);
614
707
  current = next;
615
708
  currentIndex++;
@@ -629,7 +722,7 @@ function __delete(crListReplica, startIndex, endIndex) {
629
722
  void deleteLiveEntry(crListReplica, current, delta);
630
723
  void linkEntryBetween(prev, replacement, next);
631
724
  void attachEntryToIndexes(crListReplica, replacement, delta);
632
- if (_optionalChain([next, 'optionalAccess', _72 => _72.predecessor]) === current.uuidv7)
725
+ if (_optionalChain([next, 'optionalAccess', _83 => _83.predecessor]) === current.uuidv7)
633
726
  void moveEntryToPredecessor(
634
727
  crListReplica,
635
728
  next,
@@ -660,9 +753,20 @@ function __merge(crListReplica, crListDelta) {
660
753
  const change = {};
661
754
  let tailTombstoneMovedCursor = false;
662
755
  let needsRelink = false;
663
- if (Object.hasOwn(crListDelta, "values") && Array.isArray(crListDelta.values) && crListDelta.values.length === 1 && (!Object.hasOwn(crListDelta, "tombstones") || Array.isArray(crListDelta.tombstones) && crListDelta.tombstones.length === 0)) {
756
+ const expandedValues = Object.hasOwn(crListDelta, "values") && Array.isArray(crListDelta.values) ? crListDelta.values.flatMap((entry) => {
757
+ if (!entry || !entry.tail || entry.tail.length === 0) return [entry];
758
+ return [
759
+ entry,
760
+ ...entry.tail.map((value, i) => ({
761
+ uuidv7: deriveRunUuid(entry.uuidv7, i + 1),
762
+ value,
763
+ predecessor: deriveRunUuid(entry.uuidv7, i)
764
+ }))
765
+ ];
766
+ }) : void 0;
767
+ if (_optionalChain([expandedValues, 'optionalAccess', _84 => _84.length]) === 1 && (!Object.hasOwn(crListDelta, "tombstones") || Array.isArray(crListDelta.tombstones) && crListDelta.tombstones.length === 0)) {
664
768
  const linkedListEntry = materializeSnapshotEntry(
665
- crListDelta.values[0],
769
+ expandedValues[0],
666
770
  crListReplica
667
771
  );
668
772
  if (!linkedListEntry) return false;
@@ -675,7 +779,7 @@ function __merge(crListReplica, crListDelta) {
675
779
  crListReplica.cursorIndex = linkedListEntry.index;
676
780
  void attachEntryToIndexes(crListReplica, linkedListEntry);
677
781
  crListReplica.size = crListReplica.parentMap.size;
678
- void _optionalChain([crListReplica, 'access', _73 => _73.index, 'optionalAccess', _74 => _74.set, 'call', _75 => _75(linkedListEntry.index, linkedListEntry)]);
782
+ void _optionalChain([crListReplica, 'access', _85 => _85.index, 'optionalAccess', _86 => _86.set, 'call', _87 => _87(linkedListEntry.index, linkedListEntry)]);
679
783
  return { [linkedListEntry.index]: linkedListEntry.value };
680
784
  }
681
785
  }
@@ -689,19 +793,19 @@ function __merge(crListReplica, crListDelta) {
689
793
  const wasTail = linkedListEntry.next === void 0;
690
794
  const wasCursor = crListReplica.cursor === linkedListEntry;
691
795
  void newTombsIndices.push(linkedListEntry.index);
692
- void _optionalChain([crListReplica, 'access', _76 => _76.index, 'optionalAccess', _77 => _77.delete, 'call', _78 => _78(linkedListEntry.index)]);
796
+ void _optionalChain([crListReplica, 'access', _88 => _88.index, 'optionalAccess', _89 => _89.delete, 'call', _90 => _90(linkedListEntry.index)]);
693
797
  void deleteLiveEntry(crListReplica, linkedListEntry);
694
798
  tailTombstoneMovedCursor = wasTail && wasCursor;
695
799
  needsRelink = true;
696
800
  }
697
801
  }
698
802
  }
699
- if (!Object.hasOwn(crListDelta, "values") || !Array.isArray(crListDelta.values) || crListDelta.values.length === 0 && tailTombstoneMovedCursor) {
803
+ if (!expandedValues || expandedValues.length === 0 && tailTombstoneMovedCursor) {
700
804
  if (newTombsIndices.length === 0) return false;
701
805
  if (newTombsIndices.length === 1 && tailTombstoneMovedCursor) {
702
806
  if (crListReplica.cursor) {
703
807
  crListReplica.cursorIndex = crListReplica.size - 1;
704
- void _optionalChain([crListReplica, 'access', _79 => _79.index, 'optionalAccess', _80 => _80.set, 'call', _81 => _81(
808
+ void _optionalChain([crListReplica, 'access', _91 => _91.index, 'optionalAccess', _92 => _92.set, 'call', _93 => _93(
705
809
  crListReplica.cursorIndex,
706
810
  crListReplica.cursor
707
811
  )]);
@@ -717,7 +821,7 @@ function __merge(crListReplica, crListDelta) {
717
821
  }
718
822
  return change;
719
823
  }
720
- for (const valueEntry of crListDelta.values) {
824
+ for (const valueEntry of expandedValues) {
721
825
  if (valueEntry === null || valueEntry === void 0) continue;
722
826
  const existingEntry = crListReplica.parentMap.get(valueEntry.uuidv7);
723
827
  if (existingEntry) {
@@ -748,7 +852,7 @@ function __merge(crListReplica, crListDelta) {
748
852
  crListReplica.cursor = linkedListEntry;
749
853
  crListReplica.cursorIndex = linkedListEntry.index;
750
854
  crListReplica.size = crListReplica.parentMap.size;
751
- void _optionalChain([crListReplica, 'access', _82 => _82.index, 'optionalAccess', _83 => _83.set, 'call', _84 => _84(linkedListEntry.index, linkedListEntry)]);
855
+ void _optionalChain([crListReplica, 'access', _94 => _94.index, 'optionalAccess', _95 => _95.set, 'call', _96 => _96(linkedListEntry.index, linkedListEntry)]);
752
856
  } else {
753
857
  needsRelink = true;
754
858
  }
@@ -759,7 +863,7 @@ function __merge(crListReplica, crListDelta) {
759
863
  crListReplica.cursor = linkedListEntry;
760
864
  crListReplica.cursorIndex = linkedListEntry.index;
761
865
  crListReplica.size = crListReplica.parentMap.size;
762
- void _optionalChain([crListReplica, 'access', _85 => _85.index, 'optionalAccess', _86 => _86.set, 'call', _87 => _87(linkedListEntry.index, linkedListEntry)]);
866
+ void _optionalChain([crListReplica, 'access', _97 => _97.index, 'optionalAccess', _98 => _98.set, 'call', _99 => _99(linkedListEntry.index, linkedListEntry)]);
763
867
  } else {
764
868
  needsRelink = true;
765
869
  }
@@ -924,32 +1028,32 @@ var CRList = class {
924
1028
  return this.state.size;
925
1029
  }
926
1030
  /**
927
- * Inserts a value before an index.
1031
+ * Inserts values before an index.
928
1032
  *
929
- * If `beforeIndex` is omitted, the value is inserted at the start of the list.
1033
+ * If `beforeIndex` is omitted, values are inserted at the start of the list.
930
1034
  *
931
- * @param value - The value to insert.
1035
+ * @param values - Values to insert.
932
1036
  * @param beforeIndex - The index to insert before.
933
1037
  */
934
- prepend(value, beforeIndex) {
935
- const result = __update(_nullishCoalesce(beforeIndex, () => ( 0)), [value], this.state, "before");
1038
+ prepend(values, beforeIndex) {
1039
+ const result = __update(_nullishCoalesce(beforeIndex, () => ( 0)), values, this.state, "before");
936
1040
  if (!result) return;
937
1041
  const { delta, change } = result;
938
1042
  if (delta) void dispatchCRListEvent(this.eventTarget, "delta", delta);
939
1043
  if (change) void dispatchCRListEvent(this.eventTarget, "change", change);
940
1044
  }
941
1045
  /**
942
- * Inserts a value after an index.
1046
+ * Inserts values after an index.
943
1047
  *
944
- * If `afterIndex` is omitted, the value is appended at the end of the list.
1048
+ * If `afterIndex` is omitted, values are appended at the end of the list.
945
1049
  *
946
- * @param value - The value to insert.
1050
+ * @param values - Values to insert.
947
1051
  * @param afterIndex - The index to insert after.
948
1052
  */
949
- append(value, afterIndex) {
1053
+ append(values, afterIndex) {
950
1054
  const result = __update(
951
1055
  _nullishCoalesce(afterIndex, () => ( this.state.size)),
952
- [value],
1056
+ values,
953
1057
  this.state,
954
1058
  "after"
955
1059
  );
@@ -958,6 +1062,19 @@ var CRList = class {
958
1062
  if (delta) void dispatchCRListEvent(this.eventTarget, "delta", delta);
959
1063
  if (change) void dispatchCRListEvent(this.eventTarget, "change", change);
960
1064
  }
1065
+ /**
1066
+ * Overwrites entries starting at an index.
1067
+ *
1068
+ * @param index - The index to start overwriting at.
1069
+ * @param values - Values to write.
1070
+ */
1071
+ update(index, values) {
1072
+ const result = __update(index, values, this.state, "overwrite");
1073
+ if (!result) return;
1074
+ const { delta, change } = result;
1075
+ if (delta) void dispatchCRListEvent(this.eventTarget, "delta", delta);
1076
+ if (change) void dispatchCRListEvent(this.eventTarget, "change", change);
1077
+ }
961
1078
  /**
962
1079
  * Removes one or more entries starting at an index.
963
1080
  *
@@ -981,8 +1098,8 @@ var CRList = class {
981
1098
  * @param thisArg - Optional `this` value for the predicate.
982
1099
  */
983
1100
  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;
1101
+ let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _100 => _100.state, 'access', _101 => _101.index, 'optionalAccess', _102 => _102.get, 'call', _103 => _103(0)]), () => ( this.state.cursor));
1102
+ while (_optionalChain([linkedListEntry, 'optionalAccess', _104 => _104.prev])) linkedListEntry = linkedListEntry.prev;
986
1103
  let index = 0;
987
1104
  while (linkedListEntry) {
988
1105
  if (predicate.call(thisArg, linkedListEntry.value, index, this))
@@ -1092,8 +1209,8 @@ var CRList = class {
1092
1209
  * Iterates over current live values in index order.
1093
1210
  */
1094
1211
  *[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;
1212
+ let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _105 => _105.state, 'access', _106 => _106.index, 'optionalAccess', _107 => _107.get, 'call', _108 => _108(0)]), () => ( this.state.cursor));
1213
+ while (_optionalChain([linkedListEntry, 'optionalAccess', _109 => _109.prev])) linkedListEntry = linkedListEntry.prev;
1097
1214
  while (linkedListEntry) {
1098
1215
  yield linkedListEntry.value;
1099
1216
  linkedListEntry = linkedListEntry.next;
@@ -1109,8 +1226,8 @@ var CRList = class {
1109
1226
  * @param thisArg - Optional `this` value for the callback.
1110
1227
  */
1111
1228
  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;
1229
+ let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _110 => _110.state, 'access', _111 => _111.index, 'optionalAccess', _112 => _112.get, 'call', _113 => _113(0)]), () => ( this.state.cursor));
1230
+ while (_optionalChain([linkedListEntry, 'optionalAccess', _114 => _114.prev])) linkedListEntry = linkedListEntry.prev;
1114
1231
  let index = 0;
1115
1232
  while (linkedListEntry) {
1116
1233
  void callback.call(thisArg, linkedListEntry.value, index, this);