@sovereignbase/convergent-replicated-list 1.2.1 → 1.3.3
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/README.md +229 -55
- package/dist/index.cjs +100 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +81 -9
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
package/dist/index.cjs
CHANGED
|
@@ -237,10 +237,43 @@ function moveEntryToPredecessor(crListReplica, linkedListEntry, predecessor, del
|
|
|
237
237
|
|
|
238
238
|
// src/.helpers/indexFromPropertyKey/index.ts
|
|
239
239
|
function indexFromPropertyKey(index) {
|
|
240
|
-
if (typeof index !== "string"
|
|
241
|
-
return void 0;
|
|
240
|
+
if (typeof index !== "string") return void 0;
|
|
242
241
|
const listIndex = Number(index);
|
|
243
|
-
|
|
242
|
+
if (!Number.isSafeInteger(listIndex) || listIndex < 0) return void 0;
|
|
243
|
+
return String(listIndex) === index ? listIndex : void 0;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/.helpers/trySpliceInsertedParent/index.ts
|
|
247
|
+
function trySpliceInsertedParent(crListReplica, insertedEntries, reparentedEntries) {
|
|
248
|
+
if (insertedEntries.length !== 1 || reparentedEntries.length !== 1)
|
|
249
|
+
return false;
|
|
250
|
+
const inserted = insertedEntries[0];
|
|
251
|
+
const reparented = reparentedEntries[0];
|
|
252
|
+
const moved = reparented.entry;
|
|
253
|
+
if (moved.predecessor !== inserted.uuidv7 || inserted.predecessor !== reparented.previousPredecessor)
|
|
254
|
+
return false;
|
|
255
|
+
const siblings = crListReplica.childrenMap.get(inserted.predecessor);
|
|
256
|
+
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)
|
|
258
|
+
return false;
|
|
259
|
+
const predecessor = inserted.predecessor === "\0" ? void 0 : crListReplica.parentMap.get(inserted.predecessor);
|
|
260
|
+
if (inserted.predecessor !== "\0" && !predecessor) return false;
|
|
261
|
+
const expectedIndex = predecessor ? predecessor.index + 1 : 0;
|
|
262
|
+
if (moved.index !== expectedIndex || moved.prev !== predecessor || predecessor && predecessor.next !== moved)
|
|
263
|
+
return false;
|
|
264
|
+
void linkEntryBetween(predecessor, inserted, moved);
|
|
265
|
+
let current = inserted;
|
|
266
|
+
let index = expectedIndex;
|
|
267
|
+
while (current) {
|
|
268
|
+
current.index = index;
|
|
269
|
+
index++;
|
|
270
|
+
current = current.next;
|
|
271
|
+
}
|
|
272
|
+
crListReplica.index = /* @__PURE__ */ new Map([[inserted.index, inserted]]);
|
|
273
|
+
crListReplica.cursor = inserted;
|
|
274
|
+
crListReplica.cursorIndex = inserted.index;
|
|
275
|
+
crListReplica.size = crListReplica.parentMap.size;
|
|
276
|
+
return true;
|
|
244
277
|
}
|
|
245
278
|
|
|
246
279
|
// src/core/crud/create/index.ts
|
|
@@ -274,11 +307,11 @@ function __create(snapshot) {
|
|
|
274
307
|
);
|
|
275
308
|
if (!linkedListEntry) continue;
|
|
276
309
|
void attachEntryToIndexes(crListReplica, linkedListEntry);
|
|
277
|
-
if (canUseLinearProjection && linkedListEntry.predecessor === (_nullishCoalesce(_optionalChain([previous, 'optionalAccess',
|
|
310
|
+
if (canUseLinearProjection && linkedListEntry.predecessor === (_nullishCoalesce(_optionalChain([previous, 'optionalAccess', _20 => _20.uuidv7]), () => ( "\0")))) {
|
|
278
311
|
linkedListEntry.index = crListReplica.parentMap.size - 1;
|
|
279
312
|
void linkEntryBetween(previous, linkedListEntry, void 0);
|
|
280
313
|
previous = linkedListEntry;
|
|
281
|
-
void _optionalChain([crListReplica, 'access',
|
|
314
|
+
void _optionalChain([crListReplica, 'access', _21 => _21.index, 'optionalAccess', _22 => _22.set, 'call', _23 => _23(linkedListEntry.index, linkedListEntry)]);
|
|
282
315
|
continue;
|
|
283
316
|
}
|
|
284
317
|
canUseLinearProjection = false;
|
|
@@ -297,7 +330,7 @@ function __create(snapshot) {
|
|
|
297
330
|
function __read(targetIndex, crListReplica) {
|
|
298
331
|
try {
|
|
299
332
|
void seekCursorToIndex(targetIndex, crListReplica);
|
|
300
|
-
return _optionalChain([crListReplica, 'access',
|
|
333
|
+
return _optionalChain([crListReplica, 'access', _24 => _24.cursor, 'optionalAccess', _25 => _25.value]);
|
|
301
334
|
} catch (e) {
|
|
302
335
|
return void 0;
|
|
303
336
|
}
|
|
@@ -333,7 +366,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
333
366
|
crListReplica.cursor = linkedListEntry;
|
|
334
367
|
crListReplica.cursorIndex = linkedListEntry.index;
|
|
335
368
|
void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
|
|
336
|
-
_optionalChain([crListReplica, 'access',
|
|
369
|
+
_optionalChain([crListReplica, 'access', _26 => _26.index, 'optionalAccess', _27 => _27.set, 'call', _28 => _28(linkedListEntry.index, linkedListEntry)]);
|
|
337
370
|
change[linkedListEntry.index] = linkedListEntry.value;
|
|
338
371
|
break;
|
|
339
372
|
}
|
|
@@ -349,7 +382,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
349
382
|
void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
|
|
350
383
|
crListReplica.cursor = linkedListEntry;
|
|
351
384
|
crListReplica.cursorIndex = linkedListEntry.index;
|
|
352
|
-
void _optionalChain([crListReplica, 'access',
|
|
385
|
+
void _optionalChain([crListReplica, 'access', _29 => _29.index, 'optionalAccess', _30 => _30.set, 'call', _31 => _31(linkedListEntry.index, linkedListEntry)]);
|
|
353
386
|
change[linkedListEntry.index] = linkedListEntry.value;
|
|
354
387
|
break;
|
|
355
388
|
}
|
|
@@ -376,13 +409,13 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
376
409
|
}
|
|
377
410
|
void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
|
|
378
411
|
void crListReplica.tombstones.add(entryToOverwrite.uuidv7);
|
|
379
|
-
void _optionalChain([delta, 'access',
|
|
412
|
+
void _optionalChain([delta, 'access', _32 => _32.tombstones, 'optionalAccess', _33 => _33.push, 'call', _34 => _34(entryToOverwrite.uuidv7)]);
|
|
380
413
|
void detachEntryFromIndexes(crListReplica, entryToOverwrite);
|
|
381
414
|
entryToOverwrite.next = void 0;
|
|
382
415
|
entryToOverwrite.prev = void 0;
|
|
383
416
|
crListReplica.cursor = linkedListEntry;
|
|
384
417
|
crListReplica.cursorIndex = actualIndex;
|
|
385
|
-
void _optionalChain([crListReplica, 'access',
|
|
418
|
+
void _optionalChain([crListReplica, 'access', _35 => _35.index, 'optionalAccess', _36 => _36.set, 'call', _37 => _37(linkedListEntry.index, linkedListEntry)]);
|
|
386
419
|
change[actualIndex] = linkedListEntry.value;
|
|
387
420
|
break;
|
|
388
421
|
}
|
|
@@ -391,7 +424,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
391
424
|
crListReplica.cursor = linkedListEntry;
|
|
392
425
|
crListReplica.cursorIndex = linkedListEntry.index;
|
|
393
426
|
void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
|
|
394
|
-
void _optionalChain([crListReplica, 'access',
|
|
427
|
+
void _optionalChain([crListReplica, 'access', _38 => _38.index, 'optionalAccess', _39 => _39.set, 'call', _40 => _40(linkedListEntry.index, linkedListEntry)]);
|
|
395
428
|
change[linkedListEntry.index] = linkedListEntry.value;
|
|
396
429
|
break;
|
|
397
430
|
}
|
|
@@ -420,7 +453,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
420
453
|
crListReplica.cursor = linkedListEntry;
|
|
421
454
|
crListReplica.cursorIndex = linkedListEntry.index;
|
|
422
455
|
if (next) crListReplica.index = /* @__PURE__ */ new Map();
|
|
423
|
-
void _optionalChain([crListReplica, 'access',
|
|
456
|
+
void _optionalChain([crListReplica, 'access', _41 => _41.index, 'optionalAccess', _42 => _42.set, 'call', _43 => _43(linkedListEntry.index, linkedListEntry)]);
|
|
424
457
|
change[linkedListEntry.index] = linkedListEntry.value;
|
|
425
458
|
break;
|
|
426
459
|
}
|
|
@@ -429,7 +462,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
429
462
|
crListReplica.cursor = linkedListEntry;
|
|
430
463
|
crListReplica.cursorIndex = linkedListEntry.index;
|
|
431
464
|
void attachEntryToIndexes(crListReplica, linkedListEntry, delta);
|
|
432
|
-
void _optionalChain([crListReplica, 'access',
|
|
465
|
+
void _optionalChain([crListReplica, 'access', _44 => _44.index, 'optionalAccess', _45 => _45.set, 'call', _46 => _46(linkedListEntry.index, linkedListEntry)]);
|
|
433
466
|
change[linkedListEntry.index] = linkedListEntry.value;
|
|
434
467
|
mode = "after";
|
|
435
468
|
listIndex = linkedListEntry.index - 1;
|
|
@@ -440,7 +473,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
440
473
|
const actualIndex = _nullishCoalesce(crListReplica.cursorIndex, () => ( listIndex));
|
|
441
474
|
const prev = crListReplica.cursor.prev;
|
|
442
475
|
linkedListEntry.index = actualIndex;
|
|
443
|
-
linkedListEntry.predecessor = _nullishCoalesce(_optionalChain([prev, 'optionalAccess',
|
|
476
|
+
linkedListEntry.predecessor = _nullishCoalesce(_optionalChain([prev, 'optionalAccess', _47 => _47.uuidv7]), () => ( "\0"));
|
|
444
477
|
void linkEntryBetween(prev, linkedListEntry, crListReplica.cursor);
|
|
445
478
|
if (crListReplica.cursor.predecessor === linkedListEntry.predecessor) {
|
|
446
479
|
void moveEntryToPredecessor(
|
|
@@ -454,7 +487,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
454
487
|
crListReplica.cursor = linkedListEntry;
|
|
455
488
|
crListReplica.cursorIndex = actualIndex;
|
|
456
489
|
crListReplica.index = /* @__PURE__ */ new Map();
|
|
457
|
-
void _optionalChain([crListReplica, 'access',
|
|
490
|
+
void _optionalChain([crListReplica, 'access', _48 => _48.index, 'optionalAccess', _49 => _49.set, 'call', _50 => _50(linkedListEntry.index, linkedListEntry)]);
|
|
458
491
|
change[actualIndex] = linkedListEntry.value;
|
|
459
492
|
mode = "after";
|
|
460
493
|
listIndex = linkedListEntry.index - 1;
|
|
@@ -468,6 +501,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
468
501
|
}
|
|
469
502
|
|
|
470
503
|
// src/core/crud/delete/index.ts
|
|
504
|
+
|
|
471
505
|
function __delete(crListReplica, startIndex, endIndex) {
|
|
472
506
|
const change = {};
|
|
473
507
|
const delta = { values: [], tombstones: [] };
|
|
@@ -480,17 +514,43 @@ function __delete(crListReplica, startIndex, endIndex) {
|
|
|
480
514
|
void seekCursorToIndex(listIndex, crListReplica);
|
|
481
515
|
if (!crListReplica.cursor) return false;
|
|
482
516
|
let current = crListReplica.cursor;
|
|
517
|
+
const predecessor = _nullishCoalesce(_optionalChain([current, 'access', _51 => _51.prev, 'optionalAccess', _52 => _52.uuidv7]), () => ( "\0"));
|
|
518
|
+
const deletedIds = /* @__PURE__ */ new Set();
|
|
483
519
|
let deleted = 0;
|
|
484
520
|
let currentIndex = _nullishCoalesce(crListReplica.cursorIndex, () => ( listIndex));
|
|
485
521
|
while (current && deleted < deleteCount) {
|
|
486
522
|
const next = current.next;
|
|
487
523
|
change[currentIndex] = void 0;
|
|
488
|
-
void
|
|
524
|
+
void deletedIds.add(current.uuidv7);
|
|
525
|
+
void _optionalChain([crListReplica, 'access', _53 => _53.index, 'optionalAccess', _54 => _54.delete, 'call', _55 => _55(currentIndex)]);
|
|
489
526
|
void deleteLiveEntry(crListReplica, current, delta);
|
|
490
527
|
current = next;
|
|
491
528
|
currentIndex++;
|
|
492
529
|
deleted++;
|
|
493
530
|
}
|
|
531
|
+
if (current && deletedIds.has(current.predecessor)) {
|
|
532
|
+
const replacement = {
|
|
533
|
+
uuidv7: _uuid.v7.call(void 0, ),
|
|
534
|
+
value: current.value,
|
|
535
|
+
predecessor,
|
|
536
|
+
index: listIndex,
|
|
537
|
+
next: void 0,
|
|
538
|
+
prev: void 0
|
|
539
|
+
};
|
|
540
|
+
const prev = current.prev;
|
|
541
|
+
const next = current.next;
|
|
542
|
+
void deleteLiveEntry(crListReplica, current, delta);
|
|
543
|
+
void linkEntryBetween(prev, replacement, next);
|
|
544
|
+
void attachEntryToIndexes(crListReplica, replacement, delta);
|
|
545
|
+
if (_optionalChain([next, 'optionalAccess', _56 => _56.predecessor]) === current.uuidv7)
|
|
546
|
+
void moveEntryToPredecessor(
|
|
547
|
+
crListReplica,
|
|
548
|
+
next,
|
|
549
|
+
replacement.uuidv7,
|
|
550
|
+
delta
|
|
551
|
+
);
|
|
552
|
+
current = replacement;
|
|
553
|
+
}
|
|
494
554
|
crListReplica.size = crListReplica.parentMap.size;
|
|
495
555
|
crListReplica.cursor = _nullishCoalesce(current, () => ( crListReplica.cursor));
|
|
496
556
|
crListReplica.cursorIndex = current ? listIndex : crListReplica.cursor ? Math.max(0, crListReplica.size - 1) : void 0;
|
|
@@ -509,6 +569,7 @@ function __merge(crListReplica, crListDelta) {
|
|
|
509
569
|
if (!crListDelta || _utils.prototype.call(void 0, crListDelta) !== "record") return false;
|
|
510
570
|
const newVals = [];
|
|
511
571
|
const newTombsIndices = [];
|
|
572
|
+
const reparentedVals = [];
|
|
512
573
|
const change = {};
|
|
513
574
|
let needsRelink = false;
|
|
514
575
|
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)) {
|
|
@@ -526,7 +587,7 @@ function __merge(crListReplica, crListDelta) {
|
|
|
526
587
|
crListReplica.cursorIndex = linkedListEntry.index;
|
|
527
588
|
void attachEntryToIndexes(crListReplica, linkedListEntry);
|
|
528
589
|
crListReplica.size = crListReplica.parentMap.size;
|
|
529
|
-
void _optionalChain([crListReplica, 'access',
|
|
590
|
+
void _optionalChain([crListReplica, 'access', _57 => _57.index, 'optionalAccess', _58 => _58.set, 'call', _59 => _59(linkedListEntry.index, linkedListEntry)]);
|
|
530
591
|
return { [linkedListEntry.index]: linkedListEntry.value };
|
|
531
592
|
}
|
|
532
593
|
}
|
|
@@ -538,7 +599,7 @@ function __merge(crListReplica, crListDelta) {
|
|
|
538
599
|
const linkedListEntry = crListReplica.parentMap.get(tombstone);
|
|
539
600
|
if (linkedListEntry) {
|
|
540
601
|
void newTombsIndices.push(linkedListEntry.index);
|
|
541
|
-
void _optionalChain([crListReplica, 'access',
|
|
602
|
+
void _optionalChain([crListReplica, 'access', _60 => _60.index, 'optionalAccess', _61 => _61.delete, 'call', _62 => _62(linkedListEntry.index)]);
|
|
542
603
|
void deleteLiveEntry(crListReplica, linkedListEntry);
|
|
543
604
|
needsRelink = true;
|
|
544
605
|
}
|
|
@@ -560,11 +621,13 @@ function __merge(crListReplica, crListDelta) {
|
|
|
560
621
|
if (valueEntry.predecessor !== "\0" && !_utils.isUuidV7.call(void 0, valueEntry.predecessor))
|
|
561
622
|
continue;
|
|
562
623
|
if (existingEntry.predecessor >= valueEntry.predecessor) continue;
|
|
624
|
+
const previousPredecessor = existingEntry.predecessor;
|
|
563
625
|
void moveEntryToPredecessor(
|
|
564
626
|
crListReplica,
|
|
565
627
|
existingEntry,
|
|
566
628
|
valueEntry.predecessor
|
|
567
629
|
);
|
|
630
|
+
void reparentedVals.push({ entry: existingEntry, previousPredecessor });
|
|
568
631
|
needsRelink = true;
|
|
569
632
|
continue;
|
|
570
633
|
}
|
|
@@ -581,7 +644,7 @@ function __merge(crListReplica, crListDelta) {
|
|
|
581
644
|
crListReplica.cursor = linkedListEntry;
|
|
582
645
|
crListReplica.cursorIndex = linkedListEntry.index;
|
|
583
646
|
crListReplica.size = crListReplica.parentMap.size;
|
|
584
|
-
void _optionalChain([crListReplica, 'access',
|
|
647
|
+
void _optionalChain([crListReplica, 'access', _63 => _63.index, 'optionalAccess', _64 => _64.set, 'call', _65 => _65(linkedListEntry.index, linkedListEntry)]);
|
|
585
648
|
} else {
|
|
586
649
|
needsRelink = true;
|
|
587
650
|
}
|
|
@@ -592,13 +655,15 @@ function __merge(crListReplica, crListDelta) {
|
|
|
592
655
|
crListReplica.cursor = linkedListEntry;
|
|
593
656
|
crListReplica.cursorIndex = linkedListEntry.index;
|
|
594
657
|
crListReplica.size = crListReplica.parentMap.size;
|
|
595
|
-
void _optionalChain([crListReplica, 'access',
|
|
658
|
+
void _optionalChain([crListReplica, 'access', _66 => _66.index, 'optionalAccess', _67 => _67.set, 'call', _68 => _68(linkedListEntry.index, linkedListEntry)]);
|
|
596
659
|
} else {
|
|
597
660
|
needsRelink = true;
|
|
598
661
|
}
|
|
599
662
|
}
|
|
600
663
|
if (needsRelink) {
|
|
601
|
-
|
|
664
|
+
if (!trySpliceInsertedParent(crListReplica, newVals, reparentedVals)) {
|
|
665
|
+
void rebuildLiveProjection(crListReplica);
|
|
666
|
+
}
|
|
602
667
|
}
|
|
603
668
|
if (newTombsIndices.length === 0 && newVals.length === 0) return false;
|
|
604
669
|
for (const index of newTombsIndices) {
|
|
@@ -801,8 +866,8 @@ var CRList = class {
|
|
|
801
866
|
* @param thisArg - Optional `this` value for the predicate.
|
|
802
867
|
*/
|
|
803
868
|
find(predicate, thisArg) {
|
|
804
|
-
let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access',
|
|
805
|
-
while (_optionalChain([linkedListEntry, 'optionalAccess',
|
|
869
|
+
let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _69 => _69.state, 'access', _70 => _70.index, 'optionalAccess', _71 => _71.get, 'call', _72 => _72(0)]), () => ( this.state.cursor));
|
|
870
|
+
while (_optionalChain([linkedListEntry, 'optionalAccess', _73 => _73.prev])) linkedListEntry = linkedListEntry.prev;
|
|
806
871
|
let index = 0;
|
|
807
872
|
while (linkedListEntry) {
|
|
808
873
|
if (predicate.call(thisArg, linkedListEntry.value, index, this))
|
|
@@ -912,9 +977,11 @@ var CRList = class {
|
|
|
912
977
|
* Iterates over current live values in index order.
|
|
913
978
|
*/
|
|
914
979
|
*[Symbol.iterator]() {
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
980
|
+
let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _74 => _74.state, 'access', _75 => _75.index, 'optionalAccess', _76 => _76.get, 'call', _77 => _77(0)]), () => ( this.state.cursor));
|
|
981
|
+
while (_optionalChain([linkedListEntry, 'optionalAccess', _78 => _78.prev])) linkedListEntry = linkedListEntry.prev;
|
|
982
|
+
while (linkedListEntry) {
|
|
983
|
+
yield linkedListEntry.value;
|
|
984
|
+
linkedListEntry = linkedListEntry.next;
|
|
918
985
|
}
|
|
919
986
|
}
|
|
920
987
|
/**
|
|
@@ -927,8 +994,13 @@ var CRList = class {
|
|
|
927
994
|
* @param thisArg - Optional `this` value for the callback.
|
|
928
995
|
*/
|
|
929
996
|
forEach(callback, thisArg) {
|
|
930
|
-
|
|
931
|
-
|
|
997
|
+
let linkedListEntry = _nullishCoalesce(_optionalChain([this, 'access', _79 => _79.state, 'access', _80 => _80.index, 'optionalAccess', _81 => _81.get, 'call', _82 => _82(0)]), () => ( this.state.cursor));
|
|
998
|
+
while (_optionalChain([linkedListEntry, 'optionalAccess', _83 => _83.prev])) linkedListEntry = linkedListEntry.prev;
|
|
999
|
+
let index = 0;
|
|
1000
|
+
while (linkedListEntry) {
|
|
1001
|
+
void callback.call(thisArg, linkedListEntry.value, index, this);
|
|
1002
|
+
linkedListEntry = linkedListEntry.next;
|
|
1003
|
+
index++;
|
|
932
1004
|
}
|
|
933
1005
|
}
|
|
934
1006
|
};
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/convergent-replicated-list/convergent-replicated-list/dist/index.cjs","../src/.helpers/rebuildLiveIndex/index.ts","../src/.errors/class.ts","../src/.helpers/seekCursorToIndex/index.ts","../src/.helpers/linkEntryBetween/index.ts","../src/.helpers/rebuildLiveProjection/index.ts","../src/.helpers/materializeSnapshotEntry/index.ts","../src/.helpers/attachEntryToIndexes/index.ts","../src/.helpers/detachEntryFromIndexes/index.ts","../src/.helpers/deleteLiveEntry/index.ts","../src/.helpers/dispatchCRListEvent/index.ts","../src/.helpers/moveEntryToPredecessor/index.ts","../src/.helpers/indexFromPropertyKey/index.ts","../src/core/crud/create/index.ts","../src/core/crud/read/index.ts","../src/core/crud/update/index.ts","../src/core/crud/delete/index.ts","../src/core/mags/merge/index.ts","../src/core/mags/acknowledge/index.ts","../src/core/mags/garbageCollect/index.ts","../src/core/mags/snapshot/index.ts","../src/CRList/class.ts"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;ACZO,SAAS,gBAAA,CAAoB,aAAA,EAAqC;AACvE,EAAA,GAAA,CAAI,CAAC,aAAA,CAAc,MAAA,EAAQ;AACzB,oBAAA,aAAA,qBAAc,KAAA,6BAAO,KAAA,mBAAM,GAAA;AAC3B,IAAA,aAAA,CAAc,YAAA,EAAc,KAAA,CAAA;AAC5B,IAAA,MAAA;AAAA,EACF;AACA,EAAA,IAAI,MAAA,EAAQ,aAAA,CAAc,IAAA;AAC1B,EAAA,MAAM,QAAA,mBAAU,aAAA,CAAc,KAAA,0BAAS,IAAI,GAAA,CAAI,GAAA;AAC/C,EAAA,KAAK,OAAA,CAAQ,KAAA,CAAM,CAAA;AACnB,EAAA,MAAA,CAAO,aAAA,CAAc,MAAA,CAAO,IAAA;AAC1B,IAAA,aAAA,CAAc,OAAA,EAAS,aAAA,CAAc,MAAA,CAAO,IAAA;AAE9C,EAAA,MAAA,CAAO,MAAA,GAAS,CAAA,EAAG;AACjB,IAAA,KAAA,EAAA;AACA,IAAA,aAAA,CAAc,MAAA,CAAO,MAAA,EAAQ,KAAA;AAC7B,IAAA,KAAK,OAAA,CAAQ,GAAA,CAAI,KAAA,EAAO,aAAA,CAAc,MAAM,CAAA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AA;AAAA;AAAA;AAAA;AAAA;AAaQ,EAAA;AAC0C,IAAA;AACM,MAAA;AACtD,IAAA;AACF,EAAA;AACF;ArB8kBgE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/convergent-replicated-list/convergent-replicated-list/dist/index.cjs","sourcesContent":[null,"import type { CRListState } from '../../.types/index.js'\n\n/**\n * Rebuilds the opportunistic index cache from the current live projection.\n */\nexport function rebuildLiveIndex<T>(crListReplica: CRListState<T>): void {\n if (!crListReplica.cursor) {\n crListReplica.index?.clear()\n crListReplica.cursorIndex = undefined\n return\n }\n let index = crListReplica.size\n const entries = crListReplica.index ?? new Map()\n void entries.clear()\n while (crListReplica.cursor.next)\n crListReplica.cursor = crListReplica.cursor.next\n\n while (index >= 1) {\n index--\n crListReplica.cursor.index = index\n void entries.set(index, crListReplica.cursor)\n if (crListReplica.cursor.prev === undefined) break\n crListReplica.cursor = crListReplica.cursor.prev\n }\n crListReplica.index = entries\n crListReplica.cursorIndex = 0\n}\n","/**\n * Error codes thrown by {@link CRList}.\n */\nexport type CRListErrorCode =\n | 'VALUE_NOT_CLONEABLE'\n | 'INDEX_OUT_OF_BOUNDS'\n | 'LIST_EMPTY'\n | 'LIST_INTEGRITY_VIOLATION'\n | 'UPDATE_EXPECTED_AN_ARRAY'\n\n/**\n * Represents a typed CRList runtime error.\n */\nexport class CRListError extends Error {\n /**\n * The semantic error code for the failure.\n */\n readonly code: CRListErrorCode\n\n /**\n * Creates a typed CRList error.\n *\n * @param code - The semantic error code.\n * @param message - An optional human-readable detail message.\n */\n constructor(code: CRListErrorCode, message?: string) {\n const detail = message ?? code\n super(`{@sovereignbase/convergent-replicated-list} ${detail}`)\n this.code = code\n this.name = 'CRListError'\n }\n}\n","import type { CRListState } from '../../.types/index.js'\nimport { CRListError } from '../../.errors/class.js'\n\n/**\n * Moves the replica cursor to a live index.\n *\n * A valid cached index entry is used directly. Stale cache entries are dropped\n * and the cursor walks from its current position, then repairs the cache at the\n * requested index.\n */\nexport function seekCursorToIndex<T>(\n targetIndex: number,\n crListReplica: CRListState<T>\n): void {\n if (targetIndex < 0 || targetIndex >= crListReplica.size)\n throw new CRListError('INDEX_OUT_OF_BOUNDS', 'Index out of bounds')\n const indexedEntry = crListReplica.index?.get(targetIndex)\n if (indexedEntry) {\n if (crListReplica.parentMap.get(indexedEntry.uuidv7) === indexedEntry) {\n crListReplica.cursor = indexedEntry\n crListReplica.cursorIndex = targetIndex\n return\n } else {\n void crListReplica.index?.delete(targetIndex)\n }\n }\n if (!crListReplica.cursor)\n throw new CRListError('LIST_EMPTY', 'List is empty')\n let cursorIndex = crListReplica.cursorIndex ?? crListReplica.cursor.index\n const direction = cursorIndex > targetIndex ? 'prev' : 'next'\n while (crListReplica.cursor && cursorIndex !== targetIndex) {\n crListReplica.cursor = crListReplica.cursor[direction]\n cursorIndex += direction === 'next' ? 1 : -1\n }\n if (crListReplica.cursor) {\n crListReplica.cursorIndex = targetIndex\n void crListReplica.index?.set(targetIndex, crListReplica.cursor)\n }\n}\n","import type { CRListStateEntry } from '../../.types/index.js'\n\n/**\n * Links a live entry between optional neighboring projection entries.\n */\nexport function linkEntryBetween<T>(\n prev: CRListStateEntry<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n next: CRListStateEntry<T>\n): void {\n linkedListEntry.prev = prev\n linkedListEntry.next = next\n if (prev) prev.next = linkedListEntry\n if (next) next.prev = linkedListEntry\n}\n","import type { CRListState, CRListStateEntry } from '../../.types/index.js'\nimport { linkEntryBetween } from '../linkEntryBetween/index.js'\n\n/**\n * Rebuilds the live linked-list projection and index from predecessor buckets.\n *\n * Sibling order is deterministic by UUIDv7, which keeps replicas convergent even\n * when deltas arrive in different orders.\n */\nexport function rebuildLiveProjection<T>(crListReplica: CRListState<T>) {\n crListReplica.cursor = undefined\n const entries = crListReplica.index ?? new Map()\n void entries.clear()\n for (const entry of crListReplica.parentMap.values()) {\n if (!entry) continue\n entry.prev = undefined\n entry.next = undefined\n }\n let previous: CRListStateEntry<T> = undefined\n let first: CRListStateEntry<T> = undefined\n let index = 0\n const appendChildren = (predecessorIdentifier: string): void => {\n const stack: Array<{\n predecessorIdentifier: string\n siblingIndex: number\n siblings?: Array<NonNullable<CRListStateEntry<T>>>\n }> = [{ predecessorIdentifier, siblingIndex: 0 }]\n\n while (stack.length > 0) {\n const frame = stack[stack.length - 1]\n\n if (!frame.siblings) {\n frame.siblings = crListReplica.childrenMap.get(\n frame.predecessorIdentifier\n )\n if (!frame.siblings) {\n void stack.pop()\n continue\n }\n if (frame.siblings.length > 1)\n void frame.siblings.sort((a, b) => (a.uuidv7 > b.uuidv7 ? 1 : -1))\n }\n\n if (frame.siblingIndex >= frame.siblings.length) {\n void stack.pop()\n continue\n }\n\n const sibling = frame.siblings[frame.siblingIndex]\n frame.siblingIndex++\n if (!sibling) continue\n if (crListReplica.parentMap.get(sibling.uuidv7) !== sibling) continue\n\n sibling.index = index\n index++\n void linkEntryBetween<T>(previous, sibling, undefined)\n if (!first) first = sibling\n previous = sibling\n void stack.push({\n predecessorIdentifier: sibling.uuidv7,\n siblingIndex: 0,\n })\n }\n }\n void appendChildren('\\0')\n const detachedPredecessors: Array<string> = []\n for (const predecessorIdentifier of crListReplica.childrenMap.keys()) {\n if (\n predecessorIdentifier !== '\\0' &&\n !crListReplica.parentMap.get(predecessorIdentifier)\n )\n void detachedPredecessors.push(predecessorIdentifier)\n }\n if (detachedPredecessors.length > 1)\n detachedPredecessors.sort((a, b) => (a > b ? 1 : -1))\n for (const predecessorIdentifier of detachedPredecessors)\n void appendChildren(predecessorIdentifier)\n crListReplica.cursor = first\n crListReplica.cursorIndex = first ? 0 : undefined\n if (first) void entries.set(0, first)\n crListReplica.index = entries\n crListReplica.size = crListReplica.parentMap.size\n}\n","import type {\n CRListState,\n CRListSnapshotEntry,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { isUuidV7 } from '@sovereignbase/utils'\n\n/**\n * Converts a snapshot or delta value entry into local mutable entry state.\n *\n * Invalid, deleted, duplicate, or currently unanchored entries are ignored.\n * Payload values are kept by reference.\n */\nexport function materializeSnapshotEntry<T>(\n valueEntry: CRListSnapshotEntry<T>,\n crListReplica: CRListState<T>\n): CRListStateEntry<T> {\n if (valueEntry === null || valueEntry === undefined) return undefined\n if (\n !isUuidV7(valueEntry.uuidv7) ||\n crListReplica.tombstones.has(valueEntry.uuidv7) ||\n crListReplica.parentMap.has(valueEntry.uuidv7) ||\n (!isUuidV7(valueEntry.predecessor) &&\n valueEntry.predecessor !== '\\0' &&\n !crListReplica.tombstones.has(valueEntry.predecessor))\n )\n return undefined\n\n return {\n uuidv7: valueEntry.uuidv7,\n value: valueEntry.value,\n predecessor: valueEntry.predecessor,\n index: 0,\n next: undefined,\n prev: undefined,\n }\n}\n","import type {\n CRListState,\n CRListStateEntry,\n CRListDelta,\n} from '../../.types/index.js'\n\n/**\n * Attaches a live entry to UUID and predecessor indexes.\n *\n * When a delta buffer is provided, the same live payload reference is appended\n * to the outgoing delta.\n */\nexport function attachEntryToIndexes<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n deltaBuf?: CRListDelta<T>\n): void {\n void crListReplica.parentMap.set(linkedListEntry.uuidv7, linkedListEntry)\n const siblings = crListReplica.childrenMap.get(linkedListEntry.predecessor)\n if (siblings) {\n void siblings.push(linkedListEntry)\n } else {\n void crListReplica.childrenMap.set(linkedListEntry.predecessor, [\n linkedListEntry,\n ])\n }\n if (deltaBuf && !Array.isArray(deltaBuf.values)) deltaBuf.values = []\n if (deltaBuf?.values)\n void deltaBuf.values.push({\n uuidv7: linkedListEntry.uuidv7,\n value: linkedListEntry.value,\n predecessor: linkedListEntry.predecessor,\n })\n}\n","import type { CRListState, CRListStateEntry } from '../../.types/index.js'\n\n/**\n * Removes a live entry from UUID and predecessor indexes.\n */\nexport function detachEntryFromIndexes<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>\n): void {\n void crListReplica.parentMap.delete(linkedListEntry.uuidv7)\n const siblings = crListReplica.childrenMap.get(linkedListEntry.predecessor)\n if (!siblings) return\n const index = siblings.indexOf(linkedListEntry)\n if (index !== -1) void siblings.splice(index, 1)\n}\n","import type {\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { detachEntryFromIndexes } from '../detachEntryFromIndexes/index.js'\n\n/**\n * Tombstones a live entry and unlinks it from the local projection.\n */\nexport function deleteLiveEntry<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n deltaBuf?: CRListDelta<T>\n): void {\n const prev = linkedListEntry.prev\n const next = linkedListEntry.next\n void crListReplica.tombstones.add(linkedListEntry.uuidv7)\n if (deltaBuf && !Array.isArray(deltaBuf.tombstones)) deltaBuf.tombstones = []\n void deltaBuf?.tombstones?.push(linkedListEntry.uuidv7)\n if (prev) prev.next = next\n if (next) {\n next.prev = prev\n }\n void detachEntryFromIndexes<T>(crListReplica, linkedListEntry)\n if (crListReplica.cursor === linkedListEntry)\n crListReplica.cursor = next ?? prev\n if (!crListReplica.cursor) crListReplica.cursorIndex = undefined\n linkedListEntry.prev = undefined\n linkedListEntry.next = undefined\n crListReplica.size = crListReplica.parentMap.size\n}\n","import type { CRListEventMap } from '../../.types/index.js'\n\n/**\n * Dispatches a typed CRList event payload through an EventTarget.\n */\nexport function dispatchCRListEvent<T, K extends keyof CRListEventMap<T>>(\n eventTarget: EventTarget,\n type: K,\n detail: CRListEventMap<T>[K]\n): void {\n void eventTarget.dispatchEvent(new CustomEvent(type, { detail }))\n}\n","import type {\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { detachEntryFromIndexes } from '../detachEntryFromIndexes/index.js'\nimport { attachEntryToIndexes } from '../attachEntryToIndexes/index.js'\n\n/**\n * Reattaches an existing live entry to a newer stable predecessor.\n */\nexport function moveEntryToPredecessor<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n predecessor: string,\n deltaBuf?: CRListDelta<T>\n): void {\n void detachEntryFromIndexes<T>(crListReplica, linkedListEntry)\n linkedListEntry.predecessor = predecessor\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, deltaBuf)\n}\n","/**\n * Parses a JavaScript property key as a safe non-negative list index.\n */\nexport function indexFromPropertyKey(\n index: string | symbol\n): number | undefined {\n if (typeof index !== 'string' || !/^(0|[1-9]\\d*)$/.test(index))\n return undefined\n const listIndex = Number(index)\n return Number.isSafeInteger(listIndex) ? listIndex : undefined\n}\n","import { isUuidV7, prototype } from '@sovereignbase/utils'\nimport {\n CRListSnapshot,\n CRListState,\n CRListStateEntry,\n} from '../../../.types/index.js'\nimport {\n rebuildLiveProjection,\n materializeSnapshotEntry,\n attachEntryToIndexes,\n linkEntryBetween,\n} from '../../../.helpers/index.js'\n\n/**\n * Creates a local CRList replica from an optional snapshot.\n *\n * Invalid snapshot records are ignored. Accepted values are kept by reference,\n * indexed by UUIDv7, linked through their predecessor buckets, and exposed as a\n * live doubly-linked list projection.\n *\n * @param snapshot - Optional CRList snapshot.\n * @returns - A hydrated CRList replica.\n *\n * Time complexity: O(n log n + t), worst case O(n^2 + t)\n * - n = snapshot value entry count\n * - t = snapshot tombstone count\n *\n * Space complexity: O(n + t)\n */\nexport function __create<T>(snapshot?: CRListSnapshot<T>): CRListState<T> {\n const crListReplica: CRListState<T> = {\n size: 0,\n cursor: undefined,\n cursorIndex: undefined,\n index: new Map(),\n tombstones: new Set<string>(),\n parentMap: new Map<string, NonNullable<CRListStateEntry<T>>>(),\n childrenMap: new Map<string, Array<NonNullable<CRListStateEntry<T>>>>(),\n }\n if (!snapshot || prototype(snapshot) !== 'record') return crListReplica\n\n /** Hydrate tombstone entries. */\n if (\n Object.hasOwn(snapshot, 'tombstones') &&\n Array.isArray(snapshot.tombstones)\n ) {\n for (const tombstone of snapshot.tombstones) {\n if (crListReplica.tombstones.has(tombstone) || !isUuidV7(tombstone))\n continue\n void crListReplica.tombstones.add(tombstone)\n }\n }\n\n /** Hydrate value entries. */\n if (!Object.hasOwn(snapshot, 'values') || !Array.isArray(snapshot.values))\n return crListReplica\n // Build predecessor tree.\n let canUseLinearProjection = true\n let previous: CRListStateEntry<T> = undefined\n for (const valueEntry of snapshot.values) {\n const linkedListEntry = materializeSnapshotEntry<T>(\n valueEntry,\n crListReplica\n )\n if (!linkedListEntry) continue\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry)\n if (\n canUseLinearProjection &&\n linkedListEntry.predecessor === (previous?.uuidv7 ?? '\\0')\n ) {\n linkedListEntry.index = crListReplica.parentMap.size - 1\n void linkEntryBetween<T>(previous, linkedListEntry, undefined)\n previous = linkedListEntry\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n continue\n }\n canUseLinearProjection = false\n }\n if (canUseLinearProjection) {\n crListReplica.cursor = previous\n crListReplica.cursorIndex = previous\n ? crListReplica.parentMap.size - 1\n : undefined\n crListReplica.size = crListReplica.parentMap.size\n return crListReplica\n }\n // Flatten tree into a doubly linked list and write live-view indexes.\n void rebuildLiveProjection<T>(crListReplica)\n\n return crListReplica\n}\n","import { seekCursorToIndex } from '../../../.helpers/index.js'\nimport { CRListState } from '../../../.types/index.js'\n\n/**\n * Reads the value at an index in the replica live view.\n *\n * The replica cursor is moved as part of the lookup. Successful reads return\n * the live value reference stored by the replica. Mutating that value directly\n * can mutate replica state and should only be done when the caller owns an\n * independent value object. Out-of-bounds and empty list reads resolve to\n * `undefined` instead of throwing.\n *\n * @param targetIndex - Index in the live list.\n * @param crListReplica - Replica to read from.\n * @returns - The live value at `targetIndex`, or `undefined` when\n * no value is present.\n *\n * Time complexity: O(d), worst case O(n)\n * - d = distance from cursor to target index\n * - n = list size\n *\n * Space complexity: O(1)\n */\nexport function __read<T>(\n targetIndex: number,\n crListReplica: CRListState<T>\n): T | undefined {\n try {\n void seekCursorToIndex<T>(targetIndex, crListReplica)\n return crListReplica.cursor?.value\n } catch {\n return undefined\n }\n}\n","import { CRListError } from '../../../.errors/class.js'\nimport {\n attachEntryToIndexes,\n detachEntryFromIndexes,\n seekCursorToIndex,\n moveEntryToPredecessor,\n linkEntryBetween,\n} from '../../../.helpers/index.js'\nimport { v7 as uuidv7 } from 'uuid'\nimport {\n CRListChange,\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../../.types/index.js'\n/**\n * Applies a local value mutation to the replica live view.\n *\n * The update can replace a range starting at the target entry, insert values\n * before it, or insert values after it. The returned delta is suitable for\n * gossip and the returned change describes the local live-view patch.\n *\n * @param listIndex - Target index in the live list.\n * @param listValues - Values to insert or overwrite.\n * @param crListReplica - Replica to mutate.\n * @param mode - Mutation mode relative to `listIndex`.\n * @returns - A local change and gossip delta, or `false` if no mutation occurred.\n *\n * Time complexity: O(d + v + r + vk), worst case O(vn)\n * - d = distance from cursor to target index\n * - v = amount of input values\n * - r = amount of nodes after inserted values whose indexes must be shifted\n * - k = sibling bucket size when predecessor bucket is updated\n *\n * Space complexity: O(v)\n */\nexport function __update<T>(\n listIndex: number,\n listValues: Array<T>,\n crListReplica: CRListState<T>,\n mode: 'overwrite' | 'before' | 'after'\n): { change: CRListChange<T>; delta: CRListDelta<T> } | false {\n if (listIndex < 0 || listIndex > crListReplica.size)\n throw new CRListError('INDEX_OUT_OF_BOUNDS')\n if (!Array.isArray(listValues))\n throw new CRListError(\n 'UPDATE_EXPECTED_AN_ARRAY',\n '`listValues` must be an Array'\n )\n if (listValues.length === 0) return false\n const change: CRListChange<T> = {}\n const delta: CRListDelta<T> = { values: [], tombstones: [] }\n for (const listValue of listValues) {\n const v7 = uuidv7()\n\n const linkedListEntry: NonNullable<CRListStateEntry<T>> = {\n uuidv7: v7,\n value: listValue,\n predecessor: '\\0',\n index: 0,\n next: undefined,\n prev: undefined,\n }\n\n switch (mode) {\n case 'overwrite': {\n if (listIndex === crListReplica.size) {\n if (crListReplica.size === 0) {\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n void seekCursorToIndex<T>(crListReplica.size - 1, crListReplica)\n if (!crListReplica.cursor) return false\n linkedListEntry.index = (crListReplica.cursorIndex ?? 0) + 1\n linkedListEntry.predecessor = crListReplica.cursor.uuidv7\n void linkEntryBetween<T>(\n crListReplica.cursor,\n linkedListEntry,\n undefined\n )\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n void seekCursorToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n const entryToOverwrite = crListReplica.cursor\n const actualIndex = crListReplica.cursorIndex ?? listIndex\n\n linkedListEntry.predecessor = entryToOverwrite.predecessor\n linkedListEntry.index = actualIndex\n void linkEntryBetween<T>(\n entryToOverwrite.prev,\n linkedListEntry,\n entryToOverwrite.next\n )\n if (entryToOverwrite.next) {\n if (entryToOverwrite.next.predecessor === entryToOverwrite.uuidv7) {\n void moveEntryToPredecessor<T>(\n crListReplica,\n entryToOverwrite.next,\n linkedListEntry.uuidv7,\n delta\n )\n }\n }\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n void crListReplica.tombstones.add(entryToOverwrite.uuidv7)\n void delta.tombstones?.push(entryToOverwrite.uuidv7)\n void detachEntryFromIndexes<T>(crListReplica, entryToOverwrite)\n entryToOverwrite.next = undefined\n entryToOverwrite.prev = undefined\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = actualIndex\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[actualIndex] = linkedListEntry.value\n break\n }\n case 'after': {\n if (crListReplica.size === 0 && listIndex === 0) {\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n if (listIndex === crListReplica.size) {\n void seekCursorToIndex<T>(crListReplica.size - 1, crListReplica)\n } else {\n void seekCursorToIndex<T>(listIndex, crListReplica)\n }\n if (!crListReplica.cursor) return false\n const actualIndex = crListReplica.cursorIndex ?? listIndex\n const next =\n listIndex === crListReplica.size\n ? undefined\n : crListReplica.cursor.next\n linkedListEntry.index = actualIndex + 1\n linkedListEntry.predecessor = crListReplica.cursor.uuidv7\n void linkEntryBetween<T>(crListReplica.cursor, linkedListEntry, next)\n if (next) {\n if (next.predecessor === crListReplica.cursor.uuidv7) {\n void moveEntryToPredecessor<T>(\n crListReplica,\n next,\n linkedListEntry.uuidv7,\n delta\n )\n }\n }\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n if (next) crListReplica.index = new Map()\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n case 'before': {\n if (crListReplica.size === 0 && listIndex === 0) {\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n mode = 'after'\n listIndex = linkedListEntry.index - 1\n break\n }\n void seekCursorToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n const actualIndex = crListReplica.cursorIndex ?? listIndex\n const prev = crListReplica.cursor.prev\n linkedListEntry.index = actualIndex\n linkedListEntry.predecessor = prev?.uuidv7 ?? '\\0'\n void linkEntryBetween<T>(prev, linkedListEntry, crListReplica.cursor)\n if (crListReplica.cursor.predecessor === linkedListEntry.predecessor) {\n void moveEntryToPredecessor<T>(\n crListReplica,\n crListReplica.cursor,\n linkedListEntry.uuidv7,\n delta\n )\n }\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = actualIndex\n crListReplica.index = new Map()\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[actualIndex] = linkedListEntry.value\n mode = 'after'\n listIndex = linkedListEntry.index - 1\n\n break\n }\n }\n crListReplica.size = crListReplica.parentMap.size\n listIndex++\n }\n return { change, delta }\n}\n","import { deleteLiveEntry, seekCursorToIndex } from '../../../.helpers/index.js'\nimport { CRListError } from '../../../.errors/class.js'\nimport type {\n CRListChange,\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../../.types/index.js'\n\n/**\n * Deletes a range from the replica live view.\n *\n * With no indexes, the full list is deleted. With only `startIndex`, all entries\n * from `startIndex` onward are deleted. With both indexes, the deleted range is\n * `[startIndex, endIndex)`.\n *\n * @param crListReplica - Replica to mutate.\n * @param startIndex - Inclusive start index. Defaults to `0`.\n * @param endIndex - Exclusive end index. Defaults to the current list size.\n * @returns - A local change and gossip delta, or `false` if nothing was deleted.\n *\n * Time complexity: O(d + qk + r), worst case O(n^2)\n * - d = distance from cursor to target index\n * - q = amount of deleted nodes\n * - r = amount of nodes after the deleted range whose indexes must be shifted\n * - k = sibling bucket size when deleted entries are removed from buckets\n *\n * Space complexity: O(q)\n */\nexport function __delete<T>(\n crListReplica: CRListState<T>,\n startIndex?: number,\n endIndex?: number\n): { change: CRListChange<T>; delta: CRListDelta<T> } | false {\n const change: CRListChange<T> = {}\n const delta: CRListDelta<T> = { values: [], tombstones: [] }\n const listIndex = startIndex ?? 0\n const targetEndIndex = endIndex ?? crListReplica.size\n if (\n listIndex < 0 ||\n targetEndIndex < listIndex ||\n listIndex > crListReplica.size\n )\n throw new CRListError('INDEX_OUT_OF_BOUNDS')\n const deleteCount = Math.min(targetEndIndex, crListReplica.size) - listIndex\n if (deleteCount <= 0) return false\n\n void seekCursorToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n\n let current: CRListStateEntry<T> = crListReplica.cursor\n let deleted = 0\n let currentIndex = crListReplica.cursorIndex ?? listIndex\n\n while (current && deleted < deleteCount) {\n const next: CRListStateEntry<T> = current.next\n change[currentIndex] = undefined\n void crListReplica.index?.delete(currentIndex)\n void deleteLiveEntry<T>(crListReplica, current, delta)\n current = next\n currentIndex++\n deleted++\n }\n\n crListReplica.size = crListReplica.parentMap.size\n crListReplica.cursor = current ?? crListReplica.cursor\n crListReplica.cursorIndex = current\n ? listIndex\n : crListReplica.cursor\n ? Math.max(0, crListReplica.size - 1)\n : undefined\n crListReplica.index = new Map()\n if (crListReplica.cursor && crListReplica.cursorIndex !== undefined)\n void crListReplica.index.set(\n crListReplica.cursorIndex,\n crListReplica.cursor\n )\n\n return { change, delta }\n}\n","import type {\n CRListChange,\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../../.types/index.js'\nimport {\n materializeSnapshotEntry,\n attachEntryToIndexes,\n rebuildLiveProjection,\n rebuildLiveIndex,\n deleteLiveEntry,\n moveEntryToPredecessor,\n} from '../../../.helpers/index.js'\nimport { prototype, isUuidV7 } from '@sovereignbase/utils'\n\n/**\n * Merges a remote CRList delta into the local replica.\n *\n * Accepted tombstones update the local live view and accepted values are attached\n * to the predecessor tree. Tail-append deltas are linked incrementally; deltas\n * that can affect ordering fall back to deterministic relinking.\n *\n * @param crListReplica - Replica to mutate.\n * @param crListDelta - Remote gossip delta.\n * @returns - A minimal local change patch, or `false` when the delta is ignored.\n *\n * Time complexity: O(v + t) for tail-append deltas; O(n + t + qk) for tombstone-only deletes; otherwise O(n log n + v + t + m*k)\n * Worst case: O(n^2 + (v + t)n)\n * - n = replica value entry count after merge\n * - v = delta value entry count\n * - t = delta tombstone count\n * - q = amount of live entries deleted by tombstones\n * - m = entries moved between predecessor buckets\n * - k = sibling bucket size when entries are removed from buckets\n *\n * Space complexity: O(n + v + t)\n */\nexport function __merge<T>(\n crListReplica: CRListState<T>,\n crListDelta: CRListDelta<T>\n): CRListChange<T> | false {\n if (!crListDelta || prototype(crListDelta) !== 'record') return false\n const newVals: Array<NonNullable<CRListStateEntry<T>>> = []\n const newTombsIndices: Array<number> = []\n const change: CRListChange<T> = {}\n let needsRelink = false\n if (\n Object.hasOwn(crListDelta, 'values') &&\n Array.isArray(crListDelta.values) &&\n crListDelta.values.length === 1 &&\n (!Object.hasOwn(crListDelta, 'tombstones') ||\n (Array.isArray(crListDelta.tombstones) &&\n crListDelta.tombstones.length === 0))\n ) {\n const linkedListEntry = materializeSnapshotEntry<T>(\n crListDelta.values[0],\n crListReplica\n )\n if (!linkedListEntry) return false\n const predecessor =\n linkedListEntry.predecessor === '\\0'\n ? undefined\n : crListReplica.parentMap.get(linkedListEntry.predecessor)\n if (\n (linkedListEntry.predecessor === '\\0' && crListReplica.size === 0) ||\n (predecessor && !predecessor.next)\n ) {\n linkedListEntry.prev = predecessor\n linkedListEntry.index = crListReplica.size\n if (predecessor) predecessor.next = linkedListEntry\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry)\n crListReplica.size = crListReplica.parentMap.size\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n return { [linkedListEntry.index]: linkedListEntry.value }\n }\n }\n\n /** Apply tombstone entries. */\n if (\n Object.hasOwn(crListDelta, 'tombstones') &&\n Array.isArray(crListDelta.tombstones)\n ) {\n for (const tombstone of crListDelta.tombstones) {\n if (crListReplica.tombstones.has(tombstone) || !isUuidV7(tombstone))\n continue\n void crListReplica.tombstones.add(tombstone)\n const linkedListEntry = crListReplica.parentMap.get(tombstone)\n if (linkedListEntry) {\n void newTombsIndices.push(linkedListEntry.index)\n void crListReplica.index?.delete(linkedListEntry.index)\n void deleteLiveEntry<T>(crListReplica, linkedListEntry)\n needsRelink = true\n }\n }\n }\n\n /** Apply value entries. */\n if (\n !Object.hasOwn(crListDelta, 'values') ||\n !Array.isArray(crListDelta.values)\n ) {\n if (newTombsIndices.length === 0) return false\n void rebuildLiveIndex<T>(crListReplica)\n for (const index of newTombsIndices) {\n change[index] = undefined\n }\n return change\n }\n // Attach accepted values to the predecessor tree.\n for (const valueEntry of crListDelta.values) {\n if (valueEntry === null || valueEntry === undefined) continue\n const existingEntry = crListReplica.parentMap.get(valueEntry.uuidv7)\n if (existingEntry) {\n if (crListReplica.tombstones.has(valueEntry.uuidv7)) continue\n if (valueEntry.predecessor !== '\\0' && !isUuidV7(valueEntry.predecessor))\n continue\n if (existingEntry.predecessor >= valueEntry.predecessor) continue\n void moveEntryToPredecessor<T>(\n crListReplica,\n existingEntry,\n valueEntry.predecessor\n )\n needsRelink = true\n continue\n }\n const linkedListEntry = materializeSnapshotEntry<T>(\n valueEntry,\n crListReplica\n )\n if (!linkedListEntry) continue\n const predecessor =\n linkedListEntry.predecessor === '\\0'\n ? undefined\n : crListReplica.parentMap.get(linkedListEntry.predecessor)\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry)\n void newVals.push(linkedListEntry)\n if (!needsRelink && linkedListEntry.predecessor === '\\0') {\n if (crListReplica.size === 0) {\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n crListReplica.size = crListReplica.parentMap.size\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n } else {\n needsRelink = true\n }\n } else if (!needsRelink && predecessor && !predecessor.next) {\n linkedListEntry.prev = predecessor\n linkedListEntry.index = crListReplica.size\n predecessor.next = linkedListEntry\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n crListReplica.size = crListReplica.parentMap.size\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n } else {\n needsRelink = true\n }\n }\n if (needsRelink) {\n // Flatten tree into a doubly linked list and write live-view indexes.\n void rebuildLiveProjection<T>(crListReplica)\n }\n\n if (newTombsIndices.length === 0 && newVals.length === 0) return false\n\n for (const index of newTombsIndices) {\n change[index] = undefined\n }\n for (const val of newVals) {\n change[val.index] = val.value\n }\n\n return change\n}\n","import type { CRListAck, CRListState } from '../../../.types/index.js'\n\n/**\n * Returns the replica tombstone acknowledgement frontier.\n *\n * The frontier is the greatest tombstone identifier currently retained by the\n * replica. Peers can use it as input for tombstone garbage collection.\n *\n * @param crListReplica - Replica to acknowledge.\n * @returns - The acknowledgement frontier, or `false` when there are no tombstones.\n *\n * Time complexity: O(t)\n * - t = replica tombstone count\n *\n * Space complexity: O(1)\n */\nexport function __acknowledge<T>(\n crListReplica: CRListState<T>\n): CRListAck | false {\n let largest: CRListAck | false = false\n void crListReplica.tombstones.forEach((tombstone) => {\n if (largest === false || largest < tombstone) largest = tombstone\n })\n if (typeof largest === 'string') return largest\n return false\n}\n","import { isUuidV7 } from '@sovereignbase/utils'\nimport { CRListAck, CRListState } from '../../../.types/index.js'\n\n/**\n * Removes tombstones acknowledged by all supplied frontiers.\n *\n * The smallest valid UUIDv7 frontier is used as the safe collection boundary.\n * Tombstones less than or equal to that boundary are removed from the local\n * replica.\n *\n * @param frontiers - Acknowledgement frontiers received from peers.\n * @param crListReplica - Replica whose tombstones will be collected.\n *\n * Time complexity: O(f log f + t)\n * - f = frontier count\n * - t = replica tombstone count\n *\n * Space complexity: O(1)\n */\nexport function __garbageCollect<T>(\n frontiers: Array<CRListAck>,\n crListReplica: CRListState<T>\n): void {\n if (!Array.isArray(frontiers)) return\n void frontiers.sort()\n const smallest = frontiers.find((frontier) => isUuidV7(frontier))\n if (typeof smallest !== 'string') return\n void crListReplica.tombstones.forEach((tombstone, __, tombstones) => {\n if (tombstone <= smallest) {\n void tombstones.delete(tombstone)\n }\n })\n}\n","import { CRListError } from '../../../.errors/class.js'\nimport { CRListState, CRListSnapshot } from '../../../.types/index.js'\n\n/**\n * Creates a full CRList snapshot from the current replica state.\n *\n * The snapshot contains every live value entry and all retained tombstones. Value\n * payloads are live references, so callers must not mutate snapshot values\n * unless they have first isolated them from replica state.\n *\n * @param crListReplica - Replica to snapshot.\n * @returns - A full snapshot suitable for hydration or transport.\n *\n * Time complexity: O(n + t)\n * - n = replica value entry count\n * - t = replica tombstone count\n *\n * Space complexity: O(n + t)\n */\nexport function __snapshot<T>(\n crListReplica: CRListState<T>\n): CRListSnapshot<T> {\n return {\n values: Array.from(crListReplica.parentMap.values()).map(\n (linkedListEntry) => {\n if (!linkedListEntry) throw new CRListError('LIST_INTEGRITY_VIOLATION')\n return {\n uuidv7: linkedListEntry.uuidv7,\n value: linkedListEntry.value,\n predecessor: linkedListEntry.predecessor,\n }\n }\n ),\n tombstones: Array.from(crListReplica.tombstones),\n }\n}\n","import { dispatchCRListEvent, indexFromPropertyKey } from '../.helpers/index.js'\nimport { CRListError } from '../.errors/class.js'\nimport type {\n CRListState,\n CRListSnapshot,\n CRListEventListenerFor,\n CRListEventMap,\n CRListDelta,\n CRListAck,\n} from '../.types/index.js'\nimport { __create, __read, __update, __delete } from '../core/crud/index.js'\nimport {\n __merge,\n __acknowledge,\n __garbageCollect,\n __snapshot,\n} from '../core/mags/index.js'\n\n/**\n * A convergent replicated list.\n *\n * Numeric property access reads and mutates the live list projection:\n * `list[0]` reads the live value reference, `list[0] = value` writes an\n * entry, and `delete list[0]` removes one entry. Iteration, `find()`, and\n * `forEach()` expose the same live value references. Mutating returned objects\n * directly can mutate replica state without producing a CRDT delta, so callers\n * must isolate values before out-of-band mutation. Local mutations emit `delta`\n * and `change` events; remote merges emit `change` events.\n *\n * @typeParam T - The value type stored in the list.\n */\nexport class CRList<T> {\n /**\n * Reads or overwrites an entry in the live list projection by index.\n *\n * Reads return live value references.\n */\n [index: number]: T\n declare private readonly state: CRListState<T>\n declare private readonly eventTarget: EventTarget\n\n /**\n * Creates a replicated list from an optional CRList snapshot.\n *\n * @param snapshot - A previously emitted CRList snapshot.\n */\n constructor(snapshot?: CRListSnapshot<T>) {\n void Object.defineProperties(this, {\n state: {\n value: __create<T>(snapshot),\n enumerable: false,\n configurable: false,\n writable: false,\n },\n eventTarget: {\n value: new EventTarget(),\n enumerable: false,\n configurable: false,\n writable: false,\n },\n })\n\n return new Proxy(this, {\n get(target, index, receiver) {\n const listIndex = indexFromPropertyKey(index)\n // Preserve normal property access for non-index keys.\n if (listIndex === undefined) return Reflect.get(target, index, receiver)\n return __read(listIndex, target.state)\n },\n has(target, index) {\n const listIndex = indexFromPropertyKey(index)\n // Preserve normal property checks for non-index keys.\n if (listIndex === undefined) return Reflect.has(target, index)\n return listIndex >= 0 && listIndex < target.state.size\n },\n set(target, index, value) {\n const listIndex = indexFromPropertyKey(index)\n if (listIndex === undefined) return false\n try {\n const result = __update(listIndex, [value], target.state, 'overwrite')\n if (!result) return false\n const { delta, change } = result\n if (delta)\n void dispatchCRListEvent(target.eventTarget, 'delta', delta)\n if (change)\n void dispatchCRListEvent(target.eventTarget, 'change', change)\n return true\n } catch (error) {\n if (error instanceof CRListError) throw error\n return false\n }\n },\n deleteProperty(target, index) {\n const listIndex = indexFromPropertyKey(index)\n if (listIndex === undefined) return false\n try {\n const result = __delete(target.state, listIndex, listIndex + 1)\n if (!result) return false\n const { delta, change } = result\n if (delta)\n void dispatchCRListEvent(target.eventTarget, 'delta', delta)\n if (change)\n void dispatchCRListEvent(target.eventTarget, 'change', change)\n return true\n } catch (error) {\n if (error instanceof CRListError) throw error\n return false\n }\n },\n ownKeys(target) {\n return [\n ...Reflect.ownKeys(target),\n ...Array.from({ length: target.size }, (_, index) => String(index)),\n ]\n },\n\n getOwnPropertyDescriptor(target, index) {\n const listIndex = indexFromPropertyKey(index)\n\n if (listIndex !== undefined && listIndex < target.size) {\n return {\n value: __read(listIndex, target.state),\n writable: true,\n enumerable: true,\n configurable: true,\n }\n }\n // Preserve normal property checks for non-index keys.\n return Reflect.getOwnPropertyDescriptor(target, index)\n },\n })\n }\n\n /**\n * The current number of live entries.\n */\n get size(): number {\n return this.state.size\n }\n /**\n * Inserts a value before an index.\n *\n * If `beforeIndex` is omitted, the value is inserted at the start of the list.\n *\n * @param value - The value to insert.\n * @param beforeIndex - The index to insert before.\n */\n prepend(value: T, beforeIndex?: number): void {\n const result = __update<T>(beforeIndex ?? 0, [value], this.state, 'before')\n if (!result) return\n const { delta, change } = result\n if (delta) void dispatchCRListEvent(this.eventTarget, 'delta', delta)\n if (change) void dispatchCRListEvent(this.eventTarget, 'change', change)\n }\n /**\n * Inserts a value after an index.\n *\n * If `afterIndex` is omitted, the value is appended at the end of the list.\n *\n * @param value - The value to insert.\n * @param afterIndex - The index to insert after.\n */\n append(value: T, afterIndex?: number): void {\n const result = __update<T>(\n afterIndex ?? this.state.size,\n [value],\n this.state,\n 'after'\n )\n if (!result) return\n const { delta, change } = result\n if (delta) void dispatchCRListEvent(this.eventTarget, 'delta', delta)\n if (change) void dispatchCRListEvent(this.eventTarget, 'change', change)\n }\n /**\n * Removes the entry at an index.\n *\n * @param index - The index to remove.\n */\n remove(index: number): void {\n const result = __delete(this.state, index, index + 1)\n if (!result) return\n const { delta, change } = result\n if (delta) void dispatchCRListEvent(this.eventTarget, 'delta', delta)\n if (change) void dispatchCRListEvent(this.eventTarget, 'change', change)\n }\n\n /**\n * Returns the first live value matching a predicate in index order.\n *\n * Predicate values are live references. Mutating them directly can mutate the\n * list without emitting a delta.\n *\n * @param predicate - Function to test each live value.\n * @param thisArg - Optional `this` value for the predicate.\n */\n find(\n predicate: (this: unknown, value: T, index: number, list: this) => unknown,\n thisArg?: unknown\n ): T | undefined {\n let linkedListEntry = this.state.index?.get(0) ?? this.state.cursor\n while (linkedListEntry?.prev) linkedListEntry = linkedListEntry.prev\n let index = 0\n while (linkedListEntry) {\n if (predicate.call(thisArg, linkedListEntry.value, index, this))\n return linkedListEntry.value\n linkedListEntry = linkedListEntry.next\n index++\n }\n\n return undefined\n }\n\n /**\n * Applies a remote gossip delta to this list.\n *\n * Emits a `change` event when the merge changes the live projection.\n *\n * @param delta - The remote CRList delta to merge.\n */\n merge(delta: CRListDelta<T>): void {\n const change = __merge(this.state, delta)\n if (change) void dispatchCRListEvent(this.eventTarget, 'change', change)\n }\n /**\n * Emits an acknowledgement frontier for currently retained tombstones.\n */\n acknowledge(): void {\n const ack = __acknowledge(this.state)\n if (ack) void dispatchCRListEvent(this.eventTarget, 'ack', ack)\n }\n /**\n * Garbage-collects tombstones that are covered by acknowledgement frontiers.\n *\n * @param frontiers - Replica acknowledgement frontiers.\n */\n garbageCollect(frontiers: Array<CRListAck>): void {\n void __garbageCollect(frontiers, this.state)\n }\n /**\n * Emits the current CRList snapshot.\n *\n * Snapshot value payloads are live references. Mutating them can mutate\n * replica state without emitting a delta.\n */\n snapshot(): void {\n const snapshot = __snapshot<T>(this.state)\n if (snapshot)\n void dispatchCRListEvent(this.eventTarget, 'snapshot', snapshot)\n }\n /**\n * Registers an event listener.\n *\n * @param type - The event type to listen for.\n * @param listener - The listener to register.\n * @param options - Listener registration options.\n */\n addEventListener<K extends keyof CRListEventMap<T>>(\n type: K,\n listener: CRListEventListenerFor<T, K> | null,\n options?: boolean | AddEventListenerOptions\n ): void {\n void this.eventTarget.addEventListener(\n type,\n listener as EventListenerOrEventListenerObject | null,\n options\n )\n }\n\n /**\n * Removes an event listener.\n *\n * @param type - The event type to stop listening for.\n * @param listener - The listener to remove.\n * @param options - Listener removal options.\n */\n removeEventListener<K extends keyof CRListEventMap<T>>(\n type: K,\n listener: CRListEventListenerFor<T, K> | null,\n options?: boolean | EventListenerOptions\n ): void {\n void this.eventTarget.removeEventListener(\n type,\n listener as EventListenerOrEventListenerObject | null,\n options\n )\n }\n /**\n * Returns a CRList snapshot of this list.\n *\n * Snapshot value payloads are live references. Mutating them can mutate\n * replica state without emitting a delta.\n *\n * Called automatically by `JSON.stringify`.\n */\n toJSON(): CRListSnapshot<T> {\n return __snapshot<T>(this.state)\n }\n /**\n * Attempts to return this list snapshot as a JSON string.\n *\n * This can fail when list values are not JSON-compatible.\n */\n toString(): string {\n return JSON.stringify(this)\n }\n /**\n * Returns the Node.js console inspection representation.\n */\n [Symbol.for('nodejs.util.inspect.custom')](): CRListSnapshot<T> {\n return this.toJSON()\n }\n /**\n * Returns the Deno console inspection representation.\n */\n [Symbol.for('Deno.customInspect')](): CRListSnapshot<T> {\n return this.toJSON()\n }\n /**\n * Iterates over current live values in index order.\n */\n *[Symbol.iterator](): IterableIterator<T> {\n for (let index = 0; index < this.size; index++) {\n const value = this[index]\n yield value\n }\n }\n /**\n * Calls a function once for each live value in index order.\n *\n * Callback values are live references. Mutating them directly can mutate the\n * list without emitting a delta.\n *\n * @param callback - Function to call for each live value.\n * @param thisArg - Optional `this` value for the callback.\n */\n forEach(\n callback: (value: T, index: number, list: this) => void,\n thisArg?: unknown\n ): void {\n for (let index = 0; index < this.size; index++) {\n void callback.call(thisArg, this[index], index, this)\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/convergent-replicated-list/convergent-replicated-list/dist/index.cjs","../src/.helpers/rebuildLiveIndex/index.ts","../src/.errors/class.ts","../src/.helpers/seekCursorToIndex/index.ts","../src/.helpers/linkEntryBetween/index.ts","../src/.helpers/rebuildLiveProjection/index.ts","../src/.helpers/materializeSnapshotEntry/index.ts","../src/.helpers/attachEntryToIndexes/index.ts","../src/.helpers/detachEntryFromIndexes/index.ts","../src/.helpers/deleteLiveEntry/index.ts","../src/.helpers/dispatchCRListEvent/index.ts","../src/.helpers/moveEntryToPredecessor/index.ts","../src/.helpers/indexFromPropertyKey/index.ts","../src/.helpers/trySpliceInsertedParent/index.ts","../src/core/crud/create/index.ts","../src/core/crud/read/index.ts","../src/core/crud/update/index.ts","../src/core/crud/delete/index.ts","../src/core/mags/merge/index.ts","../src/core/mags/acknowledge/index.ts","../src/core/mags/garbageCollect/index.ts","../src/core/mags/snapshot/index.ts","../src/CRList/class.ts"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AC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,IAAA;AAC5B,EAAA;AAAA;AAAA;AAAA;AAIgE,EAAA;AAC3C,IAAA;AACrB,EAAA;AAAA;AAAA;AAAA;AAIwD,EAAA;AACnC,IAAA;AACrB,EAAA;AAAA;AAAA;AAAA;AAI0C,EAAA;AACe,IAAA;AACP,IAAA;AACxB,IAAA;AACA,MAAA;AACY,MAAA;AACpC,IAAA;AACF,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaQ,EAAA;AACiD,IAAA;AACP,IAAA;AACpC,IAAA;AACY,IAAA;AACoC,MAAA;AACxB,MAAA;AAClC,MAAA;AACF,IAAA;AACF,EAAA;AACF;AtB+oBgE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/convergent-replicated-list/convergent-replicated-list/dist/index.cjs","sourcesContent":[null,"import type { CRListState } from '../../.types/index.js'\n\n/**\n * Rebuilds the opportunistic index cache from the current live projection.\n */\nexport function rebuildLiveIndex<T>(crListReplica: CRListState<T>): void {\n if (!crListReplica.cursor) {\n crListReplica.index?.clear()\n crListReplica.cursorIndex = undefined\n return\n }\n let index = crListReplica.size\n const entries = crListReplica.index ?? new Map()\n void entries.clear()\n while (crListReplica.cursor.next)\n crListReplica.cursor = crListReplica.cursor.next\n\n while (index >= 1) {\n index--\n crListReplica.cursor.index = index\n void entries.set(index, crListReplica.cursor)\n if (crListReplica.cursor.prev === undefined) break\n crListReplica.cursor = crListReplica.cursor.prev\n }\n crListReplica.index = entries\n crListReplica.cursorIndex = 0\n}\n","/**\n * Error codes thrown by {@link CRList}.\n */\nexport type CRListErrorCode =\n | 'VALUE_NOT_CLONEABLE'\n | 'INDEX_OUT_OF_BOUNDS'\n | 'LIST_EMPTY'\n | 'LIST_INTEGRITY_VIOLATION'\n | 'UPDATE_EXPECTED_AN_ARRAY'\n\n/**\n * Represents a typed CRList runtime error.\n */\nexport class CRListError extends Error {\n /**\n * The semantic error code for the failure.\n */\n readonly code: CRListErrorCode\n\n /**\n * Creates a typed CRList error.\n *\n * @param code - The semantic error code.\n * @param message - An optional human-readable detail message.\n */\n constructor(code: CRListErrorCode, message?: string) {\n const detail = message ?? code\n super(`{@sovereignbase/convergent-replicated-list} ${detail}`)\n this.code = code\n this.name = 'CRListError'\n }\n}\n","import type { CRListState } from '../../.types/index.js'\nimport { CRListError } from '../../.errors/class.js'\n\n/**\n * Moves the replica cursor to a live index.\n *\n * A valid cached index entry is used directly. Stale cache entries are dropped\n * and the cursor walks from its current position, then repairs the cache at the\n * requested index.\n */\nexport function seekCursorToIndex<T>(\n targetIndex: number,\n crListReplica: CRListState<T>\n): void {\n if (targetIndex < 0 || targetIndex >= crListReplica.size)\n throw new CRListError('INDEX_OUT_OF_BOUNDS', 'Index out of bounds')\n const indexedEntry = crListReplica.index?.get(targetIndex)\n if (indexedEntry) {\n if (crListReplica.parentMap.get(indexedEntry.uuidv7) === indexedEntry) {\n crListReplica.cursor = indexedEntry\n crListReplica.cursorIndex = targetIndex\n return\n } else {\n void crListReplica.index?.delete(targetIndex)\n }\n }\n if (!crListReplica.cursor)\n throw new CRListError('LIST_EMPTY', 'List is empty')\n let cursorIndex = crListReplica.cursorIndex ?? crListReplica.cursor.index\n const direction = cursorIndex > targetIndex ? 'prev' : 'next'\n while (crListReplica.cursor && cursorIndex !== targetIndex) {\n crListReplica.cursor = crListReplica.cursor[direction]\n cursorIndex += direction === 'next' ? 1 : -1\n }\n if (crListReplica.cursor) {\n crListReplica.cursorIndex = targetIndex\n void crListReplica.index?.set(targetIndex, crListReplica.cursor)\n }\n}\n","import type { CRListStateEntry } from '../../.types/index.js'\n\n/**\n * Links a live entry between optional neighboring projection entries.\n */\nexport function linkEntryBetween<T>(\n prev: CRListStateEntry<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n next: CRListStateEntry<T>\n): void {\n linkedListEntry.prev = prev\n linkedListEntry.next = next\n if (prev) prev.next = linkedListEntry\n if (next) next.prev = linkedListEntry\n}\n","import type { CRListState, CRListStateEntry } from '../../.types/index.js'\nimport { linkEntryBetween } from '../linkEntryBetween/index.js'\n\n/**\n * Rebuilds the live linked-list projection and index from predecessor buckets.\n *\n * Sibling order is deterministic by UUIDv7, which keeps replicas convergent even\n * when deltas arrive in different orders.\n */\nexport function rebuildLiveProjection<T>(crListReplica: CRListState<T>) {\n crListReplica.cursor = undefined\n const entries = crListReplica.index ?? new Map()\n void entries.clear()\n for (const entry of crListReplica.parentMap.values()) {\n if (!entry) continue\n entry.prev = undefined\n entry.next = undefined\n }\n let previous: CRListStateEntry<T> = undefined\n let first: CRListStateEntry<T> = undefined\n let index = 0\n const appendChildren = (predecessorIdentifier: string): void => {\n const stack: Array<{\n predecessorIdentifier: string\n siblingIndex: number\n siblings?: Array<NonNullable<CRListStateEntry<T>>>\n }> = [{ predecessorIdentifier, siblingIndex: 0 }]\n\n while (stack.length > 0) {\n const frame = stack[stack.length - 1]\n\n if (!frame.siblings) {\n frame.siblings = crListReplica.childrenMap.get(\n frame.predecessorIdentifier\n )\n if (!frame.siblings) {\n void stack.pop()\n continue\n }\n if (frame.siblings.length > 1)\n void frame.siblings.sort((a, b) => (a.uuidv7 > b.uuidv7 ? 1 : -1))\n }\n\n if (frame.siblingIndex >= frame.siblings.length) {\n void stack.pop()\n continue\n }\n\n const sibling = frame.siblings[frame.siblingIndex]\n frame.siblingIndex++\n if (!sibling) continue\n if (crListReplica.parentMap.get(sibling.uuidv7) !== sibling) continue\n\n sibling.index = index\n index++\n void linkEntryBetween<T>(previous, sibling, undefined)\n if (!first) first = sibling\n previous = sibling\n void stack.push({\n predecessorIdentifier: sibling.uuidv7,\n siblingIndex: 0,\n })\n }\n }\n void appendChildren('\\0')\n const detachedPredecessors: Array<string> = []\n for (const predecessorIdentifier of crListReplica.childrenMap.keys()) {\n if (\n predecessorIdentifier !== '\\0' &&\n !crListReplica.parentMap.get(predecessorIdentifier)\n )\n void detachedPredecessors.push(predecessorIdentifier)\n }\n if (detachedPredecessors.length > 1)\n detachedPredecessors.sort((a, b) => (a > b ? 1 : -1))\n for (const predecessorIdentifier of detachedPredecessors)\n void appendChildren(predecessorIdentifier)\n crListReplica.cursor = first\n crListReplica.cursorIndex = first ? 0 : undefined\n if (first) void entries.set(0, first)\n crListReplica.index = entries\n crListReplica.size = crListReplica.parentMap.size\n}\n","import type {\n CRListState,\n CRListSnapshotEntry,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { isUuidV7 } from '@sovereignbase/utils'\n\n/**\n * Converts a snapshot or delta value entry into local mutable entry state.\n *\n * Invalid, deleted, duplicate, or currently unanchored entries are ignored.\n * Payload values are kept by reference.\n */\nexport function materializeSnapshotEntry<T>(\n valueEntry: CRListSnapshotEntry<T>,\n crListReplica: CRListState<T>\n): CRListStateEntry<T> {\n if (valueEntry === null || valueEntry === undefined) return undefined\n if (\n !isUuidV7(valueEntry.uuidv7) ||\n crListReplica.tombstones.has(valueEntry.uuidv7) ||\n crListReplica.parentMap.has(valueEntry.uuidv7) ||\n (!isUuidV7(valueEntry.predecessor) &&\n valueEntry.predecessor !== '\\0' &&\n !crListReplica.tombstones.has(valueEntry.predecessor))\n )\n return undefined\n\n return {\n uuidv7: valueEntry.uuidv7,\n value: valueEntry.value,\n predecessor: valueEntry.predecessor,\n index: 0,\n next: undefined,\n prev: undefined,\n }\n}\n","import type {\n CRListState,\n CRListStateEntry,\n CRListDelta,\n} from '../../.types/index.js'\n\n/**\n * Attaches a live entry to UUID and predecessor indexes.\n *\n * When a delta buffer is provided, the same live payload reference is appended\n * to the outgoing delta.\n */\nexport function attachEntryToIndexes<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n deltaBuf?: CRListDelta<T>\n): void {\n void crListReplica.parentMap.set(linkedListEntry.uuidv7, linkedListEntry)\n const siblings = crListReplica.childrenMap.get(linkedListEntry.predecessor)\n if (siblings) {\n void siblings.push(linkedListEntry)\n } else {\n void crListReplica.childrenMap.set(linkedListEntry.predecessor, [\n linkedListEntry,\n ])\n }\n if (deltaBuf && !Array.isArray(deltaBuf.values)) deltaBuf.values = []\n if (deltaBuf?.values)\n void deltaBuf.values.push({\n uuidv7: linkedListEntry.uuidv7,\n value: linkedListEntry.value,\n predecessor: linkedListEntry.predecessor,\n })\n}\n","import type { CRListState, CRListStateEntry } from '../../.types/index.js'\n\n/**\n * Removes a live entry from UUID and predecessor indexes.\n */\nexport function detachEntryFromIndexes<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>\n): void {\n void crListReplica.parentMap.delete(linkedListEntry.uuidv7)\n const siblings = crListReplica.childrenMap.get(linkedListEntry.predecessor)\n if (!siblings) return\n const index = siblings.indexOf(linkedListEntry)\n if (index !== -1) void siblings.splice(index, 1)\n}\n","import type {\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { detachEntryFromIndexes } from '../detachEntryFromIndexes/index.js'\n\n/**\n * Tombstones a live entry and unlinks it from the local projection.\n */\nexport function deleteLiveEntry<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n deltaBuf?: CRListDelta<T>\n): void {\n const prev = linkedListEntry.prev\n const next = linkedListEntry.next\n void crListReplica.tombstones.add(linkedListEntry.uuidv7)\n if (deltaBuf && !Array.isArray(deltaBuf.tombstones)) deltaBuf.tombstones = []\n void deltaBuf?.tombstones?.push(linkedListEntry.uuidv7)\n if (prev) prev.next = next\n if (next) {\n next.prev = prev\n }\n void detachEntryFromIndexes<T>(crListReplica, linkedListEntry)\n if (crListReplica.cursor === linkedListEntry)\n crListReplica.cursor = next ?? prev\n if (!crListReplica.cursor) crListReplica.cursorIndex = undefined\n linkedListEntry.prev = undefined\n linkedListEntry.next = undefined\n crListReplica.size = crListReplica.parentMap.size\n}\n","import type { CRListEventMap } from '../../.types/index.js'\n\n/**\n * Dispatches a typed CRList event payload through an EventTarget.\n */\nexport function dispatchCRListEvent<T, K extends keyof CRListEventMap<T>>(\n eventTarget: EventTarget,\n type: K,\n detail: CRListEventMap<T>[K]\n): void {\n void eventTarget.dispatchEvent(new CustomEvent(type, { detail }))\n}\n","import type {\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { detachEntryFromIndexes } from '../detachEntryFromIndexes/index.js'\nimport { attachEntryToIndexes } from '../attachEntryToIndexes/index.js'\n\n/**\n * Reattaches an existing live entry to a stable predecessor.\n */\nexport function moveEntryToPredecessor<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n predecessor: string,\n deltaBuf?: CRListDelta<T>\n): void {\n void detachEntryFromIndexes<T>(crListReplica, linkedListEntry)\n linkedListEntry.predecessor = predecessor\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, deltaBuf)\n}\n","/**\n * Parses a JavaScript property key as a safe non-negative list index.\n */\nexport function indexFromPropertyKey(\n index: string | symbol\n): number | undefined {\n if (typeof index !== 'string') return undefined\n const listIndex = Number(index)\n if (!Number.isSafeInteger(listIndex) || listIndex < 0) return undefined\n return String(listIndex) === index ? listIndex : undefined\n}\n","import type {\n CRListState,\n CRListStateEntry,\n CRListReparentedEntry,\n} from '../../.types/index.js'\nimport { linkEntryBetween } from '../../.helpers/index.js'\n\n/**\n * Applies the common single-insert reparent delta without a full projection rebuild.\n */\nexport function trySpliceInsertedParent<T>(\n crListReplica: CRListState<T>,\n insertedEntries: Array<NonNullable<CRListStateEntry<T>>>,\n reparentedEntries: Array<CRListReparentedEntry<T>>\n): boolean {\n if (insertedEntries.length !== 1 || reparentedEntries.length !== 1)\n return false\n const inserted = insertedEntries[0]\n const reparented = reparentedEntries[0]\n const moved = reparented.entry\n if (\n moved.predecessor !== inserted.uuidv7 ||\n inserted.predecessor !== reparented.previousPredecessor\n )\n return false\n const siblings = crListReplica.childrenMap.get(inserted.predecessor)\n const children = crListReplica.childrenMap.get(inserted.uuidv7)\n if (\n siblings?.length !== 1 ||\n siblings[0] !== inserted ||\n children?.length !== 1 ||\n children[0] !== moved\n )\n return false\n const predecessor =\n inserted.predecessor === '\\0'\n ? undefined\n : crListReplica.parentMap.get(inserted.predecessor)\n if (inserted.predecessor !== '\\0' && !predecessor) return false\n const expectedIndex = predecessor ? predecessor.index + 1 : 0\n if (\n moved.index !== expectedIndex ||\n moved.prev !== predecessor ||\n (predecessor && predecessor.next !== moved)\n )\n return false\n\n void linkEntryBetween<T>(predecessor, inserted, moved)\n let current: CRListStateEntry<T> = inserted\n let index = expectedIndex\n while (current) {\n current.index = index\n index++\n current = current.next\n }\n crListReplica.index = new Map([[inserted.index, inserted]])\n crListReplica.cursor = inserted\n crListReplica.cursorIndex = inserted.index\n crListReplica.size = crListReplica.parentMap.size\n return true\n}\n","import { isUuidV7, prototype } from '@sovereignbase/utils'\nimport {\n CRListSnapshot,\n CRListState,\n CRListStateEntry,\n} from '../../../.types/index.js'\nimport {\n rebuildLiveProjection,\n materializeSnapshotEntry,\n attachEntryToIndexes,\n linkEntryBetween,\n} from '../../../.helpers/index.js'\n\n/**\n * Creates a local CRList replica from an optional snapshot.\n *\n * Invalid snapshot records are ignored. Accepted values are kept by reference,\n * indexed by UUIDv7, linked through their predecessor buckets, and exposed as a\n * live doubly-linked list projection.\n *\n * @param snapshot - Optional CRList snapshot.\n * @returns - A hydrated CRList replica.\n *\n * Time complexity: O(n log n + t), worst case O(n^2 + t)\n * - n = snapshot value entry count\n * - t = snapshot tombstone count\n *\n * Space complexity: O(n + t)\n */\nexport function __create<T>(snapshot?: CRListSnapshot<T>): CRListState<T> {\n const crListReplica: CRListState<T> = {\n size: 0,\n cursor: undefined,\n cursorIndex: undefined,\n index: new Map(),\n tombstones: new Set<string>(),\n parentMap: new Map<string, NonNullable<CRListStateEntry<T>>>(),\n childrenMap: new Map<string, Array<NonNullable<CRListStateEntry<T>>>>(),\n }\n if (!snapshot || prototype(snapshot) !== 'record') return crListReplica\n\n /** Hydrate tombstone entries. */\n if (\n Object.hasOwn(snapshot, 'tombstones') &&\n Array.isArray(snapshot.tombstones)\n ) {\n for (const tombstone of snapshot.tombstones) {\n if (crListReplica.tombstones.has(tombstone) || !isUuidV7(tombstone))\n continue\n void crListReplica.tombstones.add(tombstone)\n }\n }\n\n /** Hydrate value entries. */\n if (!Object.hasOwn(snapshot, 'values') || !Array.isArray(snapshot.values))\n return crListReplica\n // Build predecessor tree.\n let canUseLinearProjection = true\n let previous: CRListStateEntry<T> = undefined\n for (const valueEntry of snapshot.values) {\n const linkedListEntry = materializeSnapshotEntry<T>(\n valueEntry,\n crListReplica\n )\n if (!linkedListEntry) continue\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry)\n if (\n canUseLinearProjection &&\n linkedListEntry.predecessor === (previous?.uuidv7 ?? '\\0')\n ) {\n linkedListEntry.index = crListReplica.parentMap.size - 1\n void linkEntryBetween<T>(previous, linkedListEntry, undefined)\n previous = linkedListEntry\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n continue\n }\n canUseLinearProjection = false\n }\n if (canUseLinearProjection) {\n crListReplica.cursor = previous\n crListReplica.cursorIndex = previous\n ? crListReplica.parentMap.size - 1\n : undefined\n crListReplica.size = crListReplica.parentMap.size\n return crListReplica\n }\n // Flatten tree into a doubly linked list and write live-view indexes.\n void rebuildLiveProjection<T>(crListReplica)\n\n return crListReplica\n}\n","import { seekCursorToIndex } from '../../../.helpers/index.js'\nimport { CRListState } from '../../../.types/index.js'\n\n/**\n * Reads the value at an index in the replica live view.\n *\n * The replica cursor is moved as part of the lookup. Successful reads return\n * the live value reference stored by the replica. Mutating that value directly\n * can mutate replica state and should only be done when the caller owns an\n * independent value object. Out-of-bounds and empty list reads resolve to\n * `undefined` instead of throwing.\n *\n * @param targetIndex - Index in the live list.\n * @param crListReplica - Replica to read from.\n * @returns - The live value at `targetIndex`, or `undefined` when\n * no value is present.\n *\n * Time complexity: O(d), worst case O(n)\n * - d = distance from cursor to target index\n * - n = list size\n *\n * Space complexity: O(1)\n */\nexport function __read<T>(\n targetIndex: number,\n crListReplica: CRListState<T>\n): T | undefined {\n try {\n void seekCursorToIndex<T>(targetIndex, crListReplica)\n return crListReplica.cursor?.value\n } catch {\n return undefined\n }\n}\n","import { CRListError } from '../../../.errors/class.js'\nimport {\n attachEntryToIndexes,\n detachEntryFromIndexes,\n seekCursorToIndex,\n moveEntryToPredecessor,\n linkEntryBetween,\n} from '../../../.helpers/index.js'\nimport { v7 as uuidv7 } from 'uuid'\nimport {\n CRListChange,\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../../.types/index.js'\n/**\n * Applies a local value mutation to the replica live view.\n *\n * The update can replace a range starting at the target entry, insert values\n * before it, or insert values after it. The returned delta is suitable for\n * gossip and the returned change describes the local live-view patch.\n *\n * @param listIndex - Target index in the live list.\n * @param listValues - Values to insert or overwrite.\n * @param crListReplica - Replica to mutate.\n * @param mode - Mutation mode relative to `listIndex`.\n * @returns - A local change and gossip delta, or `false` if no mutation occurred.\n *\n * Time complexity: O(d + v + r + vk), worst case O(vn)\n * - d = distance from cursor to target index\n * - v = amount of input values\n * - r = amount of nodes after inserted values whose indexes must be shifted\n * - k = sibling bucket size when predecessor bucket is updated\n *\n * Space complexity: O(v)\n */\nexport function __update<T>(\n listIndex: number,\n listValues: Array<T>,\n crListReplica: CRListState<T>,\n mode: 'overwrite' | 'before' | 'after'\n): { change: CRListChange<T>; delta: CRListDelta<T> } | false {\n if (listIndex < 0 || listIndex > crListReplica.size)\n throw new CRListError('INDEX_OUT_OF_BOUNDS')\n if (!Array.isArray(listValues))\n throw new CRListError(\n 'UPDATE_EXPECTED_AN_ARRAY',\n '`listValues` must be an Array'\n )\n if (listValues.length === 0) return false\n const change: CRListChange<T> = {}\n const delta: CRListDelta<T> = { values: [], tombstones: [] }\n for (const listValue of listValues) {\n const v7 = uuidv7()\n\n const linkedListEntry: NonNullable<CRListStateEntry<T>> = {\n uuidv7: v7,\n value: listValue,\n predecessor: '\\0',\n index: 0,\n next: undefined,\n prev: undefined,\n }\n\n switch (mode) {\n case 'overwrite': {\n if (listIndex === crListReplica.size) {\n if (crListReplica.size === 0) {\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n void seekCursorToIndex<T>(crListReplica.size - 1, crListReplica)\n if (!crListReplica.cursor) return false\n linkedListEntry.index = (crListReplica.cursorIndex ?? 0) + 1\n linkedListEntry.predecessor = crListReplica.cursor.uuidv7\n void linkEntryBetween<T>(\n crListReplica.cursor,\n linkedListEntry,\n undefined\n )\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n void seekCursorToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n const entryToOverwrite = crListReplica.cursor\n const actualIndex = crListReplica.cursorIndex ?? listIndex\n\n linkedListEntry.predecessor = entryToOverwrite.predecessor\n linkedListEntry.index = actualIndex\n void linkEntryBetween<T>(\n entryToOverwrite.prev,\n linkedListEntry,\n entryToOverwrite.next\n )\n if (entryToOverwrite.next) {\n if (entryToOverwrite.next.predecessor === entryToOverwrite.uuidv7) {\n void moveEntryToPredecessor<T>(\n crListReplica,\n entryToOverwrite.next,\n linkedListEntry.uuidv7,\n delta\n )\n }\n }\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n void crListReplica.tombstones.add(entryToOverwrite.uuidv7)\n void delta.tombstones?.push(entryToOverwrite.uuidv7)\n void detachEntryFromIndexes<T>(crListReplica, entryToOverwrite)\n entryToOverwrite.next = undefined\n entryToOverwrite.prev = undefined\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = actualIndex\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[actualIndex] = linkedListEntry.value\n break\n }\n case 'after': {\n if (crListReplica.size === 0 && listIndex === 0) {\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n if (listIndex === crListReplica.size) {\n void seekCursorToIndex<T>(crListReplica.size - 1, crListReplica)\n } else {\n void seekCursorToIndex<T>(listIndex, crListReplica)\n }\n if (!crListReplica.cursor) return false\n const actualIndex = crListReplica.cursorIndex ?? listIndex\n const next =\n listIndex === crListReplica.size\n ? undefined\n : crListReplica.cursor.next\n linkedListEntry.index = actualIndex + 1\n linkedListEntry.predecessor = crListReplica.cursor.uuidv7\n void linkEntryBetween<T>(crListReplica.cursor, linkedListEntry, next)\n if (next) {\n if (next.predecessor === crListReplica.cursor.uuidv7) {\n void moveEntryToPredecessor<T>(\n crListReplica,\n next,\n linkedListEntry.uuidv7,\n delta\n )\n }\n }\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n if (next) crListReplica.index = new Map()\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n case 'before': {\n if (crListReplica.size === 0 && listIndex === 0) {\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[linkedListEntry.index] = linkedListEntry.value\n mode = 'after'\n listIndex = linkedListEntry.index - 1\n break\n }\n void seekCursorToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n const actualIndex = crListReplica.cursorIndex ?? listIndex\n const prev = crListReplica.cursor.prev\n linkedListEntry.index = actualIndex\n linkedListEntry.predecessor = prev?.uuidv7 ?? '\\0'\n void linkEntryBetween<T>(prev, linkedListEntry, crListReplica.cursor)\n if (crListReplica.cursor.predecessor === linkedListEntry.predecessor) {\n void moveEntryToPredecessor<T>(\n crListReplica,\n crListReplica.cursor,\n linkedListEntry.uuidv7,\n delta\n )\n }\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = actualIndex\n crListReplica.index = new Map()\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n change[actualIndex] = linkedListEntry.value\n mode = 'after'\n listIndex = linkedListEntry.index - 1\n\n break\n }\n }\n crListReplica.size = crListReplica.parentMap.size\n listIndex++\n }\n return { change, delta }\n}\n","import {\n attachEntryToIndexes,\n deleteLiveEntry,\n linkEntryBetween,\n moveEntryToPredecessor,\n seekCursorToIndex,\n} from '../../../.helpers/index.js'\nimport { CRListError } from '../../../.errors/class.js'\nimport { v7 as uuidv7 } from 'uuid'\nimport type {\n CRListChange,\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../../.types/index.js'\n\n/**\n * Deletes a range from the replica live view.\n *\n * With no indexes, the full list is deleted. With only `startIndex`, all entries\n * from `startIndex` onward are deleted. With both indexes, the deleted range is\n * `[startIndex, endIndex)`.\n *\n * @param crListReplica - Replica to mutate.\n * @param startIndex - Inclusive start index. Defaults to `0`.\n * @param endIndex - Exclusive end index. Defaults to the current list size.\n * @returns - A local change and gossip delta, or `false` if nothing was deleted.\n *\n * Time complexity: O(d + qk + r), worst case O(n^2)\n * - d = distance from cursor to target index\n * - q = amount of deleted nodes\n * - r = amount of nodes after the deleted range whose indexes must be shifted\n * - k = sibling bucket size when deleted entries are removed from buckets\n *\n * Space complexity: O(q)\n */\nexport function __delete<T>(\n crListReplica: CRListState<T>,\n startIndex?: number,\n endIndex?: number\n): { change: CRListChange<T>; delta: CRListDelta<T> } | false {\n const change: CRListChange<T> = {}\n const delta: CRListDelta<T> = { values: [], tombstones: [] }\n const listIndex = startIndex ?? 0\n const targetEndIndex = endIndex ?? crListReplica.size\n if (\n listIndex < 0 ||\n targetEndIndex < listIndex ||\n listIndex > crListReplica.size\n )\n throw new CRListError('INDEX_OUT_OF_BOUNDS')\n const deleteCount = Math.min(targetEndIndex, crListReplica.size) - listIndex\n if (deleteCount <= 0) return false\n\n void seekCursorToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n\n let current: CRListStateEntry<T> = crListReplica.cursor\n const predecessor = current.prev?.uuidv7 ?? '\\0'\n const deletedIds = new Set<string>()\n let deleted = 0\n let currentIndex = crListReplica.cursorIndex ?? listIndex\n\n while (current && deleted < deleteCount) {\n const next: CRListStateEntry<T> = current.next\n change[currentIndex] = undefined\n void deletedIds.add(current.uuidv7)\n void crListReplica.index?.delete(currentIndex)\n void deleteLiveEntry<T>(crListReplica, current, delta)\n current = next\n currentIndex++\n deleted++\n }\n if (current && deletedIds.has(current.predecessor)) {\n const replacement: NonNullable<CRListStateEntry<T>> = {\n uuidv7: uuidv7(),\n value: current.value,\n predecessor,\n index: listIndex,\n next: undefined,\n prev: undefined,\n }\n const prev = current.prev\n const next = current.next\n void deleteLiveEntry<T>(crListReplica, current, delta)\n void linkEntryBetween<T>(prev, replacement, next)\n void attachEntryToIndexes<T>(crListReplica, replacement, delta)\n if (next?.predecessor === current.uuidv7)\n void moveEntryToPredecessor<T>(\n crListReplica,\n next,\n replacement.uuidv7,\n delta\n )\n current = replacement\n }\n\n crListReplica.size = crListReplica.parentMap.size\n crListReplica.cursor = current ?? crListReplica.cursor\n crListReplica.cursorIndex = current\n ? listIndex\n : crListReplica.cursor\n ? Math.max(0, crListReplica.size - 1)\n : undefined\n crListReplica.index = new Map()\n if (crListReplica.cursor && crListReplica.cursorIndex !== undefined)\n void crListReplica.index.set(\n crListReplica.cursorIndex,\n crListReplica.cursor\n )\n\n return { change, delta }\n}\n","import type {\n CRListChange,\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../../.types/index.js'\nimport {\n materializeSnapshotEntry,\n attachEntryToIndexes,\n rebuildLiveProjection,\n rebuildLiveIndex,\n deleteLiveEntry,\n moveEntryToPredecessor,\n} from '../../../.helpers/index.js'\nimport { prototype, isUuidV7 } from '@sovereignbase/utils'\nimport { trySpliceInsertedParent } from '../../../.helpers/trySpliceInsertedParent/index.js'\n\n/**\n * Merges a remote CRList delta into the local replica.\n *\n * Accepted tombstones update the local live view and accepted values are attached\n * to the predecessor tree. Tail-append deltas are linked incrementally; deltas\n * that can affect ordering fall back to deterministic relinking.\n *\n * @param crListReplica - Replica to mutate.\n * @param crListDelta - Remote gossip delta.\n * @returns - A minimal local change patch, or `false` when the delta is ignored.\n *\n * Time complexity: O(v + t) for tail-append deltas; O(n + t + qk) for tombstone-only deletes; otherwise O(n log n + v + t + m*k)\n * Worst case: O(n^2 + (v + t)n)\n * - n = replica value entry count after merge\n * - v = delta value entry count\n * - t = delta tombstone count\n * - q = amount of live entries deleted by tombstones\n * - m = entries moved between predecessor buckets\n * - k = sibling bucket size when entries are removed from buckets\n *\n * Space complexity: O(n + v + t)\n */\nexport function __merge<T>(\n crListReplica: CRListState<T>,\n crListDelta: CRListDelta<T>\n): CRListChange<T> | false {\n if (!crListDelta || prototype(crListDelta) !== 'record') return false\n const newVals: Array<NonNullable<CRListStateEntry<T>>> = []\n const newTombsIndices: Array<number> = []\n const reparentedVals: Array<{\n entry: NonNullable<CRListStateEntry<T>>\n previousPredecessor: string\n }> = []\n const change: CRListChange<T> = {}\n let needsRelink = false\n if (\n Object.hasOwn(crListDelta, 'values') &&\n Array.isArray(crListDelta.values) &&\n crListDelta.values.length === 1 &&\n (!Object.hasOwn(crListDelta, 'tombstones') ||\n (Array.isArray(crListDelta.tombstones) &&\n crListDelta.tombstones.length === 0))\n ) {\n const linkedListEntry = materializeSnapshotEntry<T>(\n crListDelta.values[0],\n crListReplica\n )\n if (!linkedListEntry) return false\n const predecessor =\n linkedListEntry.predecessor === '\\0'\n ? undefined\n : crListReplica.parentMap.get(linkedListEntry.predecessor)\n if (\n (linkedListEntry.predecessor === '\\0' && crListReplica.size === 0) ||\n (predecessor && !predecessor.next)\n ) {\n linkedListEntry.prev = predecessor\n linkedListEntry.index = crListReplica.size\n if (predecessor) predecessor.next = linkedListEntry\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry)\n crListReplica.size = crListReplica.parentMap.size\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n return { [linkedListEntry.index]: linkedListEntry.value }\n }\n }\n\n /** Apply tombstone entries. */\n if (\n Object.hasOwn(crListDelta, 'tombstones') &&\n Array.isArray(crListDelta.tombstones)\n ) {\n for (const tombstone of crListDelta.tombstones) {\n if (crListReplica.tombstones.has(tombstone) || !isUuidV7(tombstone))\n continue\n void crListReplica.tombstones.add(tombstone)\n const linkedListEntry = crListReplica.parentMap.get(tombstone)\n if (linkedListEntry) {\n void newTombsIndices.push(linkedListEntry.index)\n void crListReplica.index?.delete(linkedListEntry.index)\n void deleteLiveEntry<T>(crListReplica, linkedListEntry)\n needsRelink = true\n }\n }\n }\n\n /** Apply value entries. */\n if (\n !Object.hasOwn(crListDelta, 'values') ||\n !Array.isArray(crListDelta.values)\n ) {\n if (newTombsIndices.length === 0) return false\n void rebuildLiveIndex<T>(crListReplica)\n for (const index of newTombsIndices) {\n change[index] = undefined\n }\n return change\n }\n // Attach accepted values to the predecessor tree.\n for (const valueEntry of crListDelta.values) {\n if (valueEntry === null || valueEntry === undefined) continue\n const existingEntry = crListReplica.parentMap.get(valueEntry.uuidv7)\n if (existingEntry) {\n if (crListReplica.tombstones.has(valueEntry.uuidv7)) continue\n if (valueEntry.predecessor !== '\\0' && !isUuidV7(valueEntry.predecessor))\n continue\n if (existingEntry.predecessor >= valueEntry.predecessor) continue\n const previousPredecessor = existingEntry.predecessor\n void moveEntryToPredecessor<T>(\n crListReplica,\n existingEntry,\n valueEntry.predecessor\n )\n void reparentedVals.push({ entry: existingEntry, previousPredecessor })\n needsRelink = true\n continue\n }\n const linkedListEntry = materializeSnapshotEntry<T>(\n valueEntry,\n crListReplica\n )\n if (!linkedListEntry) continue\n const predecessor =\n linkedListEntry.predecessor === '\\0'\n ? undefined\n : crListReplica.parentMap.get(linkedListEntry.predecessor)\n void attachEntryToIndexes<T>(crListReplica, linkedListEntry)\n void newVals.push(linkedListEntry)\n if (!needsRelink && linkedListEntry.predecessor === '\\0') {\n if (crListReplica.size === 0) {\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n crListReplica.size = crListReplica.parentMap.size\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n } else {\n needsRelink = true\n }\n } else if (!needsRelink && predecessor && !predecessor.next) {\n linkedListEntry.prev = predecessor\n linkedListEntry.index = crListReplica.size\n predecessor.next = linkedListEntry\n crListReplica.cursor = linkedListEntry\n crListReplica.cursorIndex = linkedListEntry.index\n crListReplica.size = crListReplica.parentMap.size\n void crListReplica.index?.set(linkedListEntry.index, linkedListEntry)\n } else {\n needsRelink = true\n }\n }\n if (needsRelink) {\n if (!trySpliceInsertedParent<T>(crListReplica, newVals, reparentedVals)) {\n // Flatten tree into a doubly linked list and write live-view indexes.\n void rebuildLiveProjection<T>(crListReplica)\n }\n }\n\n if (newTombsIndices.length === 0 && newVals.length === 0) return false\n\n for (const index of newTombsIndices) {\n change[index] = undefined\n }\n for (const val of newVals) {\n change[val.index] = val.value\n }\n\n return change\n}\n","import type { CRListAck, CRListState } from '../../../.types/index.js'\n\n/**\n * Returns the replica tombstone acknowledgement frontier.\n *\n * The frontier is the greatest tombstone identifier currently retained by the\n * replica. Peers can use it as input for tombstone garbage collection.\n *\n * @param crListReplica - Replica to acknowledge.\n * @returns - The acknowledgement frontier, or `false` when there are no tombstones.\n *\n * Time complexity: O(t)\n * - t = replica tombstone count\n *\n * Space complexity: O(1)\n */\nexport function __acknowledge<T>(\n crListReplica: CRListState<T>\n): CRListAck | false {\n let largest: CRListAck | false = false\n void crListReplica.tombstones.forEach((tombstone) => {\n if (largest === false || largest < tombstone) largest = tombstone\n })\n if (typeof largest === 'string') return largest\n return false\n}\n","import { isUuidV7 } from '@sovereignbase/utils'\nimport { CRListAck, CRListState } from '../../../.types/index.js'\n\n/**\n * Removes tombstones acknowledged by all supplied frontiers.\n *\n * The smallest valid UUIDv7 frontier is used as the safe collection boundary.\n * Tombstones less than or equal to that boundary are removed from the local\n * replica.\n *\n * @param frontiers - Acknowledgement frontiers received from peers.\n * @param crListReplica - Replica whose tombstones will be collected.\n *\n * Time complexity: O(f log f + t)\n * - f = frontier count\n * - t = replica tombstone count\n *\n * Space complexity: O(1)\n */\nexport function __garbageCollect<T>(\n frontiers: Array<CRListAck>,\n crListReplica: CRListState<T>\n): void {\n if (!Array.isArray(frontiers)) return\n void frontiers.sort()\n const smallest = frontiers.find((frontier) => isUuidV7(frontier))\n if (typeof smallest !== 'string') return\n void crListReplica.tombstones.forEach((tombstone, __, tombstones) => {\n if (tombstone <= smallest) {\n void tombstones.delete(tombstone)\n }\n })\n}\n","import { CRListError } from '../../../.errors/class.js'\nimport { CRListState, CRListSnapshot } from '../../../.types/index.js'\n\n/**\n * Creates a full CRList snapshot from the current replica state.\n *\n * The snapshot contains every live value entry and all retained tombstones. Value\n * payloads are live references, so callers must not mutate snapshot values\n * unless they have first isolated them from replica state.\n *\n * @param crListReplica - Replica to snapshot.\n * @returns - A full snapshot suitable for hydration or transport.\n *\n * Time complexity: O(n + t)\n * - n = replica value entry count\n * - t = replica tombstone count\n *\n * Space complexity: O(n + t)\n */\nexport function __snapshot<T>(\n crListReplica: CRListState<T>\n): CRListSnapshot<T> {\n return {\n values: Array.from(crListReplica.parentMap.values()).map(\n (linkedListEntry) => {\n if (!linkedListEntry) throw new CRListError('LIST_INTEGRITY_VIOLATION')\n return {\n uuidv7: linkedListEntry.uuidv7,\n value: linkedListEntry.value,\n predecessor: linkedListEntry.predecessor,\n }\n }\n ),\n tombstones: Array.from(crListReplica.tombstones),\n }\n}\n","import { dispatchCRListEvent, indexFromPropertyKey } from '../.helpers/index.js'\nimport { CRListError } from '../.errors/class.js'\nimport type {\n CRListState,\n CRListSnapshot,\n CRListEventListenerFor,\n CRListEventMap,\n CRListDelta,\n CRListAck,\n} from '../.types/index.js'\nimport { __create, __read, __update, __delete } from '../core/crud/index.js'\nimport {\n __merge,\n __acknowledge,\n __garbageCollect,\n __snapshot,\n} from '../core/mags/index.js'\n\n/**\n * A convergent replicated list.\n *\n * Numeric property access reads and mutates the live list projection:\n * `list[0]` reads the live value reference, `list[0] = value` writes an\n * entry, and `delete list[0]` removes one entry. Iteration, `find()`, and\n * `forEach()` expose the same live value references. Mutating returned objects\n * directly can mutate replica state without producing a CRDT delta, so callers\n * must isolate values before out-of-band mutation. Local mutations emit `delta`\n * and `change` events; remote merges emit `change` events.\n *\n * @typeParam T - The value type stored in the list.\n */\nexport class CRList<T> {\n /**\n * Reads or overwrites an entry in the live list projection by index.\n *\n * Reads return live value references.\n */\n [index: number]: T\n declare private readonly state: CRListState<T>\n declare private readonly eventTarget: EventTarget\n\n /**\n * Creates a replicated list from an optional CRList snapshot.\n *\n * @param snapshot - A previously emitted CRList snapshot.\n */\n constructor(snapshot?: CRListSnapshot<T>) {\n void Object.defineProperties(this, {\n state: {\n value: __create<T>(snapshot),\n enumerable: false,\n configurable: false,\n writable: false,\n },\n eventTarget: {\n value: new EventTarget(),\n enumerable: false,\n configurable: false,\n writable: false,\n },\n })\n\n return new Proxy(this, {\n get(target, index, receiver) {\n const listIndex = indexFromPropertyKey(index)\n // Preserve normal property access for non-index keys.\n if (listIndex === undefined) return Reflect.get(target, index, receiver)\n return __read(listIndex, target.state)\n },\n has(target, index) {\n const listIndex = indexFromPropertyKey(index)\n // Preserve normal property checks for non-index keys.\n if (listIndex === undefined) return Reflect.has(target, index)\n return listIndex >= 0 && listIndex < target.state.size\n },\n set(target, index, value) {\n const listIndex = indexFromPropertyKey(index)\n if (listIndex === undefined) return false\n try {\n const result = __update(listIndex, [value], target.state, 'overwrite')\n if (!result) return false\n const { delta, change } = result\n if (delta)\n void dispatchCRListEvent(target.eventTarget, 'delta', delta)\n if (change)\n void dispatchCRListEvent(target.eventTarget, 'change', change)\n return true\n } catch (error) {\n if (error instanceof CRListError) throw error\n return false\n }\n },\n deleteProperty(target, index) {\n const listIndex = indexFromPropertyKey(index)\n if (listIndex === undefined) return false\n try {\n const result = __delete(target.state, listIndex, listIndex + 1)\n if (!result) return false\n const { delta, change } = result\n if (delta)\n void dispatchCRListEvent(target.eventTarget, 'delta', delta)\n if (change)\n void dispatchCRListEvent(target.eventTarget, 'change', change)\n return true\n } catch (error) {\n if (error instanceof CRListError) throw error\n return false\n }\n },\n ownKeys(target) {\n return [\n ...Reflect.ownKeys(target),\n ...Array.from({ length: target.size }, (_, index) => String(index)),\n ]\n },\n\n getOwnPropertyDescriptor(target, index) {\n const listIndex = indexFromPropertyKey(index)\n\n if (listIndex !== undefined && listIndex < target.size) {\n return {\n value: __read(listIndex, target.state),\n writable: true,\n enumerable: true,\n configurable: true,\n }\n }\n // Preserve normal property checks for non-index keys.\n return Reflect.getOwnPropertyDescriptor(target, index)\n },\n })\n }\n\n /**\n * The current number of live entries.\n */\n get size(): number {\n return this.state.size\n }\n /**\n * Inserts a value before an index.\n *\n * If `beforeIndex` is omitted, the value is inserted at the start of the list.\n *\n * @param value - The value to insert.\n * @param beforeIndex - The index to insert before.\n */\n prepend(value: T, beforeIndex?: number): void {\n const result = __update<T>(beforeIndex ?? 0, [value], this.state, 'before')\n if (!result) return\n const { delta, change } = result\n if (delta) void dispatchCRListEvent(this.eventTarget, 'delta', delta)\n if (change) void dispatchCRListEvent(this.eventTarget, 'change', change)\n }\n /**\n * Inserts a value after an index.\n *\n * If `afterIndex` is omitted, the value is appended at the end of the list.\n *\n * @param value - The value to insert.\n * @param afterIndex - The index to insert after.\n */\n append(value: T, afterIndex?: number): void {\n const result = __update<T>(\n afterIndex ?? this.state.size,\n [value],\n this.state,\n 'after'\n )\n if (!result) return\n const { delta, change } = result\n if (delta) void dispatchCRListEvent(this.eventTarget, 'delta', delta)\n if (change) void dispatchCRListEvent(this.eventTarget, 'change', change)\n }\n /**\n * Removes the entry at an index.\n *\n * @param index - The index to remove.\n */\n remove(index: number): void {\n const result = __delete(this.state, index, index + 1)\n if (!result) return\n const { delta, change } = result\n if (delta) void dispatchCRListEvent(this.eventTarget, 'delta', delta)\n if (change) void dispatchCRListEvent(this.eventTarget, 'change', change)\n }\n\n /**\n * Returns the first live value matching a predicate in index order.\n *\n * Predicate values are live references. Mutating them directly can mutate the\n * list without emitting a delta.\n *\n * @param predicate - Function to test each live value.\n * @param thisArg - Optional `this` value for the predicate.\n */\n find(\n predicate: (this: unknown, value: T, index: number, list: this) => unknown,\n thisArg?: unknown\n ): T | undefined {\n let linkedListEntry = this.state.index?.get(0) ?? this.state.cursor\n while (linkedListEntry?.prev) linkedListEntry = linkedListEntry.prev\n let index = 0\n while (linkedListEntry) {\n if (predicate.call(thisArg, linkedListEntry.value, index, this))\n return linkedListEntry.value\n linkedListEntry = linkedListEntry.next\n index++\n }\n\n return undefined\n }\n\n /**\n * Applies a remote gossip delta to this list.\n *\n * Emits a `change` event when the merge changes the live projection.\n *\n * @param delta - The remote CRList delta to merge.\n */\n merge(delta: CRListDelta<T>): void {\n const change = __merge(this.state, delta)\n if (change) void dispatchCRListEvent(this.eventTarget, 'change', change)\n }\n /**\n * Emits an acknowledgement frontier for currently retained tombstones.\n */\n acknowledge(): void {\n const ack = __acknowledge(this.state)\n if (ack) void dispatchCRListEvent(this.eventTarget, 'ack', ack)\n }\n /**\n * Garbage-collects tombstones that are covered by acknowledgement frontiers.\n *\n * @param frontiers - Replica acknowledgement frontiers.\n */\n garbageCollect(frontiers: Array<CRListAck>): void {\n void __garbageCollect(frontiers, this.state)\n }\n /**\n * Emits the current CRList snapshot.\n *\n * Snapshot value payloads are live references. Mutating them can mutate\n * replica state without emitting a delta.\n */\n snapshot(): void {\n const snapshot = __snapshot<T>(this.state)\n if (snapshot)\n void dispatchCRListEvent(this.eventTarget, 'snapshot', snapshot)\n }\n /**\n * Registers an event listener.\n *\n * @param type - The event type to listen for.\n * @param listener - The listener to register.\n * @param options - Listener registration options.\n */\n addEventListener<K extends keyof CRListEventMap<T>>(\n type: K,\n listener: CRListEventListenerFor<T, K> | null,\n options?: boolean | AddEventListenerOptions\n ): void {\n void this.eventTarget.addEventListener(\n type,\n listener as EventListenerOrEventListenerObject | null,\n options\n )\n }\n\n /**\n * Removes an event listener.\n *\n * @param type - The event type to stop listening for.\n * @param listener - The listener to remove.\n * @param options - Listener removal options.\n */\n removeEventListener<K extends keyof CRListEventMap<T>>(\n type: K,\n listener: CRListEventListenerFor<T, K> | null,\n options?: boolean | EventListenerOptions\n ): void {\n void this.eventTarget.removeEventListener(\n type,\n listener as EventListenerOrEventListenerObject | null,\n options\n )\n }\n /**\n * Returns a CRList snapshot of this list.\n *\n * Snapshot value payloads are live references. Mutating them can mutate\n * replica state without emitting a delta.\n *\n * Called automatically by `JSON.stringify`.\n */\n toJSON(): CRListSnapshot<T> {\n return __snapshot<T>(this.state)\n }\n /**\n * Attempts to return this list snapshot as a JSON string.\n *\n * This can fail when list values are not JSON-compatible.\n */\n toString(): string {\n return JSON.stringify(this)\n }\n /**\n * Returns the Node.js console inspection representation.\n */\n [Symbol.for('nodejs.util.inspect.custom')](): CRListSnapshot<T> {\n return this.toJSON()\n }\n /**\n * Returns the Deno console inspection representation.\n */\n [Symbol.for('Deno.customInspect')](): CRListSnapshot<T> {\n return this.toJSON()\n }\n /**\n * Iterates over current live values in index order.\n */\n *[Symbol.iterator](): IterableIterator<T> {\n let linkedListEntry = this.state.index?.get(0) ?? this.state.cursor\n while (linkedListEntry?.prev) linkedListEntry = linkedListEntry.prev\n while (linkedListEntry) {\n yield linkedListEntry.value\n linkedListEntry = linkedListEntry.next\n }\n }\n /**\n * Calls a function once for each live value in index order.\n *\n * Callback values are live references. Mutating them directly can mutate the\n * list without emitting a delta.\n *\n * @param callback - Function to call for each live value.\n * @param thisArg - Optional `this` value for the callback.\n */\n forEach(\n callback: (value: T, index: number, list: this) => void,\n thisArg?: unknown\n ): void {\n let linkedListEntry = this.state.index?.get(0) ?? this.state.cursor\n while (linkedListEntry?.prev) linkedListEntry = linkedListEntry.prev\n let index = 0\n while (linkedListEntry) {\n void callback.call(thisArg, linkedListEntry.value, index, this)\n linkedListEntry = linkedListEntry.next\n index++\n }\n }\n}\n"]}
|