@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.
package/dist/index.cjs ADDED
@@ -0,0 +1,409 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _class;/*
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
+ var _uuid = require('uuid');
20
+
21
+ // src/.errors/class.ts
22
+ var OOStructError = class extends Error {
23
+ /**
24
+ * The semantic error code for the failure.
25
+ */
26
+
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 = _nullishCoalesce(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
+ var _utils = require('@sovereignbase/utils');
43
+ function parseSnapshotEntryToStateEntry(defaultValue, snapshotEntry) {
44
+ if (_utils.prototype.call(void 0, snapshotEntry) !== "record" || !Object.hasOwn(snapshotEntry, "__value") || !_utils.isUuidV7.call(void 0, snapshotEntry.__uuidv7) || !_utils.isUuidV7.call(void 0, snapshotEntry.__after) || !Array.isArray(snapshotEntry.__overwrites))
45
+ return false;
46
+ const [cloned, copiedValue] = _utils.safeStructuredClone.call(void 0, snapshotEntry.__value);
47
+ if (!cloned || _utils.prototype.call(void 0, copiedValue) !== _utils.prototype.call(void 0, defaultValue))
48
+ return false;
49
+ const overwrites = /* @__PURE__ */ new Set([]);
50
+ for (const overwrite of snapshotEntry.__overwrites) {
51
+ if (!_utils.isUuidV7.call(void 0, 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
+
76
+ var OOStruct = (_class = class _OOStruct {
77
+ __init() {this.__eventTarget = new EventTarget()}
78
+
79
+
80
+
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) {;_class.prototype.__init.call(this);
89
+ const [cloned, copiedDefaults] = _utils.safeStructuredClone.call(void 0, 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 && _utils.prototype.call(void 0, 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 = _uuid.v7.call(void 0, );
114
+ this.__state[key] = {
115
+ __uuidv7: _uuid.v7.call(void 0, ),
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] = _utils.safeStructuredClone.call(void 0, value);
152
+ if (!cloned)
153
+ throw new OOStructError(
154
+ "VALUE_NOT_CLONEABLE",
155
+ "Updated values must be supported by structuredClone."
156
+ );
157
+ if (_utils.prototype.call(void 0, copiedValue) !== _utils.prototype.call(void 0, 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) || !_utils.isUuidV7.call(void 0, 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 = _uuid.v7.call(void 0, );
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
+ }, _class);
406
+
407
+
408
+ exports.OOStruct = OOStruct;
409
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["c:\\Users\\jorts\\convergent-replicated-struct\\dist\\index.cjs"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,4BAAmC;AACnC;AACA;AACA,IAAI,cAAc,EAAE,MAAM,QAAQ,MAAM;AACxC;AACA;AACA;AACA,EAAE;AACF;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE;AAC7B,IAAI,MAAM,OAAO,mBAAE,OAAQ,UAAG,MAAI;AAClC,IAAI,KAAK,CAAC,CAAC,2CAA2C,EAAE,MAAM,CAAC,CAAA;AACA,IAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,EAAA;AACA,EAAA;AACA,IAAA;AACA,EAAA;AACA,EAAA;AACA,IAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA,iBAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,UAAA;AACA,UAAA;AACA,QAAA;AACA,QAAA;AACA,UAAA;AACA,UAAA;AACA,UAAA;AACA,QAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,UAAA;AACA,UAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,UAAA;AACA,QAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,UAAA;AACA,UAAA;AACA,UAAA;AACA,UAAA;AACA,UAAA;AACA,UAAA;AACA,QAAA;AACA,UAAA;AACA,YAAA;AACA,YAAA;AACA,UAAA;AACA,UAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,MAAA;AACA,EAAA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,MAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,IAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,MAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,MAAA;AACA,MAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA","file":"C:\\Users\\jorts\\convergent-replicated-struct\\dist\\index.cjs","sourcesContent":[null]}
@@ -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 };