@sovereignbase/convergent-replicated-struct 0.0.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.
@@ -0,0 +1,226 @@
1
+ /**Replica State*/
2
+ /**
3
+ * Represents the internal replicated state for a single field.
4
+ */
5
+ type OOStructStateEntry<V> = {
6
+ /**
7
+ * The identifier of the current winning value.
8
+ */
9
+ __uuidv7: string;
10
+ /**
11
+ * The current winning value.
12
+ */
13
+ __value: V;
14
+ /**
15
+ * The predecessor identifier for the current winning value.
16
+ */
17
+ __after: string;
18
+ /**
19
+ * Identifiers known to have been overwritten for the field.
20
+ */
21
+ __overwrites: Set<string>;
22
+ };
23
+ /**
24
+ * Represents the internal replicated state of an OO-Struct replica.
25
+ */
26
+ type OOStructState<T extends Record<string, unknown>> = {
27
+ [K in keyof T]: OOStructStateEntry<T[K]>;
28
+ };
29
+ /**Serlialized projection of replica state*/
30
+ /**
31
+ * Represents the serialized state for a single field.
32
+ */
33
+ type OOStructSnapshotEntry<V> = {
34
+ /**
35
+ * The identifier of the current winning value.
36
+ */
37
+ __uuidv7: string;
38
+ /**
39
+ * The serialized current winning value.
40
+ */
41
+ __value: V;
42
+ /**
43
+ * The predecessor identifier for the current winning value.
44
+ */
45
+ __after: string;
46
+ /**
47
+ * Serialized overwritten identifiers for the field.
48
+ */
49
+ __overwrites: Array<string>;
50
+ };
51
+ /**
52
+ * Represents a serialized snapshot of the full replica state.
53
+ */
54
+ type OOStructSnapshot<T extends Record<string, unknown>> = {
55
+ [K in keyof T]: OOStructSnapshotEntry<T[K]>;
56
+ };
57
+ /**Resolved projection of replica state*/
58
+ /**
59
+ * Represents visible field values that changed during a local operation or merge.
60
+ */
61
+ type OOStructChange<T extends Record<string, unknown>> = Partial<T>;
62
+ /**(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
+ /**Partial changes to gossip*/
69
+ /**
70
+ * Represents a partial serialized state projection exchanged between replicas.
71
+ */
72
+ type OOStructDelta<T extends Record<string, unknown>> = Partial<OOStructSnapshot<T>>;
73
+ /**
74
+ * Represents the current acknowledgement frontier emitted by a replica.
75
+ */
76
+ type OOStructAck<T extends Record<string, unknown>> = Partial<OOStructAcknowledgementFrontier<Extract<keyof T, string>>>;
77
+ /***/
78
+ /**
79
+ * Maps OO-Struct event names to their event payload shapes.
80
+ */
81
+ type OOStructEventMap<T extends Record<string, unknown>> = {
82
+ /** STATE / PROJECTION */
83
+ snapshot: OOStructSnapshot<T>;
84
+ change: OOStructChange<T>;
85
+ /** GOSSIP / PROTOCOL */
86
+ delta: OOStructDelta<T>;
87
+ ack: OOStructAck<T>;
88
+ };
89
+ /**
90
+ * Represents a strongly typed OO-Struct event listener.
91
+ */
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;
94
+ };
95
+ /**
96
+ * Resolves an event name to its corresponding listener type.
97
+ */
98
+ type OOStructEventListenerFor<T extends Record<string, unknown>, K extends string> = K extends keyof OOStructEventMap<T> ? OOStructEventListener<T, K> : EventListenerOrEventListenerObject;
99
+
100
+ /**
101
+ * Represents an observed-overwrite struct replica.
102
+ *
103
+ * The struct shape is fixed by the provided default values.
104
+ */
105
+ declare class OOStruct<T extends Record<string, unknown>> {
106
+ private readonly __eventTarget;
107
+ private readonly __defaults;
108
+ private readonly __state;
109
+ private __live;
110
+ /**
111
+ * Creates a replica from default values and an optional snapshot.
112
+ *
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.
123
+ *
124
+ * @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.
145
+ */
146
+ update<K extends keyof T>(key: K, value: T[K]): void;
147
+ /**
148
+ * Resets one field or the entire struct back to default values.
149
+ *
150
+ * @param key - The optional field key to reset. When omitted, every field is reset.
151
+ */
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;
160
+ /**
161
+ * Emits the current acknowledgement frontier for each field.
162
+ */
163
+ acknowledge<K extends Extract<keyof T, string>>(): void;
164
+ /**
165
+ * Removes overwritten identifiers that every provided frontier has acknowledged.
166
+ *
167
+ * @param frontiers - A collection of acknowledgement frontiers to compact against.
168
+ */
169
+ garbageCollect<K extends Extract<keyof T, string>>(frontiers: Array<OOStructAck<T>>): void;
170
+ /**
171
+ * Emits a serialized snapshot of the current replica state.
172
+ */
173
+ snapshot(): void;
174
+ /**ADDITIONAL*/
175
+ /**
176
+ * Returns the struct field keys.
177
+ *
178
+ * @returns The field keys in the current replica.
179
+ */
180
+ keys<K extends keyof T>(): Array<K>;
181
+ /**
182
+ * Returns cloned copies of the current field values.
183
+ *
184
+ * @returns The current field values.
185
+ */
186
+ values<K extends keyof T>(): Array<T[K]>;
187
+ /**
188
+ * Returns cloned key-value pairs for the current replica state.
189
+ *
190
+ * @returns The current field entries.
191
+ */
192
+ entries<K extends keyof T>(): Array<[K, T[K]]>;
193
+ /**EVENTS*/
194
+ /**
195
+ * Registers an event listener.
196
+ *
197
+ * @param type - The event type to listen for.
198
+ * @param listener - The listener to register.
199
+ * @param options - Listener registration options.
200
+ */
201
+ addEventListener<K extends keyof OOStructEventMap<T>>(type: K, listener: OOStructEventListenerFor<T, K> | null, options?: boolean | AddEventListenerOptions): void;
202
+ /**
203
+ * Removes an event listener.
204
+ *
205
+ * @param type - The event type to stop listening for.
206
+ * @param listener - The listener to remove.
207
+ * @param options - Listener removal options.
208
+ */
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;
219
+ }
220
+
221
+ /**
222
+ * Error codes thrown by {@link OOStruct}.
223
+ */
224
+ type OOStructErrorCode = 'DEFAULTS_NOT_CLONEABLE' | 'VALUE_NOT_CLONEABLE' | 'VALUE_TYPE_MISMATCH';
225
+
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 };
package/dist/index.js ADDED
@@ -0,0 +1,409 @@
1
+ /*
2
+ * Copyright 2026 Sovereignbase
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+
18
+ // src/OOStruct/class.ts
19
+ import { v7 as uuidv7 } from "uuid";
20
+
21
+ // src/.errors/class.ts
22
+ var OOStructError = class extends Error {
23
+ /**
24
+ * The semantic error code for the failure.
25
+ */
26
+ code;
27
+ /**
28
+ * Creates a typed OO-Struct error.
29
+ *
30
+ * @param code - The semantic error code.
31
+ * @param message - An optional human-readable detail message.
32
+ */
33
+ constructor(code, message) {
34
+ const detail = message ?? code;
35
+ super(`{@sovereignbase/observed-overwrite-struct} ${detail}`);
36
+ this.code = code;
37
+ this.name = "OOStructError";
38
+ }
39
+ };
40
+
41
+ // src/OOStruct/parseSnapshotEntryToStateEntry/index.ts
42
+ import { isUuidV7, prototype, safeStructuredClone } from "@sovereignbase/utils";
43
+ function parseSnapshotEntryToStateEntry(defaultValue, snapshotEntry) {
44
+ if (prototype(snapshotEntry) !== "record" || !Object.hasOwn(snapshotEntry, "__value") || !isUuidV7(snapshotEntry.__uuidv7) || !isUuidV7(snapshotEntry.__after) || !Array.isArray(snapshotEntry.__overwrites))
45
+ return false;
46
+ const [cloned, copiedValue] = safeStructuredClone(snapshotEntry.__value);
47
+ if (!cloned || prototype(copiedValue) !== prototype(defaultValue))
48
+ return false;
49
+ const overwrites = /* @__PURE__ */ new Set([]);
50
+ for (const overwrite of snapshotEntry.__overwrites) {
51
+ if (!isUuidV7(overwrite) || overwrite === snapshotEntry.__uuidv7)
52
+ continue;
53
+ overwrites.add(overwrite);
54
+ }
55
+ if (!overwrites.has(snapshotEntry.__after)) return false;
56
+ return {
57
+ __uuidv7: snapshotEntry.__uuidv7,
58
+ __value: copiedValue,
59
+ __after: snapshotEntry.__after,
60
+ __overwrites: overwrites
61
+ };
62
+ }
63
+
64
+ // src/OOStruct/parseStateEntryToSnapshotEntry/index.ts
65
+ function parseStateEntryToSnapshotEntry(stateEntry) {
66
+ return {
67
+ __uuidv7: stateEntry.__uuidv7,
68
+ __value: structuredClone(stateEntry.__value),
69
+ __after: stateEntry.__after,
70
+ __overwrites: Array.from(stateEntry.__overwrites)
71
+ };
72
+ }
73
+
74
+ // src/OOStruct/class.ts
75
+ import { isUuidV7 as isUuidV72, prototype as prototype2, safeStructuredClone as safeStructuredClone2 } from "@sovereignbase/utils";
76
+ var OOStruct = class _OOStruct {
77
+ __eventTarget = new EventTarget();
78
+ __defaults;
79
+ __state;
80
+ __live;
81
+ /**
82
+ * Creates a replica from default values and an optional snapshot.
83
+ *
84
+ * @param defaults - The default field values that define the struct shape.
85
+ * @param snapshot - An optional serialized snapshot used for hydration.
86
+ * @throws {OOStructError} Thrown when the default values are not supported by `structuredClone`.
87
+ */
88
+ constructor(defaults, snapshot) {
89
+ const [cloned, copiedDefaults] = safeStructuredClone2(defaults);
90
+ if (!cloned)
91
+ throw new OOStructError(
92
+ "DEFAULTS_NOT_CLONEABLE",
93
+ "Default values must be supported by structuredClone."
94
+ );
95
+ this.__defaults = copiedDefaults;
96
+ this.__state = {};
97
+ this.__live = {};
98
+ const snapshotIsObject = snapshot && prototype2(snapshot) === "record";
99
+ for (const key of Object.keys(this.__defaults)) {
100
+ const defaultValue = this.__defaults[key];
101
+ if (snapshotIsObject && Object.hasOwn(snapshot, key)) {
102
+ const valid = parseSnapshotEntryToStateEntry(
103
+ defaultValue,
104
+ snapshot[key]
105
+ );
106
+ if (valid) {
107
+ this.__live[key] = valid.__value;
108
+ this.__state[key] = valid;
109
+ continue;
110
+ }
111
+ }
112
+ this.__live[key] = defaultValue;
113
+ const root = uuidv7();
114
+ this.__state[key] = {
115
+ __uuidv7: uuidv7(),
116
+ __after: root,
117
+ __value: defaultValue,
118
+ __overwrites: /* @__PURE__ */ new Set([root])
119
+ };
120
+ }
121
+ }
122
+ /**CRUD*/
123
+ /**
124
+ * Creates a new replica.
125
+ *
126
+ * @param defaults - The default field values that define the struct shape.
127
+ * @param snapshot - An optional serialized snapshot used for hydration.
128
+ * @returns A new OO-Struct replica.
129
+ */
130
+ static create(defaults, snapshot) {
131
+ return new _OOStruct(defaults, snapshot);
132
+ }
133
+ /**
134
+ * Reads the current value of a field.
135
+ *
136
+ * @param key - The field key to read.
137
+ * @returns A cloned copy of the field's current value.
138
+ */
139
+ read(key) {
140
+ return structuredClone(this.__live[key]);
141
+ }
142
+ /**
143
+ * Overwrites a field with a new value.
144
+ *
145
+ * @param key - The field key to overwrite.
146
+ * @param value - The next value for the field.
147
+ * @throws {OOStructError} Thrown when the value is not supported by `structuredClone`.
148
+ * @throws {OOStructError} Thrown when the value runtime type does not match the default value runtime type.
149
+ */
150
+ update(key, value) {
151
+ const [cloned, copiedValue] = safeStructuredClone2(value);
152
+ if (!cloned)
153
+ throw new OOStructError(
154
+ "VALUE_NOT_CLONEABLE",
155
+ "Updated values must be supported by structuredClone."
156
+ );
157
+ if (prototype2(copiedValue) !== prototype2(this.__defaults[key]))
158
+ throw new OOStructError(
159
+ "VALUE_TYPE_MISMATCH",
160
+ "Updated value must match the default value runtime type."
161
+ );
162
+ const delta = {};
163
+ const change = {};
164
+ delta[key] = this.overwriteAndReturnSnapshotEntry(key, copiedValue);
165
+ change[key] = structuredClone(copiedValue);
166
+ this.__eventTarget.dispatchEvent(
167
+ new CustomEvent("delta", { detail: delta })
168
+ );
169
+ this.__eventTarget.dispatchEvent(
170
+ new CustomEvent("change", { detail: change })
171
+ );
172
+ }
173
+ /**
174
+ * Resets one field or the entire struct back to default values.
175
+ *
176
+ * @param key - The optional field key to reset. When omitted, every field is reset.
177
+ */
178
+ delete(key) {
179
+ const delta = {};
180
+ const change = {};
181
+ if (key !== void 0) {
182
+ if (!Object.hasOwn(this.__defaults, key)) return;
183
+ const value = this.__defaults[key];
184
+ delta[key] = this.overwriteAndReturnSnapshotEntry(key, value);
185
+ change[key] = structuredClone(value);
186
+ } else {
187
+ for (const [key2, value] of Object.entries(this.__defaults)) {
188
+ delta[key2] = this.overwriteAndReturnSnapshotEntry(
189
+ key2,
190
+ value
191
+ );
192
+ change[key2] = structuredClone(value);
193
+ }
194
+ }
195
+ this.__eventTarget.dispatchEvent(
196
+ new CustomEvent("delta", { detail: delta })
197
+ );
198
+ this.__eventTarget.dispatchEvent(
199
+ new CustomEvent("change", { detail: change })
200
+ );
201
+ }
202
+ /**MAGS*/
203
+ /**
204
+ * Merges an incoming delta into the current replica.
205
+ *
206
+ * @param replica - The incoming partial snapshot projection to merge.
207
+ */
208
+ merge(replica) {
209
+ if (!replica || typeof replica !== "object" || Array.isArray(replica))
210
+ return;
211
+ const delta = {};
212
+ const change = {};
213
+ let hasDelta = false;
214
+ let hasChange = false;
215
+ for (const [key, value] of Object.entries(replica)) {
216
+ if (!Object.hasOwn(this.__state, key)) continue;
217
+ const candidate = parseSnapshotEntryToStateEntry(
218
+ this.__defaults[key],
219
+ value
220
+ );
221
+ if (!candidate) continue;
222
+ const target = this.__state[key];
223
+ const current = { ...target };
224
+ let frontier = "";
225
+ for (const overwrite of target.__overwrites) {
226
+ if (frontier < overwrite) frontier = overwrite;
227
+ }
228
+ for (const overwrite of candidate.__overwrites) {
229
+ if (overwrite <= frontier || target.__overwrites.has(overwrite))
230
+ continue;
231
+ target.__overwrites.add(overwrite);
232
+ }
233
+ if (target.__overwrites.has(candidate.__uuidv7)) continue;
234
+ if (current.__uuidv7 === candidate.__uuidv7) {
235
+ if (current.__after < candidate.__after) {
236
+ target.__value = candidate.__value;
237
+ target.__after = candidate.__after;
238
+ target.__overwrites.add(candidate.__after);
239
+ this.__live[key] = candidate.__value;
240
+ change[key] = structuredClone(candidate.__value);
241
+ hasChange = true;
242
+ } else {
243
+ delta[key] = this.overwriteAndReturnSnapshotEntry(
244
+ key,
245
+ current.__value
246
+ );
247
+ hasDelta = true;
248
+ }
249
+ continue;
250
+ }
251
+ if (current.__uuidv7 === candidate.__after || target.__overwrites.has(current.__uuidv7) || candidate.__uuidv7 > current.__uuidv7) {
252
+ target.__uuidv7 = candidate.__uuidv7;
253
+ target.__value = candidate.__value;
254
+ target.__after = candidate.__after;
255
+ target.__overwrites.add(candidate.__after);
256
+ target.__overwrites.add(current.__uuidv7);
257
+ this.__live[key] = candidate.__value;
258
+ change[key] = structuredClone(candidate.__value);
259
+ hasChange = true;
260
+ continue;
261
+ }
262
+ target.__overwrites.add(candidate.__uuidv7);
263
+ delta[key] = parseStateEntryToSnapshotEntry(target);
264
+ hasDelta = true;
265
+ }
266
+ if (hasDelta)
267
+ this.__eventTarget.dispatchEvent(
268
+ new CustomEvent("delta", { detail: delta })
269
+ );
270
+ if (hasChange)
271
+ this.__eventTarget.dispatchEvent(
272
+ new CustomEvent("change", { detail: change })
273
+ );
274
+ }
275
+ /**
276
+ * Emits the current acknowledgement frontier for each field.
277
+ */
278
+ acknowledge() {
279
+ const ack = {};
280
+ for (const [key, value] of Object.entries(this.__state)) {
281
+ let max = "";
282
+ for (const overwrite of value.__overwrites) {
283
+ if (max < overwrite) max = overwrite;
284
+ }
285
+ ack[key] = max;
286
+ }
287
+ this.__eventTarget.dispatchEvent(new CustomEvent("ack", { detail: ack }));
288
+ }
289
+ /**
290
+ * Removes overwritten identifiers that every provided frontier has acknowledged.
291
+ *
292
+ * @param frontiers - A collection of acknowledgement frontiers to compact against.
293
+ */
294
+ garbageCollect(frontiers) {
295
+ if (!Array.isArray(frontiers) || frontiers.length < 1) return;
296
+ const smallestAcknowledgementsPerKey = {};
297
+ for (const frontier of frontiers) {
298
+ for (const [key, value] of Object.entries(frontier)) {
299
+ if (!Object.hasOwn(this.__state, key) || !isUuidV72(value)) continue;
300
+ const current = smallestAcknowledgementsPerKey[key];
301
+ if (typeof current === "string" && current <= value) continue;
302
+ smallestAcknowledgementsPerKey[key] = value;
303
+ }
304
+ }
305
+ for (const [key, value] of Object.entries(smallestAcknowledgementsPerKey)) {
306
+ const target = this.__state[key];
307
+ const smallest = value;
308
+ for (const uuidv72 of target.__overwrites) {
309
+ if (uuidv72 === target.__after || uuidv72 > smallest) continue;
310
+ target.__overwrites.delete(uuidv72);
311
+ }
312
+ }
313
+ }
314
+ /**
315
+ * Emits a serialized snapshot of the current replica state.
316
+ */
317
+ snapshot() {
318
+ const snapshot = {};
319
+ for (const [key, value] of Object.entries(this.__state)) {
320
+ snapshot[key] = parseStateEntryToSnapshotEntry(
321
+ value
322
+ );
323
+ }
324
+ this.__eventTarget.dispatchEvent(
325
+ new CustomEvent("snapshot", { detail: snapshot })
326
+ );
327
+ }
328
+ /**ADDITIONAL*/
329
+ /**
330
+ * Returns the struct field keys.
331
+ *
332
+ * @returns The field keys in the current replica.
333
+ */
334
+ keys() {
335
+ return Object.keys(this.__live);
336
+ }
337
+ /**
338
+ * Returns cloned copies of the current field values.
339
+ *
340
+ * @returns The current field values.
341
+ */
342
+ values() {
343
+ return Object.values(this.__live).map(
344
+ (value) => structuredClone(value)
345
+ );
346
+ }
347
+ /**
348
+ * Returns cloned key-value pairs for the current replica state.
349
+ *
350
+ * @returns The current field entries.
351
+ */
352
+ entries() {
353
+ return Object.entries(this.__live).map(([key, value]) => [
354
+ key,
355
+ structuredClone(value)
356
+ ]);
357
+ }
358
+ /**EVENTS*/
359
+ /**
360
+ * Registers an event listener.
361
+ *
362
+ * @param type - The event type to listen for.
363
+ * @param listener - The listener to register.
364
+ * @param options - Listener registration options.
365
+ */
366
+ addEventListener(type, listener, options) {
367
+ this.__eventTarget.addEventListener(
368
+ type,
369
+ listener,
370
+ options
371
+ );
372
+ }
373
+ /**
374
+ * Removes an event listener.
375
+ *
376
+ * @param type - The event type to stop listening for.
377
+ * @param listener - The listener to remove.
378
+ * @param options - Listener removal options.
379
+ */
380
+ removeEventListener(type, listener, options) {
381
+ this.__eventTarget.removeEventListener(
382
+ type,
383
+ listener,
384
+ options
385
+ );
386
+ }
387
+ /**HELPERS*/
388
+ /**
389
+ * Overwrites a field and returns the serialized delta entry for that overwrite.
390
+ *
391
+ * @param key - The field key to overwrite.
392
+ * @param value - The next value for the field.
393
+ * @returns The serialized snapshot entry for the new winning value.
394
+ */
395
+ overwriteAndReturnSnapshotEntry(key, value) {
396
+ const target = this.__state[key];
397
+ const old = { ...target };
398
+ target.__uuidv7 = uuidv7();
399
+ target.__value = value;
400
+ target.__after = old.__uuidv7;
401
+ target.__overwrites.add(old.__uuidv7);
402
+ this.__live[key] = value;
403
+ return parseStateEntryToSnapshotEntry(target);
404
+ }
405
+ };
406
+ export {
407
+ OOStruct
408
+ };
409
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/OOStruct/class.ts","../src/.errors/class.ts","../src/OOStruct/parseSnapshotEntryToStateEntry/index.ts","../src/OOStruct/parseStateEntryToSnapshotEntry/index.ts"],"sourcesContent":["import { v7 as uuidv7 } from 'uuid'\nimport type {\n OOStructChange,\n OOStructDelta,\n OOStructEventListenerFor,\n OOStructEventMap,\n OOStructSnapshot,\n OOStructSnapshotEntry,\n OOStructState,\n OOStructStateEntry,\n OOStructAck,\n} from '../.types/index.js'\nimport { OOStructError } from '../.errors/class.js'\nimport { parseSnapshotEntryToStateEntry } from './parseSnapshotEntryToStateEntry/index.js'\nimport { parseStateEntryToSnapshotEntry } from './parseStateEntryToSnapshotEntry/index.js'\nimport { isUuidV7, prototype, safeStructuredClone } from '@sovereignbase/utils'\n\n/**\n * Represents an observed-overwrite struct replica.\n *\n * The struct shape is fixed by the provided default values.\n */\nexport class OOStruct<T extends Record<string, unknown>> {\n private readonly __eventTarget = new EventTarget()\n private readonly __defaults: T\n private readonly __state: OOStructState<T>\n private __live: T\n\n /**\n * Creates a replica from default values and an optional snapshot.\n *\n * @param defaults - The default field values that define the struct shape.\n * @param snapshot - An optional serialized snapshot used for hydration.\n * @throws {OOStructError} Thrown when the default values are not supported by `structuredClone`.\n */\n constructor(\n defaults: { [K in keyof T]: T[K] },\n snapshot?: OOStructSnapshot<T>\n ) {\n const [cloned, copiedDefaults] = safeStructuredClone(defaults)\n if (!cloned)\n throw new OOStructError(\n 'DEFAULTS_NOT_CLONEABLE',\n 'Default values must be supported by structuredClone.'\n )\n this.__defaults = copiedDefaults\n this.__state = {} as OOStructState<T>\n this.__live = {} as T\n\n const snapshotIsObject = snapshot && prototype(snapshot) === 'record'\n\n for (const key of Object.keys(this.__defaults)) {\n const defaultValue = this.__defaults[key as keyof T]\n if (snapshotIsObject && Object.hasOwn(snapshot, key)) {\n const valid = parseSnapshotEntryToStateEntry(\n defaultValue,\n snapshot[key as keyof T]\n )\n if (valid) {\n this.__live[key as keyof T] = valid.__value\n this.__state[key as keyof T] = valid\n continue\n }\n }\n this.__live[key as keyof T] = defaultValue\n const root = uuidv7()\n this.__state[key as keyof T] = {\n __uuidv7: uuidv7(),\n __after: root,\n __value: defaultValue,\n __overwrites: new Set([root]),\n }\n }\n }\n\n /**CRUD*/\n /**\n * Creates a new replica.\n *\n * @param defaults - The default field values that define the struct shape.\n * @param snapshot - An optional serialized snapshot used for hydration.\n * @returns A new OO-Struct replica.\n */\n static create<T extends Record<string, unknown>>(\n defaults: { [K in keyof T]: T[K] },\n snapshot?: OOStructSnapshot<T>\n ): OOStruct<T> {\n return new OOStruct(defaults, snapshot)\n }\n\n /**\n * Reads the current value of a field.\n *\n * @param key - The field key to read.\n * @returns A cloned copy of the field's current value.\n */\n read<K extends keyof T>(key: K): T[K] {\n return structuredClone(this.__live[key])\n }\n\n /**\n * Overwrites a field with a new value.\n *\n * @param key - The field key to overwrite.\n * @param value - The next value for the field.\n * @throws {OOStructError} Thrown when the value is not supported by `structuredClone`.\n * @throws {OOStructError} Thrown when the value runtime type does not match the default value runtime type.\n */\n update<K extends keyof T>(key: K, value: T[K]): void {\n const [cloned, copiedValue] = safeStructuredClone(value)\n if (!cloned)\n throw new OOStructError(\n 'VALUE_NOT_CLONEABLE',\n 'Updated values must be supported by structuredClone.'\n )\n\n if (prototype(copiedValue) !== prototype(this.__defaults[key]))\n throw new OOStructError(\n 'VALUE_TYPE_MISMATCH',\n 'Updated value must match the default value runtime type.'\n )\n const delta: OOStructDelta<T> = {}\n const change: OOStructChange<T> = {}\n delta[key] = this.overwriteAndReturnSnapshotEntry(key, copiedValue)\n change[key] = structuredClone(copiedValue)\n this.__eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n this.__eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\n }\n\n /**\n * Resets one field or the entire struct back to default values.\n *\n * @param key - The optional field key to reset. When omitted, every field is reset.\n */\n delete<K extends keyof T>(key?: K): void {\n const delta: OOStructDelta<T> = {}\n const change: OOStructChange<T> = {}\n\n if (key !== undefined) {\n if (!Object.hasOwn(this.__defaults, key)) return\n const value = this.__defaults[key]\n delta[key] = this.overwriteAndReturnSnapshotEntry(key, value)\n change[key] = structuredClone(value)\n } else {\n for (const [key, value] of Object.entries(this.__defaults)) {\n delta[key as K] = this.overwriteAndReturnSnapshotEntry(\n key as K,\n value as T[K]\n )\n change[key as K] = structuredClone(value as T[K])\n }\n }\n this.__eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n this.__eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\n }\n\n /**MAGS*/\n /**\n * Merges an incoming delta into the current replica.\n *\n * @param replica - The incoming partial snapshot projection to merge.\n */\n merge<K extends keyof T>(replica: OOStructDelta<T>): void {\n if (!replica || typeof replica !== 'object' || Array.isArray(replica))\n return\n\n const delta: OOStructDelta<T> = {}\n const change: OOStructChange<T> = {}\n let hasDelta = false\n let hasChange = false\n\n for (const [key, value] of Object.entries(replica)) {\n if (!Object.hasOwn(this.__state, key)) continue\n\n const candidate = parseSnapshotEntryToStateEntry(\n this.__defaults[key as K],\n value as OOStructSnapshotEntry<T[K]>\n )\n if (!candidate) continue\n\n const target = this.__state[key as K]\n const current = { ...target }\n let frontier = ''\n for (const overwrite of target.__overwrites) {\n if (frontier < overwrite) frontier = overwrite\n }\n\n for (const overwrite of candidate.__overwrites) {\n if (overwrite <= frontier || target.__overwrites.has(overwrite))\n continue\n target.__overwrites.add(overwrite)\n }\n\n if (target.__overwrites.has(candidate.__uuidv7)) continue\n\n if (current.__uuidv7 === candidate.__uuidv7) {\n if (current.__after < candidate.__after) {\n target.__value = candidate.__value\n target.__after = candidate.__after\n target.__overwrites.add(candidate.__after)\n this.__live[key as K] = candidate.__value\n change[key as K] = structuredClone(candidate.__value)\n hasChange = true\n } else {\n delta[key as K] = this.overwriteAndReturnSnapshotEntry(\n key as K,\n current.__value\n )\n hasDelta = true\n }\n continue\n }\n\n if (\n current.__uuidv7 === candidate.__after ||\n target.__overwrites.has(current.__uuidv7) ||\n candidate.__uuidv7 > current.__uuidv7\n ) {\n target.__uuidv7 = candidate.__uuidv7\n target.__value = candidate.__value\n target.__after = candidate.__after\n target.__overwrites.add(candidate.__after)\n target.__overwrites.add(current.__uuidv7)\n this.__live[key as K] = candidate.__value\n change[key as K] = structuredClone(candidate.__value)\n hasChange = true\n continue\n }\n\n target.__overwrites.add(candidate.__uuidv7)\n delta[key as K] = parseStateEntryToSnapshotEntry(target)\n hasDelta = true\n }\n if (hasDelta)\n this.__eventTarget.dispatchEvent(\n new CustomEvent('delta', { detail: delta })\n )\n if (hasChange)\n this.__eventTarget.dispatchEvent(\n new CustomEvent('change', { detail: change })\n )\n }\n\n /**\n * Emits the current acknowledgement frontier for each field.\n */\n acknowledge<K extends Extract<keyof T, string>>(): void {\n const ack: OOStructAck<T> = {}\n for (const [key, value] of Object.entries(this.__state)) {\n let max = ''\n for (const overwrite of (value as OOStructStateEntry<T[K]>)\n .__overwrites) {\n if (max < overwrite) max = overwrite\n }\n ack[key as K] = max\n }\n this.__eventTarget.dispatchEvent(new CustomEvent('ack', { detail: ack }))\n }\n\n /**\n * Removes overwritten identifiers that every provided frontier has acknowledged.\n *\n * @param frontiers - A collection of acknowledgement frontiers to compact against.\n */\n garbageCollect<K extends Extract<keyof T, string>>(\n frontiers: Array<OOStructAck<T>>\n ): void {\n if (!Array.isArray(frontiers) || frontiers.length < 1) return\n const smallestAcknowledgementsPerKey: OOStructAck<T> = {}\n\n for (const frontier of frontiers) {\n for (const [key, value] of Object.entries(frontier)) {\n if (!Object.hasOwn(this.__state, key) || !isUuidV7(value)) continue\n\n const current = smallestAcknowledgementsPerKey[key as K]\n if (typeof current === 'string' && current <= value) continue\n smallestAcknowledgementsPerKey[key as K] = value\n }\n }\n\n for (const [key, value] of Object.entries(smallestAcknowledgementsPerKey)) {\n const target = this.__state[key]\n const smallest = value as string\n for (const uuidv7 of target.__overwrites) {\n if (uuidv7 === target.__after || uuidv7 > smallest) continue\n target.__overwrites.delete(uuidv7)\n }\n }\n }\n\n /**\n * Emits a serialized snapshot of the current replica state.\n */\n snapshot(): void {\n const snapshot = {} as OOStructSnapshot<T>\n\n for (const [key, value] of Object.entries(this.__state)) {\n snapshot[key as keyof T] = parseStateEntryToSnapshotEntry(\n value as OOStructStateEntry<T[keyof T]>\n )\n }\n\n this.__eventTarget.dispatchEvent(\n new CustomEvent('snapshot', { detail: snapshot })\n )\n }\n\n /**ADDITIONAL*/\n\n /**\n * Returns the struct field keys.\n *\n * @returns The field keys in the current replica.\n */\n keys<K extends keyof T>(): Array<K> {\n return Object.keys(this.__live) as Array<K>\n }\n\n /**\n * Returns cloned copies of the current field values.\n *\n * @returns The current field values.\n */\n values<K extends keyof T>(): Array<T[K]> {\n return Object.values(this.__live).map((value) =>\n structuredClone(value)\n ) as Array<T[K]>\n }\n\n /**\n * Returns cloned key-value pairs for the current replica state.\n *\n * @returns The current field entries.\n */\n entries<K extends keyof T>(): Array<[K, T[K]]> {\n return Object.entries(this.__live).map(([key, value]) => [\n key as K,\n structuredClone(value as T[K]),\n ])\n }\n\n /**EVENTS*/\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 OOStructEventMap<T>>(\n type: K,\n listener: OOStructEventListenerFor<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 OOStructEventMap<T>>(\n type: K,\n listener: OOStructEventListenerFor<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 /**HELPERS*/\n\n /**\n * Overwrites a field and returns the serialized delta entry for that overwrite.\n *\n * @param key - The field key to overwrite.\n * @param value - The next value for the field.\n * @returns The serialized snapshot entry for the new winning value.\n */\n private overwriteAndReturnSnapshotEntry<K extends keyof T>(\n key: K,\n value: T[K]\n ): OOStructSnapshotEntry<T[K]> {\n const target = this.__state[key]\n const old = { ...target }\n target.__uuidv7 = uuidv7()\n target.__value = value\n target.__after = old.__uuidv7\n target.__overwrites.add(old.__uuidv7)\n this.__live[key] = value\n return parseStateEntryToSnapshotEntry(target)\n }\n}\n","/**\n * Error codes thrown by {@link OOStruct}.\n */\nexport type OOStructErrorCode =\n | 'DEFAULTS_NOT_CLONEABLE'\n | 'VALUE_NOT_CLONEABLE'\n | 'VALUE_TYPE_MISMATCH'\n\n/**\n * Represents a typed OO-Struct runtime error.\n */\nexport class OOStructError extends Error {\n /**\n * The semantic error code for the failure.\n */\n readonly code: OOStructErrorCode\n\n /**\n * Creates a typed OO-Struct error.\n *\n * @param code - The semantic error code.\n * @param message - An optional human-readable detail message.\n */\n constructor(code: OOStructErrorCode, message?: string) {\n const detail = message ?? code\n super(`{@sovereignbase/observed-overwrite-struct} ${detail}`)\n this.code = code\n this.name = 'OOStructError'\n }\n}\n","import type {\n OOStructSnapshotEntry,\n OOStructStateEntry,\n} from '../../.types/index.js'\nimport { isUuidV7, prototype, safeStructuredClone } from '@sovereignbase/utils'\n\n/**\n * Validates and converts a serialized field entry into internal replica state.\n *\n * Invalid entries are rejected by returning `false`.\n *\n * @param defaultValue - The default value for the field used for runtime type comparison.\n * @param snapshotEntry - The serialized entry to validate and parse.\n * @returns The parsed state entry, or `false` when the entry is invalid.\n */\nexport function parseSnapshotEntryToStateEntry<V>(\n defaultValue: V,\n snapshotEntry: OOStructSnapshotEntry<V>\n): OOStructStateEntry<V> | false {\n if (\n prototype(snapshotEntry) !== 'record' ||\n !Object.hasOwn(snapshotEntry, '__value') ||\n !isUuidV7(snapshotEntry.__uuidv7) ||\n !isUuidV7(snapshotEntry.__after) ||\n !Array.isArray(snapshotEntry.__overwrites)\n )\n return false\n\n const [cloned, copiedValue] = safeStructuredClone(snapshotEntry.__value)\n if (!cloned || prototype(copiedValue) !== prototype(defaultValue))\n return false\n\n const overwrites = new Set<string>([])\n for (const overwrite of snapshotEntry.__overwrites) {\n if (\n !isUuidV7(overwrite) ||\n overwrite ===\n snapshotEntry.__uuidv7 /**if it was actually overwritten the current uuid would be different so this must be malicious*/\n )\n continue\n overwrites.add(overwrite)\n }\n\n if (!overwrites.has(snapshotEntry.__after)) return false\n\n return {\n __uuidv7: snapshotEntry.__uuidv7,\n __value: copiedValue,\n __after: snapshotEntry.__after,\n __overwrites: overwrites,\n }\n}\n","import type {\n OOStructStateEntry,\n OOStructSnapshotEntry,\n} from '../../.types/index.js'\n\n/**\n * Serializes a field state entry into a snapshot entry.\n *\n * @param stateEntry - The internal state entry to serialize.\n * @returns The serialized snapshot entry.\n */\nexport function parseStateEntryToSnapshotEntry<K>(\n stateEntry: OOStructStateEntry<K>\n): OOStructSnapshotEntry<K> {\n return {\n __uuidv7: stateEntry.__uuidv7,\n __value: structuredClone(stateEntry.__value),\n __after: stateEntry.__after,\n __overwrites: Array.from(stateEntry.__overwrites),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA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