doxum 0.1.13 → 0.1.15

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 (57) hide show
  1. package/README.md +10 -2
  2. package/dist/advanced.cjs +3 -2
  3. package/dist/advanced.cjs.map +1 -1
  4. package/dist/advanced.d.cts +1 -1
  5. package/dist/advanced.d.ts +1 -1
  6. package/dist/advanced.js +2 -1
  7. package/dist/advanced.js.map +1 -1
  8. package/dist/{contract-BT53Gg94.d.ts → contract-CATCCVu5.d.ts} +1 -1
  9. package/dist/{definition-BlMeHwdz.d.ts → definition-B6J0SSNa.d.ts} +38 -37
  10. package/dist/{definition-kxnWUvX9.d.cts → definition-DN6yW2vi.d.cts} +37 -36
  11. package/dist/definition-Z6TagMup.cjs +107 -0
  12. package/dist/definition-Z6TagMup.cjs.map +1 -0
  13. package/dist/definition-c6XQXzvK.js +72 -0
  14. package/dist/definition-c6XQXzvK.js.map +1 -0
  15. package/dist/index.cjs +2054 -55
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +16 -3
  18. package/dist/index.d.ts +17 -4
  19. package/dist/index.js +2001 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/integration-B7VDgAOJ.cjs +30 -0
  22. package/dist/integration-B7VDgAOJ.cjs.map +1 -0
  23. package/dist/{integration-Bco8ZB16.js → integration-CxVhq-KD.js} +3 -7
  24. package/dist/integration-CxVhq-KD.js.map +1 -0
  25. package/dist/integration.cjs +4 -9
  26. package/dist/integration.d.cts +2 -7
  27. package/dist/integration.d.ts +2 -7
  28. package/dist/integration.js +3 -3
  29. package/dist/local-sync.d.ts +1 -1
  30. package/dist/notification-CDSUfiip.cjs +398 -0
  31. package/dist/notification-CDSUfiip.cjs.map +1 -0
  32. package/dist/notification-IQIJyH3Q.js +285 -0
  33. package/dist/notification-IQIJyH3Q.js.map +1 -0
  34. package/dist/react.cjs +6 -52
  35. package/dist/react.cjs.map +1 -1
  36. package/dist/react.js +4 -50
  37. package/dist/react.js.map +1 -1
  38. package/dist/{definition-177L4Amk.cjs → scope-Ezs-Y3TY.cjs} +1 -118
  39. package/dist/scope-Ezs-Y3TY.cjs.map +1 -0
  40. package/dist/{definition-DXrKMVs7.js → scope-d-L87NeC.js} +2 -65
  41. package/dist/scope-d-L87NeC.js.map +1 -0
  42. package/package.json +1 -1
  43. package/skills/doxum-runtime/references/guide.en.md +2 -1
  44. package/skills/doxum-runtime/references/guide.zh-CN.md +2 -1
  45. package/skills/doxum-runtime/references/projections.en.md +15 -6
  46. package/skills/doxum-runtime/references/projections.zh-CN.md +13 -5
  47. package/dist/definition-177L4Amk.cjs.map +0 -1
  48. package/dist/definition-DXrKMVs7.js.map +0 -1
  49. package/dist/integration-Bco8ZB16.js.map +0 -1
  50. package/dist/integration-CEUn96tx.cjs +0 -52
  51. package/dist/integration-CEUn96tx.cjs.map +0 -1
  52. package/dist/store-CDeqZEE3.d.cts +0 -28
  53. package/dist/store-CtgvCVFg.js +0 -2222
  54. package/dist/store-CtgvCVFg.js.map +0 -1
  55. package/dist/store-DcDpDgjk.cjs +0 -2401
  56. package/dist/store-DcDpDgjk.cjs.map +0 -1
  57. package/dist/store-P5dJ7TVe.d.ts +0 -28
package/README.md CHANGED
@@ -220,6 +220,8 @@ const visible = derive([tasks, filter], (all, mode) => {
220
220
 
221
221
  const runtime = createProjectionRuntime({ onError: console.error });
222
222
  runtime.get(visible);
223
+ const selected = runtime.readable(visible, tasks => tasks.get('a'));
224
+ const stop = selected.subscribe(() => selected.current());
223
225
  runtime.set(filter, 'open');
224
226
  runtime.batch({ cause: { action: 'refresh' } }, () => {
225
227
  runtime.set(filter, 'all');
@@ -227,12 +229,18 @@ runtime.batch({ cause: { action: 'refresh' } }, () => {
227
229
  draft.title = 'Updated';
228
230
  });
229
231
  });
232
+ stop();
230
233
  ```
231
234
 
232
235
  Projection declarations are lazy and reusable. The only Runtime operations are
233
- `get`, `subscribe`, `set`, `batch` and `dispose`; materialization, incremental
236
+ `get`, `readable`, `set`, `batch` and `dispose`; materialization, incremental
234
237
  state, publication and recovery stay inside the Runtime. Collection values are
235
- immutable `ReadonlyMap`-like snapshots, so callers do not hold lifecycle handles.
238
+ immutable `ReadonlyMap`-like snapshots, while a `Readable` owns selector
239
+ tracking, equality and subscription lifecycle.
240
+
241
+ `observe` is the single source boundary for documents, Doxum `Readable` values,
242
+ and eventful external sources. External sources declare `kind: 'value'` or
243
+ `kind: 'collection'`; collection invalidation remains keyed internally.
236
244
 
237
245
  For retained state, reverse indexes and keyed patches, use the isolated advanced
238
246
  entry point. Whole-value processors use `incremental(...)`; keyed collection
package/dist/advanced.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_definition = require("./definition-177L4Amk.cjs");
2
+ const require_scope = require("./scope-Ezs-Y3TY.cjs");
3
+ const require_definition = require("./definition-Z6TagMup.cjs");
3
4
  //#region core/src/projection/advanced.ts
4
5
  const dependenciesOf = (dependencies) => Object.fromEntries(dependencies.map((dependency, index) => [`d${index}`, dependency]));
5
6
  const sourceChange = (sources) => {
@@ -18,7 +19,7 @@ const publicSource = (source) => {
18
19
  for (const id of read.ids()) result.set(id, read.get(id));
19
20
  return result;
20
21
  }
21
- return require_definition.snapshot(read);
22
+ return require_scope.snapshot(read);
22
23
  }
23
24
  if ("ids" in source && typeof source.ids === "function") {
24
25
  const result = /* @__PURE__ */ new Map();
@@ -1 +1 @@
1
- {"version":3,"file":"advanced.cjs","names":["snapshot","defineIncrementalValue","defineIncrementalCollection"],"sources":["../core/src/projection/advanced.ts"],"sourcesContent":["import type { Projection, ProjectionValues, PublicCollection } from './definition';\nimport { defineIncrementalCollection, defineIncrementalValue } from './definition';\nimport type { CollectionDraft, CollectionRead, GraphSources } from './contract';\nimport { snapshot } from '../access/scope';\n\nexport type IncrementalState = Record<string, unknown>;\n\nexport type IncrementalValueContext<S, T> = {\n readonly sources: S;\n readonly previous: T | undefined;\n readonly reset: boolean;\n readonly change: unknown;\n readonly cause: unknown;\n readonly state: IncrementalState;\n};\n\nexport type IncrementalValueProcessor<S, T> = (\n context: IncrementalValueContext<S, T>\n) =>\n | T\n | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n | { readonly kind: 'rebuild' };\n\nexport type IncrementalCollectionContext<S, K extends string, V> = {\n readonly sources: S;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly change: unknown;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: IncrementalState;\n readonly output: CollectionDraft<K, V>;\n};\n\nexport type IncrementalCollectionProcessor<S, K extends string, V> = (\n context: IncrementalCollectionContext<S, K, V>\n) => void | { readonly kind: 'rebuild' };\n\ntype DependencyMap<D extends readonly Projection<unknown>[]> = {\n readonly [K in keyof D as `d${Extract<K, number>}`]: D[K];\n};\n\nconst dependenciesOf = <D extends readonly Projection<unknown>[]>(dependencies: D): GraphSources =>\n Object.fromEntries(\n dependencies.map((dependency, index) => [`d${index}`, dependency])\n ) as unknown as GraphSources;\n\nconst sourceChange = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (\n source &&\n typeof source === 'object' &&\n 'change' in source &&\n (source as { readonly change?: unknown }).change !== undefined\n )\n return (source as { readonly change?: unknown }).change;\n }\n return undefined;\n};\n\nconst sourceCause = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (\n source &&\n typeof source === 'object' &&\n 'cause' in source &&\n (source as { readonly cause?: unknown }).cause !== undefined\n )\n return (source as { readonly cause?: unknown }).cause;\n }\n return undefined;\n};\n\nconst publicSource = (source: unknown): unknown => {\n if (!source || typeof source !== 'object') return source;\n if ('value' in source) return (source as { readonly value: unknown }).value;\n if ('read' in source) {\n const read = (source as { readonly read: unknown }).read;\n if (\n read &&\n typeof read === 'object' &&\n 'ids' in read &&\n typeof (read as { ids?: unknown }).ids === 'function'\n ) {\n const result = new Map<string, unknown>();\n for (const id of (read as CollectionRead<string, unknown>).ids())\n result.set(id, (read as CollectionRead<string, unknown>).get(id));\n return result;\n }\n return snapshot(read);\n }\n if ('ids' in source && typeof (source as { ids?: unknown }).ids === 'function') {\n const result = new Map<string, unknown>();\n const read = source as CollectionRead<string, unknown>;\n for (const id of read.ids()) result.set(id, read.get(id));\n return result;\n }\n return source;\n};\n\nconst publicTuple = (sources: Record<string, unknown>): readonly unknown[] =>\n Object.keys(sources)\n .sort((left, right) => Number(left.slice(1)) - Number(right.slice(1)))\n .map(key => publicSource(sources[key]));\n\nconst normalizeValue = <T>(\n result: T | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n): { value: T; state?: IncrementalState } =>\n result && typeof result === 'object' && (result as { readonly kind?: unknown }).kind === 'value'\n ? {\n value: (result as { readonly value: T }).value,\n state: (result as { readonly state?: IncrementalState }).state,\n }\n : { value: result as T };\n\nfunction createIncrementalValue<const D extends readonly Projection<unknown>[], T>(\n dependencies: D,\n processor: IncrementalValueProcessor<ProjectionValues<D>, T>\n): Projection<T> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: IncrementalState = Object.create(null) as IncrementalState;\n let current!: T;\n const build = (sources: Record<string, unknown>) => {\n state = Object.create(null) as IncrementalState;\n const result = processor({\n sources: publicTuple(sources) as ProjectionValues<D>,\n previous: undefined,\n reset: true,\n change: undefined,\n cause: sourceCause(sources),\n state,\n });\n if (result && typeof result === 'object' && 'kind' in result)\n throw new TypeError('Initial incremental processor cannot rebuild.');\n const normalized = normalizeValue(result as T);\n current = normalized.value;\n if (normalized.state) state = normalized.state;\n return {\n value: current,\n update: (nextSources: Record<string, unknown>) => {\n const next = processor({\n sources: publicTuple(nextSources) as ProjectionValues<D>,\n previous: current,\n reset: false,\n change: sourceChange(nextSources),\n cause: sourceCause(nextSources),\n state,\n });\n if (next && typeof next === 'object' && 'kind' in next) return next;\n const nextValue = normalizeValue(next as T);\n current = nextValue.value;\n if (nextValue.state) state = nextValue.state;\n return { kind: 'changed', value: current } as const;\n },\n };\n };\n return defineIncrementalValue({ dependencies: dependencyMap, build: build as never });\n}\n\nfunction createIncrementalCollection<\n const D extends readonly Projection<unknown>[],\n K extends string,\n V,\n>(\n dependencies: D,\n processor: IncrementalCollectionProcessor<ProjectionValues<D>, K, V>\n): Projection<PublicCollection<K, V>> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: IncrementalState = Object.create(null) as IncrementalState;\n const build = (input: {\n readonly sources: Record<string, unknown>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly writer: CollectionDraft<K, V>;\n }) => {\n state = Object.create(null) as IncrementalState;\n const result = processor({\n sources: publicTuple(input.sources) as ProjectionValues<D>,\n previous: input.previous,\n next: input.next,\n reset: true,\n change: undefined,\n cause: sourceCause(input.sources),\n state,\n output: input.writer,\n });\n if (result?.kind === 'rebuild')\n throw new TypeError('Initial incremental collection processor cannot rebuild.');\n return {\n update: (nextInput: typeof input) =>\n processor({\n sources: publicTuple(nextInput.sources) as ProjectionValues<D>,\n previous: nextInput.previous,\n next: nextInput.next,\n reset: false,\n change: sourceChange(nextInput.sources),\n cause: sourceCause(nextInput.sources),\n state,\n output: nextInput.writer,\n }),\n };\n };\n return defineIncrementalCollection({ dependencies: dependencyMap, build: build as never });\n}\n\nexport const incremental = Object.assign(createIncrementalValue, {\n collection: createIncrementalCollection,\n});\n\nexport type IncrementalDependencies<D extends readonly Projection<unknown>[]> = DependencyMap<D>;\n"],"mappings":";;;AA0CA,MAAM,kBAA4D,iBAChE,OAAO,YACL,aAAa,KAAK,YAAY,UAAU,CAAC,IAAI,SAAS,WAAW,CAAC,CACnE;AAEH,MAAM,gBAAgB,YAA8C;AAClE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,CACzC,KACE,UACA,OAAO,WAAW,YAClB,YAAY,UACX,OAAyC,WAAW,KAAA,EAErD,QAAQ,OAAyC;;AAKvD,MAAM,eAAe,YAA8C;AACjE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,C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AAO,YAAY,UAAU,QAAQ;GACrC;GACA,QAAQ,UAAU;GACnB,CAAC,EACL;;AAEH,QAAOC,mBAAAA,4BAA4B;EAAE,cAAc;EAAsB;EAAgB,CAAC;;AAG5F,MAAa,cAAc,OAAO,OAAO,wBAAwB,EAC/D,YAAY,6BACb,CAAC"}
1
+ {"version":3,"file":"advanced.cjs","names":["snapshot","defineIncrementalValue","defineIncrementalCollection"],"sources":["../core/src/projection/advanced.ts"],"sourcesContent":["import type { Projection, ProjectionValues, PublicCollection } from './definition';\nimport { defineIncrementalCollection, defineIncrementalValue } from './definition';\nimport type { CollectionDraft, CollectionRead, GraphSources } from './contract';\nimport { snapshot } from '../access/scope';\n\nexport type IncrementalState = Record<string, unknown>;\n\nexport type IncrementalValueContext<S, T> = {\n readonly sources: S;\n readonly previous: T | undefined;\n readonly reset: boolean;\n readonly change: unknown;\n readonly cause: unknown;\n readonly state: IncrementalState;\n};\n\nexport type IncrementalValueProcessor<S, T> = (\n context: IncrementalValueContext<S, T>\n) =>\n | T\n | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n | { readonly kind: 'rebuild' };\n\nexport type IncrementalCollectionContext<S, K extends string, V> = {\n readonly sources: S;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly change: unknown;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: IncrementalState;\n readonly output: CollectionDraft<K, V>;\n};\n\nexport type IncrementalCollectionProcessor<S, K extends string, V> = (\n context: IncrementalCollectionContext<S, K, V>\n) => void | { readonly kind: 'rebuild' };\n\ntype DependencyMap<D extends readonly Projection<unknown>[]> = {\n readonly [K in keyof D as `d${Extract<K, number>}`]: D[K];\n};\n\nconst dependenciesOf = <D extends readonly Projection<unknown>[]>(dependencies: D): GraphSources =>\n Object.fromEntries(\n dependencies.map((dependency, index) => [`d${index}`, dependency])\n ) as unknown as GraphSources;\n\nconst sourceChange = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (\n source &&\n typeof source === 'object' &&\n 'change' in source &&\n (source as { readonly change?: unknown }).change !== undefined\n )\n return (source as { readonly change?: unknown }).change;\n }\n return undefined;\n};\n\nconst sourceCause = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (\n source &&\n typeof source === 'object' &&\n 'cause' in source &&\n (source as { readonly cause?: unknown }).cause !== undefined\n )\n return (source as { readonly cause?: unknown }).cause;\n }\n return undefined;\n};\n\nconst publicSource = (source: unknown): unknown => {\n if (!source || typeof source !== 'object') return source;\n if ('value' in source) return (source as { readonly value: unknown }).value;\n if ('read' in source) {\n const read = (source as { readonly read: unknown }).read;\n if (\n read &&\n typeof read === 'object' &&\n 'ids' in read &&\n typeof (read as { ids?: unknown }).ids === 'function'\n ) {\n const result = new Map<string, unknown>();\n for (const id of (read as CollectionRead<string, unknown>).ids())\n result.set(id, (read as CollectionRead<string, unknown>).get(id));\n return result;\n }\n return snapshot(read);\n }\n if ('ids' in source && typeof (source as { ids?: unknown }).ids === 'function') {\n const result = new Map<string, unknown>();\n const read = source as CollectionRead<string, unknown>;\n for (const id of read.ids()) result.set(id, read.get(id));\n return result;\n }\n return source;\n};\n\nconst publicTuple = (sources: Record<string, unknown>): readonly unknown[] =>\n Object.keys(sources)\n .sort((left, right) => Number(left.slice(1)) - Number(right.slice(1)))\n .map(key => publicSource(sources[key]));\n\nconst normalizeValue = <T>(\n result: T | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n): { value: T; state?: IncrementalState } =>\n result && typeof result === 'object' && (result as { readonly kind?: unknown }).kind === 'value'\n ? {\n value: (result as { readonly value: T }).value,\n state: (result as { readonly state?: IncrementalState }).state,\n }\n : { value: result as T };\n\nfunction createIncrementalValue<const D extends readonly Projection<unknown>[], T>(\n dependencies: D,\n processor: IncrementalValueProcessor<ProjectionValues<D>, T>\n): Projection<T> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: IncrementalState = Object.create(null) as IncrementalState;\n let current!: T;\n const build = (sources: Record<string, unknown>) => {\n state = Object.create(null) as IncrementalState;\n const result = processor({\n sources: publicTuple(sources) as ProjectionValues<D>,\n previous: undefined,\n reset: true,\n change: undefined,\n cause: sourceCause(sources),\n state,\n });\n if (result && typeof result === 'object' && 'kind' in result)\n throw new TypeError('Initial incremental processor cannot rebuild.');\n const normalized = normalizeValue(result as T);\n current = normalized.value;\n if (normalized.state) state = normalized.state;\n return {\n value: current,\n update: (nextSources: Record<string, unknown>) => {\n const next = processor({\n sources: publicTuple(nextSources) as ProjectionValues<D>,\n previous: current,\n reset: false,\n change: sourceChange(nextSources),\n cause: sourceCause(nextSources),\n state,\n });\n if (next && typeof next === 'object' && 'kind' in next) return next;\n const nextValue = normalizeValue(next as T);\n current = nextValue.value;\n if (nextValue.state) state = nextValue.state;\n return { kind: 'changed', value: current } as const;\n },\n };\n };\n return defineIncrementalValue({ dependencies: dependencyMap, build: build as never });\n}\n\nfunction createIncrementalCollection<\n const D extends readonly Projection<unknown>[],\n K extends string,\n V,\n>(\n dependencies: D,\n processor: IncrementalCollectionProcessor<ProjectionValues<D>, K, V>\n): Projection<PublicCollection<K, V>> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: IncrementalState = Object.create(null) as IncrementalState;\n const build = (input: {\n readonly sources: Record<string, unknown>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly writer: CollectionDraft<K, V>;\n }) => {\n state = Object.create(null) as IncrementalState;\n const result = processor({\n sources: publicTuple(input.sources) as ProjectionValues<D>,\n previous: input.previous,\n next: input.next,\n reset: true,\n change: undefined,\n cause: sourceCause(input.sources),\n state,\n output: input.writer,\n });\n if (result?.kind === 'rebuild')\n throw new TypeError('Initial incremental collection processor cannot rebuild.');\n return {\n update: (nextInput: typeof input) =>\n processor({\n sources: publicTuple(nextInput.sources) as ProjectionValues<D>,\n previous: nextInput.previous,\n next: nextInput.next,\n reset: false,\n change: sourceChange(nextInput.sources),\n cause: sourceCause(nextInput.sources),\n state,\n output: nextInput.writer,\n }),\n };\n };\n return defineIncrementalCollection({ dependencies: dependencyMap, build: build as never });\n}\n\nexport const incremental = Object.assign(createIncrementalValue, {\n collection: createIncrementalCollection,\n});\n\nexport type IncrementalDependencies<D extends readonly Projection<unknown>[]> = DependencyMap<D>;\n"],"mappings":";;;;AA0CA,MAAM,kBAA4D,iBAChE,OAAO,YACL,aAAa,KAAK,YAAY,UAAU,CAAC,IAAI,SAAS,WAAW,CAAC,CACnE;AAEH,MAAM,gBAAgB,YAA8C;AAClE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,CACzC,KACE,UACA,OAAO,WAAW,YAClB,YAAY,UACX,OAAyC,WAAW,KAAA,EAErD,QAAQ,OAAyC;;AAKvD,MAAM,eAAe,YAA8C;AACjE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,CACzC,KACE,UACA,OAAO,WAAW,YAClB,WAAW,UACV,OAAwC,UAAU,KAAA,EAEnD,QAAQ,OAAwC;;AAKtD,MAAM,gBAAgB,WAA6B;AACjD,KAAI,CAAC,UAAU,OAAO,WAAW,SAAU,QAAO;AAClD,KAAI,WAAW,OAAQ,QAAQ,OAAuC;AACtE,KAAI,UAAU,QAAQ;EACpB,MAAM,OAAQ,OAAsC;AACpD,MACE,QACA,OAAO,SAAS,YAChB,SAAS,QACT,OAAQ,KAA2B,QAAQ,YAC3C;GACA,MAAM,yBAAS,IAAI,KAAsB;AACzC,QAAK,MAAM,MAAO,KAAyC,KAAK,CAC9D,QAAO,IAAI,IAAK,KAAyC,IAAI,GAAG,CAAC;AACnE,UAAO;;AAET,SAAOA,cAAAA,SAAS,KAAK;;AAEvB,KAAI,SAAS,UAAU,OAAQ,OAA6B,QAAQ,YAAY;EAC9E,MAAM,yBAAS,IAAI,KAAsB;EACzC,MAAM,OAAO;AACb,OAAK,MAAM,MAAM,KAAK,KAAK,CAAE,QAAO,IAAI,IAAI,KAAK,IAAI,GAAG,CAAC;AACzD,SAAO;;AAET,QAAO;;AAGT,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,4BAKP,cACA,WACoC;CACpC,MAAM,gBAAgB,eAAe,aAAa;CAClD,IAAI,QAA0B,OAAO,OAAO,KAAK;CACjD,MAAM,SAAS,UAKT;AACJ,UAAQ,OAAO,OAAO,KAAK;AAW3B,MAVe,UAAU;GACvB,SAAS,YAAY,MAAM,QAAQ;GACnC,UAAU,MAAM;GAChB,MAAM,MAAM;GACZ,OAAO;GACP,QAAQ,KAAA;GACR,OAAO,YAAY,MAAM,QAAQ;GACjC;GACA,QAAQ,MAAM;GACf,CACS,EAAE,SAAS,UACnB,OAAM,IAAI,UAAU,2DAA2D;AACjF,SAAO,EACL,SAAS,cACP,UAAU;GACR,SAAS,YAAY,UAAU,QAAQ;GACvC,UAAU,UAAU;GACpB,MAAM,UAAU;GAChB,OAAO;GACP,QAAQ,aAAa,UAAU,QAAQ;GACvC,OAAO,YAAY,UAAU,QAAQ;GACrC;GACA,QAAQ,UAAU;GACnB,CAAC,EACL;;AAEH,QAAOC,mBAAAA,4BAA4B;EAAE,cAAc;EAAsB;EAAgB,CAAC;;AAG5F,MAAa,cAAc,OAAO,OAAO,wBAAwB,EAC/D,YAAY,6BACb,CAAC"}
@@ -1,4 +1,4 @@
1
- import { i as PublicCollection, l as CollectionDraft, n as Projection, r as ProjectionValues, u as CollectionRead } from "./definition-kxnWUvX9.cjs";
1
+ import { i as PublicCollection, l as CollectionDraft, n as Projection, r as ProjectionValues, u as CollectionRead } from "./definition-DN6yW2vi.cjs";
2
2
 
3
3
  //#region core/src/projection/advanced.d.ts
4
4
  type IncrementalState = Record<string, unknown>;
@@ -1,4 +1,4 @@
1
- import { i as PublicCollection, l as CollectionDraft, n as Projection, r as ProjectionValues, u as CollectionRead } from "./definition-BlMeHwdz.js";
1
+ import { i as PublicCollection, l as CollectionDraft, n as Projection, r as ProjectionValues, u as CollectionRead } from "./definition-B6J0SSNa.js";
2
2
 
3
3
  //#region core/src/projection/advanced.d.ts
4
4
  type IncrementalState = Record<string, unknown>;
package/dist/advanced.js CHANGED
@@ -1,4 +1,5 @@
1
- import { i as defineIncrementalValue, p as snapshot, r as defineIncrementalCollection } from "./definition-DXrKMVs7.js";
1
+ import { i as snapshot } from "./scope-d-L87NeC.js";
2
+ import { n as defineIncrementalValue, t as defineIncrementalCollection } from "./definition-c6XQXzvK.js";
2
3
  //#region core/src/projection/advanced.ts
3
4
  const dependenciesOf = (dependencies) => Object.fromEntries(dependencies.map((dependency, index) => [`d${index}`, dependency]));
4
5
  const sourceChange = (sources) => {
@@ -1 +1 @@
1
- {"version":3,"file":"advanced.js","names":[],"sources":["../core/src/projection/advanced.ts"],"sourcesContent":["import type { Projection, ProjectionValues, PublicCollection } from './definition';\nimport { defineIncrementalCollection, defineIncrementalValue } from './definition';\nimport type { CollectionDraft, CollectionRead, GraphSources } from './contract';\nimport { snapshot } from '../access/scope';\n\nexport type IncrementalState = Record<string, unknown>;\n\nexport type IncrementalValueContext<S, T> = {\n readonly sources: S;\n readonly previous: T | undefined;\n readonly reset: boolean;\n readonly change: unknown;\n readonly cause: unknown;\n readonly state: IncrementalState;\n};\n\nexport type IncrementalValueProcessor<S, T> = (\n context: IncrementalValueContext<S, T>\n) =>\n | T\n | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n | { readonly kind: 'rebuild' };\n\nexport type IncrementalCollectionContext<S, K extends string, V> = {\n readonly sources: S;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly change: unknown;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: IncrementalState;\n readonly output: CollectionDraft<K, V>;\n};\n\nexport type IncrementalCollectionProcessor<S, K extends string, V> = (\n context: IncrementalCollectionContext<S, K, V>\n) => void | { readonly kind: 'rebuild' };\n\ntype DependencyMap<D extends readonly Projection<unknown>[]> = {\n readonly [K in keyof D as `d${Extract<K, number>}`]: D[K];\n};\n\nconst dependenciesOf = <D extends readonly Projection<unknown>[]>(dependencies: D): GraphSources =>\n Object.fromEntries(\n dependencies.map((dependency, index) => [`d${index}`, dependency])\n ) as unknown as GraphSources;\n\nconst sourceChange = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (\n source &&\n typeof source === 'object' &&\n 'change' in source &&\n (source as { readonly change?: unknown }).change !== undefined\n )\n return (source as { readonly change?: unknown }).change;\n }\n return undefined;\n};\n\nconst sourceCause = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (\n source &&\n typeof source === 'object' &&\n 'cause' in source &&\n (source as { readonly cause?: unknown }).cause !== undefined\n )\n return (source as { readonly cause?: unknown }).cause;\n }\n return undefined;\n};\n\nconst publicSource = (source: unknown): unknown => {\n if (!source || typeof source !== 'object') return source;\n if ('value' in source) return (source as { readonly value: unknown }).value;\n if ('read' in source) {\n const read = (source as { readonly read: unknown }).read;\n if (\n read &&\n typeof read === 'object' &&\n 'ids' in read &&\n typeof (read as { ids?: unknown }).ids === 'function'\n ) {\n const result = new Map<string, unknown>();\n for (const id of (read as CollectionRead<string, unknown>).ids())\n result.set(id, (read as CollectionRead<string, unknown>).get(id));\n return result;\n }\n return snapshot(read);\n }\n if ('ids' in source && typeof (source as { ids?: unknown }).ids === 'function') {\n const result = new Map<string, unknown>();\n const read = source as CollectionRead<string, unknown>;\n for (const id of read.ids()) result.set(id, read.get(id));\n return result;\n }\n return source;\n};\n\nconst publicTuple = (sources: Record<string, unknown>): readonly unknown[] =>\n Object.keys(sources)\n .sort((left, right) => Number(left.slice(1)) - Number(right.slice(1)))\n .map(key => publicSource(sources[key]));\n\nconst normalizeValue = <T>(\n result: T | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n): { value: T; state?: IncrementalState } =>\n result && typeof result === 'object' && (result as { readonly kind?: unknown }).kind === 'value'\n ? {\n value: (result as { readonly value: T }).value,\n state: (result as { readonly state?: IncrementalState }).state,\n }\n : { value: result as T };\n\nfunction createIncrementalValue<const D extends readonly Projection<unknown>[], T>(\n dependencies: D,\n processor: IncrementalValueProcessor<ProjectionValues<D>, T>\n): Projection<T> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: IncrementalState = Object.create(null) as IncrementalState;\n let current!: T;\n const build = (sources: Record<string, unknown>) => {\n state = Object.create(null) as IncrementalState;\n const result = processor({\n sources: publicTuple(sources) as ProjectionValues<D>,\n previous: undefined,\n reset: true,\n change: undefined,\n cause: sourceCause(sources),\n state,\n });\n if (result && typeof result === 'object' && 'kind' in result)\n throw new TypeError('Initial incremental processor cannot rebuild.');\n const normalized = normalizeValue(result as T);\n current = normalized.value;\n if (normalized.state) state = normalized.state;\n return {\n value: current,\n update: (nextSources: Record<string, unknown>) => {\n const next = processor({\n sources: publicTuple(nextSources) as ProjectionValues<D>,\n previous: current,\n reset: false,\n change: sourceChange(nextSources),\n cause: sourceCause(nextSources),\n state,\n });\n if (next && typeof next === 'object' && 'kind' in next) return next;\n const nextValue = normalizeValue(next as T);\n current = nextValue.value;\n if (nextValue.state) state = nextValue.state;\n return { kind: 'changed', value: current } as const;\n },\n };\n };\n return defineIncrementalValue({ dependencies: dependencyMap, build: build as never });\n}\n\nfunction createIncrementalCollection<\n const D extends readonly Projection<unknown>[],\n K extends string,\n V,\n>(\n dependencies: D,\n processor: IncrementalCollectionProcessor<ProjectionValues<D>, K, V>\n): Projection<PublicCollection<K, V>> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: IncrementalState = Object.create(null) as IncrementalState;\n const build = (input: {\n readonly sources: Record<string, unknown>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly writer: CollectionDraft<K, V>;\n }) => {\n state = Object.create(null) as IncrementalState;\n const result = processor({\n sources: publicTuple(input.sources) as ProjectionValues<D>,\n previous: input.previous,\n next: input.next,\n reset: true,\n change: undefined,\n cause: sourceCause(input.sources),\n state,\n output: input.writer,\n });\n if (result?.kind === 'rebuild')\n throw new TypeError('Initial incremental collection processor cannot rebuild.');\n return {\n update: (nextInput: typeof input) =>\n processor({\n sources: publicTuple(nextInput.sources) as ProjectionValues<D>,\n previous: nextInput.previous,\n next: nextInput.next,\n reset: false,\n change: sourceChange(nextInput.sources),\n cause: sourceCause(nextInput.sources),\n state,\n output: nextInput.writer,\n }),\n };\n };\n return defineIncrementalCollection({ dependencies: dependencyMap, build: build as never });\n}\n\nexport const incremental = Object.assign(createIncrementalValue, {\n collection: createIncrementalCollection,\n});\n\nexport type IncrementalDependencies<D extends readonly Projection<unknown>[]> = DependencyMap<D>;\n"],"mappings":";;AA0CA,MAAM,kBAA4D,iBAChE,OAAO,YACL,aAAa,KAAK,YAAY,UAAU,CAAC,IAAI,SAAS,WAAW,CAAC,CACnE;AAEH,MAAM,gBAAgB,YAA8C;AAClE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,CACzC,KACE,UACA,OAAO,WAAW,YAClB,YAAY,UACX,OAAyC,WAAW,KAAA,EAErD,QAAQ,OAAyC;;AAKvD,MAAM,eAAe,YAA8C;AACjE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,CACzC,KACE,UACA,OAAO,WAAW,YAClB,WAAW,UACV,OAAwC,UAAU,KAAA,EAEnD,QAAQ,OAAwC;;AAKtD,MAAM,gBAAgB,WAA6B;AACjD,KAAI,CAAC,UAAU,OAAO,WAAW,SAAU,QAAO;AAClD,KAAI,WAAW,OAAQ,QAAQ,OAAuC;AACtE,KAAI,UAAU,QAAQ;EACpB,MAAM,OAAQ,OAAsC;AACpD,MACE,QACA,OAAO,SAAS,YAChB,SAAS,QACT,OAAQ,KAA2B,QAAQ,YAC3C;GACA,MAAM,yBAAS,IAAI,KAAsB;AACzC,QAAK,MAAM,MAAO,KAAyC,KAAK,CAC9D,QAAO,IAAI,IAAK,KAAyC,IAAI,GAAG,CAAC;AACnE,UAAO;;AAET,SAAO,SAAS,KAAK;;AAEvB,KAAI,SAAS,UAAU,OAAQ,OAA6B,QAAQ,YAAY;EAC9E,MAAM,yBAAS,IAAI,KAAsB;EACzC,MAAM,OAAO;AACb,OAAK,MAAM,MAAM,KAAK,KAAK,CAAE,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EH,QAAO,uBAAuB;EAAE,cAAc;EAAsB;EAAgB,CAAC;;AAGvF,SAAS,4BAKP,cACA,WACoC;CACpC,MAAM,gBAAgB,eAAe,aAAa;CAClD,IAAI,QAA0B,OAAO,OAAO,KAAK;CACjD,MAAM,SAAS,UAKT;AACJ,UAAQ,OAAO,OAAO,KAAK;AAW3B,MAVe,UAAU;GACvB,SAAS,YAAY,MAAM,QAAQ;GACnC,UAAU,MAAM;GAChB,MAAM,MAAM;GACZ,OAAO;GACP,QAAQ,KAAA;GACR,OAAO,YAAY,MAAM,QAAQ;GACjC;GACA,QAAQ,MAAM;GACf,CACS,EAAE,SAAS,UACnB,OAAM,IAAI,UAAU,2DAA2D;AACjF,SAAO,EACL,SAAS,cACP,UAAU;GACR,SAAS,YAAY,UAAU,QAAQ;GACvC,UAAU,UAAU;GACpB,MAAM,UAAU;GAChB,OAAO;GACP,QAAQ,aAAa,UAAU,QAAQ;GACvC,OAAO,YAAY,UAAU,QAAQ;GACrC;GACA,QAAQ,UAAU;GACnB,CAAC,EACL;;AAEH,QAAO,4BAA4B;EAAE,cAAc;EAAsB;EAAgB,CAAC;;AAG5F,MAAa,cAAc,OAAO,OAAO,wBAAwB,EAC/D,YAAY,6BACb,CAAC"}
1
+ {"version":3,"file":"advanced.js","names":[],"sources":["../core/src/projection/advanced.ts"],"sourcesContent":["import type { Projection, ProjectionValues, PublicCollection } from './definition';\nimport { defineIncrementalCollection, defineIncrementalValue } from './definition';\nimport type { CollectionDraft, CollectionRead, GraphSources } from './contract';\nimport { snapshot } from '../access/scope';\n\nexport type IncrementalState = Record<string, unknown>;\n\nexport type IncrementalValueContext<S, T> = {\n readonly sources: S;\n readonly previous: T | undefined;\n readonly reset: boolean;\n readonly change: unknown;\n readonly cause: unknown;\n readonly state: IncrementalState;\n};\n\nexport type IncrementalValueProcessor<S, T> = (\n context: IncrementalValueContext<S, T>\n) =>\n | T\n | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n | { readonly kind: 'rebuild' };\n\nexport type IncrementalCollectionContext<S, K extends string, V> = {\n readonly sources: S;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly change: unknown;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: IncrementalState;\n readonly output: CollectionDraft<K, V>;\n};\n\nexport type IncrementalCollectionProcessor<S, K extends string, V> = (\n context: IncrementalCollectionContext<S, K, V>\n) => void | { readonly kind: 'rebuild' };\n\ntype DependencyMap<D extends readonly Projection<unknown>[]> = {\n readonly [K in keyof D as `d${Extract<K, number>}`]: D[K];\n};\n\nconst dependenciesOf = <D extends readonly Projection<unknown>[]>(dependencies: D): GraphSources =>\n Object.fromEntries(\n dependencies.map((dependency, index) => [`d${index}`, dependency])\n ) as unknown as GraphSources;\n\nconst sourceChange = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (\n source &&\n typeof source === 'object' &&\n 'change' in source &&\n (source as { readonly change?: unknown }).change !== undefined\n )\n return (source as { readonly change?: unknown }).change;\n }\n return undefined;\n};\n\nconst sourceCause = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (\n source &&\n typeof source === 'object' &&\n 'cause' in source &&\n (source as { readonly cause?: unknown }).cause !== undefined\n )\n return (source as { readonly cause?: unknown }).cause;\n }\n return undefined;\n};\n\nconst publicSource = (source: unknown): unknown => {\n if (!source || typeof source !== 'object') return source;\n if ('value' in source) return (source as { readonly value: unknown }).value;\n if ('read' in source) {\n const read = (source as { readonly read: unknown }).read;\n if (\n read &&\n typeof read === 'object' &&\n 'ids' in read &&\n typeof (read as { ids?: unknown }).ids === 'function'\n ) {\n const result = new Map<string, unknown>();\n for (const id of (read as CollectionRead<string, unknown>).ids())\n result.set(id, (read as CollectionRead<string, unknown>).get(id));\n return result;\n }\n return snapshot(read);\n }\n if ('ids' in source && typeof (source as { ids?: unknown }).ids === 'function') {\n const result = new Map<string, unknown>();\n const read = source as CollectionRead<string, unknown>;\n for (const id of read.ids()) result.set(id, read.get(id));\n return result;\n }\n return source;\n};\n\nconst publicTuple = (sources: Record<string, unknown>): readonly unknown[] =>\n Object.keys(sources)\n .sort((left, right) => Number(left.slice(1)) - Number(right.slice(1)))\n .map(key => publicSource(sources[key]));\n\nconst normalizeValue = <T>(\n result: T | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n): { value: T; state?: IncrementalState } =>\n result && typeof result === 'object' && (result as { readonly kind?: unknown }).kind === 'value'\n ? {\n value: (result as { readonly value: T }).value,\n state: (result as { readonly state?: IncrementalState }).state,\n }\n : { value: result as T };\n\nfunction createIncrementalValue<const D extends readonly Projection<unknown>[], T>(\n dependencies: D,\n processor: IncrementalValueProcessor<ProjectionValues<D>, T>\n): Projection<T> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: IncrementalState = Object.create(null) as IncrementalState;\n let current!: T;\n const build = (sources: Record<string, unknown>) => {\n state = Object.create(null) as IncrementalState;\n const result = processor({\n sources: publicTuple(sources) as ProjectionValues<D>,\n previous: undefined,\n reset: true,\n change: undefined,\n cause: sourceCause(sources),\n state,\n });\n if (result && typeof result === 'object' && 'kind' in result)\n throw new TypeError('Initial incremental processor cannot rebuild.');\n const normalized = normalizeValue(result as T);\n current = normalized.value;\n if (normalized.state) state = normalized.state;\n return {\n value: current,\n update: (nextSources: Record<string, unknown>) => {\n const next = processor({\n sources: publicTuple(nextSources) as ProjectionValues<D>,\n previous: current,\n reset: false,\n change: sourceChange(nextSources),\n cause: sourceCause(nextSources),\n state,\n });\n if (next && typeof next === 'object' && 'kind' in next) return next;\n const nextValue = normalizeValue(next as T);\n current = nextValue.value;\n if (nextValue.state) state = nextValue.state;\n return { kind: 'changed', value: current } as const;\n },\n };\n };\n return defineIncrementalValue({ dependencies: dependencyMap, build: build as never });\n}\n\nfunction createIncrementalCollection<\n const D extends readonly Projection<unknown>[],\n K extends string,\n V,\n>(\n dependencies: D,\n processor: IncrementalCollectionProcessor<ProjectionValues<D>, K, V>\n): Projection<PublicCollection<K, V>> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: IncrementalState = Object.create(null) as IncrementalState;\n const build = (input: {\n readonly sources: Record<string, unknown>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly writer: CollectionDraft<K, V>;\n }) => {\n state = Object.create(null) as IncrementalState;\n const result = processor({\n sources: publicTuple(input.sources) as ProjectionValues<D>,\n previous: input.previous,\n next: input.next,\n reset: true,\n change: undefined,\n cause: sourceCause(input.sources),\n state,\n output: input.writer,\n });\n if (result?.kind === 'rebuild')\n throw new TypeError('Initial incremental collection processor cannot rebuild.');\n return {\n update: (nextInput: typeof input) =>\n processor({\n sources: publicTuple(nextInput.sources) as ProjectionValues<D>,\n previous: nextInput.previous,\n next: nextInput.next,\n reset: false,\n change: sourceChange(nextInput.sources),\n cause: sourceCause(nextInput.sources),\n state,\n output: nextInput.writer,\n }),\n };\n };\n return defineIncrementalCollection({ dependencies: dependencyMap, build: build as never });\n}\n\nexport const incremental = Object.assign(createIncrementalValue, {\n collection: createIncrementalCollection,\n});\n\nexport type IncrementalDependencies<D extends readonly Projection<unknown>[]> = DependencyMap<D>;\n"],"mappings":";;;AA0CA,MAAM,kBAA4D,iBAChE,OAAO,YACL,aAAa,KAAK,YAAY,UAAU,CAAC,IAAI,SAAS,WAAW,CAAC,CACnE;AAEH,MAAM,gBAAgB,YAA8C;AAClE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,CACzC,KACE,UACA,OAAO,WAAW,YAClB,YAAY,UACX,OAAyC,WAAW,KAAA,EAErD,QAAQ,OAAyC;;AAKvD,MAAM,eAAe,YAA8C;AACjE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,CACzC,KACE,UACA,OAAO,WAAW,YAClB,WAAW,UACV,OAAwC,UAAU,KAAA,EAEnD,QAAQ,OAAwC;;AAKtD,MAAM,gBAAgB,WAA6B;AACjD,KAAI,CAAC,UAAU,OAAO,WAAW,SAAU,QAAO;AAClD,KAA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-456,4 +456,4 @@ type DocumentRuntime<S extends ObjectNode> = DocumentReadable<S> & {
456
456
  };
457
457
  //#endregion
458
458
  export { OptionalNode as $, overlaps as A, DocumentAddress as B, Draft as C, AddressRef as D, snapshot as E, MemberChange as F, DocumentTreeValue as G, DocumentListConfig as H, ValueTransition as I, Infer as J, FieldNode as K, CollectionId as L, resolveAddress as M, Change as N, contains as O, ChangeSet as P, ObjectShape as Q, CollectionNode as R, DocumentImpact as S, replace as T, DocumentNode as U, DocumentAnchor as V, DocumentTreeNode as W, MapNode as X, ListNode as Y, ObjectNode as Z, Unsubscribe as _, Validator as _t, DocumentDisposedError as a, VariantNode as at, MutationIssueCode as b, DocumentReentrancyError as c, list as ct, LocalHistory as d, optional as dt, PathValueOf as et, ObserverError as f, table as ft, TransactionResult as g, ParseIssue as gt, TransactionRejected as h, ParseError as ht, DocumentDiagnostic as i, TreeNode as it, read as j, debugKey as k, DocumentRuntime as l, map as lt, Synchronous as m, variant as mt, CommitSource as n, SchemaPath as nt, DocumentProblem as o, VariantShape as ot, OperationResult as p, tree as pt, ImpactTarget as q, DocumentCommit as r, TableNode as rt, DocumentReadable as s, field as st, CommitListener as t, ReadonlyValue as tt, HistoryState as u, object as ut, Readable as v, parse as vt, Read as w, CollectionImpact as x, MutationIssue as y, CollectionPath as z };
459
- //# sourceMappingURL=contract-BT53Gg94.d.ts.map
459
+ //# sourceMappingURL=contract-CATCCVu5.d.ts.map
@@ -1,57 +1,55 @@
1
- import { J as Infer, L as CollectionId, R as CollectionNode, Z as ObjectNode, _ as Unsubscribe, et as PathValueOf, m as Synchronous, nt as SchemaPath, s as DocumentReadable, tt as ReadonlyValue, v as Readable, x as CollectionImpact, z as CollectionPath } from "./contract-BT53Gg94.js";
1
+ import { J as Infer, L as CollectionId, R as CollectionNode, Z as ObjectNode, _ as Unsubscribe, et as PathValueOf, m as Synchronous, nt as SchemaPath, s as DocumentReadable, tt as ReadonlyValue, v as Readable, x as CollectionImpact, z as CollectionPath } from "./contract-CATCCVu5.js";
2
2
 
3
3
  //#region core/src/projection/contract.d.ts
4
4
  /** Opaque application metadata identifying the action that produced a source event. */
5
5
  type Cause = unknown;
6
- type BatchContext = {
7
- readonly id: number;
8
- readonly cause?: Cause;
9
- };
10
6
  type BatchOptions = {
11
7
  readonly cause?: Cause;
12
8
  };
13
- type CollectionEntryTransition<K extends string, V> = {
14
- readonly key: K;
15
- readonly kind: 'added' | 'updated' | 'removed';
16
- readonly before: V | undefined;
17
- readonly after: V | undefined;
18
- };
19
- type ValueContext<T, D = unknown> = {
20
- readonly value: T;
21
- readonly previous: T;
22
- readonly changed: boolean;
23
- readonly revision: number;
24
- readonly reset: boolean;
25
- readonly detail?: D;
26
- readonly cause?: Cause;
27
- readonly batch?: BatchContext;
28
- };
29
9
  type CollectionRead<K extends string, V> = {
30
10
  get(key: K): V | undefined;
31
11
  has(key: K): boolean;
32
12
  ids(): readonly K[];
33
13
  };
34
- type CollectionContext<K extends string, V, D = unknown> = CollectionRead<K, V> & {
35
- readonly previous: CollectionRead<K, V>;
36
- readonly change: CollectionImpact<K> | undefined;
37
- readonly revision: number;
38
- readonly reset: boolean;
39
- readonly transitions: (keys?: Iterable<K>) => readonly CollectionEntryTransition<K, V>[];
40
- readonly detail?: D;
41
- readonly cause?: Cause;
42
- readonly batch?: BatchContext;
14
+ type ExternalCollectionRead<K extends string, V> = {
15
+ readonly get: (key: K) => V | undefined;
16
+ readonly has: (key: K) => boolean;
17
+ readonly ids: () => readonly K[];
43
18
  };
44
19
  type ExternalValueSource<T, D = unknown> = {
45
20
  readonly kind: 'value';
46
21
  current(): T;
47
22
  revision(): number;
48
- subscribe(listener: (event: ValueContext<T, D>) => void): Unsubscribe;
23
+ subscribe(listener: (event: ExternalValueEvent<T, D>) => void): Unsubscribe;
49
24
  };
50
25
  type ExternalCollectionSource<K extends string, V, D = unknown> = {
51
26
  readonly kind: 'collection';
52
- current(): CollectionRead<K, V>;
27
+ current(): ExternalCollectionRead<K, V>;
53
28
  revision(): number;
54
- subscribe(listener: (event: CollectionContext<K, V, D>) => void): Unsubscribe;
29
+ subscribe(listener: (event: ExternalCollectionEvent<K, V, D>) => void): Unsubscribe;
30
+ };
31
+ type ExternalValueEvent<T, D = unknown> = {
32
+ readonly value: T;
33
+ readonly revision: number;
34
+ readonly reset?: boolean;
35
+ readonly detail?: D;
36
+ readonly cause?: Cause;
37
+ readonly batch?: {
38
+ readonly id: number;
39
+ readonly cause?: Cause;
40
+ };
41
+ };
42
+ type ExternalCollectionEvent<K extends string, V, D = unknown> = {
43
+ readonly previous: ExternalCollectionRead<K, V>;
44
+ readonly revision: number;
45
+ readonly reset?: boolean;
46
+ readonly change?: CollectionImpact<K>;
47
+ readonly detail?: D;
48
+ readonly cause?: Cause;
49
+ readonly batch?: {
50
+ readonly id: number;
51
+ readonly cause?: Cause;
52
+ };
55
53
  };
56
54
  type CollectionDraft<K extends string, V> = {
57
55
  set(key: K, value: V): void;
@@ -85,14 +83,17 @@ type PublicCollection<K extends string, V> = ReadonlyMap<K, V>;
85
83
  type ObservedCollection<P extends CollectionPath> = PublicCollection<CollectionId<P>, ReadonlyValue<Infer<CollectionNode<P>>>>;
86
84
  declare const input: <T>(initial: T, equality?: (previous: T, next: T) => boolean) => Input<T>;
87
85
  /**
88
- * Establishes a reactive boundary from a document. The selector is compiled
89
- * when the definition is materialized, so declarations stay lazy and reusable.
86
+ * Establishes a reactive boundary from a document, readable, or external
87
+ * source. The selector is compiled when the definition is materialized, so
88
+ * declarations stay lazy and reusable.
90
89
  */
91
90
  declare function observe<S extends ObjectNode>(document: DocumentReadable<S>): Projection<Infer<S>>;
91
+ declare function observe<T, D>(source: ExternalValueSource<T, D>): Projection<T>;
92
+ declare function observe<K extends string, V, D>(source: ExternalCollectionSource<K, V, D>): Projection<PublicCollection<K, V>>;
92
93
  declare function observe<T>(readable: Readable<T>): Projection<T>;
93
94
  declare function observe<S extends ObjectNode, P extends CollectionPath>(document: DocumentReadable<S>, selector: (path: SchemaPath<S['shape']>) => P): Projection<ObservedCollection<P>>;
94
95
  declare function observe<S extends ObjectNode, P>(document: DocumentReadable<S>, selector: (path: SchemaPath<S['shape']>) => P): Projection<PathValueOf<P>>;
95
96
  declare function derive<const D extends readonly Projection<unknown>[], T>(dependencies: D, compute: (...values: ProjectionValues<D>) => Synchronous<T>, equality?: (previous: T, next: T) => boolean): Projection<T>;
96
97
  //#endregion
97
- export { derive as a, BatchOptions as c, ExternalCollectionSource as d, ExternalValueSource as f, PublicCollection as i, CollectionDraft as l, ProjectionError as m, Projection as n, input as o, ProjectionDisposedError as p, ProjectionValues as r, observe as s, Input as t, CollectionRead as u };
98
- //# sourceMappingURL=definition-BlMeHwdz.d.ts.map
98
+ export { ProjectionError as _, derive as a, BatchOptions as c, ExternalCollectionEvent as d, ExternalCollectionRead as f, ProjectionDisposedError as g, ExternalValueSource as h, PublicCollection as i, CollectionDraft as l, ExternalValueEvent as m, Projection as n, input as o, ExternalCollectionSource as p, ProjectionValues as r, observe as s, Input as t, CollectionRead as u };
99
+ //# sourceMappingURL=definition-B6J0SSNa.d.ts.map
@@ -3,55 +3,53 @@ import { J as Infer, L as CollectionId, R as CollectionNode, Z as ObjectNode, _
3
3
  //#region core/src/projection/contract.d.ts
4
4
  /** Opaque application metadata identifying the action that produced a source event. */
5
5
  type Cause = unknown;
6
- type BatchContext = {
7
- readonly id: number;
8
- readonly cause?: Cause;
9
- };
10
6
  type BatchOptions = {
11
7
  readonly cause?: Cause;
12
8
  };
13
- type CollectionEntryTransition<K extends string, V> = {
14
- readonly key: K;
15
- readonly kind: 'added' | 'updated' | 'removed';
16
- readonly before: V | undefined;
17
- readonly after: V | undefined;
18
- };
19
- type ValueContext<T, D = unknown> = {
20
- readonly value: T;
21
- readonly previous: T;
22
- readonly changed: boolean;
23
- readonly revision: number;
24
- readonly reset: boolean;
25
- readonly detail?: D;
26
- readonly cause?: Cause;
27
- readonly batch?: BatchContext;
28
- };
29
9
  type CollectionRead<K extends string, V> = {
30
10
  get(key: K): V | undefined;
31
11
  has(key: K): boolean;
32
12
  ids(): readonly K[];
33
13
  };
34
- type CollectionContext<K extends string, V, D = unknown> = CollectionRead<K, V> & {
35
- readonly previous: CollectionRead<K, V>;
36
- readonly change: CollectionImpact<K> | undefined;
37
- readonly revision: number;
38
- readonly reset: boolean;
39
- readonly transitions: (keys?: Iterable<K>) => readonly CollectionEntryTransition<K, V>[];
40
- readonly detail?: D;
41
- readonly cause?: Cause;
42
- readonly batch?: BatchContext;
14
+ type ExternalCollectionRead<K extends string, V> = {
15
+ readonly get: (key: K) => V | undefined;
16
+ readonly has: (key: K) => boolean;
17
+ readonly ids: () => readonly K[];
43
18
  };
44
19
  type ExternalValueSource<T, D = unknown> = {
45
20
  readonly kind: 'value';
46
21
  current(): T;
47
22
  revision(): number;
48
- subscribe(listener: (event: ValueContext<T, D>) => void): Unsubscribe;
23
+ subscribe(listener: (event: ExternalValueEvent<T, D>) => void): Unsubscribe;
49
24
  };
50
25
  type ExternalCollectionSource<K extends string, V, D = unknown> = {
51
26
  readonly kind: 'collection';
52
- current(): CollectionRead<K, V>;
27
+ current(): ExternalCollectionRead<K, V>;
53
28
  revision(): number;
54
- subscribe(listener: (event: CollectionContext<K, V, D>) => void): Unsubscribe;
29
+ subscribe(listener: (event: ExternalCollectionEvent<K, V, D>) => void): Unsubscribe;
30
+ };
31
+ type ExternalValueEvent<T, D = unknown> = {
32
+ readonly value: T;
33
+ readonly revision: number;
34
+ readonly reset?: boolean;
35
+ readonly detail?: D;
36
+ readonly cause?: Cause;
37
+ readonly batch?: {
38
+ readonly id: number;
39
+ readonly cause?: Cause;
40
+ };
41
+ };
42
+ type ExternalCollectionEvent<K extends string, V, D = unknown> = {
43
+ readonly previous: ExternalCollectionRead<K, V>;
44
+ readonly revision: number;
45
+ readonly reset?: boolean;
46
+ readonly change?: CollectionImpact<K>;
47
+ readonly detail?: D;
48
+ readonly cause?: Cause;
49
+ readonly batch?: {
50
+ readonly id: number;
51
+ readonly cause?: Cause;
52
+ };
55
53
  };
56
54
  type CollectionDraft<K extends string, V> = {
57
55
  set(key: K, value: V): void;
@@ -85,14 +83,17 @@ type PublicCollection<K extends string, V> = ReadonlyMap<K, V>;
85
83
  type ObservedCollection<P extends CollectionPath> = PublicCollection<CollectionId<P>, ReadonlyValue<Infer<CollectionNode<P>>>>;
86
84
  declare const input: <T>(initial: T, equality?: (previous: T, next: T) => boolean) => Input<T>;
87
85
  /**
88
- * Establishes a reactive boundary from a document. The selector is compiled
89
- * when the definition is materialized, so declarations stay lazy and reusable.
86
+ * Establishes a reactive boundary from a document, readable, or external
87
+ * source. The selector is compiled when the definition is materialized, so
88
+ * declarations stay lazy and reusable.
90
89
  */
91
90
  declare function observe<S extends ObjectNode>(document: DocumentReadable<S>): Projection<Infer<S>>;
91
+ declare function observe<T, D>(source: ExternalValueSource<T, D>): Projection<T>;
92
+ declare function observe<K extends string, V, D>(source: ExternalCollectionSource<K, V, D>): Projection<PublicCollection<K, V>>;
92
93
  declare function observe<T>(readable: Readable<T>): Projection<T>;
93
94
  declare function observe<S extends ObjectNode, P extends CollectionPath>(document: DocumentReadable<S>, selector: (path: SchemaPath<S['shape']>) => P): Projection<ObservedCollection<P>>;
94
95
  declare function observe<S extends ObjectNode, P>(document: DocumentReadable<S>, selector: (path: SchemaPath<S['shape']>) => P): Projection<PathValueOf<P>>;
95
96
  declare function derive<const D extends readonly Projection<unknown>[], T>(dependencies: D, compute: (...values: ProjectionValues<D>) => Synchronous<T>, equality?: (previous: T, next: T) => boolean): Projection<T>;
96
97
  //#endregion
97
- export { derive as a, BatchOptions as c, ExternalCollectionSource as d, ExternalValueSource as f, PublicCollection as i, CollectionDraft as l, ProjectionError as m, Projection as n, input as o, ProjectionDisposedError as p, ProjectionValues as r, observe as s, Input as t, CollectionRead as u };
98
- //# sourceMappingURL=definition-kxnWUvX9.d.cts.map
98
+ export { ProjectionError as _, derive as a, BatchOptions as c, ExternalCollectionEvent as d, ExternalCollectionRead as f, ProjectionDisposedError as g, ExternalValueSource as h, PublicCollection as i, CollectionDraft as l, ExternalValueEvent as m, Projection as n, input as o, ExternalCollectionSource as p, ProjectionValues as r, observe as s, Input as t, CollectionRead as u };
99
+ //# sourceMappingURL=definition-DN6yW2vi.d.cts.map
@@ -0,0 +1,107 @@
1
+ //#region core/src/projection/definition.ts
2
+ const definitions = /* @__PURE__ */ new WeakMap();
3
+ const define = (definition) => {
4
+ const handle = Object.freeze({});
5
+ definitions.set(handle, definition);
6
+ return handle;
7
+ };
8
+ const definitionOf = (projection) => {
9
+ const definition = definitions.get(projection);
10
+ if (!definition) throw new TypeError("Unknown projection definition.");
11
+ return definition;
12
+ };
13
+ const input = (initial, equality = Object.is) => define({
14
+ kind: "input",
15
+ initial,
16
+ isEqual: equality
17
+ });
18
+ function observe(document, selector) {
19
+ if (isExternalSource(document)) return document.kind === "collection" ? defineExternalCollection(document) : defineExternalValue(document);
20
+ if ("current" in document && typeof document.current === "function") return defineReadable(document);
21
+ return define({
22
+ kind: "document",
23
+ document,
24
+ selector
25
+ });
26
+ }
27
+ const isExternalSource = (source) => {
28
+ if (!source || typeof source !== "object") return false;
29
+ const candidate = source;
30
+ return (candidate.kind === "value" || candidate.kind === "collection") && typeof candidate.current === "function" && typeof candidate.revision === "function" && typeof candidate.subscribe === "function";
31
+ };
32
+ function derive(dependencies, compute, equality = Object.is) {
33
+ if (!Array.isArray(dependencies) || dependencies.some((value) => !definitions.has(value))) throw new TypeError("derive dependencies must be projections.");
34
+ return define({
35
+ kind: "derive",
36
+ dependencies: Object.freeze([...dependencies]),
37
+ compute,
38
+ isEqual: equality
39
+ });
40
+ }
41
+ /** Internal source adapters used by observe, not exported publicly. */
42
+ const defineReadable = (readable, equality = Object.is) => define({
43
+ kind: "readable",
44
+ readable,
45
+ isEqual: equality
46
+ });
47
+ const defineExternalValue = (source) => define({
48
+ kind: "source-value",
49
+ source
50
+ });
51
+ const defineExternalCollection = (source) => define({
52
+ kind: "source-collection",
53
+ source
54
+ });
55
+ const defineIncrementalValue = (definition) => define({
56
+ kind: "incremental-value",
57
+ dependencies: definition.dependencies,
58
+ build: definition.build,
59
+ isEqual: definition.isEqual,
60
+ name: definition.name
61
+ });
62
+ const defineIncrementalCollection = (definition) => define({
63
+ kind: "incremental-collection",
64
+ dependencies: definition.dependencies,
65
+ build: definition.build,
66
+ isEqual: definition.isEqual,
67
+ name: definition.name
68
+ });
69
+ //#endregion
70
+ Object.defineProperty(exports, "defineIncrementalCollection", {
71
+ enumerable: true,
72
+ get: function() {
73
+ return defineIncrementalCollection;
74
+ }
75
+ });
76
+ Object.defineProperty(exports, "defineIncrementalValue", {
77
+ enumerable: true,
78
+ get: function() {
79
+ return defineIncrementalValue;
80
+ }
81
+ });
82
+ Object.defineProperty(exports, "definitionOf", {
83
+ enumerable: true,
84
+ get: function() {
85
+ return definitionOf;
86
+ }
87
+ });
88
+ Object.defineProperty(exports, "derive", {
89
+ enumerable: true,
90
+ get: function() {
91
+ return derive;
92
+ }
93
+ });
94
+ Object.defineProperty(exports, "input", {
95
+ enumerable: true,
96
+ get: function() {
97
+ return input;
98
+ }
99
+ });
100
+ Object.defineProperty(exports, "observe", {
101
+ enumerable: true,
102
+ get: function() {
103
+ return observe;
104
+ }
105
+ });
106
+
107
+ //# sourceMappingURL=definition-Z6TagMup.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definition-Z6TagMup.cjs","names":[],"sources":["../core/src/projection/definition.ts"],"sourcesContent":["import type { DocumentReadable, Synchronous } from '../runtime/contract';\nimport type {\n CollectionId,\n CollectionPath,\n CollectionNode as SchemaCollectionNode,\n Infer,\n ObjectNode,\n ReadonlyValue,\n SchemaPath,\n PathValueOf,\n} from '../schema';\nimport type { Readable } from './readable';\nimport type {\n BatchContext,\n Cause,\n CollectionContext,\n CollectionDraft,\n CollectionRead,\n CollectionEntryTransition,\n DocumentContext,\n ExternalCollectionSource,\n ExternalValueSource,\n GraphSources,\n InputHandles,\n NodeUpdate,\n ValueContext,\n} from './contract';\n\ndeclare const projectionDefinition: unique symbol;\ndeclare const writableInput: unique symbol;\n\n/** A lazy, reusable projection definition. Its value is materialized per runtime. */\nexport type Projection<T> = {\n readonly [projectionDefinition]: T;\n};\n\n/** A runtime-local writable projection definition. */\nexport type Input<T> = Projection<T> & { readonly [writableInput]: true };\n\nexport type ProjectionValues<D extends readonly Projection<unknown>[]> = {\n readonly [K in keyof D]: D[K] extends Projection<infer T> ? T : never;\n};\n\nexport type PublicCollection<K extends string, V> = ReadonlyMap<K, V>;\ntype ObservedCollection<P extends CollectionPath> = PublicCollection<\n CollectionId<P>,\n ReadonlyValue<Infer<SchemaCollectionNode<P>>>\n>;\n\ntype Equality = (a: unknown, b: unknown) => boolean;\ntype PathSelector<S extends ObjectNode> = (path: SchemaPath<S['shape']>) => unknown;\n\ntype Definition =\n | { readonly kind: 'input'; readonly initial: unknown; readonly isEqual?: Equality }\n | { readonly kind: 'readable'; readonly readable: Readable<unknown>; readonly isEqual?: Equality }\n | { readonly kind: 'source-value'; readonly source: ExternalValueSource<unknown, unknown> }\n | {\n readonly kind: 'source-collection';\n readonly source: ExternalCollectionSource<string, unknown, unknown>;\n }\n | {\n readonly kind: 'document';\n readonly document: DocumentReadable<ObjectNode>;\n readonly selector?: PathSelector<ObjectNode>;\n }\n | {\n readonly kind: 'derive';\n readonly dependencies: readonly Projection<unknown>[];\n readonly compute: (...values: readonly unknown[]) => unknown;\n readonly isEqual?: Equality;\n }\n | {\n readonly kind: 'incremental-value';\n readonly dependencies: GraphSources;\n readonly build: (...args: never[]) => unknown;\n readonly isEqual?: Equality;\n readonly name?: string;\n }\n | {\n readonly kind: 'incremental-collection';\n readonly dependencies: GraphSources;\n readonly build: (...args: never[]) => unknown;\n readonly isEqual?: Equality;\n readonly name?: string;\n };\n\ntype DefinitionProjection = Projection<unknown>;\nconst definitions = new WeakMap<object, Definition>();\n\nconst define = <T>(definition: Definition): Projection<T> => {\n const handle = Object.freeze({}) as Projection<T>;\n definitions.set(handle, definition);\n return handle;\n};\n\nexport const definitionOf = (projection: DefinitionProjection): Definition => {\n const definition = definitions.get(projection);\n if (!definition) throw new TypeError('Unknown projection definition.');\n return definition;\n};\n\nexport const input = <T>(\n initial: T,\n equality: (previous: T, next: T) => boolean = Object.is\n): Input<T> => define<T>({ kind: 'input', initial, isEqual: equality as Equality }) as Input<T>;\n\n/**\n * Establishes a reactive boundary from a document, readable, or external\n * source. The selector is compiled when the definition is materialized, so\n * declarations stay lazy and reusable.\n */\nexport function observe<S extends ObjectNode>(document: DocumentReadable<S>): Projection<Infer<S>>;\nexport function observe<T, D>(source: ExternalValueSource<T, D>): Projection<T>;\nexport function observe<K extends string, V, D>(\n source: ExternalCollectionSource<K, V, D>\n): Projection<PublicCollection<K, V>>;\nexport function observe<T>(readable: Readable<T>): Projection<T>;\nexport function observe<S extends ObjectNode, P extends CollectionPath>(\n document: DocumentReadable<S>,\n selector: (path: SchemaPath<S['shape']>) => P\n): Projection<ObservedCollection<P>>;\nexport function observe<S extends ObjectNode, P>(\n document: DocumentReadable<S>,\n selector: (path: SchemaPath<S['shape']>) => P\n): Projection<PathValueOf<P>>;\nexport function observe<S extends ObjectNode>(\n document:\n | DocumentReadable<S>\n | Readable<unknown>\n | ExternalValueSource<unknown, unknown>\n | ExternalCollectionSource<string, unknown, unknown>,\n selector?: PathSelector<S>\n): Projection<unknown> {\n if (isExternalSource(document))\n return document.kind === 'collection'\n ? defineExternalCollection(document)\n : defineExternalValue(document);\n if ('current' in document && typeof document.current === 'function')\n return defineReadable(document as Readable<unknown>);\n return define({\n kind: 'document',\n document: document as DocumentReadable<ObjectNode>,\n selector: selector as PathSelector<ObjectNode> | undefined,\n });\n}\n\nconst isExternalSource = (\n source: unknown\n): source is\n ExternalValueSource<unknown, unknown> | ExternalCollectionSource<string, unknown, unknown> => {\n if (!source || typeof source !== 'object') return false;\n const candidate = source as {\n readonly kind?: unknown;\n readonly current?: unknown;\n readonly revision?: unknown;\n readonly subscribe?: unknown;\n };\n return (\n (candidate.kind === 'value' || candidate.kind === 'collection') &&\n typeof candidate.current === 'function' &&\n typeof candidate.revision === 'function' &&\n typeof candidate.subscribe === 'function'\n );\n};\n\nexport function derive<const D extends readonly Projection<unknown>[], T>(\n dependencies: D,\n compute: (...values: ProjectionValues<D>) => Synchronous<T>,\n equality: (previous: T, next: T) => boolean = Object.is\n): Projection<T> {\n if (!Array.isArray(dependencies) || dependencies.some(value => !definitions.has(value)))\n throw new TypeError('derive dependencies must be projections.');\n const frozen = Object.freeze([...dependencies]) as readonly Projection<unknown>[];\n return define<T>({\n kind: 'derive',\n dependencies: frozen,\n compute: compute as (...values: readonly unknown[]) => unknown,\n isEqual: equality as Equality,\n });\n}\n\n/** Internal source adapters used by observe, not exported publicly. */\nexport const defineReadable = <T>(\n readable: Readable<T>,\n equality: (a: T, b: T) => boolean = Object.is\n): Projection<T> => define<T>({ kind: 'readable', readable, isEqual: equality as Equality });\n\nexport const defineExternalValue = <T, D>(source: ExternalValueSource<T, D>): Projection<T> =>\n define<T>({ kind: 'source-value', source: source as never });\n\nexport const defineExternalCollection = <K extends string, V, D>(\n source: ExternalCollectionSource<K, V, D>\n): Projection<PublicCollection<K, V>> =>\n define<PublicCollection<K, V>>({ kind: 'source-collection', source: source as never });\n\nexport type IncrementalValueDefinition = Extract<Definition, { kind: 'incremental-value' }>;\nexport type IncrementalCollectionDefinition = Extract<\n Definition,\n { kind: 'incremental-collection' }\n>;\n\nexport const defineIncrementalValue = <T>(definition: {\n readonly dependencies: GraphSources;\n readonly build: (...args: never[]) => unknown;\n readonly isEqual?: (a: T, b: T) => boolean;\n readonly name?: string;\n}): Projection<T> =>\n define<T>({\n kind: 'incremental-value',\n dependencies: definition.dependencies,\n build: definition.build,\n isEqual: definition.isEqual as Equality | undefined,\n name: definition.name,\n });\n\nexport const defineIncrementalCollection = <K extends string, V>(definition: {\n readonly dependencies: GraphSources;\n readonly build: (...args: never[]) => unknown;\n readonly isEqual?: (a: V, b: V) => boolean;\n readonly name?: string;\n}): Projection<PublicCollection<K, V>> =>\n define<PublicCollection<K, V>>({\n kind: 'incremental-collection',\n dependencies: definition.dependencies,\n build: definition.build,\n isEqual: definition.isEqual as Equality | undefined,\n name: definition.name,\n });\n\n// Internal event names remain private implementation detail and are not root exports.\nexport type InternalDocumentEvent<S extends ObjectNode> = DocumentContext<S>;\nexport type InternalValueEvent<T, D = unknown> = ValueContext<T, D>;\nexport type InternalCollectionEvent<K extends string, V, D = unknown> = CollectionContext<K, V, D>;\nexport type InternalCollectionTransition<K extends string, V> = CollectionEntryTransition<K, V>;\nexport type InternalCollectionDraft<K extends string, V> = CollectionDraft<K, V>;\nexport type InternalBatchContext = BatchContext;\nexport type InternalCause = Cause;\nexport type InternalInputHandles<S extends GraphSources> = InputHandles<S>;\nexport type InternalNodeUpdate<T> = NodeUpdate<T>;\nexport type InternalCollectionRead<K extends string, V> = CollectionRead<K, V>;\n"],"mappings":";AAuFA,MAAM,8BAAc,IAAI,SAA6B;AAErD,MAAM,UAAa,eAA0C;CAC3D,MAAM,SAAS,OAAO,OAAO,EAAE,CAAC;AAChC,aAAY,IAAI,QAAQ,WAAW;AACnC,QAAO;;AAGT,MAAa,gBAAgB,eAAiD;CAC5E,MAAM,aAAa,YAAY,IAAI,WAAW;AAC9C,KAAI,CAAC,WAAY,OAAM,IAAI,UAAU,iCAAiC;AACtE,QAAO;;AAGT,MAAa,SACX,SACA,WAA8C,OAAO,OACxC,OAAU;CAAE,MAAM;CAAS;CAAS,SAAS;CAAsB,CAAC;AAqBnF,SAAgB,QACd,UAKA,UACqB;AACrB,KAAI,iBAAiB,SAAS,CAC5B,QAAO,SAAS,SAAS,eACrB,yBAAyB,SAAS,GAClC,oBAAoB,SAAS;AACnC,KAAI,aAAa,YAAY,OAAO,SAAS,YAAY,WACvD,QAAO,eAAe,SAA8B;AACtD,QAAO,OAAO;EACZ,MAAM;EACI;EACA;EACX,CAAC;;AAGJ,MAAM,oBACJ,WAE8F;AAC9F,KAAI,CAAC,UAAU,OAAO,WAAW,SAAU,QAAO;CAClD,MAAM,YAAY;AAMlB,SACG,UAAU,SAAS,WAAW,UAAU,SAAS,iBAClD,OAAO,UAAU,YAAY,cAC7B,OAAO,UAAU,aAAa,cAC9B,OAAO,UAAU,cAAc;;AAInC,SAAgB,OACd,cACA,SACA,WAA8C,OAAO,IACtC;AACf,KAAI,CAAC,MAAM,QAAQ,aAAa,IAAI,aAAa,MAAK,UAAS,CAAC,YAAY,IAAI,MAAM,CAAC,CACrF,OAAM,IAAI,UAAU,2CAA2C;AAEjE,QAAO,OAAU;EACf,MAAM;EACN,cAHa,OAAO,OAAO,CAAC,GAAG,aAAa,CAGxB;EACX;EACT,SAAS;EACV,CAAC;;;AAIJ,MAAa,kBACX,UACA,WAAoC,OAAO,OACzB,OAAU;CAAE,MAAM;CAAY;CAAU,SAAS;CAAsB,CAAC;AAE5F,MAAa,uBAA6B,WACxC,OAAU;CAAE,MAAM;CAAwB;CAAiB,CAAC;AAE9D,MAAa,4BACX,WAEA,OAA+B;CAAE,MAAM;CAA6B;CAAiB,CAAC;AAQxF,MAAa,0BAA6B,eAMxC,OAAU;CACR,MAAM;CACN,cAAc,WAAW;CACzB,OAAO,WAAW;CAClB,SAAS,WAAW;CACpB,MAAM,WAAW;CAClB,CAAC;AAEJ,MAAa,+BAAoD,eAM/D,OAA+B;CAC7B,MAAM;CACN,cAAc,WAAW;CACzB,OAAO,WAAW;CAClB,SAAS,WAAW;CACpB,MAAM,WAAW;CAClB,CAAC"}
@@ -0,0 +1,72 @@
1
+ //#region core/src/projection/definition.ts
2
+ const definitions = /* @__PURE__ */ new WeakMap();
3
+ const define = (definition) => {
4
+ const handle = Object.freeze({});
5
+ definitions.set(handle, definition);
6
+ return handle;
7
+ };
8
+ const definitionOf = (projection) => {
9
+ const definition = definitions.get(projection);
10
+ if (!definition) throw new TypeError("Unknown projection definition.");
11
+ return definition;
12
+ };
13
+ const input = (initial, equality = Object.is) => define({
14
+ kind: "input",
15
+ initial,
16
+ isEqual: equality
17
+ });
18
+ function observe(document, selector) {
19
+ if (isExternalSource(document)) return document.kind === "collection" ? defineExternalCollection(document) : defineExternalValue(document);
20
+ if ("current" in document && typeof document.current === "function") return defineReadable(document);
21
+ return define({
22
+ kind: "document",
23
+ document,
24
+ selector
25
+ });
26
+ }
27
+ const isExternalSource = (source) => {
28
+ if (!source || typeof source !== "object") return false;
29
+ const candidate = source;
30
+ return (candidate.kind === "value" || candidate.kind === "collection") && typeof candidate.current === "function" && typeof candidate.revision === "function" && typeof candidate.subscribe === "function";
31
+ };
32
+ function derive(dependencies, compute, equality = Object.is) {
33
+ if (!Array.isArray(dependencies) || dependencies.some((value) => !definitions.has(value))) throw new TypeError("derive dependencies must be projections.");
34
+ return define({
35
+ kind: "derive",
36
+ dependencies: Object.freeze([...dependencies]),
37
+ compute,
38
+ isEqual: equality
39
+ });
40
+ }
41
+ /** Internal source adapters used by observe, not exported publicly. */
42
+ const defineReadable = (readable, equality = Object.is) => define({
43
+ kind: "readable",
44
+ readable,
45
+ isEqual: equality
46
+ });
47
+ const defineExternalValue = (source) => define({
48
+ kind: "source-value",
49
+ source
50
+ });
51
+ const defineExternalCollection = (source) => define({
52
+ kind: "source-collection",
53
+ source
54
+ });
55
+ const defineIncrementalValue = (definition) => define({
56
+ kind: "incremental-value",
57
+ dependencies: definition.dependencies,
58
+ build: definition.build,
59
+ isEqual: definition.isEqual,
60
+ name: definition.name
61
+ });
62
+ const defineIncrementalCollection = (definition) => define({
63
+ kind: "incremental-collection",
64
+ dependencies: definition.dependencies,
65
+ build: definition.build,
66
+ isEqual: definition.isEqual,
67
+ name: definition.name
68
+ });
69
+ //#endregion
70
+ export { input as a, derive as i, defineIncrementalValue as n, observe as o, definitionOf as r, defineIncrementalCollection as t };
71
+
72
+ //# sourceMappingURL=definition-c6XQXzvK.js.map