doxum 0.1.22 → 0.1.23

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.
Files changed (70) hide show
  1. package/README.md +14 -5
  2. package/dist/{advanced-CqrR_4dh.d.ts → advanced-CLJCWkHa.d.cts} +2 -2
  3. package/dist/{advanced-BTStm2cW.js → advanced-CRNHfJBu.js} +457 -425
  4. package/dist/advanced-CRNHfJBu.js.map +1 -0
  5. package/dist/{advanced-GKZ-CZ6a.cjs → advanced-Cle5g3B4.cjs} +590 -438
  6. package/dist/advanced-Cle5g3B4.cjs.map +1 -0
  7. package/dist/{advanced-CyR5o3Xr.d.cts → advanced-d6J61er9.d.ts} +2 -2
  8. package/dist/advanced.cjs +1 -1
  9. package/dist/advanced.d.cts +1 -1
  10. package/dist/advanced.d.ts +1 -1
  11. package/dist/advanced.js +1 -1
  12. package/dist/context-BcLKGfq8.cjs +149 -0
  13. package/dist/context-BcLKGfq8.cjs.map +1 -0
  14. package/dist/context-DFEaDUv5.js +84 -0
  15. package/dist/context-DFEaDUv5.js.map +1 -0
  16. package/dist/{contract-CxiNcqMn.d.ts → contract-SYJFP0lM.d.cts} +19 -75
  17. package/dist/{contract-CRrZ-3sF.d.cts → contract-ULPQmiEs.d.ts} +19 -75
  18. package/dist/driver-BD3DQOW6.cjs +382 -0
  19. package/dist/driver-BD3DQOW6.cjs.map +1 -0
  20. package/dist/{driver-BqRjwFmU.js → driver-yFfvDMX5.js} +155 -26
  21. package/dist/driver-yFfvDMX5.js.map +1 -0
  22. package/dist/index.cjs +2130 -529
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +6 -5
  25. package/dist/index.d.ts +6 -5
  26. package/dist/index.js +2003 -403
  27. package/dist/index.js.map +1 -1
  28. package/dist/local-sync.cjs +5 -5
  29. package/dist/local-sync.cjs.map +1 -1
  30. package/dist/local-sync.d.cts +1 -1
  31. package/dist/local-sync.d.ts +1 -1
  32. package/dist/local-sync.js +2 -2
  33. package/dist/local-sync.js.map +1 -1
  34. package/dist/react.cjs +2 -70
  35. package/dist/react.cjs.map +1 -1
  36. package/dist/react.d.cts +0 -1
  37. package/dist/react.d.ts +1 -2
  38. package/dist/react.js +3 -71
  39. package/dist/react.js.map +1 -1
  40. package/package.json +1 -6
  41. package/skills/doxum-runtime/SKILL.md +2 -1
  42. package/skills/doxum-runtime/references/guide.en.md +8 -6
  43. package/skills/doxum-runtime/references/guide.zh-CN.md +7 -5
  44. package/skills/doxum-runtime/references/invariants.en.md +5 -3
  45. package/skills/doxum-runtime/references/invariants.zh-CN.md +4 -3
  46. package/dist/access-CZXv_DIF.js +0 -958
  47. package/dist/access-CZXv_DIF.js.map +0 -1
  48. package/dist/access-D64MFRSm.cjs +0 -1101
  49. package/dist/access-D64MFRSm.cjs.map +0 -1
  50. package/dist/advanced-BTStm2cW.js.map +0 -1
  51. package/dist/advanced-GKZ-CZ6a.cjs.map +0 -1
  52. package/dist/driver-BC06FEMF.cjs +0 -241
  53. package/dist/driver-BC06FEMF.cjs.map +0 -1
  54. package/dist/driver-BqRjwFmU.js.map +0 -1
  55. package/dist/integration-BqDw7e2w.cjs +0 -30
  56. package/dist/integration-BqDw7e2w.cjs.map +0 -1
  57. package/dist/integration-CSXaF8HP.js +0 -25
  58. package/dist/integration-CSXaF8HP.js.map +0 -1
  59. package/dist/integration.cjs +0 -13
  60. package/dist/integration.d.cts +0 -17
  61. package/dist/integration.d.ts +0 -17
  62. package/dist/integration.js +0 -5
  63. package/dist/issue-9U6YLYz3.js +0 -647
  64. package/dist/issue-9U6YLYz3.js.map +0 -1
  65. package/dist/issue-Bo0FIO3b.cjs +0 -886
  66. package/dist/issue-Bo0FIO3b.cjs.map +0 -1
  67. package/dist/notification-CrZHVx3a.js +0 -176
  68. package/dist/notification-CrZHVx3a.js.map +0 -1
  69. package/dist/notification-DFoA9bQY.cjs +0 -235
  70. package/dist/notification-DFoA9bQY.cjs.map +0 -1
@@ -1,5 +1,404 @@
1
- import { B as profile } from "./issue-9U6YLYz3.js";
2
- import { _ as checkKey, t as accessOf } from "./access-CZXv_DIF.js";
1
+ import { c as validate, d as isRecord, n as contextOf, u as isPlainObject } from "./context-DFEaDUv5.js";
2
+ const profile = {
3
+ projection: (key, amount = 1) => {},
4
+ recorder: (key, amount = 1) => {},
5
+ access: (key) => {},
6
+ copy: {
7
+ structure: () => {},
8
+ treeNode: () => {},
9
+ shallowRecord: (items) => {}
10
+ },
11
+ equality: {
12
+ call: () => {},
13
+ container: () => {}
14
+ },
15
+ address: {
16
+ listIndex: (items) => {},
17
+ schemaStep: () => {},
18
+ documentStep: () => {},
19
+ prefixComparison: () => {},
20
+ segmentCompared: () => {}
21
+ },
22
+ impact: {
23
+ index: () => {},
24
+ affects: () => {}
25
+ },
26
+ collectionIndex: {
27
+ node: () => {},
28
+ build: (items) => {}
29
+ },
30
+ collectionView: {
31
+ mapped: () => {},
32
+ idsScanned: (amount = 1) => {},
33
+ arrayCopied: () => {}
34
+ },
35
+ materialized: {
36
+ updated: () => {},
37
+ rebuilt: () => {},
38
+ notification: () => {}
39
+ }
40
+ };
41
+ //#endregion
42
+ //#region core/src/order/sequence.ts
43
+ const length = (keys) => keys.length;
44
+ const keyAt = (keys, index) => Array.isArray(keys) ? keys[index] : keys.at(index);
45
+ const equal = (left, right) => {
46
+ profile.equality.call();
47
+ if (left === right) return true;
48
+ profile.equality.container();
49
+ if (left.length !== right.length) return false;
50
+ for (let index = 0; index < left.length; index++) if (left[index] !== keyAt(right, index)) return false;
51
+ return true;
52
+ };
53
+ const keys = (values, keyOf) => ({
54
+ length: values.length,
55
+ at: (index) => index >= 0 && index < values.length ? keyOf(values[index]) : void 0,
56
+ index: (key) => {
57
+ for (let index = 0; index < values.length; index += 1) if (keyOf(values[index]) === key) return index;
58
+ return -1;
59
+ }
60
+ });
61
+ const listIndexes = /* @__PURE__ */ new WeakMap();
62
+ const knownKeys = (values) => {
63
+ let positions;
64
+ return {
65
+ length: values.length,
66
+ at: (index) => values[index],
67
+ index: (key) => {
68
+ if (!positions) {
69
+ positions = /* @__PURE__ */ new Map();
70
+ for (let index = 0; index < values.length; index++) positions.set(values[index], index);
71
+ profile.address.listIndex(values.length);
72
+ }
73
+ return positions.get(key) ?? -1;
74
+ }
75
+ };
76
+ };
77
+ const cacheKnownKeys = (values, keyOf, order) => {
78
+ listIndexes.set(values, {
79
+ keyOf,
80
+ order: knownKeys(order)
81
+ });
82
+ };
83
+ /** Canonical lists keep their key positions until a structural write invalidates them. */
84
+ const indexedKeys = (values, keyOf) => {
85
+ const cached = listIndexes.get(values);
86
+ if (cached?.keyOf === keyOf) return cached.order;
87
+ let positions;
88
+ const order = {
89
+ length: values.length,
90
+ at: (index) => index >= 0 && index < values.length ? keyOf(values[index]) : void 0,
91
+ index: (key) => {
92
+ if (!positions) {
93
+ positions = /* @__PURE__ */ new Map();
94
+ for (let index = 0; index < values.length; index++) positions.set(keyOf(values[index]), index);
95
+ profile.address.listIndex(values.length);
96
+ }
97
+ return positions.get(key) ?? -1;
98
+ }
99
+ };
100
+ listIndexes.set(values, {
101
+ keyOf,
102
+ order
103
+ });
104
+ return order;
105
+ };
106
+ const toArray = (keys) => {
107
+ const result = new Array(length(keys));
108
+ for (let index = 0; index < result.length; index++) result[index] = keyAt(keys, index);
109
+ return result;
110
+ };
111
+ /** One operation-local list scan provides both stable keys and item association. */
112
+ const listSequence = (values, keyOf) => {
113
+ const current = indexedKeys(values, keyOf);
114
+ const order = new Array(values.length);
115
+ const byKey = /* @__PURE__ */ new Map();
116
+ for (let index = 0; index < values.length; index++) {
117
+ const key = current.at(index);
118
+ order[index] = key;
119
+ byKey.set(key, values[index]);
120
+ }
121
+ cacheKnownKeys(values, keyOf, order);
122
+ return {
123
+ order,
124
+ values: byKey
125
+ };
126
+ };
127
+ /** Structural sequence writes own invalidation, including rollback writes. */
128
+ const insert = (values, index, value) => {
129
+ values.splice(index, 0, value);
130
+ listIndexes.delete(values);
131
+ };
132
+ const remove = (values, index) => {
133
+ values.splice(index, 1);
134
+ listIndexes.delete(values);
135
+ };
136
+ /** Installs an exact string-key sequence such as table ids in one structural write. */
137
+ const installKeys = (values, order) => {
138
+ values.length = order.length;
139
+ for (let index = 0; index < order.length; index++) values[index] = order[index];
140
+ };
141
+ /** Installs a pre-resolved list sequence without running keyOf again. */
142
+ const installList = (values, keyOf, sequence, order) => {
143
+ values.length = order.length;
144
+ for (let index = 0; index < order.length; index++) values[index] = sequence.values.get(order[index]);
145
+ cacheKnownKeys(values, keyOf, order);
146
+ };
147
+ const install = (values, keyOf, order) => {
148
+ installList(values, keyOf, listSequence(values, keyOf), order);
149
+ };
150
+ const matches = (order, members) => {
151
+ if (order.length !== members.length) return false;
152
+ const ids = new Set(order);
153
+ return ids.size === order.length && members.every((id) => ids.has(id));
154
+ };
155
+ //#endregion
156
+ //#region core/src/schema/layout.ts
157
+ const compiledObjects = /* @__PURE__ */ new WeakMap();
158
+ const compiledEntries = /* @__PURE__ */ new WeakMap();
159
+ const objectBranch = (node, value) => {
160
+ if (node.kind === "object") return node;
161
+ if (node.kind !== "variant" || typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
162
+ const record = value;
163
+ const tag = typeof record[node.tag] === "string" ? record[node.tag] : void 0;
164
+ return node.variants[String(tag ?? Object.keys(node.variants)[0] ?? "")];
165
+ };
166
+ const compiledShape = (node, value) => {
167
+ const branch = objectBranch(node, value);
168
+ if (!branch) return void 0;
169
+ const shape = branch.shape;
170
+ let layout = compiledObjects.get(shape);
171
+ if (!layout) {
172
+ const members = /* @__PURE__ */ new Map();
173
+ for (const key of Object.keys(shape).sort()) {
174
+ profile.address.schemaStep();
175
+ members.set(key, {
176
+ kind: "fixed",
177
+ key,
178
+ node: shape[key],
179
+ field: shape[key].kind === "field",
180
+ slot: members.size
181
+ });
182
+ }
183
+ layout = {
184
+ kind: "fixed",
185
+ members
186
+ };
187
+ compiledObjects.set(shape, layout);
188
+ }
189
+ return layout;
190
+ };
191
+ const compiledLayout = (node, value) => {
192
+ if (node.kind !== "map" && node.kind !== "table" && node.kind !== "list") return compiledShape(node, value);
193
+ let layout = compiledEntries.get(node);
194
+ if (!layout) {
195
+ layout = {
196
+ kind: "dynamic",
197
+ entry: {
198
+ kind: "entry",
199
+ node: node.value,
200
+ field: node.value.kind === "field"
201
+ }
202
+ };
203
+ compiledEntries.set(node, layout);
204
+ }
205
+ return layout;
206
+ };
207
+ //#endregion
208
+ //#region core/src/schema/value.ts
209
+ var ParseError = class extends TypeError {
210
+ issues;
211
+ constructor(issue) {
212
+ super(`${issue.message} at ${JSON.stringify(issue.address)}`);
213
+ this.name = "ParseError";
214
+ this.issues = Object.freeze([Object.freeze({
215
+ ...issue,
216
+ address: Object.freeze([...issue.address])
217
+ })]);
218
+ }
219
+ };
220
+ const issue = (address, message, code = "invalid-value") => ({
221
+ address,
222
+ message,
223
+ code
224
+ });
225
+ const checkScalar = (validator, value, address, strict) => {
226
+ if (!validator) return strict ? issue(address, "A validator is required to parse an opaque value.", "missing-validator") : void 0;
227
+ try {
228
+ const result = typeof validator === "function" ? validator(value) : validator["~standard"].validate(value);
229
+ if (result && (typeof result === "object" || typeof result === "function") && "then" in result && typeof result.then === "function") return issue(address, "Validators must be synchronous.");
230
+ if (typeof validator !== "function") {
231
+ if (!isRecord(result)) return issue(address, "Malformed validator result.");
232
+ if (Array.isArray(result.issues)) {
233
+ const first = result.issues[0];
234
+ return issue([...address, ...(first?.path ?? []).map((part) => String(typeof part === "object" ? part.key : part))], first?.message ?? "Value validation failed.");
235
+ }
236
+ if (!Object.hasOwn(result, "value")) return issue(address, "Malformed validator result.");
237
+ }
238
+ return;
239
+ } catch (error) {
240
+ return issue(address, error instanceof Error ? error.message : "Value validation failed.");
241
+ }
242
+ };
243
+ const checkKey = (validator, value, address) => {
244
+ if (typeof value !== "string") return issue(address, "Collection keys must be strings.", "invalid-key");
245
+ const failure = checkScalar(validator, value, address, false);
246
+ return failure ? {
247
+ ...failure,
248
+ code: "invalid-key"
249
+ } : void 0;
250
+ };
251
+ const checkValue = (node, value, address = [], strict = false) => {
252
+ if (node.optional && value === void 0) return void 0;
253
+ if (node.kind === "field") return checkScalar(node.validator, value, address, strict);
254
+ if (node.kind === "object" || node.kind === "variant") {
255
+ if (!isPlainObject(value)) return issue(address, "Expected an object.");
256
+ if (node.kind === "variant") {
257
+ const tag = value[node.tag];
258
+ if (!Object.hasOwn(value, node.tag) || typeof tag !== "string" || !Object.hasOwn(node.variants, tag)) return issue([...address, node.tag], "Unknown variant tag.");
259
+ }
260
+ const { members } = compiledShape(node, value);
261
+ for (const key of Reflect.ownKeys(value)) {
262
+ if (node.kind === "variant" && key === node.tag) continue;
263
+ if (typeof key !== "string" || !members.has(key)) return issue([...address, String(key)], "Property is not declared in the object schema.");
264
+ }
265
+ for (const [key, member] of members) {
266
+ const child = member.node;
267
+ if (!Object.prototype.hasOwnProperty.call(value, key) && !child.optional) return issue([...address, key], "Required property is missing.");
268
+ const failure = checkValue(child, value[key], [...address, key], strict);
269
+ if (failure) return failure;
270
+ }
271
+ return;
272
+ }
273
+ if (node.kind === "table" || node.kind === "map") {
274
+ if (!isPlainObject(value)) return issue(address, "Expected a collection object.");
275
+ const entries = node.kind === "table" ? value.byId : value;
276
+ if (!isPlainObject(entries)) return issue(address, "Expected a byId object.");
277
+ if (node.kind === "table") {
278
+ if (!Array.isArray(value.ids) || value.ids.length !== Object.keys(entries).length) return issue(address, "Table ids and entries must agree.");
279
+ const seen = /* @__PURE__ */ new Set();
280
+ for (const id of value.ids) {
281
+ const failure = checkKey(node.key, id, address);
282
+ if (failure) return failure;
283
+ if (seen.has(id) || !Object.prototype.hasOwnProperty.call(entries, id)) return issue(address, "Table ids must be unique and reference existing entries.");
284
+ seen.add(id);
285
+ }
286
+ }
287
+ for (const id of Object.keys(entries)) {
288
+ const failure = checkKey(node.key, id, [...address, id]) ?? checkValue(node.value, entries[id], [...address, id], strict);
289
+ if (failure) return failure;
290
+ }
291
+ return;
292
+ }
293
+ if (node.kind === "list") {
294
+ if (!Array.isArray(value)) return issue(address, "Expected a list.");
295
+ const seen = /* @__PURE__ */ new Set();
296
+ const order = keys(value, node.keyOf);
297
+ for (let i = 0; i < value.length; i++) {
298
+ const failure = checkValue(node.value, value[i], [...address, String(i)], strict);
299
+ if (failure) return failure;
300
+ let key;
301
+ try {
302
+ key = order.at(i);
303
+ } catch {
304
+ return issue(address, "List keyOf failed.");
305
+ }
306
+ if (typeof key !== "string" || seen.has(key)) return issue(address, "List keys must be unique strings.", "invalid-key");
307
+ seen.add(key);
308
+ }
309
+ return;
310
+ }
311
+ if (!validate(value)) return issue(address, "Invalid tree structure.", "invalid-tree");
312
+ for (const id of Object.keys(value.nodes)) {
313
+ const failure = checkTreePayload(node.value, value.nodes[id], [...address, id], strict);
314
+ if (failure) return failure;
315
+ }
316
+ };
317
+ const checkTreePayload = (node, value, address, strict = false) => {
318
+ if (!isRecord(value)) return issue(address, "Invalid tree node.", "invalid-tree");
319
+ const present = Object.prototype.hasOwnProperty.call(value, "value");
320
+ if (!present && !node.optional) return issue(address, "Tree node value is required.");
321
+ return present ? checkValue(node, value.value, address, strict) : void 0;
322
+ };
323
+ const copyTreeNode = (value) => {
324
+ profile.copy.treeNode();
325
+ return {
326
+ ...value,
327
+ children: [...value.children]
328
+ };
329
+ };
330
+ const equalTreeNode = (left, right) => {
331
+ if (left === right) return true;
332
+ if (!left || !right) return false;
333
+ return left.parentId === right.parentId && equal(left.children, right.children) && Object.hasOwn(left, "value") === Object.hasOwn(right, "value") && Object.is(left.value, right.value);
334
+ };
335
+ /** Copy editable structure; atomic payloads retain their immutable ownership contract. */
336
+ const copyValue = (node, value) => {
337
+ if (value === void 0 || node.kind === "field") return value;
338
+ profile.copy.structure();
339
+ if ((node.kind === "object" || node.kind === "variant") && isRecord(value)) {
340
+ const result = Object.create(Object.getPrototypeOf(value));
341
+ const { members } = compiledShape(node, value);
342
+ for (const key of Object.getOwnPropertyNames(value)) Object.defineProperty(result, key, {
343
+ value: node.kind === "variant" && key === node.tag ? value[key] : copyValue(members.get(key).node, value[key]),
344
+ writable: true,
345
+ enumerable: Object.getOwnPropertyDescriptor(value, key)?.enumerable,
346
+ configurable: true
347
+ });
348
+ return result;
349
+ }
350
+ if ((node.kind === "table" || node.kind === "map") && isRecord(value)) {
351
+ const entries = node.kind === "table" ? value.byId : value;
352
+ const result = Object.fromEntries(Object.keys(entries).map((id) => [id, copyValue(node.value, entries[id])]));
353
+ return node.kind === "table" ? {
354
+ ids: [...value.ids],
355
+ byId: result
356
+ } : result;
357
+ }
358
+ if (node.kind === "list") return [...value];
359
+ if (node.kind === "tree" && isRecord(value)) {
360
+ const entries = value.nodes;
361
+ return {
362
+ ...value,
363
+ nodes: Object.fromEntries(Object.entries(entries).map(([id, n]) => [id, copyTreeNode(n)]))
364
+ };
365
+ }
366
+ return value;
367
+ };
368
+ const equalValue = (node, left, right) => {
369
+ if (Object.is(left, right)) return true;
370
+ if (node.kind === "field" || left === void 0 || right === void 0) return false;
371
+ if ((node.kind === "object" || node.kind === "variant") && isRecord(left) && isRecord(right)) {
372
+ if (node.kind === "variant" && left[node.tag] !== right[node.tag]) return false;
373
+ for (const [key, member] of compiledShape(node, left).members) {
374
+ if (Object.hasOwn(left, key) !== Object.hasOwn(right, key)) return false;
375
+ if (!equalValue(member.node, left[key], right[key])) return false;
376
+ }
377
+ return true;
378
+ }
379
+ if (node.kind === "list" && Array.isArray(left) && Array.isArray(right)) return left.length === right.length && left.every((v, i) => equalValue(node.value, v, right[i]));
380
+ if (node.kind === "table" && isRecord(left) && isRecord(right)) return equal(left.ids, right.ids) && equalValue({
381
+ kind: "map",
382
+ value: node.value
383
+ }, left.byId, right.byId);
384
+ if (node.kind === "tree" && isRecord(left) && isRecord(right)) {
385
+ if (left.rootId !== right.rootId) return false;
386
+ const a = left.nodes, b = right.nodes;
387
+ return Object.keys(a).length === Object.keys(b).length && Object.keys(a).every((id) => Object.hasOwn(b, id) && equalTreeNode(a[id], b[id]));
388
+ }
389
+ if (node.kind === "map" && isRecord(left) && isRecord(right)) {
390
+ const keys = Object.keys(left);
391
+ return keys.length === Object.keys(right).length && keys.every((key) => Object.hasOwn(right, key) && equalValue(node.value, left[key], right[key]));
392
+ }
393
+ return false;
394
+ };
395
+ const parse = (node, input) => {
396
+ const valueNode = node;
397
+ const failure = checkValue(valueNode, input, [], true);
398
+ if (failure) throw new ParseError(failure);
399
+ return copyValue(valueNode, input);
400
+ };
401
+ //#endregion
3
402
  //#region core/src/schema.ts
4
403
  const collectionEntryNode = (node) => node.kind === "map" || node.kind === "table" || node.kind === "list" ? node.value : void 0;
5
404
  const node = (value) => Object.freeze(value);
@@ -103,6 +502,45 @@ const compilePath = (owner, kind, pick) => {
103
502
  });
104
503
  };
105
504
  //#endregion
505
+ //#region core/src/projection/collection/view.ts
506
+ const mapRead = (value) => ({
507
+ get: (key) => value.get(key),
508
+ has: (key) => value.has(key),
509
+ ids: () => Object.freeze([...value.keys()])
510
+ });
511
+ /** Immutable map-like view over a CollectionRead. */
512
+ const collectionView = (read) => {
513
+ const ids = read.ids();
514
+ const entries = function* () {
515
+ for (const key of ids) yield [key, read.get(key)];
516
+ };
517
+ const values = function* () {
518
+ for (const key of ids) yield read.get(key);
519
+ };
520
+ const view = {
521
+ get: (key) => read.get(key),
522
+ has: (key) => read.has(key),
523
+ get size() {
524
+ return ids.length;
525
+ },
526
+ keys: () => ids[Symbol.iterator](),
527
+ values,
528
+ entries,
529
+ forEach: (callback, thisArg) => {
530
+ for (const key of ids) callback.call(thisArg, read.get(key), key, view);
531
+ },
532
+ [Symbol.iterator]: entries
533
+ };
534
+ return Object.freeze(view);
535
+ };
536
+ /** Eager durable view used when a processor result may retain its dependency value. */
537
+ const snapshotCollectionView = (read) => {
538
+ const ids = read.ids();
539
+ const values = /* @__PURE__ */ new Map();
540
+ for (const key of ids) values.set(key, read.get(key));
541
+ return collectionView(mapRead(values));
542
+ };
543
+ //#endregion
106
544
  //#region core/src/projection/contract.ts
107
545
  var ProjectionError = class extends Error {
108
546
  constructor(phase, identity, revisions, cause) {
@@ -120,7 +558,7 @@ var ProjectionDisposedError = class extends Error {
120
558
  }
121
559
  };
122
560
  //#endregion
123
- //#region core/src/projection/scheduler.ts
561
+ //#region core/src/projection/graph/scheduler.ts
124
562
  const createScheduler = (onError) => {
125
563
  let disposed = false;
126
564
  let depth = 0;
@@ -456,408 +894,6 @@ const assertScope = (active) => {
456
894
  if (!active()) throw new Error("Projection reader or writer is no longer active.");
457
895
  };
458
896
  //#endregion
459
- //#region core/src/projection/collection-output.ts
460
- const height = (node) => node?.height ?? 0;
461
- const makeTreeNode = (key, value, left, right) => Object.freeze({
462
- key,
463
- value,
464
- left,
465
- right,
466
- height: Math.max(height(left), height(right)) + 1
467
- });
468
- const rotateLeft = (node) => {
469
- const right = node.right;
470
- return makeTreeNode(right.key, right.value, makeTreeNode(node.key, node.value, node.left, right.left), right.right);
471
- };
472
- const rotateRight = (node) => {
473
- const left = node.left;
474
- return makeTreeNode(left.key, left.value, left.left, makeTreeNode(node.key, node.value, left.right, node.right));
475
- };
476
- const rebalance = (node) => {
477
- const balance = height(node.left) - height(node.right);
478
- if (balance > 1) {
479
- if (height(node.left.left) < height(node.left.right)) return rotateRight(makeTreeNode(node.key, node.value, rotateLeft(node.left), node.right));
480
- return rotateRight(node);
481
- }
482
- if (balance < -1) {
483
- if (height(node.right.right) < height(node.right.left)) return rotateLeft(makeTreeNode(node.key, node.value, node.left, rotateRight(node.right)));
484
- return rotateLeft(node);
485
- }
486
- return node;
487
- };
488
- const treeSet = (node, key, value) => {
489
- if (!node) return makeTreeNode(key, value);
490
- if (key === node.key) return makeTreeNode(key, value, node.left, node.right);
491
- if (key < node.key) return rebalance(makeTreeNode(node.key, node.value, treeSet(node.left, key, value), node.right));
492
- return rebalance(makeTreeNode(node.key, node.value, node.left, treeSet(node.right, key, value)));
493
- };
494
- const treeMin = (node) => node.left ? treeMin(node.left) : node;
495
- const treeRemove = (node, key) => {
496
- if (!node) return void 0;
497
- if (key < node.key) return rebalance(makeTreeNode(node.key, node.value, treeRemove(node.left, key), node.right));
498
- if (key > node.key) return rebalance(makeTreeNode(node.key, node.value, node.left, treeRemove(node.right, key)));
499
- if (!node.left) return node.right;
500
- if (!node.right) return node.left;
501
- const next = treeMin(node.right);
502
- return rebalance(makeTreeNode(next.key, next.value, node.left, treeRemove(node.right, next.key)));
503
- };
504
- const treeGet = (node, key) => {
505
- let current = node;
506
- while (current) {
507
- if (key === current.key) return current.value;
508
- current = key < current.key ? current.left : current.right;
509
- }
510
- };
511
- const treeHas = (node, key) => {
512
- let current = node;
513
- while (current) {
514
- if (key === current.key) return true;
515
- current = key < current.key ? current.left : current.right;
516
- }
517
- return false;
518
- };
519
- const treeFromSorted = (entries, start = 0, end = entries.length) => {
520
- if (start >= end) return void 0;
521
- const middle = start + end >> 1;
522
- const [key, value] = entries[middle];
523
- return makeTreeNode(key, value, treeFromSorted(entries, start, middle), treeFromSorted(entries, middle + 1, end));
524
- };
525
- /** Owns staged keyed intent, exact transitions and the persistent published lookup. */
526
- const createCollectionOutput = () => {
527
- const values = /* @__PURE__ */ new Map();
528
- let ids = Object.freeze([]);
529
- let staged = /* @__PURE__ */ new Map();
530
- let nextIds = ids;
531
- let change;
532
- let initialized = false;
533
- let reset = false;
534
- let revision = 0;
535
- let publishedRoot;
536
- let explicitOrder;
537
- let cleared = false;
538
- const listeners = /* @__PURE__ */ new Set();
539
- const hasNext = (key) => staged.has(key) ? staged.get(key).present : !cleared && values.has(key);
540
- const getNext = (key) => {
541
- const entry = staged.get(key);
542
- return entry ? entry.present ? entry.value : void 0 : cleared ? void 0 : values.get(key);
543
- };
544
- const deriveIds = () => {
545
- const result = cleared ? [] : ids.filter(hasNext);
546
- for (const [key, entry] of staged) if (entry.present && (cleared || !values.has(key))) result.push(key);
547
- return result;
548
- };
549
- const begin = (active, initialize) => {
550
- staged = /* @__PURE__ */ new Map();
551
- nextIds = ids;
552
- change = void 0;
553
- explicitOrder = void 0;
554
- cleared = initialize;
555
- reset = initialize;
556
- const read = (next) => ({
557
- get: (key) => {
558
- assertScope(active);
559
- return next ? getNext(key) : values.get(key);
560
- },
561
- has: (key) => {
562
- assertScope(active);
563
- return next ? hasNext(key) : values.has(key);
564
- },
565
- ids: () => {
566
- assertScope(active);
567
- return next ? Object.freeze(explicitOrder ? [...explicitOrder] : [...deriveIds()]) : ids;
568
- }
569
- });
570
- return {
571
- previous: read(false),
572
- next: read(true),
573
- output: {
574
- set: (key, value) => {
575
- assertScope(active);
576
- if (typeof key !== "string") throw new TypeError("Projection keys must be strings.");
577
- staged.set(key, {
578
- present: true,
579
- value
580
- });
581
- },
582
- remove: (key) => {
583
- assertScope(active);
584
- staged.set(key, { present: false });
585
- },
586
- order: (order) => {
587
- assertScope(active);
588
- explicitOrder = Object.freeze([...order]);
589
- }
590
- }
591
- };
592
- };
593
- const seal = (nextReset, isEqual) => {
594
- if (cleared) {
595
- for (const key of values.keys()) if (!staged.has(key)) staged.set(key, { present: false });
596
- }
597
- profile.projection("touchedKeys", staged.size);
598
- const added = /* @__PURE__ */ new Set();
599
- const removed = /* @__PURE__ */ new Set();
600
- const updated = /* @__PURE__ */ new Set();
601
- for (const [key, entry] of staged) {
602
- const existed = values.has(key);
603
- if (entry.present) {
604
- if (!existed) added.add(key);
605
- else if (!isEqual(values.get(key), entry.value)) updated.add(key);
606
- else if (!nextReset) staged.delete(key);
607
- } else if (existed) removed.add(key);
608
- else staged.delete(key);
609
- }
610
- if (explicitOrder || added.size || removed.size) {
611
- const order = explicitOrder ?? [...ids.filter((key) => !removed.has(key)), ...added];
612
- if (new Set(order).size !== order.length || order.length !== values.size + added.size - removed.size || order.some((key) => !hasNext(key))) throw new TypeError("Projection order must contain every key exactly once.");
613
- nextIds = ids.length === order.length && ids.every((key, index) => key === order[index]) ? ids : Object.freeze([...order]);
614
- } else nextIds = ids;
615
- const changedKeys = new Set([
616
- ...added,
617
- ...removed,
618
- ...updated
619
- ]);
620
- profile.projection("changedKeys", changedKeys.size);
621
- const addedEntries = [];
622
- const updatedEntries = [];
623
- const removedEntries = [];
624
- for (const key of changedKeys) {
625
- const beforePresent = values.has(key);
626
- const afterPresent = hasNext(key);
627
- if (beforePresent === afterPresent) {
628
- if (!beforePresent || isEqual(values.get(key), getNext(key))) continue;
629
- }
630
- if (!beforePresent) addedEntries.push({
631
- key,
632
- after: getNext(key)
633
- });
634
- else if (!afterPresent) removedEntries.push({
635
- key,
636
- before: values.get(key)
637
- });
638
- else updatedEntries.push({
639
- key,
640
- before: values.get(key),
641
- after: getNext(key)
642
- });
643
- }
644
- const orderChanged = nextIds !== ids;
645
- if (nextReset) change = Object.freeze({ kind: "reset" });
646
- else if (addedEntries.length || updatedEntries.length || removedEntries.length || orderChanged) {
647
- let commonOrderChanged = false;
648
- if (orderChanged) {
649
- const before = ids.filter((key) => !removed.has(key));
650
- const after = nextIds.filter((key) => !added.has(key));
651
- commonOrderChanged = before.some((key, index) => after[index] !== key);
652
- }
653
- change = Object.freeze({
654
- kind: "incremental",
655
- added: Object.freeze(addedEntries),
656
- updated: Object.freeze(updatedEntries),
657
- removed: Object.freeze(removedEntries),
658
- ...commonOrderChanged ? { order: Object.freeze({
659
- before: Object.freeze([...ids]),
660
- after: Object.freeze([...nextIds])
661
- }) } : {}
662
- });
663
- }
664
- reset = nextReset;
665
- return change !== void 0;
666
- };
667
- const context = (active, cause) => Object.freeze({
668
- kind: "collection",
669
- read: {
670
- get: (key) => {
671
- assertScope(active);
672
- return getNext(key);
673
- },
674
- has: (key) => {
675
- assertScope(active);
676
- return hasNext(key);
677
- },
678
- ids: () => {
679
- assertScope(active);
680
- return nextIds;
681
- }
682
- },
683
- change,
684
- revision,
685
- reset,
686
- cause
687
- });
688
- const current = (check) => {
689
- const root = publishedRoot;
690
- const publishedIds = ids;
691
- return Object.freeze({
692
- get: (key) => {
693
- check();
694
- return treeGet(root, key);
695
- },
696
- has: (key) => {
697
- check();
698
- return treeHas(root, key);
699
- },
700
- ids: () => {
701
- check();
702
- return publishedIds;
703
- }
704
- });
705
- };
706
- return {
707
- begin,
708
- seal,
709
- context,
710
- current,
711
- revision: () => revision,
712
- reset: () => reset,
713
- publish: () => {
714
- if (change) if (change.kind === "reset") {
715
- const entries = [];
716
- for (const [key, entry] of staged) if (entry.present) entries.push([key, entry.value]);
717
- entries.sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0);
718
- publishedRoot = treeFromSorted(entries);
719
- } else for (const [key, entry] of staged) publishedRoot = entry.present ? treeSet(publishedRoot, key, entry.value) : treeRemove(publishedRoot, key);
720
- for (const [key, entry] of staged) if (entry.present) values.set(key, entry.value);
721
- else values.delete(key);
722
- if (change && initialized) revision++;
723
- ids = nextIds;
724
- initialized = true;
725
- },
726
- emit: (call) => {
727
- const resetEvent = Object.freeze({ kind: "reset" });
728
- Array.from(listeners).forEach((listener) => call(() => listener(change ?? resetEvent)));
729
- },
730
- clear: () => {
731
- staged.clear();
732
- nextIds = ids;
733
- change = void 0;
734
- reset = false;
735
- explicitOrder = void 0;
736
- cleared = false;
737
- },
738
- release: () => {
739
- values.clear();
740
- staged.clear();
741
- publishedRoot = void 0;
742
- listeners.clear();
743
- ids = nextIds = Object.freeze([]);
744
- change = void 0;
745
- initialized = false;
746
- revision = 0;
747
- reset = false;
748
- explicitOrder = void 0;
749
- cleared = false;
750
- },
751
- subscribe: (listener) => listeners.add(listener),
752
- unsubscribe: (listener) => listeners.delete(listener)
753
- };
754
- };
755
- const mapRead = (value) => ({
756
- get: (key) => value.get(key),
757
- has: (key) => value.has(key),
758
- ids: () => Object.freeze([...value.keys()])
759
- });
760
- /** Immutable map-like view over a CollectionRead. */
761
- const collectionView = (read) => {
762
- const ids = read.ids();
763
- const entries = function* () {
764
- for (const key of ids) yield [key, read.get(key)];
765
- };
766
- const values = function* () {
767
- for (const key of ids) yield read.get(key);
768
- };
769
- const view = {
770
- get: (key) => read.get(key),
771
- has: (key) => read.has(key),
772
- get size() {
773
- return ids.length;
774
- },
775
- keys: () => ids[Symbol.iterator](),
776
- values,
777
- entries,
778
- forEach: (callback, thisArg) => {
779
- for (const key of ids) callback.call(thisArg, read.get(key), key, view);
780
- },
781
- [Symbol.iterator]: entries
782
- };
783
- return Object.freeze(view);
784
- };
785
- /** Eager durable view used when a processor result may retain its dependency value. */
786
- const snapshotCollectionView = (read) => {
787
- const ids = read.ids();
788
- const values = /* @__PURE__ */ new Map();
789
- for (const key of ids) values.set(key, read.get(key));
790
- return collectionView(mapRead(values));
791
- };
792
- /**
793
- * The one source-to-CollectionChange kernel. Candidate keys only narrow value work;
794
- * exact membership/value/order semantics remain owned here.
795
- */
796
- const stageCollectionRead = (state, read, active, options) => {
797
- const evaluation = state.begin(active, options.reset);
798
- if (options.reset) {
799
- const ids = read.ids();
800
- profile.collectionView.idsScanned(ids.length);
801
- for (const key of ids) {
802
- profile.collectionView.mapped();
803
- evaluation.output.set(key, read.get(key));
804
- }
805
- evaluation.output.order(ids);
806
- } else {
807
- const previous = state.current(() => void 0);
808
- const keys = options.candidates ? [...new Set(options.candidates)] : [...new Set([...previous.ids(), ...read.ids()])];
809
- if (!options.candidates) profile.collectionView.idsScanned(keys.length);
810
- for (const key of keys) if (read.has(key)) {
811
- profile.collectionView.mapped();
812
- evaluation.output.set(key, read.get(key));
813
- } else evaluation.output.remove(key);
814
- if (options.orderMayChange || !options.candidates) {
815
- const ids = read.ids();
816
- if (options.candidates) profile.collectionView.idsScanned(ids.length);
817
- evaluation.output.order(ids);
818
- }
819
- }
820
- return state.seal(options.reset, options.isEqual ?? Object.is);
821
- };
822
- /** Exact net transition between two published map-like snapshots. */
823
- const collectionChangeBetween = (previous, current, isEqual = Object.is) => {
824
- const added = [];
825
- const removed = [];
826
- const updated = [];
827
- for (const [key, before] of previous) if (!current.has(key)) removed.push({
828
- key,
829
- before
830
- });
831
- else {
832
- const after = current.get(key);
833
- if (!isEqual(before, after)) updated.push({
834
- key,
835
- before,
836
- after
837
- });
838
- }
839
- for (const [key, after] of current) if (!previous.has(key)) added.push({
840
- key,
841
- after
842
- });
843
- const beforeKeys = new Set(previous.keys());
844
- const afterKeys = new Set(current.keys());
845
- const beforeCommon = [...previous.keys()].filter((key) => afterKeys.has(key));
846
- const afterCommon = [...current.keys()].filter((key) => beforeKeys.has(key));
847
- const orderChanged = beforeCommon.some((key, index) => afterCommon[index] !== key);
848
- if (!added.length && !removed.length && !updated.length && !orderChanged) return void 0;
849
- return Object.freeze({
850
- kind: "incremental",
851
- added: Object.freeze(added),
852
- updated: Object.freeze(updated),
853
- removed: Object.freeze(removed),
854
- ...orderChanged ? { order: Object.freeze({
855
- before: Object.freeze([...previous.keys()]),
856
- after: Object.freeze([...current.keys()])
857
- }) } : {}
858
- });
859
- };
860
- //#endregion
861
897
  //#region core/src/projection/definition.ts
862
898
  const projections = /* @__PURE__ */ new WeakMap();
863
899
  const defineRef = (producer, output) => {
@@ -958,7 +994,7 @@ function observe(source, selector) {
958
994
  equality: Object.is
959
995
  });
960
996
  const document = source;
961
- const state = accessOf(document);
997
+ const state = contextOf(document).state;
962
998
  const selected = selector ? compilePath(state.schema, "auto", selector) : Object.freeze({
963
999
  kind: "value",
964
1000
  schema: state.schema,
@@ -1069,25 +1105,23 @@ function createIncrementalCollection(dependencies, processor) {
1069
1105
  });
1070
1106
  return projection;
1071
1107
  }
1072
- const groupOutputMetadata = /* @__PURE__ */ new WeakMap();
1073
- const isPlainGroupNamespace = (value) => value !== null && typeof value === "object" && !groupOutputMetadata.has(value) && !Array.isArray(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
1074
- const compileGroupShape = (value, path, declaredOutputs, used, outputs) => {
1108
+ const isPlainGroupNamespace = (value, metadata) => isPlainObject(value) && !metadata.has(value);
1109
+ const compileGroupShape = (value, path, metadata, used, outputs) => {
1075
1110
  if (value !== null && typeof value === "object") {
1076
- const metadata = groupOutputMetadata.get(value);
1077
- if (metadata) {
1078
- if (!declaredOutputs.has(value)) throw new TypeError("Incremental group outputs must be declared by this group.");
1111
+ const output = metadata.get(value);
1112
+ if (output) {
1079
1113
  if (used.has(value)) throw new TypeError("An incremental group output cannot be reused.");
1080
1114
  used.add(value);
1081
1115
  const index = outputs.length;
1082
1116
  outputs.push({
1083
- kind: metadata.kind,
1084
- equality: metadata.equality,
1117
+ kind: output.kind,
1118
+ equality: output.equality,
1085
1119
  path: Object.freeze([...path])
1086
1120
  });
1087
1121
  return index;
1088
1122
  }
1089
1123
  }
1090
- if (!isPlainGroupNamespace(value)) throw new TypeError("Incremental group outputs must be a static object tree.");
1124
+ if (!isPlainGroupNamespace(value, metadata)) throw new TypeError("Incremental group outputs must be a static object tree.");
1091
1125
  for (const key of Reflect.ownKeys(value)) {
1092
1126
  if (typeof key !== "string") throw new TypeError("Incremental group output names must be strings.");
1093
1127
  if (!Object.prototype.propertyIsEnumerable.call(value, key)) throw new TypeError("Incremental group namespaces must contain enumerable properties.");
@@ -1099,7 +1133,7 @@ const compileGroupShape = (value, path, declaredOutputs, used, outputs) => {
1099
1133
  if (key === "__proto__" || key === "prototype" || key === "constructor") throw new TypeError(`Invalid incremental group output name: ${key}`);
1100
1134
  const descriptor = Object.getOwnPropertyDescriptor(value, key);
1101
1135
  if (!descriptor || !("value" in descriptor)) throw new TypeError("Incremental group namespaces cannot contain accessors.");
1102
- result[key] = compileGroupShape(descriptor.value, [...path, key], declaredOutputs, used, outputs);
1136
+ result[key] = compileGroupShape(descriptor.value, [...path, key], metadata, used, outputs);
1103
1137
  }
1104
1138
  return Object.freeze(result);
1105
1139
  };
@@ -1128,26 +1162,24 @@ function createIncrementalGroup(dependencies, defineOutputs, processor) {
1128
1162
  if (typeof defineOutputs !== "function") throw new TypeError("Incremental group outputs must be declared by a callback.");
1129
1163
  if (typeof processor !== "function") throw new TypeError("Incremental group processor must be a function.");
1130
1164
  let active = true;
1131
- const declaredOutputs = /* @__PURE__ */ new Set();
1165
+ const metadata = /* @__PURE__ */ new Map();
1132
1166
  const declared = defineOutputs({
1133
1167
  collection: (equality = Object.is) => {
1134
1168
  if (!active) throw new TypeError("Incremental group output declarations are synchronous.");
1135
1169
  const descriptor = Object.freeze({});
1136
- groupOutputMetadata.set(descriptor, {
1170
+ metadata.set(descriptor, {
1137
1171
  kind: "collection",
1138
1172
  equality
1139
1173
  });
1140
- declaredOutputs.add(descriptor);
1141
1174
  return descriptor;
1142
1175
  },
1143
1176
  value: (equality = Object.is) => {
1144
1177
  if (!active) throw new TypeError("Incremental group output declarations are synchronous.");
1145
1178
  const descriptor = Object.freeze({});
1146
- groupOutputMetadata.set(descriptor, {
1179
+ metadata.set(descriptor, {
1147
1180
  kind: "value",
1148
1181
  equality
1149
1182
  });
1150
- declaredOutputs.add(descriptor);
1151
1183
  return descriptor;
1152
1184
  }
1153
1185
  });
@@ -1155,9 +1187,9 @@ function createIncrementalGroup(dependencies, defineOutputs, processor) {
1155
1187
  active = false;
1156
1188
  const outputs = [];
1157
1189
  const used = /* @__PURE__ */ new Set();
1158
- const shape = compileGroupShape(declared, [], declaredOutputs, used, outputs);
1190
+ const shape = compileGroupShape(declared, [], metadata, used, outputs);
1159
1191
  if (typeof shape === "number") throw new TypeError("Incremental group declarations must return an output namespace.");
1160
- if (used.size === 0 || used.size !== declaredOutputs.size) throw new TypeError("Every incremental group output must be returned by the declaration.");
1192
+ if (used.size === 0 || used.size !== metadata.size) throw new TypeError("Every incremental group output must be returned by the declaration.");
1161
1193
  return hydrateGroupShape(shape, defineProcessor({
1162
1194
  dependencies,
1163
1195
  outputs,
@@ -1187,6 +1219,6 @@ const incremental = Object.assign(createIncrementalValue, {
1187
1219
  group: createIncrementalGroup
1188
1220
  });
1189
1221
  //#endregion
1190
- export { list as C, table as D, optional as E, tree as O, field as S, object as T, createScheduler as _, observe as a, collectionEntryNode as b, projectionRef as c, createCollectionOutput as d, mapRead as f, assertSynchronous as g, assertScope as h, isProjection as i, variant as k, collectionChangeBetween as l, stageCollectionRead as m, derive as n, ownProjection as o, snapshotCollectionView as p, input as r, producerOf as s, incremental as t, collectionView as u, ProjectionDisposedError as v, map as w, compilePath as x, ProjectionError as y };
1222
+ export { checkValue as A, insert as B, optional as C, ParseError as D, variant as E, parse as F, listSequence as G, installKeys as H, compiledLayout as I, toArray as J, matches as K, compiledShape as L, copyValue as M, equalTreeNode as N, checkKey as O, equalValue as P, equal as R, object as S, tree as T, installList as U, install as V, keys as W, profile as Y, collectionEntryNode as _, observe as a, list as b, projectionRef as c, createScheduler as d, ProjectionDisposedError as f, snapshotCollectionView as g, mapRead as h, isProjection as i, copyTreeNode as j, checkTreePayload as k, assertScope as l, collectionView as m, derive as n, ownProjection as o, ProjectionError as p, remove as q, input as r, producerOf as s, incremental as t, assertSynchronous as u, compilePath as v, table as w, map as x, field as y, indexedKeys as z };
1191
1223
 
1192
- //# sourceMappingURL=advanced-BTStm2cW.js.map
1224
+ //# sourceMappingURL=advanced-CRNHfJBu.js.map