@sovereignbase/convergent-replicated-list 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -145,6 +145,10 @@ list.forEach((value, index, target) => {
145
145
  console.log([...restored]) // ['What is', 'up', 'dude!']
146
146
  ```
147
147
 
148
+ Numeric reads, `for...of`, and `forEach()` return detached copies of visible
149
+ values. Mutating those returned values does not mutate the underlying replica
150
+ state.
151
+
148
152
  ### Acknowledgements and garbage collection
149
153
 
150
154
  ```ts
@@ -243,6 +247,7 @@ Ingress stays tolerant:
243
247
  - Deltas are serializable gossip payloads.
244
248
  - `change` is a minimal index-keyed local patch.
245
249
  - `toJSON()` returns a detached serializable snapshot.
250
+ - Numeric reads, `for...of`, and `forEach()` expose detached copies of visible values rather than mutable references into replica state.
246
251
  - `for...of`, `forEach()`, numeric indexing, `append()`, `prepend()`, `remove()`, `merge()`, `snapshot()`, `acknowledge()`, and `garbageCollect()` all operate on the live list projection.
247
252
 
248
253
  ### Convergence and compaction
package/dist/index.cjs CHANGED
@@ -251,7 +251,7 @@ function __create(snapshot) {
251
251
  function __read(targetIndex, crListReplica) {
252
252
  try {
253
253
  void walkToIndex(targetIndex, crListReplica);
254
- return _optionalChain([crListReplica, 'optionalAccess', _7 => _7.cursor, 'optionalAccess', _8 => _8.value]);
254
+ return structuredClone(_optionalChain([crListReplica, 'optionalAccess', _7 => _7.cursor, 'optionalAccess', _8 => _8.value]));
255
255
  } catch (e) {
256
256
  return void 0;
257
257
  }
@@ -823,7 +823,7 @@ var CRList = class {
823
823
  return this.toJSON();
824
824
  }
825
825
  /**
826
- * Iterates over the current live values in index order.
826
+ * Iterates over detached copies of the current live values in index order.
827
827
  */
828
828
  *[Symbol.iterator]() {
829
829
  for (let index = 0; index < this.size; index++) {
@@ -832,9 +832,12 @@ var CRList = class {
832
832
  }
833
833
  }
834
834
  /**
835
- * Calls a function once for each live value in index order.
835
+ * Calls a function once for each live value copy in index order.
836
836
  *
837
- * @param callback - Function to call for each value.
837
+ * Callback values are detached copies, so mutating them does not mutate the
838
+ * list.
839
+ *
840
+ * @param callback - Function to call for each value copy.
838
841
  * @param thisArg - Optional `this` value for the callback.
839
842
  */
840
843
  forEach(callback, thisArg) {
@@ -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,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A;AACF,IAAA;AACF,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAM4B,EAAA;AACK,IAAA;AACjC,EAAA;AAAA;AAAA;AAAA;AAImB,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;AAUQ,EAAA;AAC0C,IAAA;AACC,MAAA;AACjD,IAAA;AACF,EAAA;AACF;ApBggBgE;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 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. Out-of-bounds and empty\n * list reads resolve to `undefined` instead of throwing.\n *\n * @param targetIndex Index in the live list.\n * @param crListReplica Replica to read from.\n * @returns The value at `targetIndex`, or `undefined` when 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 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 an entry, `list[0] = value` writes an entry, and `delete\n * list[0]` removes one entry. Local mutations emit `delta` and `change` events;\n * remote merges emit `change` events.\n *\n * @typeParam T - The value type stored in the list.\n */\nexport class CRList<T> {\n /**\n * Reads or overwrites an entry in the live list projection by index.\n */\n [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(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\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(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 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 in index order.\n *\n * @param callback - Function to call for each value.\n * @param thisArg - Optional `this` value for the callback.\n */\n forEach(\n callback: (value: T, index: number, list: this) => void,\n thisArg?: unknown\n ): void {\n for (let index = 0; index < this.size; index++) {\n 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/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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9C,MAAA;AACJ,EAAA;AAAA;AAAA;AAAA;AAIoB,EAAA;AACkB,IAAA;AAChC,IAAA;AACoB,MAAA;AACkB,QAAA;AACxC,MAAA;AACJ,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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;AAImB,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;ApB4fgE;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 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"]}
package/dist/index.d.cts CHANGED
@@ -96,15 +96,19 @@ type CRListEventListenerFor<T, K extends string> = K extends keyof CRListEventMa
96
96
  * A convergent replicated list.
97
97
  *
98
98
  * Numeric property access reads and mutates the live list projection:
99
- * `list[0]` reads an entry, `list[0] = value` writes an entry, and `delete
100
- * list[0]` removes one entry. Local mutations emit `delta` and `change` events;
101
- * remote merges emit `change` events.
99
+ * `list[0]` reads a detached copy of an entry, `list[0] = value` writes an
100
+ * entry, and `delete list[0]` removes one entry. Iteration and `forEach()`
101
+ * likewise expose detached copies rather than mutable references into the
102
+ * replica state. Local mutations emit `delta` and `change` events; remote
103
+ * merges emit `change` events.
102
104
  *
103
105
  * @typeParam T - The value type stored in the list.
104
106
  */
105
107
  declare class CRList<T> {
106
108
  /**
107
109
  * Reads or overwrites an entry in the live list projection by index.
110
+ *
111
+ * Reads return detached copies.
108
112
  */
109
113
  [index: number]: T;
110
114
  private readonly state;
@@ -192,13 +196,16 @@ declare class CRList<T> {
192
196
  */
193
197
  toString(): string;
194
198
  /**
195
- * Iterates over the current live values in index order.
199
+ * Iterates over detached copies of the current live values in index order.
196
200
  */
197
201
  [Symbol.iterator](): IterableIterator<T>;
198
202
  /**
199
- * Calls a function once for each live value in index order.
203
+ * Calls a function once for each live value copy in index order.
204
+ *
205
+ * Callback values are detached copies, so mutating them does not mutate the
206
+ * list.
200
207
  *
201
- * @param callback - Function to call for each value.
208
+ * @param callback - Function to call for each value copy.
202
209
  * @param thisArg - Optional `this` value for the callback.
203
210
  */
204
211
  forEach(callback: (value: T, index: number, list: this) => void, thisArg?: unknown): void;
@@ -231,12 +238,15 @@ declare function __create<T>(snapshot?: CRListSnapshot<T>): CRListState<T>;
231
238
  /**
232
239
  * Reads the value at an index in the replica live view.
233
240
  *
234
- * The replica cursor is moved as part of the lookup. Out-of-bounds and empty
235
- * list reads resolve to `undefined` instead of throwing.
241
+ * The replica cursor is moved as part of the lookup. Successful reads return a
242
+ * detached structured clone of the visible value, so mutating the returned
243
+ * value does not mutate the replica itself. Out-of-bounds and empty list reads
244
+ * resolve to `undefined` instead of throwing.
236
245
  *
237
246
  * @param targetIndex Index in the live list.
238
247
  * @param crListReplica Replica to read from.
239
- * @returns The value at `targetIndex`, or `undefined` when no value is present.
248
+ * @returns A detached copy of the value at `targetIndex`, or `undefined` when
249
+ * no value is present.
240
250
  *
241
251
  * Time complexity: O(d), worst case O(n)
242
252
  * - d = distance from cursor to target index
package/dist/index.d.ts CHANGED
@@ -96,15 +96,19 @@ type CRListEventListenerFor<T, K extends string> = K extends keyof CRListEventMa
96
96
  * A convergent replicated list.
97
97
  *
98
98
  * Numeric property access reads and mutates the live list projection:
99
- * `list[0]` reads an entry, `list[0] = value` writes an entry, and `delete
100
- * list[0]` removes one entry. Local mutations emit `delta` and `change` events;
101
- * remote merges emit `change` events.
99
+ * `list[0]` reads a detached copy of an entry, `list[0] = value` writes an
100
+ * entry, and `delete list[0]` removes one entry. Iteration and `forEach()`
101
+ * likewise expose detached copies rather than mutable references into the
102
+ * replica state. Local mutations emit `delta` and `change` events; remote
103
+ * merges emit `change` events.
102
104
  *
103
105
  * @typeParam T - The value type stored in the list.
104
106
  */
105
107
  declare class CRList<T> {
106
108
  /**
107
109
  * Reads or overwrites an entry in the live list projection by index.
110
+ *
111
+ * Reads return detached copies.
108
112
  */
109
113
  [index: number]: T;
110
114
  private readonly state;
@@ -192,13 +196,16 @@ declare class CRList<T> {
192
196
  */
193
197
  toString(): string;
194
198
  /**
195
- * Iterates over the current live values in index order.
199
+ * Iterates over detached copies of the current live values in index order.
196
200
  */
197
201
  [Symbol.iterator](): IterableIterator<T>;
198
202
  /**
199
- * Calls a function once for each live value in index order.
203
+ * Calls a function once for each live value copy in index order.
204
+ *
205
+ * Callback values are detached copies, so mutating them does not mutate the
206
+ * list.
200
207
  *
201
- * @param callback - Function to call for each value.
208
+ * @param callback - Function to call for each value copy.
202
209
  * @param thisArg - Optional `this` value for the callback.
203
210
  */
204
211
  forEach(callback: (value: T, index: number, list: this) => void, thisArg?: unknown): void;
@@ -231,12 +238,15 @@ declare function __create<T>(snapshot?: CRListSnapshot<T>): CRListState<T>;
231
238
  /**
232
239
  * Reads the value at an index in the replica live view.
233
240
  *
234
- * The replica cursor is moved as part of the lookup. Out-of-bounds and empty
235
- * list reads resolve to `undefined` instead of throwing.
241
+ * The replica cursor is moved as part of the lookup. Successful reads return a
242
+ * detached structured clone of the visible value, so mutating the returned
243
+ * value does not mutate the replica itself. Out-of-bounds and empty list reads
244
+ * resolve to `undefined` instead of throwing.
236
245
  *
237
246
  * @param targetIndex Index in the live list.
238
247
  * @param crListReplica Replica to read from.
239
- * @returns The value at `targetIndex`, or `undefined` when no value is present.
248
+ * @returns A detached copy of the value at `targetIndex`, or `undefined` when
249
+ * no value is present.
240
250
  *
241
251
  * Time complexity: O(d), worst case O(n)
242
252
  * - d = distance from cursor to target index
package/dist/index.js CHANGED
@@ -251,7 +251,7 @@ function __create(snapshot) {
251
251
  function __read(targetIndex, crListReplica) {
252
252
  try {
253
253
  void walkToIndex(targetIndex, crListReplica);
254
- return crListReplica?.cursor?.value;
254
+ return structuredClone(crListReplica?.cursor?.value);
255
255
  } catch {
256
256
  return void 0;
257
257
  }
@@ -823,7 +823,7 @@ var CRList = class {
823
823
  return this.toJSON();
824
824
  }
825
825
  /**
826
- * Iterates over the current live values in index order.
826
+ * Iterates over detached copies of the current live values in index order.
827
827
  */
828
828
  *[Symbol.iterator]() {
829
829
  for (let index = 0; index < this.size; index++) {
@@ -832,9 +832,12 @@ var CRList = class {
832
832
  }
833
833
  }
834
834
  /**
835
- * Calls a function once for each live value in index order.
835
+ * Calls a function once for each live value copy in index order.
836
836
  *
837
- * @param callback - Function to call for each value.
837
+ * Callback values are detached copies, so mutating them does not mutate the
838
+ * list.
839
+ *
840
+ * @param callback - Function to call for each value copy.
838
841
  * @param thisArg - Optional `this` value for the callback.
839
842
  */
840
843
  forEach(callback, thisArg) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../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"],"sourcesContent":["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. Out-of-bounds and empty\n * list reads resolve to `undefined` instead of throwing.\n *\n * @param targetIndex Index in the live list.\n * @param crListReplica Replica to read from.\n * @returns The value at `targetIndex`, or `undefined` when 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 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 an entry, `list[0] = value` writes an entry, and `delete\n * list[0]` removes one entry. Local mutations emit `delta` and `change` events;\n * remote merges emit `change` events.\n *\n * @typeParam T - The value type stored in the list.\n */\nexport class CRList<T> {\n /**\n * Reads or overwrites an entry in the live list projection by index.\n */\n [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(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\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(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 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 in index order.\n *\n * @param callback - Function to call for each value.\n * @param thisArg - Optional `this` value for the callback.\n */\n forEach(\n callback: (value: T, index: number, list: this) => void,\n thisArg?: unknown\n ): void {\n for (let index = 0; index < this.size; index++) {\n callback.call(thisArg, this[index], index, this)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAEO,SAAS,kBAAqB,eAAqC;AACxE,MAAI,CAAC,cAAc,OAAQ;AAC3B,MAAI,QAAQ,cAAc;AAC1B,SAAO,cAAc,OAAO;AAC1B,kBAAc,SAAS,cAAc,OAAO;AAE9C,SAAO,SAAS,GAAG;AACjB;AACA,kBAAc,OAAO,QAAQ;AAC7B,QAAI,cAAc,OAAO,SAAS,OAAW;AAC7C,kBAAc,SAAS,cAAc,OAAO;AAAA,EAC9C;AACF;;;ACDO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAA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AA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,oBACE,MACA,UACA,SACM;AACN,SAAK,YAAY;AAAA,MACf;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAA4B;AAC1B,WAAO,WAAc,KAAK,KAAK;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAIA,WAAmB;AACjB,WAAO,KAAK,UAAU,IAAI;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAIA,CAAC,uBAAO,IAAI,4BAA4B,CAAC,IAAuB;AAC9D,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA,EAIA,CAAC,uBAAO,IAAI,oBAAoB,CAAC,IAAuB;AACtD,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA,EAIA,EAAE,OAAO,QAAQ,IAAyB;AACxC,aAAS,QAAQ,GAAG,QAAQ,KAAK,MAAM,SAAS;AAC9C,YAAM,QAAQ,KAAK,KAAK;AACxB,YAAM;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,QACE,UACA,SACM;AACN,aAAS,QAAQ,GAAG,QAAQ,KAAK,MAAM,SAAS;AAC9C,eAAS,KAAK,SAAS,KAAK,KAAK,GAAG,OAAO,IAAI;AAAA,IACjD;AAAA,EACF;AACF;","names":["isUuidV7","isUuidV7","safeStructuredClone","safeStructuredClone","prototype","isUuidV7"]}
1
+ {"version":3,"sources":["../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"],"sourcesContent":["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"],"mappings":";;;;;;;;;;;;;;;;;;AAEO,SAAS,kBAAqB,eAAqC;AACxE,MAAI,CAAC,cAAc,OAAQ;AAC3B,MAAI,QAAQ,cAAc;AAC1B,SAAO,cAAc,OAAO;AAC1B,kBAAc,SAAS,cAAc,OAAO;AAE9C,SAAO,SAAS,GAAG;AACjB;AACA,kBAAc,OAAO,QAAQ;AAC7B,QAAI,cAAc,OAAO,SAAS,OAAW;AAC7C,kBAAc,SAAS,cAAc,OAAO;AAAA,EAC9C;AACF;;;ACDO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA,EAI5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQT,YAAY,MAAuB,SAAkB;AACnD,UAAM,SAAS,WAAW;AAC1B,UAAM,+CAA+C,MAAM,EAAE;AAC7D,SAAK,OAAO;AACZ,SAAK,OAAO;AAAA,EACd;AACF;;;AC5BO,SAAS,YACd,aACA,eACM;AACN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,EAMA,SAA4B;AAC1B,WAAO,WAAc,KAAK,KAAK;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAIA,WAAmB;AACjB,WAAO,KAAK,UAAU,IAAI;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAIA,CAAC,uBAAO,IAAI,4BAA4B,CAAC,IAAuB;AAC9D,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA,EAIA,CAAC,uBAAO,IAAI,oBAAoB,CAAC,IAAuB;AACtD,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA,EAIA,EAAE,OAAO,QAAQ,IAAyB;AACxC,aAAS,QAAQ,GAAG,QAAQ,KAAK,MAAM,SAAS;AAC9C,YAAM,QAAQ,KAAK,KAAK;AACxB,YAAM;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,QACE,UACA,SACM;AACN,aAAS,QAAQ,GAAG,QAAQ,KAAK,MAAM,SAAS;AAC9C,eAAS,KAAK,SAAS,KAAK,KAAK,GAAG,OAAO,IAAI;AAAA,IACjD;AAAA,EACF;AACF;","names":["isUuidV7","isUuidV7","safeStructuredClone","safeStructuredClone","prototype","isUuidV7"]}
package/package.json CHANGED
@@ -2,13 +2,14 @@
2
2
  "type": "module",
3
3
  "license": "Apache-2.0",
4
4
  "name": "@sovereignbase/convergent-replicated-list",
5
- "version": "1.0.1",
5
+ "version": "1.0.3",
6
6
  "description": "Convergent Replicated List (CR-List), a delta CRDT for an ordered sequence of entries.",
7
7
  "keywords": [
8
8
  "crdt",
9
9
  "list",
10
10
  "replicated",
11
11
  "convergent",
12
+ "local-first",
12
13
  "distributed-systems",
13
14
  "delta-sync",
14
15
  "gossip",