@sovereignbase/convergent-replicated-struct 1.0.1 → 1.1.0

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
@@ -2,182 +2,153 @@
2
2
  /**
3
3
  * Represents the internal replicated state for a single field.
4
4
  */
5
- type OOStructStateEntry<V> = {
5
+ type CRStructStateEntry<V> = {
6
6
  /**
7
7
  * The identifier of the current winning value.
8
8
  */
9
- __uuidv7: string;
9
+ uuidv7: string;
10
10
  /**
11
11
  * The current winning value.
12
12
  */
13
- __value: V;
13
+ value: V;
14
14
  /**
15
15
  * The predecessor identifier for the current winning value.
16
16
  */
17
- __after: string;
17
+ predecessor: string;
18
18
  /**
19
19
  * Identifiers known to have been overwritten for the field.
20
20
  */
21
- __overwrites: Set<string>;
21
+ tombstones: Set<string>;
22
22
  };
23
23
  /**
24
24
  * Represents the internal replicated state of an OO-Struct replica.
25
25
  */
26
- type OOStructState<T extends Record<string, unknown>> = {
27
- [K in keyof T]: OOStructStateEntry<T[K]>;
26
+ type CRStructState<T extends Record<string, unknown>> = {
27
+ entries: {
28
+ [K in keyof T]: CRStructStateEntry<T[K]>;
29
+ };
30
+ defaults: T;
28
31
  };
29
32
  /**Serlialized projection of replica state*/
30
33
  /**
31
34
  * Represents the serialized state for a single field.
32
35
  */
33
- type OOStructSnapshotEntry<V> = {
36
+ type CRStructSnapshotEntry<V> = {
34
37
  /**
35
38
  * The identifier of the current winning value.
36
39
  */
37
- __uuidv7: string;
40
+ uuidv7: string;
38
41
  /**
39
42
  * The serialized current winning value.
40
43
  */
41
- __value: V;
44
+ value: V;
42
45
  /**
43
46
  * The predecessor identifier for the current winning value.
44
47
  */
45
- __after: string;
48
+ predecessor: string;
46
49
  /**
47
50
  * Serialized overwritten identifiers for the field.
48
51
  */
49
- __overwrites: Array<string>;
52
+ tombstones: Array<string>;
50
53
  };
51
54
  /**
52
55
  * Represents a serialized snapshot of the full replica state.
53
56
  */
54
- type OOStructSnapshot<T extends Record<string, unknown>> = {
55
- [K in keyof T]: OOStructSnapshotEntry<T[K]>;
57
+ type CRStructSnapshot<T extends Record<string, unknown>> = {
58
+ [K in keyof T]: CRStructSnapshotEntry<T[K]>;
56
59
  };
57
60
  /**Resolved projection of replica state*/
58
61
  /**
59
62
  * Represents visible field values that changed during a local operation or merge.
60
63
  */
61
- type OOStructChange<T extends Record<string, unknown>> = Partial<T>;
64
+ type CRStructChange<T extends Record<string, unknown>> = Partial<T>;
62
65
  /**(T)*/
63
- /**A "report" on what the replica has seen*/
64
- /**
65
- * Represents the acknowledgement frontier for a set of field keys.
66
- */
67
- type OOStructAcknowledgementFrontier<K extends string> = Record<K, string>;
68
66
  /**Partial changes to gossip*/
69
67
  /**
70
68
  * Represents a partial serialized state projection exchanged between replicas.
71
69
  */
72
- type OOStructDelta<T extends Record<string, unknown>> = Partial<OOStructSnapshot<T>>;
70
+ type CRStructDelta<T extends Record<string, unknown>> = Partial<CRStructSnapshot<T>>;
71
+ /**A "report" on what the replica has seen*/
73
72
  /**
74
73
  * Represents the current acknowledgement frontier emitted by a replica.
75
74
  */
76
- type OOStructAck<T extends Record<string, unknown>> = Partial<OOStructAcknowledgementFrontier<Extract<keyof T, string>>>;
75
+ type CRStructAck<T extends Record<string, unknown>> = Partial<Record<keyof T, string>>;
77
76
  /***/
78
77
  /**
79
78
  * Maps OO-Struct event names to their event payload shapes.
80
79
  */
81
- type OOStructEventMap<T extends Record<string, unknown>> = {
80
+ type CRStructEventMap<T extends Record<string, unknown>> = {
82
81
  /** STATE / PROJECTION */
83
- snapshot: OOStructSnapshot<T>;
84
- change: OOStructChange<T>;
82
+ snapshot: CRStructSnapshot<T>;
83
+ change: CRStructChange<T>;
85
84
  /** GOSSIP / PROTOCOL */
86
- delta: OOStructDelta<T>;
87
- ack: OOStructAck<T>;
85
+ delta: CRStructDelta<T>;
86
+ ack: CRStructAck<T>;
88
87
  };
89
88
  /**
90
89
  * Represents a strongly typed OO-Struct event listener.
91
90
  */
92
- type OOStructEventListener<T extends Record<string, unknown>, K extends keyof OOStructEventMap<T>> = ((event: CustomEvent<OOStructEventMap<T>[K]>) => void) | {
93
- handleEvent(event: CustomEvent<OOStructEventMap<T>[K]>): void;
91
+ type CRStructEventListener<T extends Record<string, unknown>, K extends keyof CRStructEventMap<T>> = ((event: CustomEvent<CRStructEventMap<T>[K]>) => void) | {
92
+ handleEvent(event: CustomEvent<CRStructEventMap<T>[K]>): void;
94
93
  };
95
94
  /**
96
95
  * Resolves an event name to its corresponding listener type.
97
96
  */
98
- type OOStructEventListenerFor<T extends Record<string, unknown>, K extends string> = K extends keyof OOStructEventMap<T> ? OOStructEventListener<T, K> : EventListenerOrEventListenerObject;
97
+ type CRStructEventListenerFor<T extends Record<string, unknown>, K extends string> = K extends keyof CRStructEventMap<T> ? CRStructEventListener<T, K> : EventListenerOrEventListenerObject;
99
98
 
100
99
  /**
101
- * Represents an observed-overwrite struct replica.
102
- *
103
- * The struct shape is fixed by the provided default values.
100
+ * Runtime implementation for a proxy-backed CR-Struct replica.
104
101
  */
105
- declare class OOStruct<T extends Record<string, unknown>> {
106
- private readonly __eventTarget;
107
- private readonly __defaults;
108
- private readonly __state;
109
- private __live;
102
+ declare class CRStructRaw<T extends Record<string, unknown>> {
103
+ private readonly state;
104
+ private readonly eventTarget;
110
105
  /**
111
106
  * Creates a replica from default values and an optional snapshot.
112
107
  *
113
- * @param defaults - The default field values that define the struct shape.
114
- * @param snapshot - An optional serialized snapshot used for hydration.
115
- * @throws {OOStructError} Thrown when the default values are not supported by `structuredClone`.
116
- */
117
- constructor(defaults: {
118
- [K in keyof T]: T[K];
119
- }, snapshot?: OOStructSnapshot<T>);
120
- /**CRUD*/
121
- /**
122
- * Creates a new replica.
108
+ * The struct shape is fixed by the provided default values. The returned
109
+ * proxy exposes those fields as direct properties on the instance.
123
110
  *
124
111
  * @param defaults - The default field values that define the struct shape.
125
- * @param snapshot - An optional serialized snapshot used for hydration.
126
- * @returns A new OO-Struct replica.
127
- */
128
- static create<T extends Record<string, unknown>>(defaults: {
129
- [K in keyof T]: T[K];
130
- }, snapshot?: OOStructSnapshot<T>): OOStruct<T>;
131
- /**
132
- * Reads the current value of a field.
133
- *
134
- * @param key - The field key to read.
135
- * @returns A cloned copy of the field's current value.
136
- */
137
- read<K extends keyof T>(key: K): T[K];
138
- /**
139
- * Overwrites a field with a new value.
140
- *
141
- * @param key - The field key to overwrite.
142
- * @param value - The next value for the field.
143
- * @throws {OOStructError} Thrown when the value is not supported by `structuredClone`.
144
- * @throws {OOStructError} Thrown when the value runtime type does not match the default value runtime type.
112
+ * @param snapshot - An optional serialized snapshot used to hydrate the replica.
113
+ * @throws {CRStructError} Thrown when the default values are not supported by `structuredClone`.
145
114
  */
146
- update<K extends keyof T>(key: K, value: T[K]): void;
115
+ constructor(defaults: T, snapshot?: CRStructSnapshot<T>);
147
116
  /**
148
- * Resets one field or the entire struct back to default values.
117
+ * Applies a remote or local delta to the replica state.
149
118
  *
150
- * @param key - The optional field key to reset. When omitted, every field is reset.
119
+ * @param crStructDelta - The partial serialized field state to merge.
151
120
  */
152
- delete<K extends keyof T>(key?: K): void;
153
- /**MAGS*/
154
- /**
155
- * Merges an incoming delta into the current replica.
156
- *
157
- * @param replica - The incoming partial snapshot projection to merge.
158
- */
159
- merge<K extends keyof T>(replica: OOStructDelta<T>): void;
121
+ merge(crStructDelta: CRStructDelta<T>): void;
160
122
  /**
161
123
  * Emits the current acknowledgement frontier for each field.
162
124
  */
163
- acknowledge<K extends Extract<keyof T, string>>(): void;
125
+ acknowledge(): void;
164
126
  /**
165
127
  * Removes overwritten identifiers that every provided frontier has acknowledged.
166
128
  *
167
129
  * @param frontiers - A collection of acknowledgement frontiers to compact against.
168
130
  */
169
- garbageCollect<K extends Extract<keyof T, string>>(frontiers: Array<OOStructAck<T>>): void;
131
+ garbageCollect(frontiers: Array<CRStructAck<T>>): void;
170
132
  /**
171
133
  * Emits a serialized snapshot of the current replica state.
172
134
  */
173
135
  snapshot(): void;
174
- /**ADDITIONAL*/
175
136
  /**
176
137
  * Returns the struct field keys.
177
138
  *
178
139
  * @returns The field keys in the current replica.
179
140
  */
180
141
  keys<K extends keyof T>(): Array<K>;
142
+ /**
143
+ * Resets every field in the replica back to its default value.
144
+ */
145
+ clear(): void;
146
+ /**
147
+ * Returns a cloned plain object view of the current replica fields.
148
+ *
149
+ * @returns The current field values keyed by field name.
150
+ */
151
+ clone(): T;
181
152
  /**
182
153
  * Returns cloned copies of the current field values.
183
154
  *
@@ -190,7 +161,20 @@ declare class OOStruct<T extends Record<string, unknown>> {
190
161
  * @returns The current field entries.
191
162
  */
192
163
  entries<K extends keyof T>(): Array<[K, T[K]]>;
193
- /**EVENTS*/
164
+ /**
165
+ * Returns a serializable snapshot representation of this replica.
166
+ *
167
+ * Called automatically by `JSON.stringify`.
168
+ */
169
+ toJSON(): CRStructSnapshot<T>;
170
+ /**
171
+ * Returns this replica as a JSON string.
172
+ */
173
+ toString(): string;
174
+ /**
175
+ * Iterates over the current live field entries.
176
+ */
177
+ [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]>;
194
178
  /**
195
179
  * Registers an event listener.
196
180
  *
@@ -198,7 +182,7 @@ declare class OOStruct<T extends Record<string, unknown>> {
198
182
  * @param listener - The listener to register.
199
183
  * @param options - Listener registration options.
200
184
  */
201
- addEventListener<K extends keyof OOStructEventMap<T>>(type: K, listener: OOStructEventListenerFor<T, K> | null, options?: boolean | AddEventListenerOptions): void;
185
+ addEventListener<K extends keyof CRStructEventMap<T>>(type: K, listener: CRStructEventListenerFor<T, K> | null, options?: boolean | AddEventListenerOptions): void;
202
186
  /**
203
187
  * Removes an event listener.
204
188
  *
@@ -206,21 +190,205 @@ declare class OOStruct<T extends Record<string, unknown>> {
206
190
  * @param listener - The listener to remove.
207
191
  * @param options - Listener removal options.
208
192
  */
209
- removeEventListener<K extends keyof OOStructEventMap<T>>(type: K, listener: OOStructEventListenerFor<T, K> | null, options?: boolean | EventListenerOptions): void;
210
- /**HELPERS*/
211
- /**
212
- * Overwrites a field and returns the serialized delta entry for that overwrite.
213
- *
214
- * @param key - The field key to overwrite.
215
- * @param value - The next value for the field.
216
- * @returns The serialized snapshot entry for the new winning value.
217
- */
218
- private overwriteAndReturnSnapshotEntry;
193
+ removeEventListener<K extends keyof CRStructEventMap<T>>(type: K, listener: CRStructEventListenerFor<T, K> | null, options?: boolean | EventListenerOptions): void;
219
194
  }
195
+ type CRStruct<T extends Record<string, unknown>> = CRStructRaw<T> & T;
196
+ declare const CRStruct: {
197
+ new <T extends Record<string, unknown>>(defaults: T, snapshot?: CRStructSnapshot<T>): CRStruct<T>;
198
+ };
220
199
 
221
200
  /**
222
201
  * Error codes thrown by {@link OOStruct}.
223
202
  */
224
- type OOStructErrorCode = 'DEFAULTS_NOT_CLONEABLE' | 'VALUE_NOT_CLONEABLE' | 'VALUE_TYPE_MISMATCH';
203
+ type CRStructErrorCode = 'DEFAULTS_NOT_CLONEABLE' | 'VALUE_NOT_CLONEABLE' | 'VALUE_TYPE_MISMATCH';
204
+
205
+ /**
206
+ * Creates internal CR-Struct state from default values and an optional snapshot.
207
+ *
208
+ * Default values define the replica field set. Compatible snapshot entries are
209
+ * parsed into live state entries, and invalid snapshot entries are ignored so
210
+ * the corresponding field falls back to a freshly initialized default-backed
211
+ * entry.
212
+ *
213
+ * @param defaults - Default field values that define the replica shape.
214
+ * @param snapshot - Optional serialized state used to hydrate matching fields.
215
+ *
216
+ * @returns
217
+ * A hydrated internal CR-Struct state object.
218
+ *
219
+ * @throws {CRStructError} Thrown when the default values are not supported by `structuredClone`.
220
+ *
221
+ * Time complexity: O(k + c + s + t), worst case O(k + c + s + t)
222
+ *
223
+ * k = default field count
224
+ * c = total cloned payload size across defaults and accepted snapshot values
225
+ * s = snapshot entries inspected for matching fields
226
+ * t = tombstone count materialized for accepted snapshot entries
227
+ *
228
+ * Space complexity: O(k + c + t)
229
+ */
230
+ declare function __create<T extends Record<string, unknown>>(defaults: T, snapshot?: CRStructSnapshot<T>): CRStructState<T>;
231
+
232
+ /**
233
+ * Reads and clones the current value of a single field.
234
+ *
235
+ * @param key - The field key to read.
236
+ * @param crStructReplica - The replica state that owns the field.
237
+ *
238
+ * @returns
239
+ * A cloned copy of the field's current value.
240
+ *
241
+ * Time complexity: O(c), worst case O(c)
242
+ *
243
+ * c = cloned field payload size
244
+ *
245
+ * Space complexity: O(c)
246
+ */
247
+ declare function __read<T extends Record<string, unknown>>(key: keyof T, crStructReplica: CRStructState<T>): T[keyof T];
248
+
249
+ /**
250
+ * Overwrites a field with a new value and emits the resulting projections.
251
+ *
252
+ * The incoming value is cloned first, validated against the default field
253
+ * runtime type, and then written back as the current winning value for the
254
+ * target field.
255
+ *
256
+ * @param key - The field key to overwrite.
257
+ * @param value - The next value for the field.
258
+ * @param crStructReplica - The replica state that owns the field.
259
+ *
260
+ * @returns
261
+ * The visible change projection and serialized delta for the overwrite.
262
+ *
263
+ * @throws {CRStructError} Thrown when the value is not supported by `structuredClone`.
264
+ * @throws {CRStructError} Thrown when the value runtime type does not match the default value runtime type.
265
+ *
266
+ * Time complexity: O(c + t), worst case O(c + t)
267
+ *
268
+ * c = cloned and serialized payload size for the updated value
269
+ * t = tombstone count serialized for the target field
270
+ *
271
+ * Space complexity: O(c + t)
272
+ */
273
+ declare function __update<T extends Record<string, unknown>>(key: keyof T, value: T[keyof T], crStructReplica: CRStructState<T>): {
274
+ change: CRStructChange<T>;
275
+ delta: CRStructDelta<T>;
276
+ } | false;
277
+
278
+ /**
279
+ * Resets one field or the entire struct back to default values.
280
+ *
281
+ * Each touched field is overwritten from the replica defaults and contributes a
282
+ * visible change projection plus a serialized delta describing the reset.
283
+ *
284
+ * @param crStructReplica - The replica state to reset.
285
+ * @param key - The optional field key to reset. When omitted, every field is reset.
286
+ *
287
+ * @returns
288
+ * The visible change projection and serialized delta for the reset, or `false`
289
+ * when a keyed reset targets a field outside the replica.
290
+ *
291
+ * Time complexity: O(k + c + t), worst case O(k + c + t)
292
+ *
293
+ * k = number of fields being reset
294
+ * c = cloned and serialized payload size across reset field values
295
+ * t = tombstone count serialized across reset fields
296
+ *
297
+ * Space complexity: O(k + c + t)
298
+ */
299
+ declare function __delete<T extends Record<string, unknown>>(crStructReplica: CRStructState<T>, key?: keyof T): {
300
+ change: CRStructChange<T>;
301
+ delta: CRStructDelta<T>;
302
+ } | false;
303
+
304
+ /**
305
+ * Merges an incoming delta into the current replica.
306
+ *
307
+ * Unknown fields and invalid snapshot entries are ignored. Accepted candidates
308
+ * extend local tombstone knowledge, may advance the current winning value, and
309
+ * may emit a return delta when the local state already dominates the incoming
310
+ * entry.
311
+ *
312
+ * @param crStructDelta - The incoming partial snapshot projection to merge.
313
+ * @param crStructReplica - The local replica state to merge into.
314
+ *
315
+ * @returns
316
+ * The visible change projection and reply delta, or `false` when the input is
317
+ * invalid or produces no outbound effect.
318
+ *
319
+ * Time complexity: O(d + l + i + c), worst case O(d + l + i + c)
320
+ *
321
+ * d = incoming delta field count
322
+ * l = local tombstone count processed across touched fields
323
+ * i = incoming tombstone count processed across accepted delta entries
324
+ * c = cloned and serialized payload size across emitted changes and reply deltas
325
+ *
326
+ * Space complexity: O(d + l + c)
327
+ */
328
+ declare function __merge<T extends Record<string, unknown>>(crStructDelta: CRStructDelta<T>, crStructReplica: CRStructState<T>): {
329
+ change: CRStructChange<T>;
330
+ delta: CRStructDelta<T>;
331
+ } | false;
332
+
333
+ /**
334
+ * Emits the current acknowledgement frontier for each field.
335
+ *
336
+ * Each field reports the largest tombstone identifier currently known for that
337
+ * field.
338
+ *
339
+ * @param crStructReplica - The replica state to summarize.
340
+ *
341
+ * @returns
342
+ * An acknowledgement frontier keyed by field name.
343
+ *
344
+ * Time complexity: O(k + t), worst case O(k + t)
345
+ *
346
+ * k = replica field count
347
+ * t = total tombstone count across all fields
348
+ *
349
+ * Space complexity: O(k)
350
+ */
351
+ declare function __acknowledge<T extends Record<string, unknown>>(crStructReplica: CRStructState<T>): CRStructAck<T> | false;
352
+
353
+ /**
354
+ * Removes overwritten identifiers that every provided frontier has acknowledged.
355
+ *
356
+ * The smallest valid acknowledgement per field is collected first, then local
357
+ * tombstones at or below that frontier are removed while keeping the current
358
+ * predecessor identifier intact.
359
+ *
360
+ * @param frontiers - A collection of acknowledgement frontiers to compact against.
361
+ * @param crStructReplica - The replica state to compact.
362
+ *
363
+ * Time complexity: O(a + t), worst case O(a + t)
364
+ *
365
+ * a = acknowledgement entries scanned across all provided frontiers
366
+ * t = tombstone count scanned across compacted fields
367
+ *
368
+ * Space complexity: O(k)
369
+ *
370
+ * k = fields that receive a valid acknowledgement frontier
371
+ */
372
+ declare function __garbageCollect<T extends Record<string, unknown>>(frontiers: Array<CRStructAck<T>>, crStructReplica: CRStructState<T>): void;
373
+
374
+ /**
375
+ * Serializes the current replica state into a snapshot projection.
376
+ *
377
+ * Each processed state entry is converted into its serializable snapshot form.
378
+ *
379
+ * @param crStructReplica - The replica state to serialize.
380
+ *
381
+ * @returns
382
+ * A serializable snapshot projection of the replica state.
383
+ *
384
+ * Time complexity: O(k + t + c), worst case O(k + t + c)
385
+ *
386
+ * k = state entry count processed by the serializer
387
+ * t = tombstone count serialized across processed entries
388
+ * c = serialized payload size across processed values
389
+ *
390
+ * Space complexity: O(k + t + c)
391
+ */
392
+ declare function __snapshot<T extends Record<string, unknown>>(crStructReplica: CRStructState<T>): CRStructSnapshot<T>;
225
393
 
226
- export { OOStruct, type OOStructAck, type OOStructAcknowledgementFrontier, type OOStructChange, type OOStructDelta, type OOStructErrorCode, type OOStructEventListener, type OOStructEventListenerFor, type OOStructEventMap, type OOStructSnapshot, type OOStructSnapshotEntry, type OOStructState, type OOStructStateEntry };
394
+ export { CRStruct, type CRStructAck, type CRStructChange, type CRStructDelta, type CRStructErrorCode, type CRStructEventListener, type CRStructEventListenerFor, type CRStructEventMap, type CRStructSnapshot, type CRStructSnapshotEntry, type CRStructState, type CRStructStateEntry, __acknowledge, __create, __delete, __garbageCollect, __merge, __read, __snapshot, __update };