doxum 0.1.16 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -255,7 +255,7 @@ const doubled = incremental.collection([tasks], ({ sources, output }) => {
255
255
  ```
256
256
 
257
257
  Incremental processors receive a dependency-aligned `changes` tuple. Collection
258
- entries carry discriminated `added`/`updated`/`removed` transitions with complete
258
+ entries carry `added`/`updated`/`removed` transitions with complete
259
259
  `before`/`after` values, so a processor can patch indexes without rescanning the
260
260
  collection; scalar dependencies use `undefined` and the initial collection build
261
261
  reports `{ kind: 'reset' }`.
package/dist/advanced.cjs CHANGED
@@ -1,62 +1,51 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_scope = require("./scope-Ezs-Y3TY.cjs");
3
- const require_definition = require("./definition-Z6TagMup.cjs");
3
+ const require_definition = require("./definition-De6rhTlZ.cjs");
4
4
  //#region core/src/projection/advanced.ts
5
- const dependenciesOf = (dependencies) => Object.fromEntries(dependencies.map((dependency, index) => [`d${index}`, dependency]));
6
5
  const resetCollectionChange = Object.freeze({ kind: "reset" });
7
- const isCollectionSource = (source) => Boolean(source && typeof source === "object" && "ids" in source && typeof source.ids === "function" && "previous" in source && "transitions" in source && typeof source.transitions === "function");
6
+ const isRebuild = (value) => value !== null && typeof value === "object" && value.kind === "rebuild";
8
7
  const collectionChange = (source) => {
9
- if (!isCollectionSource(source)) return void 0;
10
- if (source.reset || source.change?.kind === "reset") return resetCollectionChange;
11
- const impact = source.change;
12
- if (!impact) return void 0;
13
- const added = [];
14
- const updated = [];
15
- const removed = [];
16
- for (const transition of source.transitions()) if (transition.kind === "added") added.push({
17
- key: transition.key,
18
- kind: transition.kind,
19
- after: transition.after
20
- });
21
- else if (transition.kind === "updated") updated.push(transition);
22
- else removed.push({
23
- key: transition.key,
24
- kind: transition.kind,
25
- before: transition.before
26
- });
27
- const order = impact.orderChanged ? {
28
- before: Object.freeze([...source.previous.ids()]),
29
- after: Object.freeze([...source.ids()])
30
- } : void 0;
31
- if (!added.length && !updated.length && !removed.length && !order) return void 0;
32
- return Object.freeze({
33
- kind: "incremental",
34
- added: Object.freeze(added),
35
- updated: Object.freeze(updated),
36
- removed: Object.freeze(removed),
37
- ...order ? { order: Object.freeze(order) } : {}
38
- });
8
+ if (!source || typeof source !== "object" || !("kind" in source)) return void 0;
9
+ if (source.kind !== "collection") return void 0;
10
+ return source.change;
39
11
  };
40
12
  const sourceCause = (sources) => {
41
- for (const source of Object.values(sources)) if (source && typeof source === "object" && "cause" in source && source.cause !== void 0) return source.cause;
13
+ for (const source of Object.values(sources)) if (source && typeof source === "object" && "kind" in source) {
14
+ const cause = source.cause;
15
+ if (cause !== void 0) return cause;
16
+ }
17
+ };
18
+ const collectionView = (read) => {
19
+ const entries = function* () {
20
+ for (const key of read.ids()) yield [key, read.get(key)];
21
+ };
22
+ const values = function* () {
23
+ for (const key of read.ids()) yield read.get(key);
24
+ };
25
+ const view = {
26
+ get: (key) => read.get(key),
27
+ has: (key) => read.has(key),
28
+ get size() {
29
+ return read.ids().length;
30
+ },
31
+ keys: () => read.ids()[Symbol.iterator](),
32
+ values,
33
+ entries,
34
+ forEach: (callback, thisArg) => {
35
+ for (const key of read.ids()) callback.call(thisArg, read.get(key), key, view);
36
+ },
37
+ [Symbol.iterator]: entries
38
+ };
39
+ return Object.freeze(view);
42
40
  };
43
41
  const publicSource = (source) => {
44
42
  if (!source || typeof source !== "object") return source;
45
- if ("value" in source) return source.value;
46
- if ("read" in source) {
43
+ if (!("kind" in source)) return source;
44
+ if (source.kind === "value") return source.value;
45
+ if (source.kind === "document") return require_scope.snapshot(source.read);
46
+ if (source.kind === "collection") {
47
47
  const read = source.read;
48
- if (read && typeof read === "object" && "ids" in read && typeof read.ids === "function") {
49
- const result = /* @__PURE__ */ new Map();
50
- for (const id of read.ids()) result.set(id, read.get(id));
51
- return result;
52
- }
53
- return require_scope.snapshot(read);
54
- }
55
- if ("ids" in source && typeof source.ids === "function") {
56
- const result = /* @__PURE__ */ new Map();
57
- const read = source;
58
- for (const id of read.ids()) result.set(id, read.get(id));
59
- return result;
48
+ return collectionView(read);
60
49
  }
61
50
  return source;
62
51
  };
@@ -66,17 +55,13 @@ const publicInputs = (sources, initial = false) => {
66
55
  for (const key of Object.keys(sources).sort((left, right) => Number(left.slice(1)) - Number(right.slice(1)))) {
67
56
  const source = sources[key];
68
57
  values.push(publicSource(source));
69
- changes.push(initial && isCollectionSource(source) ? resetCollectionChange : collectionChange(source));
58
+ changes.push(initial && source && typeof source === "object" && "kind" in source && source.kind === "collection" ? resetCollectionChange : collectionChange(source));
70
59
  }
71
60
  return {
72
61
  values,
73
62
  changes
74
63
  };
75
64
  };
76
- const normalizeValue = (result) => result && typeof result === "object" && result.kind === "value" ? {
77
- value: result.value,
78
- state: result.state
79
- } : { value: result };
80
65
  function createIncrementalValue(dependencies, processor) {
81
66
  const dependencyMap = dependenciesOf(dependencies);
82
67
  let state = Object.create(null);
@@ -92,10 +77,8 @@ function createIncrementalValue(dependencies, processor) {
92
77
  cause: sourceCause(sources),
93
78
  state
94
79
  });
95
- if (result && typeof result === "object" && "kind" in result) throw new TypeError("Initial incremental processor cannot rebuild.");
96
- const normalized = normalizeValue(result);
97
- current = normalized.value;
98
- if (normalized.state) state = normalized.state;
80
+ if (isRebuild(result)) throw new TypeError("Initial incremental processor cannot rebuild.");
81
+ current = result;
99
82
  return {
100
83
  value: current,
101
84
  update: (nextSources) => {
@@ -108,10 +91,8 @@ function createIncrementalValue(dependencies, processor) {
108
91
  cause: sourceCause(nextSources),
109
92
  state
110
93
  });
111
- if (next && typeof next === "object" && "kind" in next) return next;
112
- const nextValue = normalizeValue(next);
113
- current = nextValue.value;
114
- if (nextValue.state) state = nextValue.state;
94
+ if (isRebuild(next)) return next;
95
+ current = next;
115
96
  return {
116
97
  kind: "changed",
117
98
  value: current
@@ -124,6 +105,9 @@ function createIncrementalValue(dependencies, processor) {
124
105
  build
125
106
  });
126
107
  }
108
+ function dependenciesOf(dependencies) {
109
+ return Object.fromEntries(dependencies.map((dependency, index) => [`d${index}`, dependency]));
110
+ }
127
111
  function createIncrementalCollection(dependencies, processor) {
128
112
  const dependencyMap = dependenciesOf(dependencies);
129
113
  let state = Object.create(null);
@@ -138,7 +122,7 @@ function createIncrementalCollection(dependencies, processor) {
138
122
  reset: true,
139
123
  cause: sourceCause(input.sources),
140
124
  state,
141
- output: input.writer
125
+ output: input.output
142
126
  })?.kind === "rebuild") throw new TypeError("Initial incremental collection processor cannot rebuild.");
143
127
  return { update: (nextInput) => {
144
128
  const publicInputsForUpdate = publicInputs(nextInput.sources);
@@ -150,7 +134,7 @@ function createIncrementalCollection(dependencies, processor) {
150
134
  reset: false,
151
135
  cause: sourceCause(nextInput.sources),
152
136
  state,
153
- output: nextInput.writer
137
+ output: nextInput.output
154
138
  });
155
139
  } };
156
140
  };
@@ -1 +1 @@
1
- {"version":3,"file":"advanced.cjs","names":["snapshot","defineIncrementalValue","defineIncrementalCollection"],"sources":["../core/src/projection/advanced.ts"],"sourcesContent":["import type {\n Projection,\n CollectionProjection,\n ProjectionChanges,\n ProjectionValues,\n PublicCollection,\n} from './definition';\nimport { defineIncrementalCollection, defineIncrementalValue } from './definition';\nimport type {\n CollectionChange,\n CollectionDraft,\n CollectionEntryTransition,\n CollectionRead,\n GraphSources,\n} from './contract';\nimport { snapshot } from '../access/scope';\n\nexport type IncrementalState = Record<string, unknown>;\n\nexport type IncrementalValueContext<D extends readonly Projection<unknown, unknown>[], T> = {\n readonly sources: ProjectionValues<D>;\n readonly changes: ProjectionChanges<D>;\n readonly previous: T | undefined;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: IncrementalState;\n};\n\nexport type IncrementalValueProcessor<D extends readonly Projection<unknown, unknown>[], T> = (\n context: IncrementalValueContext<D, T>\n) =>\n | T\n | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n | { readonly kind: 'rebuild' };\n\nexport type IncrementalCollectionContext<\n D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n> = {\n readonly sources: ProjectionValues<D>;\n readonly changes: ProjectionChanges<D>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: IncrementalState;\n readonly output: CollectionDraft<K, V>;\n};\n\nexport type IncrementalCollectionProcessor<\n D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n> = (context: IncrementalCollectionContext<D, K, V>) => void | { readonly kind: 'rebuild' };\n\ntype DependencyMap<D extends readonly Projection<unknown, unknown>[]> = {\n readonly [K in keyof D as `d${Extract<K, number>}`]: D[K];\n};\n\nconst dependenciesOf = <D extends readonly Projection<unknown, unknown>[]>(\n dependencies: D\n): GraphSources =>\n Object.fromEntries(\n dependencies.map((dependency, index) => [`d${index}`, dependency])\n ) as unknown as GraphSources;\n\ntype RuntimeCollectionSource = {\n readonly ids: () => readonly string[];\n readonly previous: CollectionRead<string, unknown>;\n readonly reset: boolean;\n readonly change?: { readonly kind: 'reset' | 'incremental'; readonly orderChanged?: boolean };\n readonly transitions: () => readonly CollectionEntryTransition<string, unknown>[];\n};\nconst resetCollectionChange = Object.freeze({ kind: 'reset' as const });\ntype IncrementalCollectionChange = Extract<\n CollectionChange<string, unknown>,\n { readonly kind: 'incremental' }\n>;\n\nconst isCollectionSource = (source: unknown): source is RuntimeCollectionSource =>\n Boolean(\n source &&\n typeof source === 'object' &&\n 'ids' in source &&\n typeof (source as { readonly ids?: unknown }).ids === 'function' &&\n 'previous' in source &&\n 'transitions' in source &&\n typeof (source as { readonly transitions?: unknown }).transitions === 'function'\n );\n\nconst collectionChange = (source: unknown): CollectionChange<string, unknown> | undefined => {\n if (!isCollectionSource(source)) return undefined;\n if (source.reset || source.change?.kind === 'reset') return resetCollectionChange;\n const impact = source.change;\n if (!impact) return undefined;\n const added: IncrementalCollectionChange['added'][number][] = [];\n const updated: IncrementalCollectionChange['updated'][number][] = [];\n const removed: IncrementalCollectionChange['removed'][number][] = [];\n for (const transition of source.transitions()) {\n if (transition.kind === 'added')\n added.push({ key: transition.key, kind: transition.kind, after: transition.after });\n else if (transition.kind === 'updated') updated.push(transition);\n else removed.push({ key: transition.key, kind: transition.kind, before: transition.before });\n }\n const order = impact.orderChanged\n ? {\n before: Object.freeze([...source.previous.ids()]),\n after: Object.freeze([...source.ids()]),\n }\n : undefined;\n if (!added.length && !updated.length && !removed.length && !order) return undefined;\n return Object.freeze({\n kind: 'incremental' as const,\n added: Object.freeze(added),\n updated: Object.freeze(updated),\n removed: Object.freeze(removed),\n ...(order ? { order: Object.freeze(order) } : {}),\n });\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 publicInputs = (\n sources: Record<string, unknown>,\n initial = false\n): { readonly values: readonly unknown[]; readonly changes: readonly unknown[] } => {\n const values: unknown[] = [];\n const changes: unknown[] = [];\n for (const key of Object.keys(sources).sort(\n (left, right) => Number(left.slice(1)) - Number(right.slice(1))\n )) {\n const source = sources[key];\n values.push(publicSource(source));\n changes.push(\n initial && isCollectionSource(source) ? resetCollectionChange : collectionChange(source)\n );\n }\n return { values, changes };\n};\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, unknown>[], T>(\n dependencies: D,\n processor: IncrementalValueProcessor<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 publicInputsForBuild = publicInputs(sources, true);\n const result = processor({\n sources: publicInputsForBuild.values as ProjectionValues<D>,\n changes: publicInputsForBuild.changes as ProjectionChanges<D>,\n previous: undefined,\n reset: true,\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 publicInputsForUpdate = publicInputs(nextSources);\n const next = processor({\n sources: publicInputsForUpdate.values as ProjectionValues<D>,\n changes: publicInputsForUpdate.changes as ProjectionChanges<D>,\n previous: current,\n reset: false,\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, unknown>[],\n K extends string,\n V,\n>(dependencies: D, processor: IncrementalCollectionProcessor<D, K, V>): CollectionProjection<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 publicInputsForBuild = publicInputs(input.sources, true);\n const result = processor({\n sources: publicInputsForBuild.values as ProjectionValues<D>,\n changes: publicInputsForBuild.changes as ProjectionChanges<D>,\n previous: input.previous,\n next: input.next,\n reset: true,\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 const publicInputsForUpdate = publicInputs(nextInput.sources);\n return processor({\n sources: publicInputsForUpdate.values as ProjectionValues<D>,\n changes: publicInputsForUpdate.changes as ProjectionChanges<D>,\n previous: nextInput.previous,\n next: nextInput.next,\n reset: false,\n cause: sourceCause(nextInput.sources),\n state,\n output: nextInput.writer,\n });\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, unknown>[]> =\n DependencyMap<D>;\n\nexport type { CollectionChange } from './contract';\n"],"mappings":";;;;AA4DA,MAAM,kBACJ,iBAEA,OAAO,YACL,aAAa,KAAK,YAAY,UAAU,CAAC,IAAI,SAAS,WAAW,CAAC,CACnE;AASH,MAAM,wBAAwB,OAAO,OAAO,EAAE,MAAM,SAAkB,CAAC;AAMvE,MAAM,sBAAsB,WAC1B,QACE,UACA,OAAO,WAAW,YAClB,SAAS,UACT,OAAQ,OAAsC,QAAQ,cACtD,cAAc,UACd,iBAAiB,UACjB,OAAQ,OAA8C,gBAAgB,WACvE;AAEH,MAAM,oBAAoB,WAAmE;AAC3F,KAAI,C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A2D;AACjF,SAAO,EACL,SAAS,cAA4B;GACnC,MAAM,wBAAwB,aAAa,UAAU,QAAQ;AAC7D,UAAO,UAAU;IACf,SAAS,sBAAsB;IAC/B,SAAS,sBAAsB;IAC/B,UAAU,UAAU;IACpB,MAAM,UAAU;IAChB,OAAO;IACP,OAAO,YAAY,UAAU,QAAQ;IACrC;IACA,QAAQ,UAAU;IACnB,CAAC;KAEL;;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 } from './definition';\nimport { defineIncrementalCollection, defineIncrementalValue } from './definition';\nimport type { CollectionChange, CollectionDraft, CollectionRead, GraphSources } from './contract';\nimport { snapshot } from '../access/scope';\n\ntype ProjectionValues<D extends readonly Projection<unknown, unknown>[]> = {\n readonly [K in keyof D]: D[K] extends Projection<infer T, unknown> ? T : never;\n};\n\ntype ProjectionChanges<D extends readonly Projection<unknown, unknown>[]> = {\n readonly [K in keyof D]: D[K] extends Projection<unknown, infer C>\n ? [C] extends [undefined]\n ? undefined\n : C | undefined\n : undefined;\n};\n\nexport type IncrementalValueContext<D extends readonly Projection<unknown, unknown>[], T> = {\n readonly sources: ProjectionValues<D>;\n readonly changes: ProjectionChanges<D>;\n readonly previous: T | undefined;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: Record<string, unknown>;\n};\n\nexport type IncrementalValueProcessor<D extends readonly Projection<unknown, unknown>[], T> = (\n context: IncrementalValueContext<D, T>\n) => T | { readonly kind: 'rebuild' };\n\nexport type IncrementalCollectionContext<\n D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n> = {\n readonly sources: ProjectionValues<D>;\n readonly changes: ProjectionChanges<D>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: Record<string, unknown>;\n readonly output: CollectionDraft<K, V>;\n};\n\nexport type IncrementalCollectionProcessor<\n D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n> = (context: IncrementalCollectionContext<D, K, V>) => void | { readonly kind: 'rebuild' };\n\ntype DependencyMap<D extends readonly Projection<unknown, unknown>[]> = {\n readonly [K in keyof D as `d${Extract<K, number>}`]: D[K];\n};\n\nconst resetCollectionChange = Object.freeze({ kind: 'reset' as const });\n\nconst isRebuild = (value: unknown): value is { readonly kind: 'rebuild' } =>\n value !== null &&\n typeof value === 'object' &&\n (value as { readonly kind?: unknown }).kind === 'rebuild';\n\nconst collectionChange = (source: unknown): CollectionChange<string, unknown> | undefined => {\n if (!source || typeof source !== 'object' || !('kind' in source)) return undefined;\n if (source.kind !== 'collection') return undefined;\n return (source as { readonly change?: CollectionChange<string, unknown> }).change;\n};\n\nconst sourceCause = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (source && typeof source === 'object' && 'kind' in source) {\n const cause = (source as { readonly cause?: unknown }).cause;\n if (cause !== undefined) return cause;\n }\n }\n return undefined;\n};\n\nconst collectionView = <K extends string, V>(read: CollectionRead<K, V>): ReadonlyMap<K, V> => {\n const entries = function* (): IterableIterator<[K, V]> {\n for (const key of read.ids()) yield [key, read.get(key) as V];\n };\n const values = function* (): IterableIterator<V> {\n for (const key of read.ids()) yield read.get(key) as V;\n };\n const view: ReadonlyMap<K, V> = {\n get: key => read.get(key),\n has: key => read.has(key),\n get size() {\n return read.ids().length;\n },\n keys: () => read.ids()[Symbol.iterator](),\n values,\n entries,\n forEach: (callback, thisArg) => {\n for (const key of read.ids()) callback.call(thisArg, read.get(key) as V, key, view);\n },\n [Symbol.iterator]: entries,\n };\n return Object.freeze(view);\n};\n\nconst publicSource = (source: unknown): unknown => {\n if (!source || typeof source !== 'object') return source;\n if (!('kind' in source)) return source;\n if (source.kind === 'value') return (source as unknown as { readonly value: unknown }).value;\n if (source.kind === 'document')\n return snapshot((source as unknown as { readonly read: unknown }).read as never);\n if (source.kind === 'collection') {\n const read = (source as unknown as { readonly read: CollectionRead<string, unknown> }).read;\n return collectionView(read);\n }\n return source;\n};\n\nconst publicInputs = (\n sources: Record<string, unknown>,\n initial = false\n): { readonly values: readonly unknown[]; readonly changes: readonly unknown[] } => {\n const values: unknown[] = [];\n const changes: unknown[] = [];\n for (const key of Object.keys(sources).sort(\n (left, right) => Number(left.slice(1)) - Number(right.slice(1))\n )) {\n const source = sources[key];\n values.push(publicSource(source));\n changes.push(\n initial &&\n source &&\n typeof source === 'object' &&\n 'kind' in source &&\n source.kind === 'collection'\n ? resetCollectionChange\n : collectionChange(source)\n );\n }\n return { values, changes };\n};\n\nfunction createIncrementalValue<const D extends readonly Projection<unknown, unknown>[], T>(\n dependencies: D,\n processor: IncrementalValueProcessor<D, T>\n): Projection<T> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: Record<string, unknown> = Object.create(null) as Record<string, unknown>;\n let current!: T;\n const build = (sources: Record<string, unknown>) => {\n state = Object.create(null) as Record<string, unknown>;\n const publicInputsForBuild = publicInputs(sources, true);\n const result = processor({\n sources: publicInputsForBuild.values as ProjectionValues<D>,\n changes: publicInputsForBuild.changes as ProjectionChanges<D>,\n previous: undefined,\n reset: true,\n cause: sourceCause(sources),\n state,\n });\n if (isRebuild(result)) throw new TypeError('Initial incremental processor cannot rebuild.');\n current = result as T;\n return {\n value: current,\n update: (nextSources: Record<string, unknown>) => {\n const publicInputsForUpdate = publicInputs(nextSources);\n const next = processor({\n sources: publicInputsForUpdate.values as ProjectionValues<D>,\n changes: publicInputsForUpdate.changes as ProjectionChanges<D>,\n previous: current,\n reset: false,\n cause: sourceCause(nextSources),\n state,\n });\n if (isRebuild(next)) return next;\n current = next as T;\n return { kind: 'changed', value: current } as const;\n },\n };\n };\n return defineIncrementalValue({ dependencies: dependencyMap, build: build as never });\n}\n\nfunction dependenciesOf<D extends readonly Projection<unknown, unknown>[]>(\n dependencies: D\n): GraphSources {\n return Object.fromEntries(\n dependencies.map((dependency, index) => [`d${index}`, dependency])\n ) as unknown as GraphSources;\n}\n\nfunction createIncrementalCollection<\n const D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n>(\n dependencies: D,\n processor: IncrementalCollectionProcessor<D, K, V>\n): Projection<ReadonlyMap<K, V>, CollectionChange<K, V>> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: Record<string, unknown> = Object.create(null) as Record<string, unknown>;\n const build = (input: {\n readonly sources: Record<string, unknown>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly output: CollectionDraft<K, V>;\n }) => {\n state = Object.create(null) as Record<string, unknown>;\n const publicInputsForBuild = publicInputs(input.sources, true);\n const result = processor({\n sources: publicInputsForBuild.values as ProjectionValues<D>,\n changes: publicInputsForBuild.changes as ProjectionChanges<D>,\n previous: input.previous,\n next: input.next,\n reset: true,\n cause: sourceCause(input.sources),\n state,\n output: input.output,\n });\n if (result?.kind === 'rebuild')\n throw new TypeError('Initial incremental collection processor cannot rebuild.');\n return {\n update: (nextInput: typeof input) => {\n const publicInputsForUpdate = publicInputs(nextInput.sources);\n return processor({\n sources: publicInputsForUpdate.values as ProjectionValues<D>,\n changes: publicInputsForUpdate.changes as ProjectionChanges<D>,\n previous: nextInput.previous,\n next: nextInput.next,\n reset: false,\n cause: sourceCause(nextInput.sources),\n state,\n output: nextInput.output,\n });\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, unknown>[]> =\n DependencyMap<D>;\n\nexport type { CollectionChange } from './contract';\n"],"mappings":";;;;AAuDA,MAAM,wBAAwB,OAAO,OAAO,EAAE,MAAM,SAAkB,CAAC;AAEvE,MAAM,aAAa,UACjB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AAEH,QAAOC,mBAAAA,uBAAuB;EAAE,cAAc;EAAsB;EAAgB,CAAC;;AAGvF,SAAS,eACP,cACc;AACd,QAAO,OAAO,YACZ,aAAa,KAAK,YAAY,UAAU,CAAC,IAAI,SAAS,WAAW,CAAC,CACnE;;AAGH,SAAS,4BAKP,cACA,WACuD;CACvD,MAAM,gBAAgB,eAAe,aAAa;CAClD,IAAI,QAAiC,OAAO,OAAO,KAAK;CACxD,MAAM,SAAS,UAKT;AACJ,UAAQ,OAAO,OAAO,KAAK;EAC3B,MAAM,uBAAuB,aAAa,MAAM,SAAS,KAAK;AAW9D,MAVe,UAAU;GACvB,SAAS,qBAAqB;GAC9B,SAAS,qBAAqB;GAC9B,UAAU,MAAM;GAChB,MAAM,MAAM;GACZ,OAAO;GACP,OAAO,YAAY,MAAM,QAAQ;GACjC;GACA,QAAQ,MAAM;GACf,CACS,EAAE,SAAS,UACnB,OAAM,IAAI,UAAU,2DAA2D;AACjF,SAAO,EACL,SAAS,cAA4B;GACnC,MAAM,wBAAwB,aAAa,UAAU,QAAQ;AAC7D,UAAO,UAAU;IACf,SAAS,sBAAsB;IAC/B,SAAS,sBAAsB;IAC/B,UAAU,UAAU;IACpB,MAAM,UAAU;IAChB,OAAO;IACP,OAAO,YAAY,UAAU,QAAQ;IACrC;IACA,QAAQ,UAAU;IACnB,CAAC;KAEL;;AAEH,QAAOC,mBAAAA,4BAA4B;EAAE,cAAc;EAAsB;EAAgB,CAAC;;AAG5F,MAAa,cAAc,OAAO,OAAO,wBAAwB,EAC/D,YAAY,6BACb,CAAC"}
@@ -1,20 +1,17 @@
1
- import { a as ProjectionValues, d as CollectionChange, f as CollectionDraft, i as ProjectionChanges, p as CollectionRead, r as Projection, t as CollectionProjection } from "./definition-C__VJqA4.cjs";
1
+ import { c as CollectionRead, n as Projection, o as CollectionChange, s as CollectionDraft } from "./definition-Bf8Z7xtX.cjs";
2
2
 
3
3
  //#region core/src/projection/advanced.d.ts
4
- type IncrementalState = Record<string, unknown>;
4
+ type ProjectionValues<D extends readonly Projection<unknown, unknown>[]> = { readonly [K in keyof D]: D[K] extends Projection<infer T, unknown> ? T : never };
5
+ type ProjectionChanges<D extends readonly Projection<unknown, unknown>[]> = { readonly [K in keyof D]: D[K] extends Projection<unknown, infer C> ? [C] extends [undefined] ? undefined : C | undefined : undefined };
5
6
  type IncrementalValueContext<D extends readonly Projection<unknown, unknown>[], T> = {
6
7
  readonly sources: ProjectionValues<D>;
7
8
  readonly changes: ProjectionChanges<D>;
8
9
  readonly previous: T | undefined;
9
10
  readonly reset: boolean;
10
11
  readonly cause: unknown;
11
- readonly state: IncrementalState;
12
+ readonly state: Record<string, unknown>;
12
13
  };
13
14
  type IncrementalValueProcessor<D extends readonly Projection<unknown, unknown>[], T> = (context: IncrementalValueContext<D, T>) => T | {
14
- readonly kind: 'value';
15
- readonly value: T;
16
- readonly state?: IncrementalState;
17
- } | {
18
15
  readonly kind: 'rebuild';
19
16
  };
20
17
  type IncrementalCollectionContext<D extends readonly Projection<unknown, unknown>[], K extends string, V> = {
@@ -24,7 +21,7 @@ type IncrementalCollectionContext<D extends readonly Projection<unknown, unknown
24
21
  readonly next: CollectionRead<K, V>;
25
22
  readonly reset: boolean;
26
23
  readonly cause: unknown;
27
- readonly state: IncrementalState;
24
+ readonly state: Record<string, unknown>;
28
25
  readonly output: CollectionDraft<K, V>;
29
26
  };
30
27
  type IncrementalCollectionProcessor<D extends readonly Projection<unknown, unknown>[], K extends string, V> = (context: IncrementalCollectionContext<D, K, V>) => void | {
@@ -32,11 +29,11 @@ type IncrementalCollectionProcessor<D extends readonly Projection<unknown, unkno
32
29
  };
33
30
  type DependencyMap<D extends readonly Projection<unknown, unknown>[]> = { readonly [K in keyof D as `d${Extract<K, number>}`]: D[K] };
34
31
  declare function createIncrementalValue<const D extends readonly Projection<unknown, unknown>[], T>(dependencies: D, processor: IncrementalValueProcessor<D, T>): Projection<T>;
35
- declare function createIncrementalCollection<const D extends readonly Projection<unknown, unknown>[], K extends string, V>(dependencies: D, processor: IncrementalCollectionProcessor<D, K, V>): CollectionProjection<K, V>;
32
+ declare function createIncrementalCollection<const D extends readonly Projection<unknown, unknown>[], K extends string, V>(dependencies: D, processor: IncrementalCollectionProcessor<D, K, V>): Projection<ReadonlyMap<K, V>, CollectionChange<K, V>>;
36
33
  declare const incremental: typeof createIncrementalValue & {
37
34
  collection: typeof createIncrementalCollection;
38
35
  };
39
36
  type IncrementalDependencies<D extends readonly Projection<unknown, unknown>[]> = DependencyMap<D>;
40
37
  //#endregion
41
- export { type CollectionChange, IncrementalCollectionContext, IncrementalCollectionProcessor, IncrementalDependencies, IncrementalState, IncrementalValueContext, IncrementalValueProcessor, incremental };
38
+ export { type CollectionChange, IncrementalCollectionContext, IncrementalCollectionProcessor, IncrementalDependencies, IncrementalValueContext, IncrementalValueProcessor, incremental };
42
39
  //# sourceMappingURL=advanced.d.cts.map
@@ -1,20 +1,17 @@
1
- import { a as ProjectionValues, d as CollectionChange, f as CollectionDraft, i as ProjectionChanges, p as CollectionRead, r as Projection, t as CollectionProjection } from "./definition-Dc1Udkop.js";
1
+ import { c as CollectionRead, n as Projection, o as CollectionChange, s as CollectionDraft } from "./definition-BdLGD8Rq.js";
2
2
 
3
3
  //#region core/src/projection/advanced.d.ts
4
- type IncrementalState = Record<string, unknown>;
4
+ type ProjectionValues<D extends readonly Projection<unknown, unknown>[]> = { readonly [K in keyof D]: D[K] extends Projection<infer T, unknown> ? T : never };
5
+ type ProjectionChanges<D extends readonly Projection<unknown, unknown>[]> = { readonly [K in keyof D]: D[K] extends Projection<unknown, infer C> ? [C] extends [undefined] ? undefined : C | undefined : undefined };
5
6
  type IncrementalValueContext<D extends readonly Projection<unknown, unknown>[], T> = {
6
7
  readonly sources: ProjectionValues<D>;
7
8
  readonly changes: ProjectionChanges<D>;
8
9
  readonly previous: T | undefined;
9
10
  readonly reset: boolean;
10
11
  readonly cause: unknown;
11
- readonly state: IncrementalState;
12
+ readonly state: Record<string, unknown>;
12
13
  };
13
14
  type IncrementalValueProcessor<D extends readonly Projection<unknown, unknown>[], T> = (context: IncrementalValueContext<D, T>) => T | {
14
- readonly kind: 'value';
15
- readonly value: T;
16
- readonly state?: IncrementalState;
17
- } | {
18
15
  readonly kind: 'rebuild';
19
16
  };
20
17
  type IncrementalCollectionContext<D extends readonly Projection<unknown, unknown>[], K extends string, V> = {
@@ -24,7 +21,7 @@ type IncrementalCollectionContext<D extends readonly Projection<unknown, unknown
24
21
  readonly next: CollectionRead<K, V>;
25
22
  readonly reset: boolean;
26
23
  readonly cause: unknown;
27
- readonly state: IncrementalState;
24
+ readonly state: Record<string, unknown>;
28
25
  readonly output: CollectionDraft<K, V>;
29
26
  };
30
27
  type IncrementalCollectionProcessor<D extends readonly Projection<unknown, unknown>[], K extends string, V> = (context: IncrementalCollectionContext<D, K, V>) => void | {
@@ -32,11 +29,11 @@ type IncrementalCollectionProcessor<D extends readonly Projection<unknown, unkno
32
29
  };
33
30
  type DependencyMap<D extends readonly Projection<unknown, unknown>[]> = { readonly [K in keyof D as `d${Extract<K, number>}`]: D[K] };
34
31
  declare function createIncrementalValue<const D extends readonly Projection<unknown, unknown>[], T>(dependencies: D, processor: IncrementalValueProcessor<D, T>): Projection<T>;
35
- declare function createIncrementalCollection<const D extends readonly Projection<unknown, unknown>[], K extends string, V>(dependencies: D, processor: IncrementalCollectionProcessor<D, K, V>): CollectionProjection<K, V>;
32
+ declare function createIncrementalCollection<const D extends readonly Projection<unknown, unknown>[], K extends string, V>(dependencies: D, processor: IncrementalCollectionProcessor<D, K, V>): Projection<ReadonlyMap<K, V>, CollectionChange<K, V>>;
36
33
  declare const incremental: typeof createIncrementalValue & {
37
34
  collection: typeof createIncrementalCollection;
38
35
  };
39
36
  type IncrementalDependencies<D extends readonly Projection<unknown, unknown>[]> = DependencyMap<D>;
40
37
  //#endregion
41
- export { type CollectionChange, IncrementalCollectionContext, IncrementalCollectionProcessor, IncrementalDependencies, IncrementalState, IncrementalValueContext, IncrementalValueProcessor, incremental };
38
+ export { type CollectionChange, IncrementalCollectionContext, IncrementalCollectionProcessor, IncrementalDependencies, IncrementalValueContext, IncrementalValueProcessor, incremental };
42
39
  //# sourceMappingURL=advanced.d.ts.map
package/dist/advanced.js CHANGED
@@ -1,61 +1,50 @@
1
1
  import { i as snapshot } from "./scope-d-L87NeC.js";
2
- import { n as defineIncrementalValue, t as defineIncrementalCollection } from "./definition-c6XQXzvK.js";
2
+ import { n as defineIncrementalValue, t as defineIncrementalCollection } from "./definition-DUo1lfQ8.js";
3
3
  //#region core/src/projection/advanced.ts
4
- const dependenciesOf = (dependencies) => Object.fromEntries(dependencies.map((dependency, index) => [`d${index}`, dependency]));
5
4
  const resetCollectionChange = Object.freeze({ kind: "reset" });
6
- const isCollectionSource = (source) => Boolean(source && typeof source === "object" && "ids" in source && typeof source.ids === "function" && "previous" in source && "transitions" in source && typeof source.transitions === "function");
5
+ const isRebuild = (value) => value !== null && typeof value === "object" && value.kind === "rebuild";
7
6
  const collectionChange = (source) => {
8
- if (!isCollectionSource(source)) return void 0;
9
- if (source.reset || source.change?.kind === "reset") return resetCollectionChange;
10
- const impact = source.change;
11
- if (!impact) return void 0;
12
- const added = [];
13
- const updated = [];
14
- const removed = [];
15
- for (const transition of source.transitions()) if (transition.kind === "added") added.push({
16
- key: transition.key,
17
- kind: transition.kind,
18
- after: transition.after
19
- });
20
- else if (transition.kind === "updated") updated.push(transition);
21
- else removed.push({
22
- key: transition.key,
23
- kind: transition.kind,
24
- before: transition.before
25
- });
26
- const order = impact.orderChanged ? {
27
- before: Object.freeze([...source.previous.ids()]),
28
- after: Object.freeze([...source.ids()])
29
- } : void 0;
30
- if (!added.length && !updated.length && !removed.length && !order) return void 0;
31
- return Object.freeze({
32
- kind: "incremental",
33
- added: Object.freeze(added),
34
- updated: Object.freeze(updated),
35
- removed: Object.freeze(removed),
36
- ...order ? { order: Object.freeze(order) } : {}
37
- });
7
+ if (!source || typeof source !== "object" || !("kind" in source)) return void 0;
8
+ if (source.kind !== "collection") return void 0;
9
+ return source.change;
38
10
  };
39
11
  const sourceCause = (sources) => {
40
- for (const source of Object.values(sources)) if (source && typeof source === "object" && "cause" in source && source.cause !== void 0) return source.cause;
12
+ for (const source of Object.values(sources)) if (source && typeof source === "object" && "kind" in source) {
13
+ const cause = source.cause;
14
+ if (cause !== void 0) return cause;
15
+ }
16
+ };
17
+ const collectionView = (read) => {
18
+ const entries = function* () {
19
+ for (const key of read.ids()) yield [key, read.get(key)];
20
+ };
21
+ const values = function* () {
22
+ for (const key of read.ids()) yield read.get(key);
23
+ };
24
+ const view = {
25
+ get: (key) => read.get(key),
26
+ has: (key) => read.has(key),
27
+ get size() {
28
+ return read.ids().length;
29
+ },
30
+ keys: () => read.ids()[Symbol.iterator](),
31
+ values,
32
+ entries,
33
+ forEach: (callback, thisArg) => {
34
+ for (const key of read.ids()) callback.call(thisArg, read.get(key), key, view);
35
+ },
36
+ [Symbol.iterator]: entries
37
+ };
38
+ return Object.freeze(view);
41
39
  };
42
40
  const publicSource = (source) => {
43
41
  if (!source || typeof source !== "object") return source;
44
- if ("value" in source) return source.value;
45
- if ("read" in source) {
42
+ if (!("kind" in source)) return source;
43
+ if (source.kind === "value") return source.value;
44
+ if (source.kind === "document") return snapshot(source.read);
45
+ if (source.kind === "collection") {
46
46
  const read = source.read;
47
- if (read && typeof read === "object" && "ids" in read && typeof read.ids === "function") {
48
- const result = /* @__PURE__ */ new Map();
49
- for (const id of read.ids()) result.set(id, read.get(id));
50
- return result;
51
- }
52
- return snapshot(read);
53
- }
54
- if ("ids" in source && typeof source.ids === "function") {
55
- const result = /* @__PURE__ */ new Map();
56
- const read = source;
57
- for (const id of read.ids()) result.set(id, read.get(id));
58
- return result;
47
+ return collectionView(read);
59
48
  }
60
49
  return source;
61
50
  };
@@ -65,17 +54,13 @@ const publicInputs = (sources, initial = false) => {
65
54
  for (const key of Object.keys(sources).sort((left, right) => Number(left.slice(1)) - Number(right.slice(1)))) {
66
55
  const source = sources[key];
67
56
  values.push(publicSource(source));
68
- changes.push(initial && isCollectionSource(source) ? resetCollectionChange : collectionChange(source));
57
+ changes.push(initial && source && typeof source === "object" && "kind" in source && source.kind === "collection" ? resetCollectionChange : collectionChange(source));
69
58
  }
70
59
  return {
71
60
  values,
72
61
  changes
73
62
  };
74
63
  };
75
- const normalizeValue = (result) => result && typeof result === "object" && result.kind === "value" ? {
76
- value: result.value,
77
- state: result.state
78
- } : { value: result };
79
64
  function createIncrementalValue(dependencies, processor) {
80
65
  const dependencyMap = dependenciesOf(dependencies);
81
66
  let state = Object.create(null);
@@ -91,10 +76,8 @@ function createIncrementalValue(dependencies, processor) {
91
76
  cause: sourceCause(sources),
92
77
  state
93
78
  });
94
- if (result && typeof result === "object" && "kind" in result) throw new TypeError("Initial incremental processor cannot rebuild.");
95
- const normalized = normalizeValue(result);
96
- current = normalized.value;
97
- if (normalized.state) state = normalized.state;
79
+ if (isRebuild(result)) throw new TypeError("Initial incremental processor cannot rebuild.");
80
+ current = result;
98
81
  return {
99
82
  value: current,
100
83
  update: (nextSources) => {
@@ -107,10 +90,8 @@ function createIncrementalValue(dependencies, processor) {
107
90
  cause: sourceCause(nextSources),
108
91
  state
109
92
  });
110
- if (next && typeof next === "object" && "kind" in next) return next;
111
- const nextValue = normalizeValue(next);
112
- current = nextValue.value;
113
- if (nextValue.state) state = nextValue.state;
93
+ if (isRebuild(next)) return next;
94
+ current = next;
114
95
  return {
115
96
  kind: "changed",
116
97
  value: current
@@ -123,6 +104,9 @@ function createIncrementalValue(dependencies, processor) {
123
104
  build
124
105
  });
125
106
  }
107
+ function dependenciesOf(dependencies) {
108
+ return Object.fromEntries(dependencies.map((dependency, index) => [`d${index}`, dependency]));
109
+ }
126
110
  function createIncrementalCollection(dependencies, processor) {
127
111
  const dependencyMap = dependenciesOf(dependencies);
128
112
  let state = Object.create(null);
@@ -137,7 +121,7 @@ function createIncrementalCollection(dependencies, processor) {
137
121
  reset: true,
138
122
  cause: sourceCause(input.sources),
139
123
  state,
140
- output: input.writer
124
+ output: input.output
141
125
  })?.kind === "rebuild") throw new TypeError("Initial incremental collection processor cannot rebuild.");
142
126
  return { update: (nextInput) => {
143
127
  const publicInputsForUpdate = publicInputs(nextInput.sources);
@@ -149,7 +133,7 @@ function createIncrementalCollection(dependencies, processor) {
149
133
  reset: false,
150
134
  cause: sourceCause(nextInput.sources),
151
135
  state,
152
- output: nextInput.writer
136
+ output: nextInput.output
153
137
  });
154
138
  } };
155
139
  };
@@ -1 +1 @@
1
- {"version":3,"file":"advanced.js","names":[],"sources":["../core/src/projection/advanced.ts"],"sourcesContent":["import type {\n Projection,\n CollectionProjection,\n ProjectionChanges,\n ProjectionValues,\n PublicCollection,\n} from './definition';\nimport { defineIncrementalCollection, defineIncrementalValue } from './definition';\nimport type {\n CollectionChange,\n CollectionDraft,\n CollectionEntryTransition,\n CollectionRead,\n GraphSources,\n} from './contract';\nimport { snapshot } from '../access/scope';\n\nexport type IncrementalState = Record<string, unknown>;\n\nexport type IncrementalValueContext<D extends readonly Projection<unknown, unknown>[], T> = {\n readonly sources: ProjectionValues<D>;\n readonly changes: ProjectionChanges<D>;\n readonly previous: T | undefined;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: IncrementalState;\n};\n\nexport type IncrementalValueProcessor<D extends readonly Projection<unknown, unknown>[], T> = (\n context: IncrementalValueContext<D, T>\n) =>\n | T\n | { readonly kind: 'value'; readonly value: T; readonly state?: IncrementalState }\n | { readonly kind: 'rebuild' };\n\nexport type IncrementalCollectionContext<\n D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n> = {\n readonly sources: ProjectionValues<D>;\n readonly changes: ProjectionChanges<D>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: IncrementalState;\n readonly output: CollectionDraft<K, V>;\n};\n\nexport type IncrementalCollectionProcessor<\n D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n> = (context: IncrementalCollectionContext<D, K, V>) => void | { readonly kind: 'rebuild' };\n\ntype DependencyMap<D extends readonly Projection<unknown, unknown>[]> = {\n readonly [K in keyof D as `d${Extract<K, number>}`]: D[K];\n};\n\nconst dependenciesOf = <D extends readonly Projection<unknown, unknown>[]>(\n dependencies: D\n): GraphSources =>\n Object.fromEntries(\n dependencies.map((dependency, index) => [`d${index}`, dependency])\n ) as unknown as GraphSources;\n\ntype RuntimeCollectionSource = {\n readonly ids: () => readonly string[];\n readonly previous: CollectionRead<string, unknown>;\n readonly reset: boolean;\n readonly change?: { readonly kind: 'reset' | 'incremental'; readonly orderChanged?: boolean };\n readonly transitions: () => readonly CollectionEntryTransition<string, unknown>[];\n};\nconst resetCollectionChange = Object.freeze({ kind: 'reset' as const });\ntype IncrementalCollectionChange = Extract<\n CollectionChange<string, unknown>,\n { readonly kind: 'incremental' }\n>;\n\nconst isCollectionSource = (source: unknown): source is RuntimeCollectionSource =>\n Boolean(\n source &&\n typeof source === 'object' &&\n 'ids' in source &&\n typeof (source as { readonly ids?: unknown }).ids === 'function' &&\n 'previous' in source &&\n 'transitions' in source &&\n typeof (source as { readonly transitions?: unknown }).transitions === 'function'\n );\n\nconst collectionChange = (source: unknown): CollectionChange<string, unknown> | undefined => {\n if (!isCollectionSource(source)) return undefined;\n if (source.reset || source.change?.kind === 'reset') return resetCollectionChange;\n const impact = source.change;\n if (!impact) return undefined;\n const added: IncrementalCollectionChange['added'][number][] = [];\n const updated: IncrementalCollectionChange['updated'][number][] = [];\n const removed: IncrementalCollectionChange['removed'][number][] = [];\n for (const transition of source.transitions()) {\n if (transition.kind === 'added')\n added.push({ key: transition.key, kind: transition.kind, after: transition.after });\n else if (transition.kind === 'updated') updated.push(transition);\n else removed.push({ key: transition.key, kind: transition.kind, before: transition.before });\n }\n const order = impact.orderChanged\n ? {\n before: Object.freeze([...source.previous.ids()]),\n after: Object.freeze([...source.ids()]),\n }\n : undefined;\n if (!added.length && !updated.length && !removed.length && !order) return undefined;\n return Object.freeze({\n kind: 'incremental' as const,\n added: Object.freeze(added),\n updated: Object.freeze(updated),\n removed: Object.freeze(removed),\n ...(order ? { order: Object.freeze(order) } : {}),\n });\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 publicInputs = (\n sources: Record<string, unknown>,\n initial = false\n): { readonly values: readonly unknown[]; readonly changes: readonly unknown[] } => {\n const values: unknown[] = [];\n const changes: unknown[] = [];\n for (const key of Object.keys(sources).sort(\n (left, right) => Number(left.slice(1)) - Number(right.slice(1))\n )) {\n const source = sources[key];\n values.push(publicSource(source));\n changes.push(\n initial && isCollectionSource(source) ? resetCollectionChange : collectionChange(source)\n );\n }\n return { values, changes };\n};\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, unknown>[], T>(\n dependencies: D,\n processor: IncrementalValueProcessor<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 publicInputsForBuild = publicInputs(sources, true);\n const result = processor({\n sources: publicInputsForBuild.values as ProjectionValues<D>,\n changes: publicInputsForBuild.changes as ProjectionChanges<D>,\n previous: undefined,\n reset: true,\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 publicInputsForUpdate = publicInputs(nextSources);\n const next = processor({\n sources: publicInputsForUpdate.values as ProjectionValues<D>,\n changes: publicInputsForUpdate.changes as ProjectionChanges<D>,\n previous: current,\n reset: false,\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, unknown>[],\n K extends string,\n V,\n>(dependencies: D, processor: IncrementalCollectionProcessor<D, K, V>): CollectionProjection<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 publicInputsForBuild = publicInputs(input.sources, true);\n const result = processor({\n sources: publicInputsForBuild.values as ProjectionValues<D>,\n changes: publicInputsForBuild.changes as ProjectionChanges<D>,\n previous: input.previous,\n next: input.next,\n reset: true,\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 const publicInputsForUpdate = publicInputs(nextInput.sources);\n return processor({\n sources: publicInputsForUpdate.values as ProjectionValues<D>,\n changes: publicInputsForUpdate.changes as ProjectionChanges<D>,\n previous: nextInput.previous,\n next: nextInput.next,\n reset: false,\n cause: sourceCause(nextInput.sources),\n state,\n output: nextInput.writer,\n });\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, unknown>[]> =\n DependencyMap<D>;\n\nexport type { CollectionChange } from './contract';\n"],"mappings":";;;AA4DA,MAAM,kBACJ,iBAEA,OAAO,YACL,aAAa,KAAK,YAAY,UAAU,CAAC,IAAI,SAAS,WAAW,CAAC,CACnE;AASH,MAAM,wBAAwB,OAAO,OAAO,EAAE,MAAM,SAAkB,CAAC;AAMvE,MAAM,sBAAsB,WAC1B,QACE,UACA,OAAO,WAAW,YAClB,SAAS,UACT,OAAQ,OAAsC,QAAQ,cACtD,cAAc,UACd,iBAAiB,UACjB,OAAQ,OAA8C,gBAAgB,WACvE;AAEH,MAAM,oBAAoB,WAAmE;AAC3F,KAAI,CAAC,mBAAmB,OAAO,CAAE,QAAO,KAAA;AACxC,KAAI,OAAO,SAAS,OAAO,QAAQ,SAAS,QAAS,QAAO;CAC5D,MAAM,SAAS,OAAO;AACtB,KAAI,CAAC,OAAQ,QAAO,KAAA;CACpB,MAAM,QAAwD,EAAE;CAChE,MAAM,UAA4D,EAAE;CACpE,MAAM,UAA4D,EAAE;AACpE,MAAK,MAAM,cAAc,OAAO,aAAa,CAC3C,KAAI,WAAW,SAAS,QACtB,OAAM,KAAK;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c,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 } from './definition';\nimport { defineIncrementalCollection, defineIncrementalValue } from './definition';\nimport type { CollectionChange, CollectionDraft, CollectionRead, GraphSources } from './contract';\nimport { snapshot } from '../access/scope';\n\ntype ProjectionValues<D extends readonly Projection<unknown, unknown>[]> = {\n readonly [K in keyof D]: D[K] extends Projection<infer T, unknown> ? T : never;\n};\n\ntype ProjectionChanges<D extends readonly Projection<unknown, unknown>[]> = {\n readonly [K in keyof D]: D[K] extends Projection<unknown, infer C>\n ? [C] extends [undefined]\n ? undefined\n : C | undefined\n : undefined;\n};\n\nexport type IncrementalValueContext<D extends readonly Projection<unknown, unknown>[], T> = {\n readonly sources: ProjectionValues<D>;\n readonly changes: ProjectionChanges<D>;\n readonly previous: T | undefined;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: Record<string, unknown>;\n};\n\nexport type IncrementalValueProcessor<D extends readonly Projection<unknown, unknown>[], T> = (\n context: IncrementalValueContext<D, T>\n) => T | { readonly kind: 'rebuild' };\n\nexport type IncrementalCollectionContext<\n D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n> = {\n readonly sources: ProjectionValues<D>;\n readonly changes: ProjectionChanges<D>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly reset: boolean;\n readonly cause: unknown;\n readonly state: Record<string, unknown>;\n readonly output: CollectionDraft<K, V>;\n};\n\nexport type IncrementalCollectionProcessor<\n D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n> = (context: IncrementalCollectionContext<D, K, V>) => void | { readonly kind: 'rebuild' };\n\ntype DependencyMap<D extends readonly Projection<unknown, unknown>[]> = {\n readonly [K in keyof D as `d${Extract<K, number>}`]: D[K];\n};\n\nconst resetCollectionChange = Object.freeze({ kind: 'reset' as const });\n\nconst isRebuild = (value: unknown): value is { readonly kind: 'rebuild' } =>\n value !== null &&\n typeof value === 'object' &&\n (value as { readonly kind?: unknown }).kind === 'rebuild';\n\nconst collectionChange = (source: unknown): CollectionChange<string, unknown> | undefined => {\n if (!source || typeof source !== 'object' || !('kind' in source)) return undefined;\n if (source.kind !== 'collection') return undefined;\n return (source as { readonly change?: CollectionChange<string, unknown> }).change;\n};\n\nconst sourceCause = (sources: Record<string, unknown>): unknown => {\n for (const source of Object.values(sources)) {\n if (source && typeof source === 'object' && 'kind' in source) {\n const cause = (source as { readonly cause?: unknown }).cause;\n if (cause !== undefined) return cause;\n }\n }\n return undefined;\n};\n\nconst collectionView = <K extends string, V>(read: CollectionRead<K, V>): ReadonlyMap<K, V> => {\n const entries = function* (): IterableIterator<[K, V]> {\n for (const key of read.ids()) yield [key, read.get(key) as V];\n };\n const values = function* (): IterableIterator<V> {\n for (const key of read.ids()) yield read.get(key) as V;\n };\n const view: ReadonlyMap<K, V> = {\n get: key => read.get(key),\n has: key => read.has(key),\n get size() {\n return read.ids().length;\n },\n keys: () => read.ids()[Symbol.iterator](),\n values,\n entries,\n forEach: (callback, thisArg) => {\n for (const key of read.ids()) callback.call(thisArg, read.get(key) as V, key, view);\n },\n [Symbol.iterator]: entries,\n };\n return Object.freeze(view);\n};\n\nconst publicSource = (source: unknown): unknown => {\n if (!source || typeof source !== 'object') return source;\n if (!('kind' in source)) return source;\n if (source.kind === 'value') return (source as unknown as { readonly value: unknown }).value;\n if (source.kind === 'document')\n return snapshot((source as unknown as { readonly read: unknown }).read as never);\n if (source.kind === 'collection') {\n const read = (source as unknown as { readonly read: CollectionRead<string, unknown> }).read;\n return collectionView(read);\n }\n return source;\n};\n\nconst publicInputs = (\n sources: Record<string, unknown>,\n initial = false\n): { readonly values: readonly unknown[]; readonly changes: readonly unknown[] } => {\n const values: unknown[] = [];\n const changes: unknown[] = [];\n for (const key of Object.keys(sources).sort(\n (left, right) => Number(left.slice(1)) - Number(right.slice(1))\n )) {\n const source = sources[key];\n values.push(publicSource(source));\n changes.push(\n initial &&\n source &&\n typeof source === 'object' &&\n 'kind' in source &&\n source.kind === 'collection'\n ? resetCollectionChange\n : collectionChange(source)\n );\n }\n return { values, changes };\n};\n\nfunction createIncrementalValue<const D extends readonly Projection<unknown, unknown>[], T>(\n dependencies: D,\n processor: IncrementalValueProcessor<D, T>\n): Projection<T> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: Record<string, unknown> = Object.create(null) as Record<string, unknown>;\n let current!: T;\n const build = (sources: Record<string, unknown>) => {\n state = Object.create(null) as Record<string, unknown>;\n const publicInputsForBuild = publicInputs(sources, true);\n const result = processor({\n sources: publicInputsForBuild.values as ProjectionValues<D>,\n changes: publicInputsForBuild.changes as ProjectionChanges<D>,\n previous: undefined,\n reset: true,\n cause: sourceCause(sources),\n state,\n });\n if (isRebuild(result)) throw new TypeError('Initial incremental processor cannot rebuild.');\n current = result as T;\n return {\n value: current,\n update: (nextSources: Record<string, unknown>) => {\n const publicInputsForUpdate = publicInputs(nextSources);\n const next = processor({\n sources: publicInputsForUpdate.values as ProjectionValues<D>,\n changes: publicInputsForUpdate.changes as ProjectionChanges<D>,\n previous: current,\n reset: false,\n cause: sourceCause(nextSources),\n state,\n });\n if (isRebuild(next)) return next;\n current = next as T;\n return { kind: 'changed', value: current } as const;\n },\n };\n };\n return defineIncrementalValue({ dependencies: dependencyMap, build: build as never });\n}\n\nfunction dependenciesOf<D extends readonly Projection<unknown, unknown>[]>(\n dependencies: D\n): GraphSources {\n return Object.fromEntries(\n dependencies.map((dependency, index) => [`d${index}`, dependency])\n ) as unknown as GraphSources;\n}\n\nfunction createIncrementalCollection<\n const D extends readonly Projection<unknown, unknown>[],\n K extends string,\n V,\n>(\n dependencies: D,\n processor: IncrementalCollectionProcessor<D, K, V>\n): Projection<ReadonlyMap<K, V>, CollectionChange<K, V>> {\n const dependencyMap = dependenciesOf(dependencies);\n let state: Record<string, unknown> = Object.create(null) as Record<string, unknown>;\n const build = (input: {\n readonly sources: Record<string, unknown>;\n readonly previous: CollectionRead<K, V>;\n readonly next: CollectionRead<K, V>;\n readonly output: CollectionDraft<K, V>;\n }) => {\n state = Object.create(null) as Record<string, unknown>;\n const publicInputsForBuild = publicInputs(input.sources, true);\n const result = processor({\n sources: publicInputsForBuild.values as ProjectionValues<D>,\n changes: publicInputsForBuild.changes as ProjectionChanges<D>,\n previous: input.previous,\n next: input.next,\n reset: true,\n cause: sourceCause(input.sources),\n state,\n output: input.output,\n });\n if (result?.kind === 'rebuild')\n throw new TypeError('Initial incremental collection processor cannot rebuild.');\n return {\n update: (nextInput: typeof input) => {\n const publicInputsForUpdate = publicInputs(nextInput.sources);\n return processor({\n sources: publicInputsForUpdate.values as ProjectionValues<D>,\n changes: publicInputsForUpdate.changes as ProjectionChanges<D>,\n previous: nextInput.previous,\n next: nextInput.next,\n reset: false,\n cause: sourceCause(nextInput.sources),\n state,\n output: nextInput.output,\n });\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, unknown>[]> =\n DependencyMap<D>;\n\nexport type { CollectionChange } from './contract';\n"],"mappings":";;;AAuDA,MAAM,wBAAwB,OAAO,OAAO,EAAE,MAAM,SAAkB,CAAC;AAEvE,MAAM,aAAa,UACjB,UAAU,QACV,OAAO,UAAU,YAChB,MAAsC,SAAS;AAElD,MAAM,oBAAoB,WAAmE;AAC3F,KAAI,CAAC,UAAU,OAAO,WAAW,YAAY,EAAE,UAAU,QAAS,QAAO,KAAA;AACzE,KAAI,OAAO,SAAS,aAAc,QAAO,KAAA;AACzC,QAAQ,OAAmE;;AAG7E,MAAM,eAAe,YAA8C;AACjE,MAAK,MAAM,UAAU,OAAO,OAAO,QAAQ,CACzC,KAAI,UAAU,OAAO,WAAW,YAAY,UAAU,QAAQ;EAC5D,MAAM,QAAS,OAAwC;AACvD,MAAI,UAAU,K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