doxum 0.1.12 → 0.1.13

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 (64) hide show
  1. package/README.md +44 -39
  2. package/dist/advanced.cjs +116 -0
  3. package/dist/advanced.cjs.map +1 -0
  4. package/dist/advanced.d.cts +42 -0
  5. package/dist/advanced.d.ts +42 -0
  6. package/dist/advanced.js +115 -0
  7. package/dist/advanced.js.map +1 -0
  8. package/dist/{contract-CeAnEPBA.d.ts → contract-BT53Gg94.d.ts} +5 -4
  9. package/dist/{contract-DtGVSXSK.d.cts → contract-XmKnroJq.d.cts} +5 -4
  10. package/dist/definition-177L4Amk.cjs +898 -0
  11. package/dist/definition-177L4Amk.cjs.map +1 -0
  12. package/dist/definition-BlMeHwdz.d.ts +98 -0
  13. package/dist/definition-DXrKMVs7.js +785 -0
  14. package/dist/definition-DXrKMVs7.js.map +1 -0
  15. package/dist/definition-kxnWUvX9.d.cts +98 -0
  16. package/dist/{driver-xOIkwrB8.cjs → driver-BCyfqxsl.cjs} +1 -1
  17. package/dist/{driver-xOIkwrB8.cjs.map → driver-BCyfqxsl.cjs.map} +1 -1
  18. package/dist/{driver-BlR81Dqg.js → driver-CSnhMjbP.js} +1 -1
  19. package/dist/{driver-BlR81Dqg.js.map → driver-CSnhMjbP.js.map} +1 -1
  20. package/dist/index.cjs +31 -29
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +5 -4
  23. package/dist/index.d.ts +5 -4
  24. package/dist/index.js +7 -6
  25. package/dist/index.js.map +1 -1
  26. package/dist/{integration-CWjttg7H.js → integration-Bco8ZB16.js} +7 -3
  27. package/dist/integration-Bco8ZB16.js.map +1 -0
  28. package/dist/integration-CEUn96tx.cjs +52 -0
  29. package/dist/integration-CEUn96tx.cjs.map +1 -0
  30. package/dist/integration.cjs +7 -3
  31. package/dist/integration.d.cts +7 -3
  32. package/dist/integration.d.ts +7 -3
  33. package/dist/integration.js +3 -3
  34. package/dist/local-sync.cjs +1 -1
  35. package/dist/local-sync.d.cts +1 -1
  36. package/dist/local-sync.d.ts +1 -1
  37. package/dist/local-sync.js +1 -1
  38. package/dist/react.cjs +63 -36
  39. package/dist/react.cjs.map +1 -1
  40. package/dist/react.d.cts +8 -7
  41. package/dist/react.d.ts +8 -7
  42. package/dist/react.js +63 -35
  43. package/dist/react.js.map +1 -1
  44. package/dist/store-CDeqZEE3.d.cts +28 -0
  45. package/dist/{store-7XBEcR8s.js → store-CtgvCVFg.js} +585 -922
  46. package/dist/store-CtgvCVFg.js.map +1 -0
  47. package/dist/{store-DBv58dns.cjs → store-DcDpDgjk.cjs} +595 -992
  48. package/dist/store-DcDpDgjk.cjs.map +1 -0
  49. package/dist/store-P5dJ7TVe.d.ts +28 -0
  50. package/package.json +6 -1
  51. package/skills/doxum-runtime/SKILL.md +5 -7
  52. package/skills/doxum-runtime/references/guide.en.md +12 -16
  53. package/skills/doxum-runtime/references/guide.zh-CN.md +9 -14
  54. package/skills/doxum-runtime/references/patterns.en.md +12 -20
  55. package/skills/doxum-runtime/references/patterns.zh-CN.md +12 -18
  56. package/skills/doxum-runtime/references/projections.en.md +56 -199
  57. package/skills/doxum-runtime/references/projections.zh-CN.md +50 -180
  58. package/dist/integration-BShL68Nc.cjs +0 -30
  59. package/dist/integration-BShL68Nc.cjs.map +0 -1
  60. package/dist/integration-CWjttg7H.js.map +0 -1
  61. package/dist/store-2i8pYXqa.d.ts +0 -263
  62. package/dist/store-7XBEcR8s.js.map +0 -1
  63. package/dist/store-DBv58dns.cjs.map +0 -1
  64. package/dist/store-Ks80xWS_.d.cts +0 -263
@@ -0,0 +1,898 @@
1
+ const require_issue = require("./issue-DhrNdQNg.cjs");
2
+ //#region core/src/schema-value.ts
3
+ var ParseError = class extends TypeError {
4
+ issues;
5
+ constructor(issue) {
6
+ super(`${issue.message} at ${JSON.stringify(issue.address)}`);
7
+ this.name = "ParseError";
8
+ this.issues = Object.freeze([Object.freeze({
9
+ ...issue,
10
+ address: Object.freeze([...issue.address])
11
+ })]);
12
+ }
13
+ };
14
+ const issue = (address, message, code = "invalid-value") => ({
15
+ address,
16
+ message,
17
+ code
18
+ });
19
+ const checkScalar = (validator, value, address, strict) => {
20
+ if (!validator) return strict ? issue(address, "A validator is required to parse an opaque value.", "missing-validator") : void 0;
21
+ try {
22
+ const result = typeof validator === "function" ? validator(value) : validator["~standard"].validate(value);
23
+ if (result && (typeof result === "object" || typeof result === "function") && "then" in result && typeof result.then === "function") return issue(address, "Validators must be synchronous.");
24
+ if (typeof validator !== "function") {
25
+ if (!require_issue.isRecord(result)) return issue(address, "Malformed validator result.");
26
+ if (Array.isArray(result.issues)) {
27
+ const first = result.issues[0];
28
+ return issue([...address, ...(first?.path ?? []).map((part) => String(typeof part === "object" ? part.key : part))], first?.message ?? "Value validation failed.");
29
+ }
30
+ if (!Object.hasOwn(result, "value")) return issue(address, "Malformed validator result.");
31
+ }
32
+ return;
33
+ } catch (error) {
34
+ return issue(address, error instanceof Error ? error.message : "Value validation failed.");
35
+ }
36
+ };
37
+ const checkKey = (validator, value, address) => {
38
+ if (typeof value !== "string") return issue(address, "Collection keys must be strings.", "invalid-key");
39
+ const failure = checkScalar(validator, value, address, false);
40
+ return failure ? {
41
+ ...failure,
42
+ code: "invalid-key"
43
+ } : void 0;
44
+ };
45
+ const checkValue = (node, value, address = [], strict = false) => {
46
+ if (node.optional && value === void 0) return void 0;
47
+ if (node.kind === "field") return checkScalar(node.validator, value, address, strict);
48
+ if (node.kind === "object" || node.kind === "variant") {
49
+ if (!require_issue.isPlainObject(value)) return issue(address, "Expected an object.");
50
+ if (node.kind === "variant") {
51
+ const tag = value[node.tag];
52
+ if (!Object.hasOwn(value, node.tag) || typeof tag !== "string" || !Object.hasOwn(node.variants, tag)) return issue([...address, node.tag], "Unknown variant tag.");
53
+ }
54
+ const { members } = require_issue.compiledShape(node, value);
55
+ for (const key of Reflect.ownKeys(value)) {
56
+ if (node.kind === "variant" && key === node.tag) continue;
57
+ if (typeof key !== "string" || !members.has(key)) return issue([...address, String(key)], "Property is not declared in the object schema.");
58
+ }
59
+ for (const [key, member] of members) {
60
+ const child = member.node;
61
+ if (!Object.prototype.hasOwnProperty.call(value, key) && !child.optional) return issue([...address, key], "Required property is missing.");
62
+ const failure = checkValue(child, value[key], [...address, key], strict);
63
+ if (failure) return failure;
64
+ }
65
+ return;
66
+ }
67
+ if (node.kind === "table" || node.kind === "map") {
68
+ if (!require_issue.isPlainObject(value)) return issue(address, "Expected a collection object.");
69
+ const entries = node.kind === "table" ? value.byId : value;
70
+ if (!require_issue.isPlainObject(entries)) return issue(address, "Expected a byId object.");
71
+ if (node.kind === "table") {
72
+ if (!Array.isArray(value.ids) || value.ids.length !== Object.keys(entries).length) return issue(address, "Table ids and entries must agree.");
73
+ const seen = /* @__PURE__ */ new Set();
74
+ for (const id of value.ids) {
75
+ const failure = checkKey(node.key, id, address);
76
+ if (failure) return failure;
77
+ if (seen.has(id) || !Object.prototype.hasOwnProperty.call(entries, id)) return issue(address, "Table ids must be unique and reference existing entries.");
78
+ seen.add(id);
79
+ }
80
+ }
81
+ for (const id of Object.keys(entries)) {
82
+ const failure = checkKey(node.key, id, [...address, id]) ?? checkValue(node.value, entries[id], [...address, id], strict);
83
+ if (failure) return failure;
84
+ }
85
+ return;
86
+ }
87
+ if (node.kind === "list") {
88
+ if (!Array.isArray(value)) return issue(address, "Expected a list.");
89
+ const seen = /* @__PURE__ */ new Set();
90
+ const order = require_issue.keys(value, node.keyOf);
91
+ for (let i = 0; i < value.length; i++) {
92
+ const failure = checkValue(node.value, value[i], [...address, String(i)], strict);
93
+ if (failure) return failure;
94
+ let key;
95
+ try {
96
+ key = order.at(i);
97
+ } catch {
98
+ return issue(address, "List keyOf failed.");
99
+ }
100
+ if (typeof key !== "string" || seen.has(key)) return issue(address, "List keys must be unique strings.", "invalid-key");
101
+ seen.add(key);
102
+ }
103
+ return;
104
+ }
105
+ if (!require_issue.validate(value)) return issue(address, "Invalid tree structure.", "invalid-tree");
106
+ if (require_issue.isRecord(value) && require_issue.isRecord(value.nodes)) for (const id of Object.keys(value.nodes)) {
107
+ const entry = value.nodes[id];
108
+ if (require_issue.isRecord(entry) && Object.prototype.hasOwnProperty.call(entry, "value")) {
109
+ const failure = checkValue(node.value, entry.value, [...address, id], strict);
110
+ if (failure) return failure;
111
+ }
112
+ }
113
+ };
114
+ /** Copy editable structure; atomic payloads retain their immutable ownership contract. */
115
+ const copyValue = (node, value) => {
116
+ if (value === void 0 || node.kind === "field") return value;
117
+ require_issue.profile.copy.structure();
118
+ if ((node.kind === "object" || node.kind === "variant") && require_issue.isRecord(value)) {
119
+ const result = Object.create(Object.getPrototypeOf(value));
120
+ const { members } = require_issue.compiledShape(node, value);
121
+ for (const key of Object.getOwnPropertyNames(value)) Object.defineProperty(result, key, {
122
+ value: node.kind === "variant" && key === node.tag ? value[key] : copyValue(members.get(key).node, value[key]),
123
+ writable: true,
124
+ enumerable: Object.getOwnPropertyDescriptor(value, key)?.enumerable,
125
+ configurable: true
126
+ });
127
+ return result;
128
+ }
129
+ if ((node.kind === "table" || node.kind === "map") && require_issue.isRecord(value)) {
130
+ const entries = node.kind === "table" ? value.byId : value;
131
+ const result = Object.fromEntries(Object.keys(entries).map((id) => [id, copyValue(node.value, entries[id])]));
132
+ return node.kind === "table" ? {
133
+ ids: [...value.ids],
134
+ byId: result
135
+ } : result;
136
+ }
137
+ if (node.kind === "list") return [...value];
138
+ if (node.kind === "tree" && require_issue.isRecord(value)) {
139
+ const entries = value.nodes;
140
+ return {
141
+ ...value,
142
+ nodes: Object.fromEntries(Object.entries(entries).map(([id, n]) => [id, {
143
+ ...n,
144
+ children: [...n.children]
145
+ }]))
146
+ };
147
+ }
148
+ return value;
149
+ };
150
+ const equalValue = (node, left, right) => {
151
+ if (Object.is(left, right)) return true;
152
+ if (node.kind === "field" || left === void 0 || right === void 0) return false;
153
+ if ((node.kind === "object" || node.kind === "variant") && require_issue.isRecord(left) && require_issue.isRecord(right)) {
154
+ if (node.kind === "variant" && left[node.tag] !== right[node.tag]) return false;
155
+ for (const [key, member] of require_issue.compiledShape(node, left).members) {
156
+ if (Object.hasOwn(left, key) !== Object.hasOwn(right, key)) return false;
157
+ if (!equalValue(member.node, left[key], right[key])) return false;
158
+ }
159
+ return true;
160
+ }
161
+ 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]));
162
+ if (node.kind === "table" && require_issue.isRecord(left) && require_issue.isRecord(right)) return require_issue.equal(left.ids, right.ids) && equalValue({
163
+ kind: "map",
164
+ value: node.value
165
+ }, left.byId, right.byId);
166
+ if (node.kind === "tree" && require_issue.isRecord(left) && require_issue.isRecord(right)) {
167
+ if (left.rootId !== right.rootId) return false;
168
+ const a = left.nodes, b = right.nodes;
169
+ return Object.keys(a).length === Object.keys(b).length && Object.keys(a).every((id) => Object.hasOwn(b, id) && a[id].parentId === b[id].parentId && require_issue.equal(a[id].children, b[id].children) && Object.hasOwn(a[id], "value") === Object.hasOwn(b[id], "value") && equalValue(node.value, a[id].value, b[id].value));
170
+ }
171
+ if (node.kind === "map" && require_issue.isRecord(left) && require_issue.isRecord(right)) {
172
+ const keys = Object.keys(left);
173
+ return keys.length === Object.keys(right).length && keys.every((key) => Object.hasOwn(right, key) && equalValue(node.value, left[key], right[key]));
174
+ }
175
+ return false;
176
+ };
177
+ const parse = (node, input) => {
178
+ const valueNode = node;
179
+ const failure = checkValue(valueNode, input, [], true);
180
+ if (failure) throw new ParseError(failure);
181
+ return copyValue(valueNode, input);
182
+ };
183
+ //#endregion
184
+ //#region core/src/mutation/operations/map.ts
185
+ function put(session, container, id, value) {
186
+ if (container.node.kind !== "map") return require_issue.fail(container.at, "invalid-collection", "Expected a map.");
187
+ session.writeMember(container, id, value, "set");
188
+ }
189
+ function remove$3(session, container, id) {
190
+ if (container.node.kind !== "map") return require_issue.fail(container.at, "invalid-collection", "Expected a map.");
191
+ session.writeMember(container, id, void 0, "remove");
192
+ }
193
+ //#endregion
194
+ //#region core/src/mutation/operations/table.ts
195
+ function create(session, container, entries, position) {
196
+ const { at, node, value } = container;
197
+ if (node.kind !== "table") return require_issue.fail(at, "invalid-collection", "Expected a table.");
198
+ const table = value;
199
+ if (!require_issue.valid(table.ids, position)) return require_issue.fail(at, "invalid-anchor", "Unknown table anchor.");
200
+ const ids = /* @__PURE__ */ new Set();
201
+ for (const entry of entries) {
202
+ if (Object.hasOwn(table.byId, entry.id) || ids.has(entry.id)) return require_issue.fail(at, "duplicate-entity", "Table key already exists.");
203
+ ids.add(entry.id);
204
+ }
205
+ if (!entries.length) return;
206
+ const index$1 = require_issue.index(table.ids, position);
207
+ for (const entry of entries) session.writeMember(container, entry.id, entry.value, "set");
208
+ const length = table.ids.length;
209
+ table.ids.length += entries.length;
210
+ table.ids.copyWithin(index$1 + entries.length, index$1, length);
211
+ for (let i = 0; i < entries.length; i++) table.ids[index$1 + i] = entries[i].id;
212
+ }
213
+ function remove$2(session, container, ids) {
214
+ const { at, node, value } = container;
215
+ if (node.kind !== "table") return require_issue.fail(at, "invalid-collection", "Expected a table.");
216
+ const table = value;
217
+ for (const id of ids) if (!Object.hasOwn(table.byId, id)) return require_issue.fail(at, "missing-entity", "Table key does not exist.");
218
+ if (!ids.length) return;
219
+ const removed = new Set(ids);
220
+ for (const id of removed) session.writeMember(container, id, void 0, "remove");
221
+ table.ids = table.ids.filter((id) => !removed.has(id));
222
+ }
223
+ function replace$3(session, container, id, value) {
224
+ const { at, node, value: tableValue } = container;
225
+ if (node.kind !== "table") return require_issue.fail(at, "invalid-collection", "Expected a table.");
226
+ const table = tableValue;
227
+ if (!Object.hasOwn(table.byId, id)) return require_issue.fail(at.concat(id), "missing-entity", "Table key does not exist.");
228
+ session.writeMember(container, id, value, "set");
229
+ }
230
+ //#endregion
231
+ //#region core/src/mutation/operations/list.ts
232
+ function insert$1(session, container, value, position) {
233
+ const { at, node, value: current } = container;
234
+ if (node.kind !== "list") return require_issue.fail(at, "invalid-list-key", "Expected a list.");
235
+ session.validate(node.value, value, at);
236
+ const id = node.keyOf(value);
237
+ if (typeof id !== "string") return require_issue.fail(at, "invalid-list-key", "List key must be a string.");
238
+ const items = current, keys$2 = require_issue.keys(items, node.keyOf);
239
+ if (keys$2.index(id) >= 0) return require_issue.fail(at, "duplicate-list-item", "List key already exists.");
240
+ if (!require_issue.valid(keys$2, position)) return require_issue.fail(at, "invalid-anchor", "Unknown list anchor.");
241
+ session.recorder.order(container);
242
+ session.recorder.member(container, id, session.definition(container, id), -1);
243
+ require_issue.insert(items, require_issue.index(keys$2, position), value);
244
+ session.invalidate();
245
+ }
246
+ function replace$2(session, container, id, value) {
247
+ const { at, node, value: items } = container;
248
+ if (node.kind !== "list") return require_issue.fail(at, "invalid-list-key", "Expected a list.");
249
+ const index = require_issue.indexedKeys(items, node.keyOf).index(id);
250
+ if (index < 0) return require_issue.fail(at, "missing-list-item", "List key does not exist.");
251
+ session.writeMember(container, id, value, "set", index);
252
+ }
253
+ function remove$1(session, container, id) {
254
+ const { at, node, value } = container;
255
+ if (node.kind !== "list") return require_issue.fail(at, "missing-list-item", "List key does not exist.");
256
+ const index = require_issue.keys(value, node.keyOf).index(id);
257
+ if (index < 0) return require_issue.fail(at, "missing-list-item", "List key does not exist.");
258
+ session.writeMember(container, id, void 0, "remove", index);
259
+ }
260
+ //#endregion
261
+ //#region core/src/mutation/operations/tree.ts
262
+ const node = (tree, id) => Object.hasOwn(tree.nodes, id) ? tree.nodes[id] : void 0;
263
+ const positionIndex = (index, length, at) => {
264
+ if (index !== void 0 && (!Number.isInteger(index) || index < 0)) return require_issue.fail(at, "invalid-tree-index", "Tree index must be nonnegative.");
265
+ return Math.min(index ?? length, length);
266
+ };
267
+ const insert = (session, container, id, value, position) => {
268
+ const { at, node: schema, value: tree } = container;
269
+ if (typeof id !== "string") return require_issue.fail(at, "invalid-key", "Tree keys must be strings.");
270
+ if (require_issue.contains$1(tree, id)) return require_issue.fail(at, "duplicate-tree-node", "Tree node already exists.");
271
+ const parentId = position?.parentId;
272
+ const parent = parentId === void 0 ? void 0 : node(tree, parentId);
273
+ if (tree.rootId !== void 0 && !parent || tree.rootId === void 0 && parentId !== void 0) return require_issue.fail(at, "missing-tree-parent", "A nonempty tree requires an existing parent.");
274
+ const index = positionIndex(position?.index, parent?.children.length ?? 0, at);
275
+ session.validate(schema.value, value, at.concat(id));
276
+ session.recorder.tree(container, id);
277
+ if (parentId !== void 0) session.recorder.tree(container, parentId);
278
+ Object.defineProperty(tree.nodes, id, {
279
+ value: {
280
+ ...parentId === void 0 ? {} : { parentId },
281
+ children: [],
282
+ value
283
+ },
284
+ writable: true,
285
+ configurable: true,
286
+ enumerable: true
287
+ });
288
+ if (parent) parent.children.splice(index, 0, id);
289
+ else tree.rootId = id;
290
+ session.invalidate();
291
+ };
292
+ const replace$1 = (session, container, id, value) => {
293
+ const { at, node: schema, value: tree } = container;
294
+ const entry = node(tree, id);
295
+ if (!entry) return require_issue.fail(at, "missing-tree-node", "Tree node does not exist.");
296
+ if (Object.is(entry.value, value) && Object.hasOwn(entry, "value")) return;
297
+ session.validate(schema.value, value, at.concat(id));
298
+ session.recorder.tree(container, id);
299
+ entry.value = value;
300
+ };
301
+ const remove = (session, container, id) => {
302
+ const { at, value: tree } = container;
303
+ const entry = node(tree, id);
304
+ if (!entry) return require_issue.fail(at, "missing-tree-node", "Tree node does not exist.");
305
+ const ids = [], stack = [id];
306
+ while (stack.length) {
307
+ const current = stack.pop();
308
+ ids.push(current);
309
+ for (const child of tree.nodes[current].children) stack.push(child);
310
+ }
311
+ for (const key of ids) session.recorder.tree(container, key);
312
+ if (entry.parentId !== void 0) session.recorder.tree(container, entry.parentId);
313
+ if (entry.parentId === void 0) delete tree.rootId;
314
+ else {
315
+ const children = tree.nodes[entry.parentId].children;
316
+ children.splice(children.indexOf(id), 1);
317
+ }
318
+ for (const key of ids) delete tree.nodes[key];
319
+ session.invalidate();
320
+ };
321
+ const move$1 = (session, container, id, position) => {
322
+ const { at, value: tree } = container;
323
+ const entry = node(tree, id), parentId = position?.parentId;
324
+ if (!entry) return require_issue.fail(at, "missing-tree-node", "Tree node does not exist.");
325
+ if (entry.parentId === void 0 || parentId === void 0 || !require_issue.contains$1(tree, parentId)) return require_issue.fail(at, "missing-tree-parent", "Moving requires a non-root node and an existing parent.");
326
+ for (let ancestor = parentId; ancestor !== void 0; ancestor = tree.nodes[ancestor].parentId) if (ancestor === id) return require_issue.fail(at, "tree-cycle", "Cannot move a node under its descendant.");
327
+ const old = tree.nodes[entry.parentId], next = tree.nodes[parentId];
328
+ const index = positionIndex(position?.index, next.children.length - (old === next ? 1 : 0), at);
329
+ const previous = old.children.indexOf(id);
330
+ if (old === next && previous === index) return;
331
+ session.recorder.tree(container, id);
332
+ session.recorder.tree(container, entry.parentId);
333
+ session.recorder.tree(container, parentId);
334
+ old.children.splice(previous, 1);
335
+ next.children.splice(index, 0, id);
336
+ entry.parentId = parentId;
337
+ session.invalidate();
338
+ };
339
+ //#endregion
340
+ //#region core/src/mutation/operations/order.ts
341
+ function move(session, container, id, position) {
342
+ const { at, node, value } = container;
343
+ if (node.kind !== "table" && node.kind !== "list") return require_issue.fail(at, "invalid-collection", "Expected an ordered container.");
344
+ const items = node.kind === "table" ? value.ids : value;
345
+ const keys$1 = node.kind === "table" ? items : require_issue.keys(items, node.keyOf);
346
+ const index = node.kind === "table" ? items.indexOf(id) : keys$1.index(id);
347
+ if (index < 0) return require_issue.fail(at, "missing-entity", "Ordered key does not exist.");
348
+ if (!require_issue.valid(keys$1, position)) return require_issue.fail(at, "invalid-anchor", "Unknown order anchor.");
349
+ const next = require_issue.afterRemove(keys$1, index, position);
350
+ if (next === index) return;
351
+ session.recorder.order(container);
352
+ require_issue.move(items, index, next);
353
+ session.invalidate();
354
+ }
355
+ //#endregion
356
+ //#region core/src/access/scope.ts
357
+ const locations = /* @__PURE__ */ new WeakMap();
358
+ const locationOf = (value) => locations.get(value);
359
+ const addressOf = (target) => {
360
+ if (target.at) return target.at;
361
+ require_issue.profile.access("addresses");
362
+ return target.at = addressOf(target.parent).concat(target.key);
363
+ };
364
+ const assertActive = (context) => {
365
+ if (context.active && !context.active()) throw new Error("Document access scope has expired.");
366
+ };
367
+ const collect = (context, at, kind = "value", id) => {
368
+ context.dependencies?.record(kind === "value" ? {
369
+ kind,
370
+ at
371
+ } : {
372
+ kind,
373
+ at,
374
+ ...id === void 0 ? {} : { id }
375
+ });
376
+ };
377
+ const snapshot = (value) => {
378
+ const location = value && typeof value === "object" ? locationOf(value) : void 0;
379
+ if (!location) return value;
380
+ const { context } = location;
381
+ const at = addressOf(location.target);
382
+ require_issue.profile.access("snapshots");
383
+ collect(context, at);
384
+ const resolved = resolve(context, location.target);
385
+ if (!resolved.node) throw new Error("The selected schema address no longer exists.");
386
+ return copyValue(resolved.node, resolved.value);
387
+ };
388
+ const replace = (container, key, value) => {
389
+ if (!locationOf(container)) throw new TypeError("replace requires scoped document access.");
390
+ if (typeof key !== "string") throw new TypeError("Document keys must be strings.");
391
+ Reflect.set(container, key, value);
392
+ };
393
+ const resolve = (context, target) => {
394
+ assertActive(context);
395
+ const generation = context.session?.generation ?? 0;
396
+ if (target.generation !== generation) {
397
+ require_issue.profile.access("resolutions");
398
+ if (!target.parent && !target.at.length) {
399
+ target.node = context.state.schema;
400
+ target.value = context.state.document;
401
+ } else {
402
+ const parent = target.parent && resolve(context, target.parent);
403
+ const location = parent ? require_issue.resolveChild(parent.node, parent.value, target.key) : require_issue.resolveLocated(context.state.schema, context.state.document, target.at);
404
+ target.node = location?.node ?? require_issue.nodeAt(context.state.schema, addressOf(target), context.state.document);
405
+ target.value = location && Object.hasOwn(location.parent, location.key) ? location.parent[location.key] : void 0;
406
+ }
407
+ target.generation = generation;
408
+ }
409
+ return target;
410
+ };
411
+ const createAccess = (context, initial = []) => {
412
+ require_issue.profile.access("scopes");
413
+ const mutable = () => {
414
+ assertActive(context);
415
+ if (!context.session) throw new TypeError("Cannot modify read-only document access.");
416
+ return context.session;
417
+ };
418
+ const writableContainer = (target, session) => {
419
+ if (target.container?.generation === session.generation && "layout" in target.container) return target.container;
420
+ return target.container = session.bind(addressOf(target), target.node, target.value);
421
+ };
422
+ const childAt = (target, key) => {
423
+ require_issue.profile.access("addresses");
424
+ return addressOf(target).concat(key);
425
+ };
426
+ const access = (target) => {
427
+ if (target.proxy) return target.proxy;
428
+ const proxy = new Proxy(target, handler);
429
+ require_issue.profile.access("proxies");
430
+ locations.set(proxy, {
431
+ context,
432
+ target
433
+ });
434
+ target.proxy = proxy;
435
+ return proxy;
436
+ };
437
+ const createTarget = (at, parent, key) => ({
438
+ at,
439
+ parent,
440
+ key,
441
+ node: void 0,
442
+ value: void 0,
443
+ generation: -1,
444
+ children: void 0,
445
+ proxy: void 0
446
+ });
447
+ const childAccess = (target, member, node, value) => {
448
+ const key = typeof member === "string" ? member : member.key;
449
+ if (node.kind === "field" || value === void 0) {
450
+ if (context.dependencies) collect(context, childAt(target, key));
451
+ return value;
452
+ }
453
+ let children = target.children;
454
+ let child;
455
+ if (typeof member !== "string") {
456
+ if (children?.kind !== "fixed" || children.schema !== target.node) children = {
457
+ kind: "fixed",
458
+ schema: target.node,
459
+ slots: new Array(require_issue.compiledShape(target.node, target.value).members.size)
460
+ };
461
+ child = children.slots[member.slot] ??= createTarget(void 0, target, key);
462
+ } else {
463
+ if (children?.kind !== "dynamic") children = {
464
+ kind: "dynamic",
465
+ keys: /* @__PURE__ */ new Map()
466
+ };
467
+ const retained = children.keys.get(key);
468
+ child = retained ?? createTarget(void 0, target, key);
469
+ if (!retained) children.keys.set(key, child);
470
+ }
471
+ target.children = children;
472
+ if (child.generation === target.generation) return child.proxy;
473
+ child.node = node;
474
+ child.value = value;
475
+ child.generation = target.generation;
476
+ return access(child);
477
+ };
478
+ const collectionValue = (target, node) => {
479
+ const current = resolve(context, target);
480
+ if (current.node !== node || current.value === void 0) throw new TypeError("The collection method belongs to a replaced schema branch.");
481
+ return current.value;
482
+ };
483
+ const writableCollection = (target, node) => {
484
+ const session = mutable();
485
+ collectionValue(target, node);
486
+ return writableContainer(target, session);
487
+ };
488
+ const writableTree = (target, node) => {
489
+ const session = mutable();
490
+ collectionValue(target, node);
491
+ if (target.container?.generation === session.generation && !("layout" in target.container)) return target.container;
492
+ return target.container = session.bindTree(addressOf(target), target.node, target.value);
493
+ };
494
+ const replaceCollection = (target, node, value) => {
495
+ const session = mutable();
496
+ collectionValue(target, node);
497
+ session.replace(addressOf(target), value);
498
+ };
499
+ const mapMethod = (target, property, node) => {
500
+ if (property === "ids") return () => {
501
+ const current = collectionValue(target, node);
502
+ if (context.dependencies) collect(context, addressOf(target), "collection");
503
+ return Object.keys(current);
504
+ };
505
+ if (property === "has") return (id) => {
506
+ const current = collectionValue(target, node);
507
+ if (context.dependencies) collect(context, addressOf(target), "collection", id);
508
+ return Object.hasOwn(current, id);
509
+ };
510
+ if (property === "get") return (id) => {
511
+ const current = collectionValue(target, node);
512
+ if (context.dependencies) collect(context, addressOf(target), "collection", id);
513
+ if (!Object.hasOwn(current, id)) return void 0;
514
+ if (node.value.kind === "field") {
515
+ if (context.dependencies) collect(context, childAt(target, id));
516
+ return current[id];
517
+ }
518
+ return childAccess(target, id, node.value, current[id]);
519
+ };
520
+ if (property === "put") return (id, value) => {
521
+ const writable = writableCollection(target, node);
522
+ put(context.session, writable, id, value);
523
+ };
524
+ if (property === "remove") return (id) => {
525
+ const writable = writableCollection(target, node);
526
+ remove$3(context.session, writable, id);
527
+ };
528
+ if (property === "replace") return (value) => replaceCollection(target, node, value);
529
+ };
530
+ const orderedMethod = (target, property, node) => {
531
+ const at = addressOf(target);
532
+ if (property === "ids") return () => {
533
+ const current = collectionValue(target, node);
534
+ collect(context, at, "collection");
535
+ return node.kind === "table" ? [...current.ids] : current.map(node.keyOf);
536
+ };
537
+ if (property === "has") return (id) => {
538
+ const current = collectionValue(target, node);
539
+ collect(context, at, "collection", id);
540
+ return node.kind === "table" ? Object.hasOwn(current.byId, id) : require_issue.indexedKeys(current, node.keyOf).index(id) >= 0;
541
+ };
542
+ if (property === "get") return (id) => {
543
+ const current = collectionValue(target, node);
544
+ collect(context, at, "collection", id);
545
+ const location = require_issue.resolveChild(node, current, id);
546
+ if (!location || !Object.hasOwn(location.parent, location.key)) return void 0;
547
+ return childAccess(target, id, location.node, location.parent[location.key]);
548
+ };
549
+ if (property === "move") return (id, position) => {
550
+ const writable = writableCollection(target, node);
551
+ move(context.session, writable, id, position);
552
+ };
553
+ };
554
+ const tableMethod = (target, property, node) => {
555
+ if (property === "create") return (input, valueOrPosition, position) => {
556
+ const writable = writableCollection(target, node);
557
+ create(context.session, writable, Array.isArray(input) ? input : [{
558
+ id: input,
559
+ value: valueOrPosition
560
+ }], Array.isArray(input) ? valueOrPosition : position);
561
+ };
562
+ if (property === "remove") return (ids) => {
563
+ const writable = writableCollection(target, node);
564
+ remove$2(context.session, writable, typeof ids === "string" ? [ids] : ids);
565
+ };
566
+ if (property === "replace") return (...args) => {
567
+ const [valueOrId, value] = args;
568
+ if (args.length === 1) return replaceCollection(target, node, valueOrId);
569
+ const writable = writableCollection(target, node);
570
+ replace$3(context.session, writable, valueOrId, value);
571
+ };
572
+ return orderedMethod(target, property, node);
573
+ };
574
+ const listMethod = (target, property, node) => {
575
+ if (property === "insert") return (value, position) => {
576
+ const writable = writableCollection(target, node);
577
+ insert$1(context.session, writable, value, position);
578
+ };
579
+ if (property === "remove") return (id) => {
580
+ const writable = writableCollection(target, node);
581
+ remove$1(context.session, writable, id);
582
+ };
583
+ if (property === "replace") return (...args) => {
584
+ const [valueOrId, value] = args;
585
+ if (args.length === 1) return replaceCollection(target, node, valueOrId);
586
+ const writable = writableCollection(target, node);
587
+ replace$2(context.session, writable, valueOrId, value);
588
+ };
589
+ return orderedMethod(target, property, node);
590
+ };
591
+ const treeMethod = (target, property, node) => {
592
+ const at = addressOf(target);
593
+ if (property === "insert") return (id, value, position) => {
594
+ const writable = writableTree(target, node);
595
+ insert(context.session, writable, id, value, position);
596
+ };
597
+ if (property === "remove") return (id) => {
598
+ const writable = writableTree(target, node);
599
+ remove(context.session, writable, id);
600
+ };
601
+ if (property === "move") return (id, position) => {
602
+ const writable = writableTree(target, node);
603
+ move$1(context.session, writable, id, position);
604
+ };
605
+ if (property === "replace") return (...args) => {
606
+ const [valueOrId, value] = args;
607
+ if (args.length === 1) return replaceCollection(target, node, valueOrId);
608
+ const writable = writableTree(target, node);
609
+ replace$1(context.session, writable, valueOrId, value);
610
+ };
611
+ const current = () => {
612
+ collect(context, at);
613
+ return collectionValue(target, node);
614
+ };
615
+ if (property === "rootId") return () => current().rootId;
616
+ if (property === "get") return (id) => {
617
+ const value = current();
618
+ return require_issue.contains$1(value, id) ? value.nodes[id].value : void 0;
619
+ };
620
+ if (property === "has") return (id) => require_issue.contains$1(current(), id);
621
+ if (property === "parent") return (id) => require_issue.parent(current(), id);
622
+ if (property === "children") return (id) => {
623
+ const children$1 = require_issue.children(current(), id);
624
+ return children$1 ? [...children$1] : void 0;
625
+ };
626
+ };
627
+ const handler = {
628
+ get: (target, property) => {
629
+ const { node, value } = resolve(context, target);
630
+ if (!node || value === void 0) return void 0;
631
+ if (typeof property !== "string") return void 0;
632
+ if (node.kind === "map") return mapMethod(target, property, node);
633
+ if (node.kind === "table") return tableMethod(target, property, node);
634
+ if (node.kind === "list") return listMethod(target, property, node);
635
+ if (node.kind === "tree") return treeMethod(target, property, node);
636
+ if (node.kind === "variant" && property === node.tag) {
637
+ collect(context, childAt(target, property));
638
+ return value[property];
639
+ }
640
+ const member = require_issue.compiledShape(node, value)?.members.get(property);
641
+ if (!member) return void 0;
642
+ if (member.field) {
643
+ if (context.dependencies) collect(context, childAt(target, property));
644
+ return value[property];
645
+ }
646
+ return childAccess(target, node.kind === "object" ? member : property, member.node, value[property]);
647
+ },
648
+ set: (target, property, value) => {
649
+ const session = mutable(), { node } = resolve(context, target);
650
+ if (typeof property !== "string" || !node || node.kind !== "object" && node.kind !== "variant") throw new TypeError("Invalid structural assignment.");
651
+ if (node.kind === "variant" && node.tag === property) throw new TypeError("Variant discriminants are read-only.");
652
+ session.assignMember(writableContainer(target, session), property, value);
653
+ return true;
654
+ },
655
+ deleteProperty: (target, property) => {
656
+ const session = mutable(), { node } = resolve(context, target);
657
+ if (typeof property !== "string" || node?.kind === "map" || node?.kind === "variant" && node.tag === property) throw new TypeError("Invalid structural deletion.");
658
+ session.removeMember(writableContainer(target, session), property);
659
+ return true;
660
+ },
661
+ has: (target, property) => {
662
+ const { node, value } = resolve(context, target);
663
+ if (typeof property !== "string") return false;
664
+ if (node?.kind === "map") return false;
665
+ collect(context, addressOf(target));
666
+ return value !== void 0 && Object.hasOwn(value, property);
667
+ },
668
+ ownKeys: (target) => {
669
+ const { node, value } = resolve(context, target);
670
+ if (node?.kind === "map") return [];
671
+ if (context.dependencies) collect(context, addressOf(target));
672
+ return value && typeof value === "object" ? Object.keys(value) : [];
673
+ },
674
+ getOwnPropertyDescriptor: (target, property) => {
675
+ const { node, value } = resolve(context, target);
676
+ if (node?.kind === "map") return void 0;
677
+ return value && Object.hasOwn(value, property) ? {
678
+ enumerable: true,
679
+ configurable: true
680
+ } : void 0;
681
+ },
682
+ defineProperty: () => {
683
+ assertActive(context);
684
+ throw new TypeError("Use schema assignments.");
685
+ },
686
+ setPrototypeOf: () => {
687
+ assertActive(context);
688
+ throw new TypeError("Document prototypes are immutable.");
689
+ },
690
+ preventExtensions: () => {
691
+ assertActive(context);
692
+ throw new TypeError("Document access cannot be frozen.");
693
+ }
694
+ };
695
+ const root = resolve(context, createTarget(initial, void 0, ""));
696
+ if (!root.node || root.node.kind === "field" || root.value === void 0) {
697
+ if (context.dependencies) collect(context, initial);
698
+ return root.value;
699
+ }
700
+ return access(root);
701
+ };
702
+ const collectionAccess = (context, at) => {
703
+ const value = createAccess(context, at);
704
+ if (value === void 0) return {
705
+ get: () => {
706
+ assertActive(context);
707
+ },
708
+ has: () => {
709
+ assertActive(context);
710
+ return false;
711
+ },
712
+ ids: () => {
713
+ assertActive(context);
714
+ return [];
715
+ }
716
+ };
717
+ return value;
718
+ };
719
+ //#endregion
720
+ //#region core/src/projection/definition.ts
721
+ const definitions = /* @__PURE__ */ new WeakMap();
722
+ const define = (definition) => {
723
+ const handle = Object.freeze({});
724
+ definitions.set(handle, definition);
725
+ return handle;
726
+ };
727
+ const definitionOf = (projection) => {
728
+ const definition = definitions.get(projection);
729
+ if (!definition) throw new TypeError("Unknown projection definition.");
730
+ return definition;
731
+ };
732
+ const input = (initial, equality = Object.is) => define({
733
+ kind: "input",
734
+ initial,
735
+ isEqual: equality
736
+ });
737
+ function observe(document, selector) {
738
+ if ("current" in document && typeof document.current === "function") return defineReadable(document);
739
+ return define({
740
+ kind: "document",
741
+ document,
742
+ selector
743
+ });
744
+ }
745
+ function derive(dependencies, compute, equality = Object.is) {
746
+ if (!Array.isArray(dependencies) || dependencies.some((value) => !definitions.has(value))) throw new TypeError("derive dependencies must be projections.");
747
+ return define({
748
+ kind: "derive",
749
+ dependencies: Object.freeze([...dependencies]),
750
+ compute,
751
+ isEqual: equality
752
+ });
753
+ }
754
+ /** Internal adapters used by the integration boundary, not exported publicly. */
755
+ const defineReadable = (readable, equality = Object.is) => define({
756
+ kind: "readable",
757
+ readable,
758
+ isEqual: equality
759
+ });
760
+ const defineExternalValue = (source) => define({
761
+ kind: "source-value",
762
+ source
763
+ });
764
+ const defineExternalCollection = (source) => define({
765
+ kind: "source-collection",
766
+ source
767
+ });
768
+ const defineIncrementalValue = (definition) => define({
769
+ kind: "incremental-value",
770
+ dependencies: definition.dependencies,
771
+ build: definition.build,
772
+ isEqual: definition.isEqual,
773
+ name: definition.name
774
+ });
775
+ const defineIncrementalCollection = (definition) => define({
776
+ kind: "incremental-collection",
777
+ dependencies: definition.dependencies,
778
+ build: definition.build,
779
+ isEqual: definition.isEqual,
780
+ name: definition.name
781
+ });
782
+ //#endregion
783
+ Object.defineProperty(exports, "ParseError", {
784
+ enumerable: true,
785
+ get: function() {
786
+ return ParseError;
787
+ }
788
+ });
789
+ Object.defineProperty(exports, "checkKey", {
790
+ enumerable: true,
791
+ get: function() {
792
+ return checkKey;
793
+ }
794
+ });
795
+ Object.defineProperty(exports, "checkValue", {
796
+ enumerable: true,
797
+ get: function() {
798
+ return checkValue;
799
+ }
800
+ });
801
+ Object.defineProperty(exports, "collectionAccess", {
802
+ enumerable: true,
803
+ get: function() {
804
+ return collectionAccess;
805
+ }
806
+ });
807
+ Object.defineProperty(exports, "copyValue", {
808
+ enumerable: true,
809
+ get: function() {
810
+ return copyValue;
811
+ }
812
+ });
813
+ Object.defineProperty(exports, "createAccess", {
814
+ enumerable: true,
815
+ get: function() {
816
+ return createAccess;
817
+ }
818
+ });
819
+ Object.defineProperty(exports, "defineExternalCollection", {
820
+ enumerable: true,
821
+ get: function() {
822
+ return defineExternalCollection;
823
+ }
824
+ });
825
+ Object.defineProperty(exports, "defineExternalValue", {
826
+ enumerable: true,
827
+ get: function() {
828
+ return defineExternalValue;
829
+ }
830
+ });
831
+ Object.defineProperty(exports, "defineIncrementalCollection", {
832
+ enumerable: true,
833
+ get: function() {
834
+ return defineIncrementalCollection;
835
+ }
836
+ });
837
+ Object.defineProperty(exports, "defineIncrementalValue", {
838
+ enumerable: true,
839
+ get: function() {
840
+ return defineIncrementalValue;
841
+ }
842
+ });
843
+ Object.defineProperty(exports, "defineReadable", {
844
+ enumerable: true,
845
+ get: function() {
846
+ return defineReadable;
847
+ }
848
+ });
849
+ Object.defineProperty(exports, "definitionOf", {
850
+ enumerable: true,
851
+ get: function() {
852
+ return definitionOf;
853
+ }
854
+ });
855
+ Object.defineProperty(exports, "derive", {
856
+ enumerable: true,
857
+ get: function() {
858
+ return derive;
859
+ }
860
+ });
861
+ Object.defineProperty(exports, "equalValue", {
862
+ enumerable: true,
863
+ get: function() {
864
+ return equalValue;
865
+ }
866
+ });
867
+ Object.defineProperty(exports, "input", {
868
+ enumerable: true,
869
+ get: function() {
870
+ return input;
871
+ }
872
+ });
873
+ Object.defineProperty(exports, "observe", {
874
+ enumerable: true,
875
+ get: function() {
876
+ return observe;
877
+ }
878
+ });
879
+ Object.defineProperty(exports, "parse", {
880
+ enumerable: true,
881
+ get: function() {
882
+ return parse;
883
+ }
884
+ });
885
+ Object.defineProperty(exports, "replace", {
886
+ enumerable: true,
887
+ get: function() {
888
+ return replace;
889
+ }
890
+ });
891
+ Object.defineProperty(exports, "snapshot", {
892
+ enumerable: true,
893
+ get: function() {
894
+ return snapshot;
895
+ }
896
+ });
897
+
898
+ //# sourceMappingURL=definition-177L4Amk.cjs.map