doxum 0.1.15 → 0.1.16

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.
@@ -6,6 +6,40 @@ type Cause = unknown;
6
6
  type BatchOptions = {
7
7
  readonly cause?: Cause;
8
8
  };
9
+ type CollectionEntryTransition<K extends string, V> = {
10
+ readonly key: K;
11
+ readonly kind: 'added';
12
+ readonly before: undefined;
13
+ readonly after: V;
14
+ } | {
15
+ readonly key: K;
16
+ readonly kind: 'updated';
17
+ readonly before: V;
18
+ readonly after: V;
19
+ } | {
20
+ readonly key: K;
21
+ readonly kind: 'removed';
22
+ readonly before: V;
23
+ readonly after: undefined;
24
+ };
25
+ type CollectionChange<K extends string, V> = {
26
+ readonly kind: 'reset';
27
+ } | {
28
+ readonly kind: 'incremental';
29
+ readonly added: readonly Omit<Extract<CollectionEntryTransition<K, V>, {
30
+ readonly kind: 'added';
31
+ }>, 'before'>[];
32
+ readonly updated: readonly Extract<CollectionEntryTransition<K, V>, {
33
+ readonly kind: 'updated';
34
+ }>[];
35
+ readonly removed: readonly Omit<Extract<CollectionEntryTransition<K, V>, {
36
+ readonly kind: 'removed';
37
+ }>, 'after'>[];
38
+ readonly order?: {
39
+ readonly before: readonly K[];
40
+ readonly after: readonly K[];
41
+ };
42
+ };
9
43
  type CollectionRead<K extends string, V> = {
10
44
  get(key: K): V | undefined;
11
45
  has(key: K): boolean;
@@ -69,18 +103,21 @@ declare class ProjectionDisposedError extends Error {
69
103
  //#endregion
70
104
  //#region core/src/projection/definition.d.ts
71
105
  declare const projectionDefinition: unique symbol;
106
+ declare const projectionChanges: unique symbol;
72
107
  declare const writableInput: unique symbol;
73
108
  /** A lazy, reusable projection definition. Its value is materialized per runtime. */
74
- type Projection<T> = {
109
+ type Projection<T, C = undefined> = {
75
110
  readonly [projectionDefinition]: T;
111
+ readonly [projectionChanges]: C;
76
112
  };
77
113
  /** A runtime-local writable projection definition. */
78
114
  type Input<T> = Projection<T> & {
79
115
  readonly [writableInput]: true;
80
116
  };
81
- type ProjectionValues<D extends readonly Projection<unknown>[]> = { readonly [K in keyof D]: D[K] extends Projection<infer T> ? T : never };
117
+ type ProjectionValues<D extends readonly Projection<unknown, unknown>[]> = { readonly [K in keyof D]: D[K] extends Projection<infer T, unknown> ? T : never };
118
+ 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 };
82
119
  type PublicCollection<K extends string, V> = ReadonlyMap<K, V>;
83
- type ObservedCollection<P extends CollectionPath> = PublicCollection<CollectionId<P>, ReadonlyValue<Infer<CollectionNode<P>>>>;
120
+ type CollectionProjection<K extends string, V> = Projection<PublicCollection<K, V>, CollectionChange<K, V>>;
84
121
  declare const input: <T>(initial: T, equality?: (previous: T, next: T) => boolean) => Input<T>;
85
122
  /**
86
123
  * Establishes a reactive boundary from a document, readable, or external
@@ -89,11 +126,11 @@ declare const input: <T>(initial: T, equality?: (previous: T, next: T) => boolea
89
126
  */
90
127
  declare function observe<S extends ObjectNode>(document: DocumentReadable<S>): Projection<Infer<S>>;
91
128
  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>>;
129
+ declare function observe<K extends string, V, D>(source: ExternalCollectionSource<K, V, D>): CollectionProjection<K, V>;
93
130
  declare function observe<T>(readable: Readable<T>): Projection<T>;
94
- declare function observe<S extends ObjectNode, P extends CollectionPath>(document: DocumentReadable<S>, selector: (path: SchemaPath<S['shape']>) => P): Projection<ObservedCollection<P>>;
131
+ declare function observe<S extends ObjectNode, P extends CollectionPath>(document: DocumentReadable<S>, selector: (path: SchemaPath<S['shape']>) => P): CollectionProjection<CollectionId<P>, ReadonlyValue<Infer<CollectionNode<P>>>>;
95
132
  declare function observe<S extends ObjectNode, P>(document: DocumentReadable<S>, selector: (path: SchemaPath<S['shape']>) => P): Projection<PathValueOf<P>>;
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>;
133
+ declare function derive<const D extends readonly Projection<unknown, unknown>[], T>(dependencies: D, compute: (...values: ProjectionValues<D>) => Synchronous<T>, equality?: (previous: T, next: T) => boolean): Projection<T>;
97
134
  //#endregion
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
135
+ export { ExternalValueEvent as _, ProjectionValues as a, ProjectionError as b, input as c, CollectionChange as d, CollectionDraft as f, ExternalCollectionSource as g, ExternalCollectionRead as h, ProjectionChanges as i, observe as l, ExternalCollectionEvent as m, Input as n, PublicCollection as o, CollectionRead as p, Projection as r, derive as s, CollectionProjection as t, BatchOptions as u, ExternalValueSource as v, ProjectionDisposedError as y };
136
+ //# sourceMappingURL=definition-Dc1Udkop.d.ts.map
@@ -1 +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"}
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 CollectionChange,\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 projectionChanges: unique symbol;\ndeclare const writableInput: unique symbol;\n\n/** A lazy, reusable projection definition. Its value is materialized per runtime. */\nexport type Projection<T, C = undefined> = {\n readonly [projectionDefinition]: T;\n readonly [projectionChanges]: C;\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, unknown>[]> = {\n readonly [K in keyof D]: D[K] extends Projection<infer T, unknown> ? T : never;\n};\n\nexport type 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 PublicCollection<K extends string, V> = ReadonlyMap<K, V>;\nexport type CollectionProjection<K extends string, V> = Projection<\n PublicCollection<K, V>,\n CollectionChange<K, V>\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, 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, unknown>;\nconst definitions = new WeakMap<object, Definition>();\n\nconst define = <T, C = undefined>(definition: Definition): Projection<T, C> => {\n const handle = Object.freeze({}) as Projection<T, C>;\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): CollectionProjection<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): CollectionProjection<CollectionId<P>, ReadonlyValue<Infer<SchemaCollectionNode<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, 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, 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, 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): CollectionProjection<K, V> =>\n define<PublicCollection<K, V>, CollectionChange<K, V>>({\n kind: 'source-collection',\n source: source as never,\n });\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}): CollectionProjection<K, V> =>\n define<PublicCollection<K, V>, CollectionChange<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":";AAkGA,MAAM,8BAAc,IAAI,SAA6B;AAErD,MAAM,UAA4B,eAA6C;CAC7E,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,UAC8B;AAC9B,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,OAAuD;CACrD,MAAM;CACE;CACT,CAAC;AAQJ,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,OAAuD;CACrD,MAAM;CACN,cAAc,WAAW;CACzB,OAAO,WAAW;CAClB,SAAS,WAAW;CACpB,MAAM,WAAW;CAClB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"definition-c6XQXzvK.js","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"}
1
+ {"version":3,"file":"definition-c6XQXzvK.js","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 CollectionChange,\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 projectionChanges: unique symbol;\ndeclare const writableInput: unique symbol;\n\n/** A lazy, reusable projection definition. Its value is materialized per runtime. */\nexport type Projection<T, C = undefined> = {\n readonly [projectionDefinition]: T;\n readonly [projectionChanges]: C;\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, unknown>[]> = {\n readonly [K in keyof D]: D[K] extends Projection<infer T, unknown> ? T : never;\n};\n\nexport type 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 PublicCollection<K extends string, V> = ReadonlyMap<K, V>;\nexport type CollectionProjection<K extends string, V> = Projection<\n PublicCollection<K, V>,\n CollectionChange<K, V>\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, 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, unknown>;\nconst definitions = new WeakMap<object, Definition>();\n\nconst define = <T, C = undefined>(definition: Definition): Projection<T, C> => {\n const handle = Object.freeze({}) as Projection<T, C>;\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): CollectionProjection<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): CollectionProjection<CollectionId<P>, ReadonlyValue<Infer<SchemaCollectionNode<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, 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, 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, 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): CollectionProjection<K, V> =>\n define<PublicCollection<K, V>, CollectionChange<K, V>>({\n kind: 'source-collection',\n source: source as never,\n });\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}): CollectionProjection<K, V> =>\n define<PublicCollection<K, V>, CollectionChange<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":";AAkGA,MAAM,8BAAc,IAAI,SAA6B;AAErD,MAAM,UAA4B,eAA6C;CAC7E,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,UAC8B;AAC9B,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,OAAuD;CACrD,MAAM;CACE;CACT,CAAC;AAQJ,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,OAAuD;CACrD,MAAM;CACN,cAAc,WAAW;CACzB,OAAO,WAAW;CAClB,SAAS,WAAW;CACpB,MAAM,WAAW;CAClB,CAAC"}
package/dist/index.cjs CHANGED
@@ -1683,20 +1683,34 @@ const createCollection = (scheduler, spec) => {
1683
1683
  ].forEach((key) => changedKeys.add(key));
1684
1684
  require_issue.profile.projection("changedKeys", changedKeys.size);
1685
1685
  }
1686
- publishedTransitions = Object.freeze([...changedKeys].flatMap((key) => {
1686
+ const transitions = [];
1687
+ for (const key of changedKeys) {
1687
1688
  const beforePresent = values.has(key);
1688
1689
  const afterPresent = hasNext(key);
1689
1690
  if (beforePresent === afterPresent) {
1690
- if (!beforePresent) return [];
1691
- if ((spec.isEqual ?? Object.is)(values.get(key), getNext(key))) return [];
1691
+ if (!beforePresent) continue;
1692
+ if ((spec.isEqual ?? Object.is)(values.get(key), getNext(key))) continue;
1692
1693
  }
1693
- return [{
1694
+ if (!beforePresent) transitions.push({
1694
1695
  key,
1695
- kind: !beforePresent ? "added" : !afterPresent ? "removed" : "updated",
1696
- before: beforePresent ? values.get(key) : void 0,
1697
- after: afterPresent ? getNext(key) : void 0
1698
- }];
1699
- }));
1696
+ kind: "added",
1697
+ before: void 0,
1698
+ after: getNext(key)
1699
+ });
1700
+ else if (!afterPresent) transitions.push({
1701
+ key,
1702
+ kind: "removed",
1703
+ before: values.get(key),
1704
+ after: void 0
1705
+ });
1706
+ else transitions.push({
1707
+ key,
1708
+ kind: "updated",
1709
+ before: values.get(key),
1710
+ after: getNext(key)
1711
+ });
1712
+ }
1713
+ publishedTransitions = Object.freeze(transitions);
1700
1714
  reset = build;
1701
1715
  return change !== void 0;
1702
1716
  },
@@ -2219,9 +2233,21 @@ const createSources = (scheduler) => {
2219
2233
  const before = beforePresent ? previous.get(key) : void 0;
2220
2234
  const after = afterPresent ? read.get(key) : void 0;
2221
2235
  if (beforePresent === afterPresent && Object.is(before, after)) continue;
2222
- transitions.push({
2236
+ if (!beforePresent) transitions.push({
2237
+ key,
2238
+ kind: "added",
2239
+ before: void 0,
2240
+ after
2241
+ });
2242
+ else if (!afterPresent) transitions.push({
2243
+ key,
2244
+ kind: "removed",
2245
+ before,
2246
+ after: void 0
2247
+ });
2248
+ else transitions.push({
2223
2249
  key,
2224
- kind: !beforePresent ? "added" : !afterPresent ? "removed" : "updated",
2250
+ kind: "updated",
2225
2251
  before,
2226
2252
  after
2227
2253
  });