@sovereignbase/convergent-replicated-list 1.0.3 → 1.0.5
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 +22 -9
- package/dist/index.cjs +33 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -29
- package/dist/index.d.ts +47 -29
- package/dist/index.js +32 -23
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
|
|
8
8
|
Convergent Replicated List (CR-List), a delta CRDT for an ordered sequence of entries.
|
|
9
9
|
|
|
10
|
+
Read the specification:
|
|
11
|
+
|
|
12
|
+
- https://sovereignbase.dev/convergent-replicated-list
|
|
13
|
+
|
|
10
14
|
## Compatibility
|
|
11
15
|
|
|
12
16
|
- Runtimes: Node >= 20, modern browsers, Bun, Deno, Cloudflare Workers, Edge Runtime.
|
|
@@ -127,8 +131,8 @@ list[0] = 'up'
|
|
|
127
131
|
list.append('dude!')
|
|
128
132
|
list.prepend('What is')
|
|
129
133
|
|
|
130
|
-
const
|
|
131
|
-
const restored = new CRList<string>(JSON.parse(
|
|
134
|
+
const snapshotJson = JSON.stringify(list)
|
|
135
|
+
const restored = new CRList<string>(JSON.parse(snapshotJson))
|
|
132
136
|
|
|
133
137
|
for (const value of list) {
|
|
134
138
|
console.log(value)
|
|
@@ -145,6 +149,11 @@ list.forEach((value, index, target) => {
|
|
|
145
149
|
console.log([...restored]) // ['What is', 'up', 'dude!']
|
|
146
150
|
```
|
|
147
151
|
|
|
152
|
+
This example assumes your list values are JSON-compatible. For general
|
|
153
|
+
`structuredClone`-compatible values such as `Date`, `Map`, or `BigInt`, persist
|
|
154
|
+
snapshots with a structured-clone-capable store or an application-level codec
|
|
155
|
+
instead of plain `JSON.stringify` / `JSON.parse`.
|
|
156
|
+
|
|
148
157
|
Numeric reads, `for...of`, and `forEach()` return detached copies of visible
|
|
149
158
|
values. Mutating those returned values does not mutate the underlying replica
|
|
150
159
|
state.
|
|
@@ -200,17 +209,18 @@ import {
|
|
|
200
209
|
type CRListSnapshot,
|
|
201
210
|
} from '@sovereignbase/convergent-replicated-list'
|
|
202
211
|
|
|
203
|
-
const
|
|
204
|
-
const
|
|
212
|
+
const source = __create<string>()
|
|
213
|
+
const target = __create<string>()
|
|
214
|
+
const local = __update(0, ['hello', 'world'], source, 'after')
|
|
205
215
|
|
|
206
216
|
if (local) {
|
|
207
217
|
const outgoing: CRListDelta<string> = local.delta
|
|
208
|
-
const remoteChange = __merge(
|
|
218
|
+
const remoteChange = __merge(target, outgoing)
|
|
209
219
|
|
|
210
220
|
console.log(remoteChange)
|
|
211
221
|
}
|
|
212
222
|
|
|
213
|
-
const snapshot: CRListSnapshot<string> = __snapshot(
|
|
223
|
+
const snapshot: CRListSnapshot<string> = __snapshot(target)
|
|
214
224
|
console.log(snapshot)
|
|
215
225
|
```
|
|
216
226
|
|
|
@@ -243,10 +253,13 @@ Ingress stays tolerant:
|
|
|
243
253
|
|
|
244
254
|
### Safety and copying semantics
|
|
245
255
|
|
|
246
|
-
- Snapshots are
|
|
247
|
-
- Deltas are
|
|
256
|
+
- Snapshots are detached structured-clone full-state payloads.
|
|
257
|
+
- Deltas are detached structured-clone gossip payloads intended to be forwarded
|
|
258
|
+
as-is.
|
|
248
259
|
- `change` is a minimal index-keyed local patch.
|
|
249
|
-
- `toJSON()` returns a detached
|
|
260
|
+
- `toJSON()` returns a detached structured-clone snapshot.
|
|
261
|
+
- `JSON.stringify()` and `toString()` are only reliable when list values are
|
|
262
|
+
JSON-compatible.
|
|
250
263
|
- Numeric reads, `for...of`, and `forEach()` expose detached copies of visible values rather than mutable references into replica state.
|
|
251
264
|
- `for...of`, `forEach()`, numeric indexing, `append()`, `prepend()`, `remove()`, `merge()`, `snapshot()`, `acknowledge()`, and `garbageCollect()` all operate on the live list projection.
|
|
252
265
|
|
package/dist/index.cjs
CHANGED
|
@@ -134,9 +134,10 @@ function flattenAndLinkTrustedState(crListReplica) {
|
|
|
134
134
|
crListReplica.size = crListReplica.parentMap.size;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
// src/.helpers/
|
|
137
|
+
// src/.helpers/transformSnapshotEntryToStateEntry/index.ts
|
|
138
138
|
var _utils = require('@sovereignbase/utils');
|
|
139
|
-
function
|
|
139
|
+
function transformSnapshotEntryToStateEntry(valueEntry, crListReplica) {
|
|
140
|
+
if (valueEntry === null || valueEntry === void 0) return void 0;
|
|
140
141
|
if (!_utils.isUuidV7.call(void 0, valueEntry.uuidv7) || crListReplica.tombstones.has(valueEntry.uuidv7) || crListReplica.parentMap.has(valueEntry.uuidv7) || !_utils.isUuidV7.call(void 0, valueEntry.predecessor) && valueEntry.predecessor !== "\0" && !crListReplica.tombstones.has(valueEntry.predecessor))
|
|
141
142
|
return void 0;
|
|
142
143
|
const [cloned, copiedValue] = _utils.safeStructuredClone.call(void 0, valueEntry.value);
|
|
@@ -235,7 +236,7 @@ function __create(snapshot) {
|
|
|
235
236
|
if (!Object.hasOwn(snapshot, "values") || !Array.isArray(snapshot.values))
|
|
236
237
|
return crListReplica;
|
|
237
238
|
for (const valueEntry of snapshot.values) {
|
|
238
|
-
const linkedListEntry =
|
|
239
|
+
const linkedListEntry = transformSnapshotEntryToStateEntry(
|
|
239
240
|
valueEntry,
|
|
240
241
|
crListReplica
|
|
241
242
|
);
|
|
@@ -300,7 +301,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
300
301
|
insertBetween(crListReplica.cursor, linkedListEntry, void 0);
|
|
301
302
|
void updateEntryToMaps(crListReplica, linkedListEntry, delta);
|
|
302
303
|
crListReplica.cursor = linkedListEntry;
|
|
303
|
-
change[linkedListEntry.index] = linkedListEntry.value;
|
|
304
|
+
change[linkedListEntry.index] = structuredClone(linkedListEntry.value);
|
|
304
305
|
break;
|
|
305
306
|
}
|
|
306
307
|
void walkToIndex(listIndex, crListReplica);
|
|
@@ -330,14 +331,14 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
330
331
|
entryToOverwrite.next = void 0;
|
|
331
332
|
entryToOverwrite.prev = void 0;
|
|
332
333
|
crListReplica.cursor = linkedListEntry;
|
|
333
|
-
change[linkedListEntry.index] = linkedListEntry.value;
|
|
334
|
+
change[linkedListEntry.index] = structuredClone(linkedListEntry.value);
|
|
334
335
|
break;
|
|
335
336
|
}
|
|
336
337
|
case "after": {
|
|
337
338
|
if (crListReplica.size === 0 && listIndex === 0) {
|
|
338
339
|
crListReplica.cursor = linkedListEntry;
|
|
339
340
|
void updateEntryToMaps(crListReplica, linkedListEntry, delta);
|
|
340
|
-
change[linkedListEntry.index] = linkedListEntry.value;
|
|
341
|
+
change[linkedListEntry.index] = structuredClone(linkedListEntry.value);
|
|
341
342
|
break;
|
|
342
343
|
}
|
|
343
344
|
if (listIndex === crListReplica.size) {
|
|
@@ -363,14 +364,14 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
363
364
|
}
|
|
364
365
|
void updateEntryToMaps(crListReplica, linkedListEntry, delta);
|
|
365
366
|
crListReplica.cursor = linkedListEntry;
|
|
366
|
-
change[linkedListEntry.index] = linkedListEntry.value;
|
|
367
|
+
change[linkedListEntry.index] = structuredClone(linkedListEntry.value);
|
|
367
368
|
break;
|
|
368
369
|
}
|
|
369
370
|
case "before": {
|
|
370
371
|
if (crListReplica.size === 0 && listIndex === 0) {
|
|
371
372
|
crListReplica.cursor = linkedListEntry;
|
|
372
373
|
void updateEntryToMaps(crListReplica, linkedListEntry, delta);
|
|
373
|
-
change[linkedListEntry.index] = linkedListEntry.value;
|
|
374
|
+
change[linkedListEntry.index] = structuredClone(linkedListEntry.value);
|
|
374
375
|
mode = "after";
|
|
375
376
|
listIndex = linkedListEntry.index - 1;
|
|
376
377
|
break;
|
|
@@ -392,7 +393,7 @@ function __update(listIndex, listValues, crListReplica, mode) {
|
|
|
392
393
|
}
|
|
393
394
|
void updateEntryToMaps(crListReplica, linkedListEntry, delta);
|
|
394
395
|
crListReplica.cursor = linkedListEntry;
|
|
395
|
-
change[linkedListEntry.index] = linkedListEntry.value;
|
|
396
|
+
change[linkedListEntry.index] = structuredClone(linkedListEntry.value);
|
|
396
397
|
mode = "after";
|
|
397
398
|
listIndex = linkedListEntry.index - 1;
|
|
398
399
|
break;
|
|
@@ -468,6 +469,7 @@ function __merge(crListReplica, crListDelta) {
|
|
|
468
469
|
return change;
|
|
469
470
|
}
|
|
470
471
|
for (const valueEntry of crListDelta.values) {
|
|
472
|
+
if (valueEntry === null || valueEntry === void 0) continue;
|
|
471
473
|
const existingEntry = crListReplica.parentMap.get(valueEntry.uuidv7);
|
|
472
474
|
if (existingEntry) {
|
|
473
475
|
if (crListReplica.tombstones.has(valueEntry.uuidv7) || !_utils.isUuidV7.call(void 0, valueEntry.predecessor) && valueEntry.predecessor !== "\0")
|
|
@@ -482,7 +484,7 @@ function __merge(crListReplica, crListDelta) {
|
|
|
482
484
|
void newVals.push(existingEntry);
|
|
483
485
|
continue;
|
|
484
486
|
}
|
|
485
|
-
const linkedListEntry =
|
|
487
|
+
const linkedListEntry = transformSnapshotEntryToStateEntry(
|
|
486
488
|
valueEntry,
|
|
487
489
|
crListReplica
|
|
488
490
|
);
|
|
@@ -516,28 +518,30 @@ function __merge(crListReplica, crListDelta) {
|
|
|
516
518
|
change[index] = void 0;
|
|
517
519
|
}
|
|
518
520
|
for (const val of newVals) {
|
|
519
|
-
change[val.index] = val.value;
|
|
521
|
+
change[val.index] = structuredClone(val.value);
|
|
520
522
|
}
|
|
521
523
|
return change;
|
|
522
524
|
}
|
|
523
525
|
|
|
524
526
|
// src/core/mags/acknowledge/index.ts
|
|
525
527
|
function __acknowledge(crListReplica) {
|
|
526
|
-
let
|
|
528
|
+
let largest = false;
|
|
527
529
|
crListReplica.tombstones.forEach((tombstone) => {
|
|
528
|
-
if (
|
|
530
|
+
if (largest === false || largest < tombstone) largest = tombstone;
|
|
529
531
|
});
|
|
530
|
-
if (typeof
|
|
532
|
+
if (typeof largest === "string") return largest;
|
|
531
533
|
return false;
|
|
532
534
|
}
|
|
533
535
|
|
|
534
536
|
// src/core/mags/garbageCollect/index.ts
|
|
537
|
+
|
|
535
538
|
function __garbageCollect(frontiers, crListReplica) {
|
|
536
539
|
if (!Array.isArray(frontiers)) return;
|
|
537
|
-
|
|
538
|
-
|
|
540
|
+
frontiers.sort();
|
|
541
|
+
const smallest = frontiers.find((frontier) => _utils.isUuidV7.call(void 0, frontier));
|
|
542
|
+
if (typeof smallest !== "string") return;
|
|
539
543
|
crListReplica.tombstones.forEach((tombstone, __, tombstones) => {
|
|
540
|
-
if (tombstone <=
|
|
544
|
+
if (tombstone <= smallest) {
|
|
541
545
|
tombstones.delete(tombstone);
|
|
542
546
|
}
|
|
543
547
|
});
|
|
@@ -563,7 +567,7 @@ function __snapshot(crListReplica) {
|
|
|
563
567
|
// src/CRList/class.ts
|
|
564
568
|
var CRList = class {
|
|
565
569
|
/**
|
|
566
|
-
* Creates a replicated list from an optional
|
|
570
|
+
* Creates a replicated list from an optional detached structured-clone-compatible snapshot.
|
|
567
571
|
*
|
|
568
572
|
* @param snapshot - A previously emitted CRList snapshot.
|
|
569
573
|
*/
|
|
@@ -609,7 +613,8 @@ var CRList = class {
|
|
|
609
613
|
new CustomEvent("change", { detail: change })
|
|
610
614
|
);
|
|
611
615
|
return true;
|
|
612
|
-
} catch (
|
|
616
|
+
} catch (error) {
|
|
617
|
+
if (error instanceof CRListError) throw error;
|
|
613
618
|
return false;
|
|
614
619
|
}
|
|
615
620
|
},
|
|
@@ -631,7 +636,8 @@ var CRList = class {
|
|
|
631
636
|
);
|
|
632
637
|
}
|
|
633
638
|
return true;
|
|
634
|
-
} catch (
|
|
639
|
+
} catch (error) {
|
|
640
|
+
if (error instanceof CRListError) throw error;
|
|
635
641
|
return false;
|
|
636
642
|
}
|
|
637
643
|
},
|
|
@@ -759,7 +765,7 @@ var CRList = class {
|
|
|
759
765
|
void __garbageCollect(frontiers, this.state);
|
|
760
766
|
}
|
|
761
767
|
/**
|
|
762
|
-
* Emits the current
|
|
768
|
+
* Emits the current detached structured-clone-compatible list snapshot.
|
|
763
769
|
*/
|
|
764
770
|
snapshot() {
|
|
765
771
|
const snapshot = __snapshot(this.state);
|
|
@@ -797,7 +803,7 @@ var CRList = class {
|
|
|
797
803
|
);
|
|
798
804
|
}
|
|
799
805
|
/**
|
|
800
|
-
* Returns a
|
|
806
|
+
* Returns a detached structured-clone-compatible snapshot of this list.
|
|
801
807
|
*
|
|
802
808
|
* Called automatically by `JSON.stringify`.
|
|
803
809
|
*/
|
|
@@ -805,7 +811,9 @@ var CRList = class {
|
|
|
805
811
|
return __snapshot(this.state);
|
|
806
812
|
}
|
|
807
813
|
/**
|
|
808
|
-
*
|
|
814
|
+
* Attempts to return this list snapshot as a JSON string.
|
|
815
|
+
*
|
|
816
|
+
* This can fail when list values are not JSON-compatible.
|
|
809
817
|
*/
|
|
810
818
|
toString() {
|
|
811
819
|
return JSON.stringify(this);
|
|
@@ -856,5 +864,6 @@ var CRList = class {
|
|
|
856
864
|
|
|
857
865
|
|
|
858
866
|
|
|
859
|
-
|
|
867
|
+
|
|
868
|
+
exports.CRList = CRList; exports.CRListError = CRListError; exports.__acknowledge = __acknowledge; exports.__create = __create; exports.__delete = __delete; exports.__garbageCollect = __garbageCollect; exports.__merge = __merge; exports.__read = __read; exports.__snapshot = __snapshot; exports.__update = __update;
|
|
860
869
|
//# sourceMappingURL=index.cjs.map
|
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/assertListIndices/index.ts","../src/.errors/class.ts","../src/.helpers/walkToIndex/index.ts","../src/.helpers/insertBetween/index.ts","../src/.helpers/flattenAndLinkTrustedState/index.ts","../src/.helpers/snapshotValueToLinkedListValue/index.ts","../src/.helpers/updateEntryToMaps/index.ts","../src/.helpers/deleteEntryFromMaps/index.ts","../src/.helpers/deleteLinkedEntry/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":["isUuidV7"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;ACfO,SAAS,iBAAA,CAAqB,aAAA,EAAqC;AACxE,EAAA,GAAA,CAAI,CAAC,aAAA,CAAc,MAAA,EAAQ,MAAA;AAC3B,EAAA,IAAI,MAAA,EAAQ,aAAA,CAAc,IAAA;AAC1B,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,GAAA,CAAI,aAAA,CAAc,MAAA,CAAO,KAAA,IAAS,KAAA,CAAA,EAAW,KAAA;AAC7C,IAAA,aAAA,CAAc,OAAA,EAAS,aAAA,CAAc,MAAA,CAAO,IAAA;AAAA,EAC9C;AACF;ADgBA;AACA;AElBO,IAAM,YAAA,EAAN,MAAA,QAA0B,MAAM;AAAA;AAAA;AAAA;AAAA,EAI5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQT,WAAA,CAAY,IAAA,EAAuB,OAAA,EAAkB;AACnD,IAAA,MAAM,OAAA,mBAAS,OAAA,UAAW,MAAA;AAC1B,IAAA,KAAA,CAAM,CAAA,4CAAA,EAA+C,MAAM,CAAA,CAAA;AAC/C,IAA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A;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\nexport function assertListIndices<T>(crListReplica: CRListState<T>): void {\n if (!crListReplica.cursor) return\n let index = crListReplica.size\n while (crListReplica.cursor.next)\n crListReplica.cursor = crListReplica.cursor.next\n\n while (index >= 1) {\n index--\n crListReplica.cursor.index = index\n if (crListReplica.cursor.prev === undefined) break\n crListReplica.cursor = crListReplica.cursor.prev\n }\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\nexport function walkToIndex<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 if (!crListReplica.cursor)\n throw new CRListError('LIST_EMPTY', 'List is empty')\n const direction = crListReplica.cursor.index > targetIndex ? 'prev' : 'next'\n while (crListReplica.cursor && crListReplica.cursor.index !== targetIndex) {\n crListReplica.cursor = crListReplica.cursor[direction]\n }\n}\n","import type { CRListStateEntry } from '../../.types/index.js'\n\nexport function insertBetween<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 { insertBetween } from '../insertBetween/index.js'\nexport function flattenAndLinkTrustedState<T>(crListReplica: CRListState<T>) {\n crListReplica.cursor = undefined\n const resolvedSiblingPredecessors = new Set<string>()\n for (const entry of crListReplica.parentMap.values()) {\n if (!entry) continue\n entry.prev = undefined\n entry.next = undefined\n }\n const keys = [...crListReplica.childrenMap.keys()].sort((a, b) =>\n a > b ? 1 : -1\n )\n let hasProgress = true\n while (hasProgress) {\n hasProgress = false\n for (const predecessorIdentifier of keys) {\n if (resolvedSiblingPredecessors.has(predecessorIdentifier)) continue\n const siblings = crListReplica.childrenMap.get(predecessorIdentifier)\n if (!siblings) continue\n\n if (siblings.length > 1)\n siblings.sort((a, b) => (a.uuidv7 > b.uuidv7 ? 1 : -1))\n\n const predecessor =\n predecessorIdentifier === '\\0'\n ? undefined\n : crListReplica.parentMap.get(predecessorIdentifier)\n if (\n predecessor &&\n !predecessor.prev &&\n !predecessor.next &&\n crListReplica.cursor !== predecessor\n )\n continue\n let prev: CRListStateEntry<T> = predecessor ?? crListReplica.cursor\n const predecessorNext = predecessor?.next\n if (siblings.length === 1) {\n const sibling = siblings[0]\n insertBetween<T>(prev, sibling, sibling.next)\n prev = sibling\n if (predecessorNext && predecessorNext !== sibling) {\n prev.next = predecessorNext\n predecessorNext.prev = prev\n } else {\n prev.next = undefined\n }\n if (!predecessorNext) crListReplica.cursor = prev\n resolvedSiblingPredecessors.add(predecessorIdentifier)\n hasProgress = true\n continue\n }\n const siblingSet = new Set(siblings)\n for (let index = 0; index < siblings.length; index++) {\n const sibling = siblings[index]\n const next = siblings[index + 1]\n\n insertBetween<T>(prev, sibling, sibling.next)\n prev = sibling\n\n if (next) {\n prev.next = next\n next.prev = prev\n } else if (predecessorNext && !siblingSet.has(predecessorNext)) {\n prev.next = predecessorNext\n predecessorNext.prev = prev\n } else {\n prev.next = undefined\n }\n }\n if (!predecessorNext) crListReplica.cursor = prev\n resolvedSiblingPredecessors.add(predecessorIdentifier)\n hasProgress = true\n }\n }\n crListReplica.size = crListReplica.parentMap.size\n}\n","import type {\n CRListState,\n CRListSnapshotEntry,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { isUuidV7, safeStructuredClone } from '@sovereignbase/utils'\nexport function snapshotValueToLinkedListValue<T>(\n valueEntry: CRListSnapshotEntry<T>,\n crListReplica: CRListState<T>\n): CRListStateEntry<T> {\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 const [cloned, copiedValue] = safeStructuredClone(valueEntry.value)\n\n if (!cloned) return undefined\n return {\n uuidv7: valueEntry.uuidv7,\n value: copiedValue,\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'\nexport function updateEntryToMaps<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n deltaBuf?: CRListDelta<T>\n): void {\n crListReplica.parentMap.set(linkedListEntry.uuidv7, linkedListEntry)\n const siblings = crListReplica.childrenMap.get(linkedListEntry.predecessor)\n if (siblings) {\n siblings.push(linkedListEntry)\n } else {\n crListReplica.childrenMap.set(linkedListEntry.predecessor, [\n linkedListEntry,\n ])\n }\n if (deltaBuf && !Array.isArray(deltaBuf.values)) deltaBuf.values = []\n if (deltaBuf?.values)\n 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'\nexport function deleteEntryFromMaps<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>\n): void {\n 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) siblings.splice(index, 1)\n}\n","import type {\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { deleteEntryFromMaps } from '../deleteEntryFromMaps/index.js'\n\nexport function deleteLinkedEntry<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 crListReplica.tombstones.add(linkedListEntry.uuidv7)\n if (deltaBuf && !Array.isArray(deltaBuf.tombstones)) deltaBuf.tombstones = []\n deltaBuf?.tombstones?.push(linkedListEntry.uuidv7)\n if (prev) prev.next = next\n if (next) {\n next.prev = prev\n }\n void deleteEntryFromMaps<T>(crListReplica, linkedListEntry)\n if (crListReplica.cursor === linkedListEntry)\n crListReplica.cursor = next ?? prev\n linkedListEntry.prev = undefined\n linkedListEntry.next = undefined\n crListReplica.size = crListReplica.parentMap.size\n}\n","import type {\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { deleteEntryFromMaps } from '../deleteEntryFromMaps/index.js'\nimport { updateEntryToMaps } from '../updateEntryToMaps/index.js'\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 deleteEntryFromMaps<T>(crListReplica, linkedListEntry)\n linkedListEntry.predecessor = predecessor\n void updateEntryToMaps<T>(crListReplica, linkedListEntry, deltaBuf)\n}\n","export 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 flattenAndLinkTrustedState,\n assertListIndices,\n snapshotValueToLinkedListValue,\n updateEntryToMaps,\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 cloned, indexed by\n * UUIDv7, linked through their predecessor buckets, and exposed as a live\n * doubly-linked list.\n *\n * @param snapshot Optional serializable CRList state.\n * @returns A hydrated CRList replica.\n *\n * Time complexity: O(n log n + t + c), worst case O(n^2 + t + c)\n * - n = snapshot value entry count\n * - t = snapshot tombstone count\n * - c = cloned value payload\n *\n * Space complexity: O(n + t + c)\n */\nexport function __create<T>(snapshot?: CRListSnapshot<T>): CRListState<T> {\n const crListReplica: CRListState<T> = {\n size: 0,\n cursor: undefined,\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 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 for (const valueEntry of snapshot.values) {\n const linkedListEntry = snapshotValueToLinkedListValue<T>(\n valueEntry,\n crListReplica\n )\n if (!linkedListEntry) continue\n void updateEntryToMaps<T>(crListReplica, linkedListEntry)\n }\n // Flatten tree into a doubly linked list.\n void flattenAndLinkTrustedState<T>(crListReplica)\n // Write live-view indexes.\n void assertListIndices<T>(crListReplica)\n\n return crListReplica\n}\n","import { walkToIndex } 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 a\n * detached structured clone of the visible value, so mutating the returned\n * value does not mutate the replica itself. Out-of-bounds and empty list reads\n * resolve to `undefined` instead of throwing.\n *\n * @param targetIndex Index in the live list.\n * @param crListReplica Replica to read from.\n * @returns A detached copy of the 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 walkToIndex<T>(targetIndex, crListReplica)\n return structuredClone(crListReplica?.cursor?.value)\n } catch {\n return undefined\n }\n}\n","import { CRListError } from '../../../.errors/class.js'\nimport { safeStructuredClone } from '@sovereignbase/utils'\nimport {\n updateEntryToMaps,\n deleteEntryFromMaps,\n walkToIndex,\n moveEntryToPredecessor,\n insertBetween,\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 + c), worst case O(vn + c)\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 * - c = cloned value payload size across all input values\n *\n * Space complexity: O(v + c)\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 let shiftCursor: CRListStateEntry<T>\n for (const listValue of listValues) {\n const [cloned, copiedValue] = safeStructuredClone(listValue)\n\n if (!cloned) throw new CRListError('VALUE_NOT_CLONEABLE')\n\n const v7 = uuidv7()\n\n const linkedListEntry: NonNullable<CRListStateEntry<T>> = {\n uuidv7: v7,\n value: copiedValue,\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 void updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n void walkToIndex<T>(crListReplica.size - 1, crListReplica)\n if (!crListReplica.cursor) return false\n linkedListEntry.index = crListReplica.cursor.index + 1\n linkedListEntry.predecessor = crListReplica.cursor.uuidv7\n insertBetween<T>(crListReplica.cursor, linkedListEntry, undefined)\n void updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n void walkToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n const entryToOverwrite = crListReplica.cursor\n\n linkedListEntry.predecessor = entryToOverwrite.predecessor\n linkedListEntry.index = entryToOverwrite.index\n insertBetween<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 updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n crListReplica.tombstones.add(entryToOverwrite.uuidv7)\n delta.tombstones?.push(entryToOverwrite.uuidv7)\n void deleteEntryFromMaps<T>(crListReplica, entryToOverwrite)\n entryToOverwrite.next = undefined\n entryToOverwrite.prev = undefined\n crListReplica.cursor = linkedListEntry\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n case 'after': {\n if (crListReplica.size === 0 && listIndex === 0) {\n crListReplica.cursor = linkedListEntry\n void updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n if (listIndex === crListReplica.size) {\n void walkToIndex<T>(crListReplica.size - 1, crListReplica)\n } else {\n void walkToIndex<T>(listIndex, crListReplica)\n }\n if (!crListReplica.cursor) return false\n const next =\n listIndex === crListReplica.size\n ? undefined\n : crListReplica.cursor.next\n shiftCursor = next\n linkedListEntry.index = crListReplica.cursor.index + 1\n linkedListEntry.predecessor = crListReplica.cursor.uuidv7\n insertBetween<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 updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = 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 void updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n change[linkedListEntry.index] = linkedListEntry.value\n mode = 'after'\n listIndex = linkedListEntry.index - 1\n break\n }\n void walkToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n const prev = crListReplica.cursor.prev\n shiftCursor = crListReplica.cursor\n linkedListEntry.index = crListReplica.cursor.index\n linkedListEntry.predecessor = prev?.uuidv7 ?? '\\0'\n insertBetween<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 updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n change[linkedListEntry.index] = 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 if (mode !== 'overwrite')\n while (shiftCursor) {\n shiftCursor.index += listValues.length\n shiftCursor = shiftCursor.next\n }\n return { change, delta }\n}\n","import { deleteLinkedEntry, walkToIndex } 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 walkToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n\n let current: CRListStateEntry<T> = crListReplica.cursor\n let deleted = 0\n\n while (current && deleted < deleteCount) {\n const next: CRListStateEntry<T> = current.next\n change[current.index] = undefined\n void deleteLinkedEntry<T>(crListReplica, current, delta)\n current = next\n deleted++\n }\n\n crListReplica.size = crListReplica.parentMap.size\n\n while (current) {\n current.index -= deleted\n current = current.next\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 snapshotValueToLinkedListValue,\n updateEntryToMaps,\n flattenAndLinkTrustedState,\n assertListIndices,\n deleteLinkedEntry,\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 + c) for tail-append deltas; O(n + t + qk) for tombstone-only deletes; otherwise O(n log n + v + t + m*k + c)\n * Worst case: O(n^2 + (v + t)n + c)\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 * - c = cloned delta value payload size\n *\n * Space complexity: O(n + v + t + c)\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\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 crListReplica.tombstones.add(tombstone)\n const linkedListEntry = crListReplica.parentMap.get(tombstone)\n if (linkedListEntry) {\n void newTombsIndices.push(linkedListEntry.index)\n void deleteLinkedEntry<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 assertListIndices<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 const existingEntry = crListReplica.parentMap.get(valueEntry.uuidv7)\n if (existingEntry) {\n if (\n crListReplica.tombstones.has(valueEntry.uuidv7) ||\n (!isUuidV7(valueEntry.predecessor) && valueEntry.predecessor !== '\\0')\n )\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 void newVals.push(existingEntry)\n continue\n }\n const linkedListEntry = snapshotValueToLinkedListValue<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 updateEntryToMaps<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.size = crListReplica.parentMap.size\n } else {\n needsRelink = true\n }\n } else if (!needsRelink && predecessor && !predecessor.next) {\n linkedListEntry.prev = predecessor\n linkedListEntry.index = predecessor.index + 1\n predecessor.next = linkedListEntry\n crListReplica.cursor = linkedListEntry\n crListReplica.size = crListReplica.parentMap.size\n } else {\n needsRelink = true\n }\n }\n if (needsRelink) {\n // Flatten tree into a doubly linked list.\n void flattenAndLinkTrustedState<T>(crListReplica)\n // Write live-view indexes.\n void assertListIndices<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 frontier: CRListAck | false = false\n crListReplica.tombstones.forEach((tombstone) => {\n if (frontier === false || frontier < tombstone) frontier = tombstone\n })\n if (typeof frontier === 'string') return frontier\n return false\n}\n","import { CRListAck, CRListState } from '../../../.types/index.js'\n\n/**\n * Removes tombstones acknowledged by all supplied frontiers.\n *\n * The minimum frontier is used as the safe collection boundary. Tombstones less\n * than or equal to that boundary are removed from the local 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 const frontier = frontiers.sort().shift()\n if (typeof frontier !== 'string') return\n crListReplica.tombstones.forEach((tombstone, __, tombstones) => {\n if (tombstone <= frontier) {\n 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 serializable CRList snapshot from the current replica state.\n *\n * The snapshot contains every live value entry and all retained tombstones. Value\n * payloads are cloned so callers cannot mutate the replica through the snapshot.\n *\n * @param crListReplica Replica to snapshot.\n * @returns A full snapshot suitable for hydration or transport.\n *\n * Time complexity: O(n + t + c)\n * - n = replica value entry count\n * - t = replica tombstone count\n * - c = cloned value payload size\n *\n * Space complexity: O(n + t + c)\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: structuredClone(linkedListEntry.value),\n predecessor: linkedListEntry.predecessor,\n }\n }\n ),\n tombstones: Array.from(crListReplica.tombstones),\n }\n}\n","import { indexFromPropertyKey } from '../.helpers/index.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 a detached copy of an entry, `list[0] = value` writes an\n * entry, and `delete list[0]` removes one entry. Iteration and `forEach()`\n * likewise expose detached copies rather than mutable references into the\n * replica state. Local mutations emit `delta` and `change` events; remote\n * 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 detached copies.\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 serializable snapshot.\n *\n * @param snapshot - A previously emitted CRList snapshot.\n */\n constructor(snapshot?: CRListSnapshot<T>) {\n 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 target.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (change)\n void target.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\n return true\n } catch {\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 target.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n }\n if (change) {\n void target.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\n }\n return true\n } catch {\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 * 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)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (change)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\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)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (change)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\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)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (change)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\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)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\n }\n /**\n * Emits an acknowledgement frontier for currently retained tombstones.\n */\n acknowledge(): void {\n const ack = __acknowledge(this.state)\n if (ack)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('ack', { detail: ack })\n )\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 serializable list snapshot.\n */\n snapshot(): void {\n const snapshot = __snapshot<T>(this.state)\n if (snapshot)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('snapshot', { detail: snapshot })\n )\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 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 this.eventTarget.removeEventListener(\n type,\n listener as EventListenerOrEventListenerObject | null,\n options\n )\n }\n /**\n * Returns a serializable snapshot representation of this list.\n *\n * Called automatically by `JSON.stringify`.\n */\n toJSON(): CRListSnapshot<T> {\n return __snapshot<T>(this.state)\n }\n /**\n * Returns this list as a JSON string.\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 detached copies of the 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 copy in index order.\n *\n * Callback values are detached copies, so mutating them does not mutate the\n * list.\n *\n * @param callback - Function to call for each value copy.\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 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/assertListIndices/index.ts","../src/.errors/class.ts","../src/.helpers/walkToIndex/index.ts","../src/.helpers/insertBetween/index.ts","../src/.helpers/flattenAndLinkTrustedState/index.ts","../src/.helpers/transformSnapshotEntryToStateEntry/index.ts","../src/.helpers/updateEntryToMaps/index.ts","../src/.helpers/deleteEntryFromMaps/index.ts","../src/.helpers/deleteLinkedEntry/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":["isUuidV7"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;ACfO,SAAS,iBAAA,CAAqB,aAAA,EAAqC;AACxE,EAAA,GAAA,CAAI,CAAC,aAAA,CAAc,MAAA,EAAQ,MAAA;AAC3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;AAMkD,EAAA;AACL,IAAA;AAC7C,EAAA;AAAA;AAAA;AAAA;AAIiB,EAAA;AAC0B,IAAA;AACrC,IAAA;AACoB,MAAA;AAC4B,QAAA;AAClD,MAAA;AACJ,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYQ,EAAA;AACW,IAAA;AACf,MAAA;AACA,MAAA;AACA,MAAA;AACF,IAAA;AACF,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaQ,EAAA;AACW,IAAA;AACf,MAAA;AACA,MAAA;AACA,MAAA;AACF,IAAA;AACF,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAM4B,EAAA;AACK,IAAA;AACjC,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMmB,EAAA;AACS,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;AACQ,IAAA;AACtB,MAAA;AAClB,MAAA;AACR,IAAA;AACF,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaQ,EAAA;AAC0C,IAAA;AACC,MAAA;AACjD,IAAA;AACF,EAAA;AACF;ApB+fgE;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\nexport function assertListIndices<T>(crListReplica: CRListState<T>): void {\n if (!crListReplica.cursor) return\n let index = crListReplica.size\n while (crListReplica.cursor.next)\n crListReplica.cursor = crListReplica.cursor.next\n\n while (index >= 1) {\n index--\n crListReplica.cursor.index = index\n if (crListReplica.cursor.prev === undefined) break\n crListReplica.cursor = crListReplica.cursor.prev\n }\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\nexport function walkToIndex<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 if (!crListReplica.cursor)\n throw new CRListError('LIST_EMPTY', 'List is empty')\n const direction = crListReplica.cursor.index > targetIndex ? 'prev' : 'next'\n while (crListReplica.cursor && crListReplica.cursor.index !== targetIndex) {\n crListReplica.cursor = crListReplica.cursor[direction]\n }\n}\n","import type { CRListStateEntry } from '../../.types/index.js'\n\nexport function insertBetween<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 { insertBetween } from '../insertBetween/index.js'\nexport function flattenAndLinkTrustedState<T>(crListReplica: CRListState<T>) {\n crListReplica.cursor = undefined\n const resolvedSiblingPredecessors = new Set<string>()\n for (const entry of crListReplica.parentMap.values()) {\n if (!entry) continue\n entry.prev = undefined\n entry.next = undefined\n }\n const keys = [...crListReplica.childrenMap.keys()].sort((a, b) =>\n a > b ? 1 : -1\n )\n let hasProgress = true\n while (hasProgress) {\n hasProgress = false\n for (const predecessorIdentifier of keys) {\n if (resolvedSiblingPredecessors.has(predecessorIdentifier)) continue\n const siblings = crListReplica.childrenMap.get(predecessorIdentifier)\n if (!siblings) continue\n\n if (siblings.length > 1)\n siblings.sort((a, b) => (a.uuidv7 > b.uuidv7 ? 1 : -1))\n\n const predecessor =\n predecessorIdentifier === '\\0'\n ? undefined\n : crListReplica.parentMap.get(predecessorIdentifier)\n if (\n predecessor &&\n !predecessor.prev &&\n !predecessor.next &&\n crListReplica.cursor !== predecessor\n )\n continue\n let prev: CRListStateEntry<T> = predecessor ?? crListReplica.cursor\n const predecessorNext = predecessor?.next\n if (siblings.length === 1) {\n const sibling = siblings[0]\n insertBetween<T>(prev, sibling, sibling.next)\n prev = sibling\n if (predecessorNext && predecessorNext !== sibling) {\n prev.next = predecessorNext\n predecessorNext.prev = prev\n } else {\n prev.next = undefined\n }\n if (!predecessorNext) crListReplica.cursor = prev\n resolvedSiblingPredecessors.add(predecessorIdentifier)\n hasProgress = true\n continue\n }\n const siblingSet = new Set(siblings)\n for (let index = 0; index < siblings.length; index++) {\n const sibling = siblings[index]\n const next = siblings[index + 1]\n\n insertBetween<T>(prev, sibling, sibling.next)\n prev = sibling\n\n if (next) {\n prev.next = next\n next.prev = prev\n } else if (predecessorNext && !siblingSet.has(predecessorNext)) {\n prev.next = predecessorNext\n predecessorNext.prev = prev\n } else {\n prev.next = undefined\n }\n }\n if (!predecessorNext) crListReplica.cursor = prev\n resolvedSiblingPredecessors.add(predecessorIdentifier)\n hasProgress = true\n }\n }\n crListReplica.size = crListReplica.parentMap.size\n}\n","import type {\n CRListState,\n CRListSnapshotEntry,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { isUuidV7, safeStructuredClone } from '@sovereignbase/utils'\nexport function transformSnapshotEntryToStateEntry<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 const [cloned, copiedValue] = safeStructuredClone(valueEntry.value)\n\n if (!cloned) return undefined\n return {\n uuidv7: valueEntry.uuidv7,\n value: copiedValue,\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'\nexport function updateEntryToMaps<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>,\n deltaBuf?: CRListDelta<T>\n): void {\n crListReplica.parentMap.set(linkedListEntry.uuidv7, linkedListEntry)\n const siblings = crListReplica.childrenMap.get(linkedListEntry.predecessor)\n if (siblings) {\n siblings.push(linkedListEntry)\n } else {\n crListReplica.childrenMap.set(linkedListEntry.predecessor, [\n linkedListEntry,\n ])\n }\n if (deltaBuf && !Array.isArray(deltaBuf.values)) deltaBuf.values = []\n if (deltaBuf?.values)\n 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'\nexport function deleteEntryFromMaps<T>(\n crListReplica: CRListState<T>,\n linkedListEntry: NonNullable<CRListStateEntry<T>>\n): void {\n 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) siblings.splice(index, 1)\n}\n","import type {\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { deleteEntryFromMaps } from '../deleteEntryFromMaps/index.js'\n\nexport function deleteLinkedEntry<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 crListReplica.tombstones.add(linkedListEntry.uuidv7)\n if (deltaBuf && !Array.isArray(deltaBuf.tombstones)) deltaBuf.tombstones = []\n deltaBuf?.tombstones?.push(linkedListEntry.uuidv7)\n if (prev) prev.next = next\n if (next) {\n next.prev = prev\n }\n void deleteEntryFromMaps<T>(crListReplica, linkedListEntry)\n if (crListReplica.cursor === linkedListEntry)\n crListReplica.cursor = next ?? prev\n linkedListEntry.prev = undefined\n linkedListEntry.next = undefined\n crListReplica.size = crListReplica.parentMap.size\n}\n","import type {\n CRListDelta,\n CRListState,\n CRListStateEntry,\n} from '../../.types/index.js'\nimport { deleteEntryFromMaps } from '../deleteEntryFromMaps/index.js'\nimport { updateEntryToMaps } from '../updateEntryToMaps/index.js'\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 deleteEntryFromMaps<T>(crListReplica, linkedListEntry)\n linkedListEntry.predecessor = predecessor\n void updateEntryToMaps<T>(crListReplica, linkedListEntry, deltaBuf)\n}\n","export 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 flattenAndLinkTrustedState,\n assertListIndices,\n transformSnapshotEntryToStateEntry,\n updateEntryToMaps,\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 cloned, indexed by\n * UUIDv7, linked through their predecessor buckets, and exposed as a live\n * doubly-linked list.\n *\n * @param snapshot - Optional detached structured-clone-compatible CRList snapshot.\n * @returns - A hydrated CRList replica.\n *\n * Time complexity: O(n log n + t + c), worst case O(n^2 + t + c)\n * - n = snapshot value entry count\n * - t = snapshot tombstone count\n * - c = cloned value payload\n *\n * Space complexity: O(n + t + c)\n */\nexport function __create<T>(snapshot?: CRListSnapshot<T>): CRListState<T> {\n const crListReplica: CRListState<T> = {\n size: 0,\n cursor: undefined,\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 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 for (const valueEntry of snapshot.values) {\n const linkedListEntry = transformSnapshotEntryToStateEntry<T>(\n valueEntry,\n crListReplica\n )\n if (!linkedListEntry) continue\n void updateEntryToMaps<T>(crListReplica, linkedListEntry)\n }\n // Flatten tree into a doubly linked list.\n void flattenAndLinkTrustedState<T>(crListReplica)\n // Write live-view indexes.\n void assertListIndices<T>(crListReplica)\n\n return crListReplica\n}\n","import { walkToIndex } 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 a\n * detached structured clone of the visible value, so mutating the returned\n * value does not mutate the replica itself. Out-of-bounds and empty list reads\n * resolve to `undefined` instead of throwing.\n *\n * @param targetIndex - Index in the live list.\n * @param crListReplica - Replica to read from.\n * @returns - A detached copy of the 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 walkToIndex<T>(targetIndex, crListReplica)\n return structuredClone(crListReplica?.cursor?.value)\n } catch {\n return undefined\n }\n}\n","import { CRListError } from '../../../.errors/class.js'\nimport { safeStructuredClone } from '@sovereignbase/utils'\nimport {\n updateEntryToMaps,\n deleteEntryFromMaps,\n walkToIndex,\n moveEntryToPredecessor,\n insertBetween,\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 + c), worst case O(vn + c)\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 * - c = cloned value payload size across all input values\n *\n * Space complexity: O(v + c)\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 let shiftCursor: CRListStateEntry<T>\n for (const listValue of listValues) {\n const [cloned, copiedValue] = safeStructuredClone(listValue)\n\n if (!cloned) throw new CRListError('VALUE_NOT_CLONEABLE')\n\n const v7 = uuidv7()\n\n const linkedListEntry: NonNullable<CRListStateEntry<T>> = {\n uuidv7: v7,\n value: copiedValue,\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 void updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n change[linkedListEntry.index] = linkedListEntry.value\n break\n }\n void walkToIndex<T>(crListReplica.size - 1, crListReplica)\n if (!crListReplica.cursor) return false\n linkedListEntry.index = crListReplica.cursor.index + 1\n linkedListEntry.predecessor = crListReplica.cursor.uuidv7\n insertBetween<T>(crListReplica.cursor, linkedListEntry, undefined)\n void updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n change[linkedListEntry.index] = structuredClone(linkedListEntry.value)\n break\n }\n void walkToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n const entryToOverwrite = crListReplica.cursor\n\n linkedListEntry.predecessor = entryToOverwrite.predecessor\n linkedListEntry.index = entryToOverwrite.index\n insertBetween<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 updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n crListReplica.tombstones.add(entryToOverwrite.uuidv7)\n delta.tombstones?.push(entryToOverwrite.uuidv7)\n void deleteEntryFromMaps<T>(crListReplica, entryToOverwrite)\n entryToOverwrite.next = undefined\n entryToOverwrite.prev = undefined\n crListReplica.cursor = linkedListEntry\n change[linkedListEntry.index] = structuredClone(linkedListEntry.value)\n break\n }\n case 'after': {\n if (crListReplica.size === 0 && listIndex === 0) {\n crListReplica.cursor = linkedListEntry\n void updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n change[linkedListEntry.index] = structuredClone(linkedListEntry.value)\n break\n }\n if (listIndex === crListReplica.size) {\n void walkToIndex<T>(crListReplica.size - 1, crListReplica)\n } else {\n void walkToIndex<T>(listIndex, crListReplica)\n }\n if (!crListReplica.cursor) return false\n const next =\n listIndex === crListReplica.size\n ? undefined\n : crListReplica.cursor.next\n shiftCursor = next\n linkedListEntry.index = crListReplica.cursor.index + 1\n linkedListEntry.predecessor = crListReplica.cursor.uuidv7\n insertBetween<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 updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n change[linkedListEntry.index] = structuredClone(linkedListEntry.value)\n break\n }\n case 'before': {\n if (crListReplica.size === 0 && listIndex === 0) {\n crListReplica.cursor = linkedListEntry\n void updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n change[linkedListEntry.index] = structuredClone(linkedListEntry.value)\n mode = 'after'\n listIndex = linkedListEntry.index - 1\n break\n }\n void walkToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n const prev = crListReplica.cursor.prev\n shiftCursor = crListReplica.cursor\n linkedListEntry.index = crListReplica.cursor.index\n linkedListEntry.predecessor = prev?.uuidv7 ?? '\\0'\n insertBetween<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 updateEntryToMaps<T>(crListReplica, linkedListEntry, delta)\n crListReplica.cursor = linkedListEntry\n change[linkedListEntry.index] = structuredClone(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 if (mode !== 'overwrite')\n while (shiftCursor) {\n shiftCursor.index += listValues.length\n shiftCursor = shiftCursor.next\n }\n return { change, delta }\n}\n","import { deleteLinkedEntry, walkToIndex } 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 walkToIndex<T>(listIndex, crListReplica)\n if (!crListReplica.cursor) return false\n\n let current: CRListStateEntry<T> = crListReplica.cursor\n let deleted = 0\n\n while (current && deleted < deleteCount) {\n const next: CRListStateEntry<T> = current.next\n change[current.index] = undefined\n void deleteLinkedEntry<T>(crListReplica, current, delta)\n current = next\n deleted++\n }\n\n crListReplica.size = crListReplica.parentMap.size\n\n while (current) {\n current.index -= deleted\n current = current.next\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 transformSnapshotEntryToStateEntry,\n updateEntryToMaps,\n flattenAndLinkTrustedState,\n assertListIndices,\n deleteLinkedEntry,\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 + c) for tail-append deltas; O(n + t + qk) for tombstone-only deletes; otherwise O(n log n + v + t + m*k + c)\n * Worst case: O(n^2 + (v + t)n + c)\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 * - c = cloned delta value payload size\n *\n * Space complexity: O(n + v + t + c)\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\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 crListReplica.tombstones.add(tombstone)\n const linkedListEntry = crListReplica.parentMap.get(tombstone)\n if (linkedListEntry) {\n void newTombsIndices.push(linkedListEntry.index)\n void deleteLinkedEntry<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 assertListIndices<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 (\n crListReplica.tombstones.has(valueEntry.uuidv7) ||\n (!isUuidV7(valueEntry.predecessor) && valueEntry.predecessor !== '\\0')\n )\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 void newVals.push(existingEntry)\n continue\n }\n const linkedListEntry = transformSnapshotEntryToStateEntry<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 updateEntryToMaps<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.size = crListReplica.parentMap.size\n } else {\n needsRelink = true\n }\n } else if (!needsRelink && predecessor && !predecessor.next) {\n linkedListEntry.prev = predecessor\n linkedListEntry.index = predecessor.index + 1\n predecessor.next = linkedListEntry\n crListReplica.cursor = linkedListEntry\n crListReplica.size = crListReplica.parentMap.size\n } else {\n needsRelink = true\n }\n }\n if (needsRelink) {\n // Flatten tree into a doubly linked list.\n void flattenAndLinkTrustedState<T>(crListReplica)\n // Write live-view indexes.\n void assertListIndices<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] = structuredClone(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 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 minimum frontier is used as the safe collection boundary. Tombstones less\n * than or equal to that boundary are removed from the local 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 frontiers.sort()\n const smallest = frontiers.find((frontier) => isUuidV7(frontier))\n if (typeof smallest !== 'string') return\n crListReplica.tombstones.forEach((tombstone, __, tombstones) => {\n if (tombstone <= smallest) {\n 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 detached structured-clone-compatible CRList snapshot from the current replica state.\n *\n * The snapshot contains every live value entry and all retained tombstones. Value\n * payloads are cloned so callers cannot mutate the replica through the snapshot.\n *\n * @param crListReplica - Replica to snapshot.\n * @returns - A full snapshot suitable for hydration or transport.\n *\n * Time complexity: O(n + t + c)\n * - n = replica value entry count\n * - t = replica tombstone count\n * - c = cloned value payload size\n *\n * Space complexity: O(n + t + c)\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: structuredClone(linkedListEntry.value),\n predecessor: linkedListEntry.predecessor,\n }\n }\n ),\n tombstones: Array.from(crListReplica.tombstones),\n }\n}\n","import { 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 a detached copy of an entry, `list[0] = value` writes an\n * entry, and `delete list[0]` removes one entry. Iteration and `forEach()`\n * likewise expose detached copies rather than mutable references into the\n * replica state. Local mutations emit `delta` and `change` events; remote\n * 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 detached copies.\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 detached structured-clone-compatible snapshot.\n *\n * @param snapshot - A previously emitted CRList snapshot.\n */\n constructor(snapshot?: CRListSnapshot<T>) {\n 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 target.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (change)\n void target.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\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 target.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n }\n if (change) {\n void target.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\n }\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 * 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)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (change)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\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)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (change)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\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)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (change)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\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)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\n }\n /**\n * Emits an acknowledgement frontier for currently retained tombstones.\n */\n acknowledge(): void {\n const ack = __acknowledge(this.state)\n if (ack)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('ack', { detail: ack })\n )\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 detached structured-clone-compatible list snapshot.\n */\n snapshot(): void {\n const snapshot = __snapshot<T>(this.state)\n if (snapshot)\n void this.eventTarget.dispatchEvent(\n new CustomEvent('snapshot', { detail: snapshot })\n )\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 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 this.eventTarget.removeEventListener(\n type,\n listener as EventListenerOrEventListenerObject | null,\n options\n )\n }\n /**\n * Returns a detached structured-clone-compatible snapshot of this list.\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 detached copies of the 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 copy in index order.\n *\n * Callback values are detached copies, so mutating them does not mutate the\n * list.\n *\n * @param callback - Function to call for each value copy.\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 callback.call(thisArg, this[index], index, this)\n }\n }\n}\n"]}
|