@codefast/di 0.7.1 → 0.8.1
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/CHANGELOG.md +40 -0
- package/README.md +242 -159
- package/dist/ambient/active-container.d.ts +1 -2
- package/dist/ambient/active-container.js +1 -2
- package/dist/container/binding-builders.d.ts +1 -2
- package/dist/container/binding-builders.js +5 -7
- package/dist/container/container.d.ts +1 -2
- package/dist/container/container.js +4 -6
- package/dist/core/binding-scope.d.ts +1 -2
- package/dist/core/binding-scope.js +1 -2
- package/dist/core/binding.d.ts +16 -4
- package/dist/core/binding.js +30 -6
- package/dist/core/constraint-requirement.d.ts +1 -2
- package/dist/core/constraint-requirement.js +1 -2
- package/dist/core/constructor-type.d.ts +1 -2
- package/dist/core/constructor-type.js +1 -2
- package/dist/core/map-upsert.d.ts +1 -2
- package/dist/core/map-upsert.js +1 -2
- package/dist/core/module.d.ts +1 -2
- package/dist/core/module.js +1 -2
- package/dist/core/registry.d.ts +1 -5
- package/dist/core/registry.js +15 -63
- package/dist/core/tag.d.ts +27 -2
- package/dist/core/tag.js +55 -13
- package/dist/core/token.d.ts +1 -2
- package/dist/core/token.js +1 -2
- package/dist/core/types.d.ts +1 -2
- package/dist/core/types.js +1 -2
- package/dist/decorators/inject.d.ts +1 -2
- package/dist/decorators/inject.js +1 -2
- package/dist/decorators/injectable.d.ts +1 -2
- package/dist/decorators/injectable.js +1 -2
- package/dist/decorators/lifecycle-decorators.d.ts +1 -2
- package/dist/decorators/lifecycle-decorators.js +1 -2
- package/dist/errors/diagnostics.d.ts +1 -2
- package/dist/errors/diagnostics.js +1 -2
- package/dist/errors/errors.d.ts +1 -2
- package/dist/errors/errors.js +29 -5
- package/dist/index.d.ts +2 -3
- package/dist/index.js +2 -3
- package/dist/injection/descriptor.d.ts +1 -2
- package/dist/injection/descriptor.js +1 -2
- package/dist/injection/resolve-options.d.ts +18 -14
- package/dist/injection/resolve-options.js +70 -17
- package/dist/introspection/dependency-graph.d.ts +1 -2
- package/dist/introspection/dependency-graph.js +23 -11
- package/dist/introspection/graph-adapters/cytoscape.d.ts +1 -2
- package/dist/introspection/graph-adapters/cytoscape.js +1 -2
- package/dist/introspection/graph-adapters/dot.d.ts +1 -2
- package/dist/introspection/graph-adapters/dot.js +1 -2
- package/dist/introspection/graph-adapters/mermaid.d.ts +1 -2
- package/dist/introspection/graph-adapters/mermaid.js +1 -2
- package/dist/introspection/graph-adapters/reactflow.d.ts +1 -2
- package/dist/introspection/graph-adapters/reactflow.js +1 -2
- package/dist/introspection/inspector.d.ts +1 -2
- package/dist/introspection/inspector.js +1 -2
- package/dist/lifecycle/lifecycle-manager.d.ts +1 -2
- package/dist/lifecycle/lifecycle-manager.js +1 -2
- package/dist/lifecycle/scope-manager.d.ts +1 -2
- package/dist/lifecycle/scope-manager.js +1 -2
- package/dist/metadata/metadata-keys.d.ts +1 -2
- package/dist/metadata/metadata-keys.js +1 -2
- package/dist/metadata/metadata-reader-token.d.ts +1 -2
- package/dist/metadata/metadata-reader-token.js +1 -2
- package/dist/metadata/metadata-types.d.ts +1 -2
- package/dist/metadata/metadata-types.js +1 -2
- package/dist/metadata/symbol-metadata-reader.d.ts +1 -2
- package/dist/metadata/symbol-metadata-reader.js +1 -2
- package/dist/metadata/verifying-metadata-reader.d.ts +1 -2
- package/dist/metadata/verifying-metadata-reader.js +1 -2
- package/dist/resolution/cache/activation-need.d.ts +1 -2
- package/dist/resolution/cache/activation-need.js +1 -2
- package/dist/resolution/cache/binding-lookup-cache.d.ts +2 -5
- package/dist/resolution/cache/binding-lookup-cache.js +2 -40
- package/dist/resolution/cache/class-introspector.d.ts +1 -2
- package/dist/resolution/cache/class-introspector.js +1 -2
- package/dist/resolution/context.d.ts +1 -2
- package/dist/resolution/context.js +1 -2
- package/dist/resolution/path/resolution-path.d.ts +1 -2
- package/dist/resolution/path/resolution-path.js +1 -2
- package/dist/resolution/plan/instantiation-plan.d.ts +5 -14
- package/dist/resolution/plan/instantiation-plan.js +10 -29
- package/dist/resolution/resolver.d.ts +13 -7
- package/dist/resolution/resolver.js +107 -143
- package/dist/resolution/select/binding-select.d.ts +5 -7
- package/dist/resolution/select/binding-select.js +19 -22
- package/dist/resolution/select/constraints.d.ts +1 -2
- package/dist/resolution/select/constraints.js +1 -2
- package/package.json +6 -58
- package/dist/ambient/active-container.d.ts.map +0 -1
- package/dist/ambient/active-container.js.map +0 -1
- package/dist/container/binding-builders.d.ts.map +0 -1
- package/dist/container/binding-builders.js.map +0 -1
- package/dist/container/container.d.ts.map +0 -1
- package/dist/container/container.js.map +0 -1
- package/dist/core/binding-scope.d.ts.map +0 -1
- package/dist/core/binding-scope.js.map +0 -1
- package/dist/core/binding.d.ts.map +0 -1
- package/dist/core/binding.js.map +0 -1
- package/dist/core/constraint-requirement.d.ts.map +0 -1
- package/dist/core/constraint-requirement.js.map +0 -1
- package/dist/core/constructor-type.d.ts.map +0 -1
- package/dist/core/constructor-type.js.map +0 -1
- package/dist/core/map-upsert.d.ts.map +0 -1
- package/dist/core/map-upsert.js.map +0 -1
- package/dist/core/module.d.ts.map +0 -1
- package/dist/core/module.js.map +0 -1
- package/dist/core/registry.d.ts.map +0 -1
- package/dist/core/registry.js.map +0 -1
- package/dist/core/tag.d.ts.map +0 -1
- package/dist/core/tag.js.map +0 -1
- package/dist/core/token.d.ts.map +0 -1
- package/dist/core/token.js.map +0 -1
- package/dist/core/types.d.ts.map +0 -1
- package/dist/core/types.js.map +0 -1
- package/dist/decorators/inject.d.ts.map +0 -1
- package/dist/decorators/inject.js.map +0 -1
- package/dist/decorators/injectable.d.ts.map +0 -1
- package/dist/decorators/injectable.js.map +0 -1
- package/dist/decorators/lifecycle-decorators.d.ts.map +0 -1
- package/dist/decorators/lifecycle-decorators.js.map +0 -1
- package/dist/errors/diagnostics.d.ts.map +0 -1
- package/dist/errors/diagnostics.js.map +0 -1
- package/dist/errors/errors.d.ts.map +0 -1
- package/dist/errors/errors.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/injection/descriptor.d.ts.map +0 -1
- package/dist/injection/descriptor.js.map +0 -1
- package/dist/injection/resolve-options.d.ts.map +0 -1
- package/dist/injection/resolve-options.js.map +0 -1
- package/dist/introspection/dependency-graph.d.ts.map +0 -1
- package/dist/introspection/dependency-graph.js.map +0 -1
- package/dist/introspection/graph-adapters/cytoscape.d.ts.map +0 -1
- package/dist/introspection/graph-adapters/cytoscape.js.map +0 -1
- package/dist/introspection/graph-adapters/dot.d.ts.map +0 -1
- package/dist/introspection/graph-adapters/dot.js.map +0 -1
- package/dist/introspection/graph-adapters/mermaid.d.ts.map +0 -1
- package/dist/introspection/graph-adapters/mermaid.js.map +0 -1
- package/dist/introspection/graph-adapters/reactflow.d.ts.map +0 -1
- package/dist/introspection/graph-adapters/reactflow.js.map +0 -1
- package/dist/introspection/inspector.d.ts.map +0 -1
- package/dist/introspection/inspector.js.map +0 -1
- package/dist/lifecycle/lifecycle-manager.d.ts.map +0 -1
- package/dist/lifecycle/lifecycle-manager.js.map +0 -1
- package/dist/lifecycle/scope-manager.d.ts.map +0 -1
- package/dist/lifecycle/scope-manager.js.map +0 -1
- package/dist/metadata/metadata-keys.d.ts.map +0 -1
- package/dist/metadata/metadata-keys.js.map +0 -1
- package/dist/metadata/metadata-reader-token.d.ts.map +0 -1
- package/dist/metadata/metadata-reader-token.js.map +0 -1
- package/dist/metadata/metadata-types.d.ts.map +0 -1
- package/dist/metadata/metadata-types.js.map +0 -1
- package/dist/metadata/symbol-metadata-reader.d.ts.map +0 -1
- package/dist/metadata/symbol-metadata-reader.js.map +0 -1
- package/dist/metadata/verifying-metadata-reader.d.ts.map +0 -1
- package/dist/metadata/verifying-metadata-reader.js.map +0 -1
- package/dist/resolution/cache/activation-need.d.ts.map +0 -1
- package/dist/resolution/cache/activation-need.js.map +0 -1
- package/dist/resolution/cache/binding-lookup-cache.d.ts.map +0 -1
- package/dist/resolution/cache/binding-lookup-cache.js.map +0 -1
- package/dist/resolution/cache/class-introspector.d.ts.map +0 -1
- package/dist/resolution/cache/class-introspector.js.map +0 -1
- package/dist/resolution/context.d.ts.map +0 -1
- package/dist/resolution/context.js.map +0 -1
- package/dist/resolution/path/resolution-path.d.ts.map +0 -1
- package/dist/resolution/path/resolution-path.js.map +0 -1
- package/dist/resolution/plan/instantiation-plan.d.ts.map +0 -1
- package/dist/resolution/plan/instantiation-plan.js.map +0 -1
- package/dist/resolution/resolver.d.ts.map +0 -1
- package/dist/resolution/resolver.js.map +0 -1
- package/dist/resolution/select/binding-select.d.ts.map +0 -1
- package/dist/resolution/select/binding-select.js.map +0 -1
- package/dist/resolution/select/constraints.d.ts.map +0 -1
- package/dist/resolution/select/constraints.js.map +0 -1
- package/src/ambient/active-container.ts +0 -69
- package/src/container/binding-builders.ts +0 -332
- package/src/container/container.ts +0 -973
- package/src/core/binding-scope.ts +0 -15
- package/src/core/binding.ts +0 -477
- package/src/core/constraint-requirement.ts +0 -94
- package/src/core/constructor-type.ts +0 -18
- package/src/core/map-upsert.ts +0 -53
- package/src/core/module.ts +0 -117
- package/src/core/registry.ts +0 -371
- package/src/core/tag.ts +0 -158
- package/src/core/token.ts +0 -31
- package/src/core/types.ts +0 -149
- package/src/decorators/inject.ts +0 -101
- package/src/decorators/injectable.ts +0 -134
- package/src/decorators/lifecycle-decorators.ts +0 -43
- package/src/errors/diagnostics.ts +0 -45
- package/src/errors/errors.ts +0 -476
- package/src/index.ts +0 -139
- package/src/injection/descriptor.ts +0 -218
- package/src/injection/resolve-options.ts +0 -144
- package/src/introspection/dependency-graph.ts +0 -282
- package/src/introspection/graph-adapters/cytoscape.ts +0 -78
- package/src/introspection/graph-adapters/dot.ts +0 -32
- package/src/introspection/graph-adapters/mermaid.ts +0 -61
- package/src/introspection/graph-adapters/reactflow.ts +0 -84
- package/src/introspection/inspector.ts +0 -134
- package/src/lifecycle/lifecycle-manager.ts +0 -272
- package/src/lifecycle/scope-manager.ts +0 -158
- package/src/metadata/metadata-keys.ts +0 -28
- package/src/metadata/metadata-reader-token.ts +0 -10
- package/src/metadata/metadata-types.ts +0 -57
- package/src/metadata/symbol-metadata-reader.ts +0 -126
- package/src/metadata/verifying-metadata-reader.ts +0 -41
- package/src/resolution/cache/activation-need.ts +0 -87
- package/src/resolution/cache/binding-lookup-cache.ts +0 -209
- package/src/resolution/cache/class-introspector.ts +0 -256
- package/src/resolution/context.ts +0 -331
- package/src/resolution/path/resolution-path.ts +0 -167
- package/src/resolution/plan/instantiation-plan.ts +0 -617
- package/src/resolution/resolver.ts +0 -1675
- package/src/resolution/select/binding-select.ts +0 -210
- package/src/resolution/select/constraints.ts +0 -146
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options-less token → terminal binding, memoized per container chain.
|
|
3
|
-
*
|
|
4
|
-
* @remarks Each cache links to its parent's, so a child answers from its own chain without walking
|
|
5
|
-
* the container hierarchy again.
|
|
6
|
-
*/
|
|
7
|
-
import type { Binding } from "#/core/binding";
|
|
8
|
-
import { getOrInsertComputed } from "#/core/map-upsert";
|
|
9
|
-
import type { BindingRegistry } from "#/core/registry";
|
|
10
|
-
import type { BindingTag } from "#/core/tag";
|
|
11
|
-
import type { Token } from "#/core/token";
|
|
12
|
-
import type { Constructor } from "#/core/types";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A token's terminal binding with alias hops already folded, plus the container that owns it.
|
|
16
|
-
*
|
|
17
|
-
* @typeParam Owner - the resolver type, kept generic so this module stays free of resolver internals
|
|
18
|
-
*
|
|
19
|
-
* @since 0.5.0-canary.8
|
|
20
|
-
*/
|
|
21
|
-
export interface DefaultLookupEntry<Owner> {
|
|
22
|
-
readonly binding: Binding;
|
|
23
|
-
readonly owner: Owner;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Alias folding gives up past this many hops and defers to the full resolve loop, whose
|
|
28
|
-
* Set-based traversal detects genuine cycles exactly rather than by an arbitrary cap.
|
|
29
|
-
*
|
|
30
|
-
* @since 0.5.0-canary.8
|
|
31
|
-
*/
|
|
32
|
-
export const ALIAS_HOP_LIMIT = 32;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @since 0.5.0-canary.8
|
|
36
|
-
*/
|
|
37
|
-
const newNameToEntryMap = <Owner>(): Map<string, DefaultLookupEntry<Owner> | null> => new Map();
|
|
38
|
-
|
|
39
|
-
const newTagToEntryMap = <Owner>(): Map<BindingTag, DefaultLookupEntry<Owner> | null> => new Map();
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* A version-stamped cache of binding lookups by token, name, and tag across the container chain.
|
|
43
|
-
*
|
|
44
|
-
* @since 0.5.0-canary.9
|
|
45
|
-
*/
|
|
46
|
-
export class BindingLookupCache<Owner> {
|
|
47
|
-
readonly #byToken = new Map<Token<unknown> | Constructor, DefaultLookupEntry<Owner> | null>();
|
|
48
|
-
#version = -1;
|
|
49
|
-
// One entry in front of the map: the two shapes that reach here — an alias, and a token owned by
|
|
50
|
-
// a parent — are both resolved in a loop over the same token. `null` is a real answer, so absence
|
|
51
|
-
// is tracked by the token slot rather than by the entry.
|
|
52
|
-
#lastToken: Token<unknown> | Constructor | undefined;
|
|
53
|
-
#lastEntry: DefaultLookupEntry<Owner> | null = null;
|
|
54
|
-
readonly #byTokenAndName = new Map<Token<unknown> | Constructor, Map<string, DefaultLookupEntry<Owner> | null>>();
|
|
55
|
-
#namedVersion = -1;
|
|
56
|
-
readonly #byTokenAndTag = new Map<Token<unknown> | Constructor, Map<BindingTag, DefaultLookupEntry<Owner> | null>>();
|
|
57
|
-
#taggedVersion = -1;
|
|
58
|
-
// One entry in front of the tag map, and the map is not written until a second distinct request
|
|
59
|
-
// shape appears: a per-request child usually asks one (token, tag) once, and the inner-map
|
|
60
|
-
// allocation was that shape's whole regression when this memo landed.
|
|
61
|
-
#lastTagToken: Token<unknown> | Constructor | undefined;
|
|
62
|
-
#lastTag: BindingTag | undefined;
|
|
63
|
-
#lastTaggedEntry: DefaultLookupEntry<Owner> | null = null;
|
|
64
|
-
|
|
65
|
-
readonly #registry: BindingRegistry;
|
|
66
|
-
readonly #owner: Owner;
|
|
67
|
-
readonly #parent: BindingLookupCache<Owner> | undefined;
|
|
68
|
-
|
|
69
|
-
constructor(registry: BindingRegistry, owner: Owner, parent: BindingLookupCache<Owner> | undefined) {
|
|
70
|
-
this.#registry = registry;
|
|
71
|
-
this.#owner = owner;
|
|
72
|
-
this.#parent = parent;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Summed registry versions of this cache's whole chain — the memo stamp. */
|
|
76
|
-
chainVersion(): number {
|
|
77
|
-
let version = this.#registry.version;
|
|
78
|
-
for (let cache = this.#parent; cache !== undefined; cache = cache.#parent) {
|
|
79
|
-
version += cache.#registry.version;
|
|
80
|
-
}
|
|
81
|
-
return version;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** `null` when the token's shape needs the full selection path. */
|
|
85
|
-
defaultEntry(token: Token<unknown> | Constructor): DefaultLookupEntry<Owner> | null {
|
|
86
|
-
const version = this.chainVersion();
|
|
87
|
-
if (version !== this.#version) {
|
|
88
|
-
this.#byToken.clear();
|
|
89
|
-
this.#version = version;
|
|
90
|
-
this.#lastToken = undefined;
|
|
91
|
-
} else if (token === this.#lastToken) {
|
|
92
|
-
return this.#lastEntry;
|
|
93
|
-
}
|
|
94
|
-
let entry = this.#byToken.get(token);
|
|
95
|
-
if (entry === undefined) {
|
|
96
|
-
entry = this.#foldAliases(token);
|
|
97
|
-
this.#byToken.set(token, entry);
|
|
98
|
-
}
|
|
99
|
-
this.#lastToken = token;
|
|
100
|
-
this.#lastEntry = entry;
|
|
101
|
-
return entry;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** `null` when the name's shape needs the full selection path. */
|
|
105
|
-
namedEntry(token: Token<unknown> | Constructor, name: string): DefaultLookupEntry<Owner> | null {
|
|
106
|
-
const version = this.chainVersion();
|
|
107
|
-
if (version !== this.#namedVersion) {
|
|
108
|
-
this.#byTokenAndName.clear();
|
|
109
|
-
this.#namedVersion = version;
|
|
110
|
-
}
|
|
111
|
-
// Computed, not eager: this runs on every named resolve, and the eager form would allocate a
|
|
112
|
-
// Map per call only to discard it on the hit that follows.
|
|
113
|
-
const byName = getOrInsertComputed(this.#byTokenAndName, token, newNameToEntryMap);
|
|
114
|
-
let entry = byName.get(name);
|
|
115
|
-
if (entry === undefined) {
|
|
116
|
-
entry = this.#findNamedInChain(token, name);
|
|
117
|
-
byName.set(name, entry);
|
|
118
|
-
}
|
|
119
|
-
return entry;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** `null` when the tag's shape needs the full selection path. */
|
|
123
|
-
taggedEntry(token: Token<unknown> | Constructor, tag: BindingTag): DefaultLookupEntry<Owner> | null {
|
|
124
|
-
const version = this.chainVersion();
|
|
125
|
-
if (version !== this.#taggedVersion) {
|
|
126
|
-
this.#byTokenAndTag.clear();
|
|
127
|
-
this.#taggedVersion = version;
|
|
128
|
-
this.#lastTagToken = undefined;
|
|
129
|
-
this.#lastTag = undefined;
|
|
130
|
-
} else if (token === this.#lastTagToken && tag === this.#lastTag) {
|
|
131
|
-
return this.#lastTaggedEntry;
|
|
132
|
-
}
|
|
133
|
-
let entry: DefaultLookupEntry<Owner> | null | undefined;
|
|
134
|
-
if (this.#lastTagToken === undefined) {
|
|
135
|
-
// First shape this cache generation sees: answer from the walk and defer the map entirely.
|
|
136
|
-
entry = this.#findTaggedInChain(token, tag);
|
|
137
|
-
} else {
|
|
138
|
-
// Keyed by the criterion object itself: criteria are interned, so identity is the slot
|
|
139
|
-
// contract's own `Object.is` — the same exactness the registry's tagged index relies on.
|
|
140
|
-
const byTag = getOrInsertComputed(this.#byTokenAndTag, token, newTagToEntryMap);
|
|
141
|
-
entry = byTag.get(tag);
|
|
142
|
-
if (entry === undefined) {
|
|
143
|
-
entry = this.#findTaggedInChain(token, tag);
|
|
144
|
-
byTag.set(tag, entry);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
this.#lastTagToken = token;
|
|
148
|
-
this.#lastTag = tag;
|
|
149
|
-
this.#lastTaggedEntry = entry;
|
|
150
|
-
return entry;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
#foldAliases(token: Token<unknown> | Constructor): DefaultLookupEntry<Owner> | null {
|
|
154
|
-
let current = token;
|
|
155
|
-
for (let hop = 0; hop < ALIAS_HOP_LIMIT; hop += 1) {
|
|
156
|
-
const entry = this.#findDefaultInChain(current);
|
|
157
|
-
if (entry === null) {
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
|
-
if (entry.binding.kind !== "alias") {
|
|
161
|
-
return entry;
|
|
162
|
-
}
|
|
163
|
-
current = entry.binding.target;
|
|
164
|
-
}
|
|
165
|
-
return null;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
#findDefaultInChain(token: Token<unknown> | Constructor): DefaultLookupEntry<Owner> | null {
|
|
169
|
-
const fast = this.#registry.getFastDefault(token);
|
|
170
|
-
if (fast !== undefined) {
|
|
171
|
-
return { binding: fast, owner: this.#owner };
|
|
172
|
-
}
|
|
173
|
-
// A level with non-fast bindings (multi-slot / predicate) needs full selection — bail.
|
|
174
|
-
if (this.#registry.has(token)) {
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
return this.#parent === undefined ? null : this.#parent.#findDefaultInChain(token);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
#findNamedInChain(token: Token<unknown> | Constructor, name: string): DefaultLookupEntry<Owner> | null {
|
|
181
|
-
const named = this.#registry.getSimpleNamed(token, name);
|
|
182
|
-
if (named !== undefined) {
|
|
183
|
-
// Predicates need a live context; aliases carry options through the full path.
|
|
184
|
-
if (named.predicate !== undefined || named.kind === "alias") {
|
|
185
|
-
return null;
|
|
186
|
-
}
|
|
187
|
-
return { binding: named, owner: this.#owner };
|
|
188
|
-
}
|
|
189
|
-
if (this.#registry.has(token)) {
|
|
190
|
-
return null;
|
|
191
|
-
}
|
|
192
|
-
return this.#parent === undefined ? null : this.#parent.#findNamedInChain(token, name);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
#findTaggedInChain(token: Token<unknown> | Constructor, tag: BindingTag): DefaultLookupEntry<Owner> | null {
|
|
196
|
-
const tagged = this.#registry.getSimpleTagged(token, tag);
|
|
197
|
-
if (tagged !== undefined) {
|
|
198
|
-
// Predicates need a live context; aliases carry options through the full path.
|
|
199
|
-
if (tagged.predicate !== undefined || tagged.kind === "alias") {
|
|
200
|
-
return null;
|
|
201
|
-
}
|
|
202
|
-
return { binding: tagged, owner: this.#owner };
|
|
203
|
-
}
|
|
204
|
-
if (this.#registry.has(token)) {
|
|
205
|
-
return null;
|
|
206
|
-
}
|
|
207
|
-
return this.#parent === undefined ? null : this.#parent.#findTaggedInChain(token, tag);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Per-class decorator metadata, cached by constructor.
|
|
3
|
-
*
|
|
4
|
-
* @remarks Metadata cannot change once a class is defined, so nothing here needs version stamping.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { runWithAmbientResolution } from "#/ambient/active-container";
|
|
8
|
-
import type { AmbientResolution } from "#/ambient/active-container";
|
|
9
|
-
import type { Container } from "#/container/container";
|
|
10
|
-
import type { ConstructorInvocation } from "#/core/constructor-type";
|
|
11
|
-
import type { Constructor } from "#/core/types";
|
|
12
|
-
import { InvalidMetadataError } from "#/errors/errors";
|
|
13
|
-
import type { InjectionDescriptor } from "#/injection/descriptor";
|
|
14
|
-
import type { ConstructorMetadata, LifecycleMetadata, MetadataReader } from "#/metadata/metadata-types";
|
|
15
|
-
|
|
16
|
-
// Verified pairs, not verified classes: two readers may disagree about the same class, and a reader
|
|
17
|
-
// that goes out of scope takes its record with it.
|
|
18
|
-
const verifiedTargets = new WeakMap<MetadataReader, WeakSet<Constructor>>();
|
|
19
|
-
const verifiedLifecycleTargets = new WeakMap<MetadataReader, WeakSet<Constructor>>();
|
|
20
|
-
const verifiedAccessorTargets = new WeakMap<MetadataReader, WeakSet<Constructor>>();
|
|
21
|
-
|
|
22
|
-
function isVerified(
|
|
23
|
-
cache: WeakMap<MetadataReader, WeakSet<Constructor>>,
|
|
24
|
-
reader: MetadataReader,
|
|
25
|
-
target: Constructor,
|
|
26
|
-
): boolean {
|
|
27
|
-
return cache.get(reader)?.has(target) ?? false;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function markVerified(
|
|
31
|
-
cache: WeakMap<MetadataReader, WeakSet<Constructor>>,
|
|
32
|
-
reader: MetadataReader,
|
|
33
|
-
target: Constructor,
|
|
34
|
-
): void {
|
|
35
|
-
let verified = cache.get(reader);
|
|
36
|
-
if (verified === undefined) {
|
|
37
|
-
verified = new WeakSet();
|
|
38
|
-
cache.set(reader, verified);
|
|
39
|
-
}
|
|
40
|
-
verified.add(target);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* A reader's constructor metadata for a class, verified the first time this process asks.
|
|
45
|
-
*
|
|
46
|
-
* @remarks Metadata cannot change once a class is defined, so re-checking per container would charge
|
|
47
|
-
* every fresh container for a fact already established — a per-request child or a cold boot pays
|
|
48
|
-
* that repeatedly.
|
|
49
|
-
*
|
|
50
|
-
* @since 0.6.0
|
|
51
|
-
*/
|
|
52
|
-
export function verifyConstructorMetadata(
|
|
53
|
-
reader: MetadataReader,
|
|
54
|
-
target: Constructor,
|
|
55
|
-
): ConstructorMetadata | undefined {
|
|
56
|
-
const metadata = reader.getConstructorMetadata(target);
|
|
57
|
-
if (metadata === undefined) {
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
if (isVerified(verifiedTargets, reader, target)) {
|
|
61
|
-
return metadata;
|
|
62
|
-
}
|
|
63
|
-
assertConstructorMetadata(metadata, target);
|
|
64
|
-
markVerified(verifiedTargets, reader, target);
|
|
65
|
-
|
|
66
|
-
return metadata;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Verifies what a reader claims about a class, since a `MetadataReader` is a public seam.
|
|
71
|
-
*
|
|
72
|
-
* @remarks Every path that reads constructor metadata comes through here, so a reader that answers
|
|
73
|
-
* wrongly is a named error at the class it described rather than a `TypeError` raised later inside a
|
|
74
|
-
* resolve. Only what a consumer dereferences is checked: `params` and each entry's `token`.
|
|
75
|
-
* `optional`/`multi` degrade to falsy without crashing, and `index` is decorative — dependencies are
|
|
76
|
-
* consumed positionally.
|
|
77
|
-
*
|
|
78
|
-
* @since 0.6.0
|
|
79
|
-
*/
|
|
80
|
-
export function assertConstructorMetadata(metadata: unknown, target: Constructor): ConstructorMetadata | undefined {
|
|
81
|
-
if (metadata === undefined) {
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
84
|
-
if (typeof metadata !== "object" || metadata === null) {
|
|
85
|
-
throw new InvalidMetadataError(
|
|
86
|
-
target.name,
|
|
87
|
-
`constructor metadata: expected an object, received ${typeof metadata}`,
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
const params: unknown = Reflect.get(metadata, "params");
|
|
91
|
-
if (!Array.isArray(params)) {
|
|
92
|
-
throw new InvalidMetadataError(target.name, "constructor metadata: params is not an array");
|
|
93
|
-
}
|
|
94
|
-
for (const [position, param] of params.entries()) {
|
|
95
|
-
if (typeof param !== "object" || param === null) {
|
|
96
|
-
throw new InvalidMetadataError(target.name, `constructor metadata: params[${String(position)}] is not an object`);
|
|
97
|
-
}
|
|
98
|
-
const dependency: unknown = Reflect.get(param, "token");
|
|
99
|
-
if (typeof dependency !== "object" && typeof dependency !== "function") {
|
|
100
|
-
throw new InvalidMetadataError(
|
|
101
|
-
target.name,
|
|
102
|
-
`constructor metadata: params[${String(position)}].token is not a token or a class`,
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return metadata as ConstructorMetadata;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* A reader's lifecycle metadata for a class, verified the first time this process asks.
|
|
112
|
-
*
|
|
113
|
-
* @since 0.6.0
|
|
114
|
-
*/
|
|
115
|
-
export function verifyLifecycleMetadata(reader: MetadataReader, target: Constructor): LifecycleMetadata | undefined {
|
|
116
|
-
const metadata = reader.getLifecycleMetadata(target);
|
|
117
|
-
if (metadata === undefined || isVerified(verifiedLifecycleTargets, reader, target)) {
|
|
118
|
-
return metadata;
|
|
119
|
-
}
|
|
120
|
-
if (typeof metadata !== "object" || metadata === null) {
|
|
121
|
-
throw new InvalidMetadataError(target.name, `lifecycle metadata: expected an object, received ${typeof metadata}`);
|
|
122
|
-
}
|
|
123
|
-
for (const phase of ["postConstruct", "preDestroy"] as const) {
|
|
124
|
-
const methods: unknown = Reflect.get(metadata, phase);
|
|
125
|
-
if (methods === undefined) {
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
if (!Array.isArray(methods)) {
|
|
129
|
-
throw new InvalidMetadataError(target.name, `lifecycle metadata: ${phase} is not an array`);
|
|
130
|
-
}
|
|
131
|
-
for (const [position, name] of methods.entries()) {
|
|
132
|
-
if (typeof name !== "string") {
|
|
133
|
-
throw new InvalidMetadataError(
|
|
134
|
-
target.name,
|
|
135
|
-
`lifecycle metadata: ${phase}[${String(position)}] is not a string`,
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
markVerified(verifiedLifecycleTargets, reader, target);
|
|
141
|
-
return metadata;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* A reader's accessor metadata for a class, verified the first time this process asks.
|
|
146
|
-
*
|
|
147
|
-
* @since 0.6.0
|
|
148
|
-
*/
|
|
149
|
-
export function verifyAccessorMetadata(
|
|
150
|
-
reader: MetadataReader,
|
|
151
|
-
target: Constructor,
|
|
152
|
-
): ReadonlyArray<{ readonly key: string | symbol; readonly descriptor: InjectionDescriptor }> | undefined {
|
|
153
|
-
const metadata = reader.getAccessorMetadata?.(target);
|
|
154
|
-
if (metadata === undefined || isVerified(verifiedAccessorTargets, reader, target)) {
|
|
155
|
-
return metadata;
|
|
156
|
-
}
|
|
157
|
-
if (!Array.isArray(metadata)) {
|
|
158
|
-
throw new InvalidMetadataError(target.name, "accessor metadata: expected an array");
|
|
159
|
-
}
|
|
160
|
-
for (const [position, entry] of metadata.entries()) {
|
|
161
|
-
if (typeof entry !== "object" || entry === null) {
|
|
162
|
-
throw new InvalidMetadataError(target.name, `accessor metadata: [${String(position)}] is not an object`);
|
|
163
|
-
}
|
|
164
|
-
const key: unknown = Reflect.get(entry, "key");
|
|
165
|
-
if (typeof key !== "string" && typeof key !== "symbol") {
|
|
166
|
-
throw new InvalidMetadataError(
|
|
167
|
-
target.name,
|
|
168
|
-
`accessor metadata: [${String(position)}].key is not a string or symbol`,
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
const descriptor: unknown = Reflect.get(entry, "descriptor");
|
|
172
|
-
if (typeof descriptor !== "object" || descriptor === null) {
|
|
173
|
-
throw new InvalidMetadataError(
|
|
174
|
-
target.name,
|
|
175
|
-
`accessor metadata: [${String(position)}].descriptor is not an object`,
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
const dependency: unknown = Reflect.get(descriptor, "token");
|
|
179
|
-
if (typeof dependency !== "object" && typeof dependency !== "function") {
|
|
180
|
-
throw new InvalidMetadataError(
|
|
181
|
-
target.name,
|
|
182
|
-
`accessor metadata: [${String(position)}].descriptor.token is not a token or a class`,
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
markVerified(verifiedAccessorTargets, reader, target);
|
|
187
|
-
return metadata;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* A per-class cache of constructor metadata and the activation facts derived from it.
|
|
192
|
-
*
|
|
193
|
-
* @since 0.5.0-canary.8
|
|
194
|
-
*/
|
|
195
|
-
export class ClassIntrospector {
|
|
196
|
-
// Unallocated until the container resolves its first class binding — a container bound entirely
|
|
197
|
-
// to constants, factories or aliases never introspects one.
|
|
198
|
-
#constructorMetadata: WeakMap<Constructor, ConstructorMetadata | null> | undefined;
|
|
199
|
-
#hasPostConstruct: WeakMap<Constructor, boolean> | undefined;
|
|
200
|
-
#needsActiveContainer: WeakMap<Constructor, boolean> | undefined;
|
|
201
|
-
readonly #reader: MetadataReader;
|
|
202
|
-
readonly #container: Container;
|
|
203
|
-
|
|
204
|
-
constructor(reader: MetadataReader, container: Container) {
|
|
205
|
-
this.#reader = reader;
|
|
206
|
-
this.#container = container;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
constructorMetadata(target: Constructor): ConstructorMetadata | undefined {
|
|
210
|
-
const cached = this.#constructorMetadata?.get(target);
|
|
211
|
-
if (cached !== undefined) {
|
|
212
|
-
return cached === null ? undefined : cached;
|
|
213
|
-
}
|
|
214
|
-
const metadata = this.#reader.getConstructorMetadata(target);
|
|
215
|
-
(this.#constructorMetadata ??= new WeakMap<Constructor, ConstructorMetadata | null>()).set(
|
|
216
|
-
target,
|
|
217
|
-
metadata ?? null,
|
|
218
|
-
);
|
|
219
|
-
return metadata;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Whether the class has a `@postConstruct` hook, or `undefined` until {@link discoverPostConstruct}.
|
|
224
|
-
*
|
|
225
|
-
* @remarks Callers treat unknown as "assume it does", so the first activation settles it.
|
|
226
|
-
*/
|
|
227
|
-
knownPostConstruct(target: Constructor): boolean | undefined {
|
|
228
|
-
return this.#hasPostConstruct?.get(target);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
discoverPostConstruct(target: Constructor): void {
|
|
232
|
-
const lifecycle = this.#reader.getLifecycleMetadata(target);
|
|
233
|
-
(this.#hasPostConstruct ??= new WeakMap<Constructor, boolean>()).set(
|
|
234
|
-
target,
|
|
235
|
-
lifecycle !== undefined && lifecycle.postConstruct !== undefined && lifecycle.postConstruct.length > 0,
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/** True when the class has accessor injection, which reads the container during construction. */
|
|
240
|
-
needsActiveContainer(target: Constructor): boolean {
|
|
241
|
-
let needsActiveContainer = this.#needsActiveContainer?.get(target);
|
|
242
|
-
if (needsActiveContainer === undefined) {
|
|
243
|
-
needsActiveContainer = (this.#reader.getAccessorMetadata?.(target)?.length ?? 0) > 0;
|
|
244
|
-
(this.#needsActiveContainer ??= new WeakMap<Constructor, boolean>()).set(target, needsActiveContainer);
|
|
245
|
-
}
|
|
246
|
-
return needsActiveContainer;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
instantiate(target: Constructor, deps: Array<unknown>, resolution?: AmbientResolution): unknown {
|
|
250
|
-
const invokable = target as ConstructorInvocation;
|
|
251
|
-
if (!this.needsActiveContainer(target)) {
|
|
252
|
-
return new invokable(...deps);
|
|
253
|
-
}
|
|
254
|
-
return runWithAmbientResolution(this.#container, resolution, () => new invokable(...deps));
|
|
255
|
-
}
|
|
256
|
-
}
|