@sovereignbase/convergent-replicated-list 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * The `predecessor` field is the stable ordering anchor used for convergence;
5
5
  * `prev` and `next` are local projection links for indexed reads and mutations.
6
6
  */
7
- type DoublyLinkedListEntry<T> = {
7
+ type CRListStateEntry<T> = {
8
8
  /** Stable UUIDv7 identity for this entry. */
9
9
  uuidv7: string;
10
10
  /** User payload stored in the list. */
@@ -14,9 +14,9 @@ type DoublyLinkedListEntry<T> = {
14
14
  /** Current zero-based index in the local live view. */
15
15
  index: number;
16
16
  /** Previous live entry in the local projection. */
17
- prev: DoublyLinkedListEntry<T> | undefined;
17
+ prev: CRListStateEntry<T> | undefined;
18
18
  /** Next live entry in the local projection. */
19
- next: DoublyLinkedListEntry<T> | undefined;
19
+ next: CRListStateEntry<T> | undefined;
20
20
  } | undefined;
21
21
  /**
22
22
  * Mutable CRList replica state.
@@ -26,22 +26,22 @@ type DoublyLinkedListEntry<T> = {
26
26
  * UUIDv7 entries until they are garbage collected through acknowledgement
27
27
  * frontiers.
28
28
  */
29
- type CRListReplica<T> = {
29
+ type CRListState<T> = {
30
30
  /** Number of live entries in the local projection. */
31
31
  size: number;
32
32
  /** Current live entry used as the walking cursor. */
33
- cursor: DoublyLinkedListEntry<T>;
33
+ cursor: CRListStateEntry<T>;
34
34
  /** Deleted UUIDv7 entries retained for gossip and convergence. */
35
35
  tombstones: Set<string>;
36
36
  /** Live entries by UUIDv7. */
37
- parentMap: Map<string, DoublyLinkedListEntry<T>>;
37
+ parentMap: Map<string, CRListStateEntry<T>>;
38
38
  /** Live entries grouped by stable predecessor identifier. */
39
- childrenMap: Map<string, Array<NonNullable<DoublyLinkedListEntry<T>>>>;
39
+ childrenMap: Map<string, Array<NonNullable<CRListStateEntry<T>>>>;
40
40
  };
41
41
  /**
42
42
  * Serializable value entry used by snapshots and deltas.
43
43
  */
44
- type CRListSnapshotValueEntry<T> = {
44
+ type CRListSnapshotEntry<T> = {
45
45
  /** Stable UUIDv7 identity for this entry. */
46
46
  uuidv7: string;
47
47
  /** User payload for this entry. */
@@ -54,14 +54,10 @@ type CRListSnapshotValueEntry<T> = {
54
54
  */
55
55
  type CRListSnapshot<T> = {
56
56
  /** Serializable live values. */
57
- values: Array<CRListSnapshotValueEntry<T>>;
57
+ values: Array<CRListSnapshotEntry<T>>;
58
58
  /** Retained deleted UUIDv7 entries. */
59
59
  tombstones: Array<string>;
60
60
  };
61
- /**
62
- * Partial CRList state gossiped between replicas.
63
- */
64
- type CRListDelta<T> = Partial<CRListSnapshot<T>>;
65
61
  /**
66
62
  * Minimal local live-view patch keyed by list index.
67
63
  *
@@ -70,8 +66,9 @@ type CRListDelta<T> = Partial<CRListSnapshot<T>>;
70
66
  */
71
67
  type CRListChange<T> = Record<number, T | undefined>;
72
68
  /**
73
- * Tombstone acknowledgement frontier.
69
+ * Partial CRList state gossiped between replicas.
74
70
  */
71
+ type CRListDelta<T> = Partial<CRListSnapshot<T>>;
75
72
  type CRListAck = string;
76
73
  /**
77
74
  * Maps CRList event names to their event payload shapes.
@@ -95,6 +92,123 @@ type CRListEventListener<T, K extends keyof CRListEventMap<T>> = ((event: Custom
95
92
  */
96
93
  type CRListEventListenerFor<T, K extends string> = K extends keyof CRListEventMap<T> ? CRListEventListener<T, K> : EventListenerOrEventListenerObject;
97
94
 
95
+ /**
96
+ * A convergent replicated list.
97
+ *
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.
102
+ *
103
+ * @typeParam T - The value type stored in the list.
104
+ */
105
+ declare class CRList<T> {
106
+ /**
107
+ * Reads or overwrites an entry in the live list projection by index.
108
+ */
109
+ [index: number]: T;
110
+ private readonly state;
111
+ private readonly eventTarget;
112
+ /**
113
+ * Creates a replicated list from an optional serializable snapshot.
114
+ *
115
+ * @param snapshot - A previously emitted CRList snapshot.
116
+ */
117
+ constructor(snapshot?: CRListSnapshot<T>);
118
+ /**
119
+ * The current number of live entries.
120
+ */
121
+ get size(): number;
122
+ /**
123
+ * Inserts a value before an index.
124
+ *
125
+ * If `beforeIndex` is omitted, the value is inserted at the start of the list.
126
+ *
127
+ * @param value - The value to insert.
128
+ * @param beforeIndex - The index to insert before.
129
+ */
130
+ prepend(value: T, beforeIndex?: number): void;
131
+ /**
132
+ * Inserts a value after an index.
133
+ *
134
+ * If `afterIndex` is omitted, the value is appended at the end of the list.
135
+ *
136
+ * @param value - The value to insert.
137
+ * @param afterIndex - The index to insert after.
138
+ */
139
+ append(value: T, afterIndex?: number): void;
140
+ /**
141
+ * Removes the entry at an index.
142
+ *
143
+ * @param index - The index to remove.
144
+ */
145
+ remove(index: number): void;
146
+ /**
147
+ * Applies a remote gossip delta to this list.
148
+ *
149
+ * Emits a `change` event when the merge changes the live projection.
150
+ *
151
+ * @param delta - The remote CRList delta to merge.
152
+ */
153
+ merge(delta: CRListDelta<T>): void;
154
+ /**
155
+ * Emits an acknowledgement frontier for currently retained tombstones.
156
+ */
157
+ acknowledge(): void;
158
+ /**
159
+ * Garbage-collects tombstones that are covered by acknowledgement frontiers.
160
+ *
161
+ * @param frontiers - Replica acknowledgement frontiers.
162
+ */
163
+ garbageCollect(frontiers: Array<CRListAck>): void;
164
+ /**
165
+ * Emits the current serializable list snapshot.
166
+ */
167
+ snapshot(): void;
168
+ /**
169
+ * Registers an event listener.
170
+ *
171
+ * @param type - The event type to listen for.
172
+ * @param listener - The listener to register.
173
+ * @param options - Listener registration options.
174
+ */
175
+ addEventListener<K extends keyof CRListEventMap<T>>(type: K, listener: CRListEventListenerFor<T, K> | null, options?: boolean | AddEventListenerOptions): void;
176
+ /**
177
+ * Removes an event listener.
178
+ *
179
+ * @param type - The event type to stop listening for.
180
+ * @param listener - The listener to remove.
181
+ * @param options - Listener removal options.
182
+ */
183
+ removeEventListener<K extends keyof CRListEventMap<T>>(type: K, listener: CRListEventListenerFor<T, K> | null, options?: boolean | EventListenerOptions): void;
184
+ /**
185
+ * Returns a serializable snapshot representation of this list.
186
+ *
187
+ * Called automatically by `JSON.stringify`.
188
+ */
189
+ toJSON(): CRListSnapshot<T>;
190
+ /**
191
+ * Returns this list as a JSON string.
192
+ */
193
+ toString(): string;
194
+ /**
195
+ * Iterates over the current live values in index order.
196
+ */
197
+ [Symbol.iterator](): IterableIterator<T>;
198
+ /**
199
+ * Calls a function once for each live value in index order.
200
+ *
201
+ * @param callback - Function to call for each value.
202
+ * @param thisArg - Optional `this` value for the callback.
203
+ */
204
+ forEach(callback: (value: T, index: number, list: this) => void, thisArg?: unknown): void;
205
+ }
206
+
207
+ /**
208
+ * Error codes thrown by {@link CRList}.
209
+ */
210
+ type CRListErrorCode = 'VALUE_NOT_CLONEABLE' | 'INDEX_OUT_OF_BOUNDS' | 'LIST_EMPTY' | 'LIST_INTEGRITY_VIOLATION' | 'UPDATE_EXPECTED_AN_ARRAY';
211
+
98
212
  /**
99
213
  * Creates a local CRList replica from an optional snapshot.
100
214
  *
@@ -112,7 +226,7 @@ type CRListEventListenerFor<T, K extends string> = K extends keyof CRListEventMa
112
226
  *
113
227
  * Space complexity: O(n + t + c)
114
228
  */
115
- declare function __create<T>(snapshot?: CRListSnapshot<T>): CRListReplica<T>;
229
+ declare function __create<T>(snapshot?: CRListSnapshot<T>): CRListState<T>;
116
230
 
117
231
  /**
118
232
  * Reads the value at an index in the replica live view.
@@ -130,7 +244,7 @@ declare function __create<T>(snapshot?: CRListSnapshot<T>): CRListReplica<T>;
130
244
  *
131
245
  * Space complexity: O(1)
132
246
  */
133
- declare function __read<T>(targetIndex: number, crListReplica: CRListReplica<T>): T | undefined;
247
+ declare function __read<T>(targetIndex: number, crListReplica: CRListState<T>): T | undefined;
134
248
 
135
249
  /**
136
250
  * Applies a local value mutation to the replica live view.
@@ -154,7 +268,7 @@ declare function __read<T>(targetIndex: number, crListReplica: CRListReplica<T>)
154
268
  *
155
269
  * Space complexity: O(v + c)
156
270
  */
157
- declare function __update<T>(listIndex: number, listValues: Array<T>, crListReplica: CRListReplica<T>, mode: 'overwrite' | 'before' | 'after'): {
271
+ declare function __update<T>(listIndex: number, listValues: Array<T>, crListReplica: CRListState<T>, mode: 'overwrite' | 'before' | 'after'): {
158
272
  change: CRListChange<T>;
159
273
  delta: CRListDelta<T>;
160
274
  } | false;
@@ -179,7 +293,7 @@ declare function __update<T>(listIndex: number, listValues: Array<T>, crListRepl
179
293
  *
180
294
  * Space complexity: O(q)
181
295
  */
182
- declare function __delete<T>(crListReplica: CRListReplica<T>, startIndex?: number, endIndex?: number): {
296
+ declare function __delete<T>(crListReplica: CRListState<T>, startIndex?: number, endIndex?: number): {
183
297
  change: CRListChange<T>;
184
298
  delta: CRListDelta<T>;
185
299
  } | false;
@@ -207,7 +321,7 @@ declare function __delete<T>(crListReplica: CRListReplica<T>, startIndex?: numbe
207
321
  *
208
322
  * Space complexity: O(n + v + t + c)
209
323
  */
210
- declare function __merge<T>(crListReplica: CRListReplica<T>, crListDelta: CRListDelta<T>): CRListChange<T> | false;
324
+ declare function __merge<T>(crListReplica: CRListState<T>, crListDelta: CRListDelta<T>): CRListChange<T> | false;
211
325
 
212
326
  /**
213
327
  * Returns the replica tombstone acknowledgement frontier.
@@ -223,7 +337,7 @@ declare function __merge<T>(crListReplica: CRListReplica<T>, crListDelta: CRList
223
337
  *
224
338
  * Space complexity: O(1)
225
339
  */
226
- declare function __acknowledge<T>(crListReplica: CRListReplica<T>): CRListAck | false;
340
+ declare function __acknowledge<T>(crListReplica: CRListState<T>): CRListAck | false;
227
341
 
228
342
  /**
229
343
  * Removes tombstones acknowledged by all supplied frontiers.
@@ -240,7 +354,7 @@ declare function __acknowledge<T>(crListReplica: CRListReplica<T>): CRListAck |
240
354
  *
241
355
  * Space complexity: O(1)
242
356
  */
243
- declare function __garbageCollect<T>(frontiers: Array<CRListAck>, crListReplica: CRListReplica<T>): void;
357
+ declare function __garbageCollect<T>(frontiers: Array<CRListAck>, crListReplica: CRListState<T>): void;
244
358
 
245
359
  /**
246
360
  * Creates a full serializable CRList snapshot from the current replica state.
@@ -258,123 +372,6 @@ declare function __garbageCollect<T>(frontiers: Array<CRListAck>, crListReplica:
258
372
  *
259
373
  * Space complexity: O(n + t + c)
260
374
  */
261
- declare function __snapshot<T>(crListReplica: CRListReplica<T>): CRListSnapshot<T>;
262
-
263
- /**
264
- * Error codes thrown by {@link CRList}.
265
- */
266
- type CRListErrorCode = 'VALUE_NOT_CLONEABLE' | 'INDEX_OUT_OF_BOUNDS' | 'LIST_EMPTY' | 'LIST_INTEGRITY_VIOLATION' | 'UPDATE_EXPECTED_AN_ARRAY';
267
-
268
- /**
269
- * A convergent replicated list.
270
- *
271
- * Numeric property access reads and mutates the live list projection:
272
- * `list[0]` reads an entry, `list[0] = value` writes an entry, and `delete
273
- * list[0]` removes one entry. Local mutations emit `delta` and `change` events;
274
- * remote merges emit `change` events.
275
- *
276
- * @typeParam T - The value type stored in the list.
277
- */
278
- declare class CRList<T> {
279
- /**
280
- * Reads or overwrites an entry in the live list projection by index.
281
- */
282
- [index: number]: T;
283
- private readonly state;
284
- private readonly eventTarget;
285
- /**
286
- * Creates a replicated list from an optional serializable snapshot.
287
- *
288
- * @param snapshot - A previously emitted CRList snapshot.
289
- */
290
- constructor(snapshot?: CRListSnapshot<T>);
291
- /**
292
- * The current number of live entries.
293
- */
294
- get size(): number;
295
- /**
296
- * Inserts a value before an index.
297
- *
298
- * If `beforeIndex` is omitted, the value is inserted at the start of the list.
299
- *
300
- * @param value - The value to insert.
301
- * @param beforeIndex - The index to insert before.
302
- */
303
- prepend(value: T, beforeIndex?: number): void;
304
- /**
305
- * Inserts a value after an index.
306
- *
307
- * If `afterIndex` is omitted, the value is appended at the end of the list.
308
- *
309
- * @param value - The value to insert.
310
- * @param afterIndex - The index to insert after.
311
- */
312
- append(value: T, afterIndex?: number): void;
313
- /**
314
- * Removes the entry at an index.
315
- *
316
- * @param index - The index to remove.
317
- */
318
- remove(index: number): void;
319
- /**
320
- * Applies a remote gossip delta to this list.
321
- *
322
- * Emits a `change` event when the merge changes the live projection.
323
- *
324
- * @param delta - The remote CRList delta to merge.
325
- */
326
- merge(delta: CRListDelta<T>): void;
327
- /**
328
- * Emits an acknowledgement frontier for currently retained tombstones.
329
- */
330
- acknowledge(): void;
331
- /**
332
- * Garbage-collects tombstones that are covered by acknowledgement frontiers.
333
- *
334
- * @param frontiers - Replica acknowledgement frontiers.
335
- */
336
- garbageCollect(frontiers: Array<CRListAck>): void;
337
- /**
338
- * Emits the current serializable list snapshot.
339
- */
340
- snapshot(): void;
341
- /**
342
- * Registers an event listener.
343
- *
344
- * @param type - The event type to listen for.
345
- * @param listener - The listener to register.
346
- * @param options - Listener registration options.
347
- */
348
- addEventListener<K extends keyof CRListEventMap<T>>(type: K, listener: CRListEventListenerFor<T, K> | null, options?: boolean | AddEventListenerOptions): void;
349
- /**
350
- * Removes an event listener.
351
- *
352
- * @param type - The event type to stop listening for.
353
- * @param listener - The listener to remove.
354
- * @param options - Listener removal options.
355
- */
356
- removeEventListener<K extends keyof CRListEventMap<T>>(type: K, listener: CRListEventListenerFor<T, K> | null, options?: boolean | EventListenerOptions): void;
357
- /**
358
- * Returns a serializable snapshot representation of this list.
359
- *
360
- * Called automatically by `JSON.stringify`.
361
- */
362
- toJSON(): CRListSnapshot<T>;
363
- /**
364
- * Returns this list as a JSON string.
365
- */
366
- toString(): string;
367
- /**
368
- * Iterates over the current live values in index order.
369
- */
370
- [Symbol.iterator](): IterableIterator<T>;
371
- /**
372
- * Calls a function once for each live value in index order.
373
- *
374
- * @param callback - Function to call for each value.
375
- * @param thisArg - Optional `this` value for the callback.
376
- */
377
- forEach(callback: (value: T, index: number, list: this) => void, thisArg?: unknown): void;
378
- }
375
+ declare function __snapshot<T>(crListReplica: CRListState<T>): CRListSnapshot<T>;
379
376
 
380
- export { CRList, type CRListAck, type CRListChange, type CRListDelta, type CRListErrorCode, type CRListReplica, type CRListSnapshot, __acknowledge, __create, __delete, __garbageCollect, __merge, __read, __snapshot, __update };
377
+ export { CRList, type CRListAck, type CRListChange, type CRListDelta, type CRListErrorCode, type CRListEventListener, type CRListEventListenerFor, type CRListEventMap, type CRListSnapshot, type CRListSnapshotEntry, type CRListState, type CRListStateEntry, __acknowledge, __create, __delete, __garbageCollect, __merge, __read, __snapshot, __update };
package/dist/index.js CHANGED
@@ -15,9 +15,6 @@
15
15
  */
16
16
 
17
17
 
18
- // src/core/crud/create/index.ts
19
- import { isUuidV7 as isUuidV72, prototype } from "@sovereignbase/utils";
20
-
21
18
  // src/.helpers/assertListIndices/index.ts
22
19
  function assertListIndices(crListReplica) {
23
20
  if (!crListReplica.cursor) return;
@@ -218,6 +215,7 @@ function indexFromPropertyKey(index) {
218
215
  }
219
216
 
220
217
  // src/core/crud/create/index.ts
218
+ import { isUuidV7 as isUuidV72, prototype } from "@sovereignbase/utils";
221
219
  function __create(snapshot) {
222
220
  const crListReplica = {
223
221
  size: 0,