@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,218 +0,0 @@
|
|
|
1
|
-
/** The one shape every declared dependency is normalised to, whatever channel declared it. */
|
|
2
|
-
import type { Token } from "#/core/token";
|
|
3
|
-
import type { BindingTag, Constructor, TokenValue } from "#/core/types";
|
|
4
|
-
import type { DependencySlot } from "#/injection/resolve-options";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Slot-selection options — a name and tags — a declared dependency narrows its binding with.
|
|
8
|
-
*
|
|
9
|
-
* @since 0.3.16-canary.0
|
|
10
|
-
*/
|
|
11
|
-
export interface InjectOptions {
|
|
12
|
-
name?: string | undefined;
|
|
13
|
-
/**
|
|
14
|
-
* Single-tag shorthand, equivalent to listing the one pair in `tags`.
|
|
15
|
-
*
|
|
16
|
-
* @remarks Folded into `tags` when the descriptor is built, so nothing downstream sees two
|
|
17
|
-
* spellings of one request. Giving both is a request for every pair across the two.
|
|
18
|
-
*/
|
|
19
|
-
tag?: BindingTag | undefined;
|
|
20
|
-
tags?: ReadonlyArray<BindingTag> | undefined;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* One dependency a `toResolved` factory or an `@inject` accessor declares.
|
|
25
|
-
*
|
|
26
|
-
* @remarks Extends {@link DependencySlot} so the two dependency sources stay literally one shape,
|
|
27
|
-
* narrowing only `token` to carry the value type a factory's parameter is checked against.
|
|
28
|
-
*
|
|
29
|
-
* @since 0.3.16-canary.0
|
|
30
|
-
*/
|
|
31
|
-
export interface InjectionDescriptor<out Value = unknown> extends DependencySlot {
|
|
32
|
-
readonly token: Token<Value> | Constructor<Value>;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* A dependency as declared: a bare token, a class, or a full injection descriptor.
|
|
37
|
-
*
|
|
38
|
-
* @since 0.3.16-canary.0
|
|
39
|
-
*/
|
|
40
|
-
export type InjectableDependency<Value = unknown> = Token<Value> | Constructor<Value> | InjectionDescriptor<Value>;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The value a factory receives for one declared dependency.
|
|
44
|
-
*
|
|
45
|
-
* @remarks `optional()` and `injectAll()` fold their effect into the descriptor's own type
|
|
46
|
-
* parameter, so reading it back is enough; bare tokens fall through to {@link TokenValue}.
|
|
47
|
-
*
|
|
48
|
-
* @since 0.5.0-canary.7
|
|
49
|
-
*/
|
|
50
|
-
export type ResolvedDependencyValue<Dependency> = Dependency extends { readonly multi: true }
|
|
51
|
-
? Array<DescribedValue<Dependency>>
|
|
52
|
-
: Dependency extends { readonly optional: true }
|
|
53
|
-
? DescribedValue<Dependency> | undefined
|
|
54
|
-
: DescribedValue<Dependency>;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* The value a dependency's own type parameter carries, before its flags are read.
|
|
58
|
-
*
|
|
59
|
-
* @remarks Split out because a hand-written descriptor states its flags and its value type
|
|
60
|
-
* separately, and only the flags are load-bearing: `{ token: Plugin, multi: true }` says `Plugin`
|
|
61
|
-
* and delivers `Array<Plugin>`. `injectAll()` and `optional()` already fold their effect in, so the
|
|
62
|
-
* flags find an array or an optional there and leave it alone.
|
|
63
|
-
*/
|
|
64
|
-
type DescribedValue<Dependency> = Dependency extends InjectionDescriptor<infer Value> ? Value : TokenValue<Dependency>;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Returns whether the value is an `InjectionDescriptor`, dual-role `inject()` functions included.
|
|
68
|
-
*
|
|
69
|
-
* @since 0.3.16-canary.0
|
|
70
|
-
*/
|
|
71
|
-
export function isInjectionDescriptor(value: unknown): value is InjectionDescriptor {
|
|
72
|
-
if (value === null || value === undefined) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
const type = typeof value;
|
|
76
|
-
// inject() returns a function (dual-role), so must check both object and function
|
|
77
|
-
if (type !== "object" && type !== "function") {
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
return (
|
|
81
|
-
"token" in (value as object) &&
|
|
82
|
-
"optional" in (value as object) &&
|
|
83
|
-
"multi" in (value as object) &&
|
|
84
|
-
typeof (value as InjectionDescriptor).optional === "boolean" &&
|
|
85
|
-
typeof (value as InjectionDescriptor).multi === "boolean"
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Normalises a declared dependency to a plain `InjectionDescriptor`.
|
|
91
|
-
*
|
|
92
|
-
* @since 0.3.16-canary.0
|
|
93
|
-
*/
|
|
94
|
-
export function normalizeToDescriptor(dependency: InjectableDependency): InjectionDescriptor {
|
|
95
|
-
if (isInjectionDescriptor(dependency)) {
|
|
96
|
-
return materializeInjectionDescriptor(dependency);
|
|
97
|
-
}
|
|
98
|
-
return { token: dependency, optional: false, multi: false };
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Dual-role `inject()` values are functions: [[Function]].name must not be treated as a DI slot name.
|
|
103
|
-
* Only enumerable own `name` / `tags` from `Object.defineProperties` are real injection options.
|
|
104
|
-
*/
|
|
105
|
-
function materializeInjectionDescriptor(dependency: InjectionDescriptor): InjectionDescriptor {
|
|
106
|
-
if (typeof dependency !== "function") {
|
|
107
|
-
return dependency;
|
|
108
|
-
}
|
|
109
|
-
const dualRole = dependency as InjectionDescriptor & ((...args: Array<unknown>) => unknown);
|
|
110
|
-
const base: Pick<InjectionDescriptor, "token" | "optional" | "multi"> = {
|
|
111
|
-
token: dualRole.token,
|
|
112
|
-
optional: dualRole.optional,
|
|
113
|
-
multi: dualRole.multi,
|
|
114
|
-
};
|
|
115
|
-
const nameDesc = Object.getOwnPropertyDescriptor(dualRole, "name");
|
|
116
|
-
const tagsDesc = Object.getOwnPropertyDescriptor(dualRole, "tags");
|
|
117
|
-
const explicitName = nameDesc?.enumerable === true && typeof nameDesc.value === "string" ? nameDesc.value : undefined;
|
|
118
|
-
// Annotated because `PropertyDescriptor.value` is `any`, and an `any` reaching the cast below
|
|
119
|
-
// would make it look checked when nothing checked it.
|
|
120
|
-
const explicitTags: unknown = tagsDesc?.enumerable === true ? tagsDesc.value : undefined;
|
|
121
|
-
|
|
122
|
-
if (explicitName !== undefined && explicitTags !== undefined) {
|
|
123
|
-
return {
|
|
124
|
-
...base,
|
|
125
|
-
name: explicitName,
|
|
126
|
-
tags: explicitTags as NonNullable<InjectionDescriptor["tags"]>,
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
if (explicitName !== undefined) {
|
|
130
|
-
return { ...base, name: explicitName };
|
|
131
|
-
}
|
|
132
|
-
if (explicitTags !== undefined) {
|
|
133
|
-
return { ...base, tags: explicitTags as NonNullable<InjectionDescriptor["tags"]> };
|
|
134
|
-
}
|
|
135
|
-
return base;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* The one tag list a request carries, with the single-tag shorthand folded in.
|
|
140
|
-
*
|
|
141
|
-
* @remarks Folding here is what keeps `tag` from reaching `InjectionDescriptor` and everything
|
|
142
|
-
* derived from it, so one request never has two spellings past this point.
|
|
143
|
-
*/
|
|
144
|
-
function requestedTagsOf(options: InjectOptions | undefined): ReadonlyArray<BindingTag> | undefined {
|
|
145
|
-
const shorthand = options?.tag;
|
|
146
|
-
const listed = options?.tags;
|
|
147
|
-
if (shorthand === undefined) {
|
|
148
|
-
return listed;
|
|
149
|
-
}
|
|
150
|
-
return listed === undefined || listed.length === 0 ? [shorthand] : [shorthand, ...listed];
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function withOptions<DescValue>(
|
|
154
|
-
base: Pick<InjectionDescriptor<DescValue>, "token" | "optional" | "multi">,
|
|
155
|
-
options: InjectOptions | undefined,
|
|
156
|
-
): InjectionDescriptor<DescValue> {
|
|
157
|
-
const tags = requestedTagsOf(options);
|
|
158
|
-
if (options?.name !== undefined && tags !== undefined) {
|
|
159
|
-
return { ...base, name: options.name, tags };
|
|
160
|
-
}
|
|
161
|
-
if (options?.name !== undefined) {
|
|
162
|
-
return { ...base, name: options.name };
|
|
163
|
-
}
|
|
164
|
-
if (tags !== undefined) {
|
|
165
|
-
return { ...base, tags };
|
|
166
|
-
}
|
|
167
|
-
return base;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* The descriptor half of `inject()`, shared with the accessor decorator that wraps it.
|
|
172
|
-
*
|
|
173
|
-
* @since 0.6.0
|
|
174
|
-
*/
|
|
175
|
-
export function buildInjectionDescriptor<Value>(
|
|
176
|
-
token: Token<Value> | Constructor<Value>,
|
|
177
|
-
options?: InjectOptions,
|
|
178
|
-
): InjectionDescriptor<Value> {
|
|
179
|
-
return withOptions({ token, optional: false, multi: false }, options);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Creates a descriptor that resolves to `undefined` instead of throwing when no binding matches.
|
|
184
|
-
*
|
|
185
|
-
* @since 0.3.16-canary.0
|
|
186
|
-
*/
|
|
187
|
-
export function optional<Value>(
|
|
188
|
-
token: Token<Value> | Constructor<Value>,
|
|
189
|
-
options?: InjectOptions,
|
|
190
|
-
): InjectionDescriptor<Value | undefined> {
|
|
191
|
-
return withOptions(
|
|
192
|
-
{
|
|
193
|
-
token: token as Token<Value | undefined> | Constructor<Value | undefined>,
|
|
194
|
-
optional: true,
|
|
195
|
-
multi: false,
|
|
196
|
-
},
|
|
197
|
-
options,
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Creates a descriptor that resolves every matching binding for the token into an array.
|
|
203
|
-
*
|
|
204
|
-
* @since 0.3.16-canary.0
|
|
205
|
-
*/
|
|
206
|
-
export function injectAll<Value>(
|
|
207
|
-
token: Token<Value> | Constructor<Value>,
|
|
208
|
-
options?: InjectOptions,
|
|
209
|
-
): InjectionDescriptor<Array<Value>> {
|
|
210
|
-
return withOptions(
|
|
211
|
-
{
|
|
212
|
-
token: token as Token<Array<Value>> | Constructor<Array<Value>>,
|
|
213
|
-
optional: false,
|
|
214
|
-
multi: true,
|
|
215
|
-
},
|
|
216
|
-
options,
|
|
217
|
-
);
|
|
218
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import type { BindingTag } from "#/core/tag";
|
|
2
|
-
import type { Token } from "#/core/token";
|
|
3
|
-
import type { Constructor, ResolveOptions } from "#/core/types";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* What one resolvable dependency declares.
|
|
7
|
-
*
|
|
8
|
-
* @remarks Both dependency sources — a constructor's `ParamMetadata` and a `toResolved`
|
|
9
|
-
* `InjectionDescriptor` — are this shape, which is why one resolve routine serves both.
|
|
10
|
-
*
|
|
11
|
-
* @since 0.5.0-canary.9
|
|
12
|
-
*/
|
|
13
|
-
export interface DependencySlot {
|
|
14
|
-
readonly token: Token<unknown> | Constructor;
|
|
15
|
-
readonly optional: boolean;
|
|
16
|
-
readonly multi: boolean;
|
|
17
|
-
readonly name?: string | undefined;
|
|
18
|
-
readonly tags?: ReadonlyArray<BindingTag> | undefined;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* A request whose only criterion is a name — the shape the registry has a direct index for.
|
|
23
|
-
*
|
|
24
|
-
* @since 0.5.0-canary.9
|
|
25
|
-
*/
|
|
26
|
-
export function isNameOnlyOptions(options: ResolveOptions): options is ResolveOptions & { name: string } {
|
|
27
|
-
return (
|
|
28
|
-
options.name !== undefined && options.tag === undefined && (options.tags === undefined || options.tags.length === 0)
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The lone tag of a request that asks for exactly one, written either way — the shape the registry
|
|
34
|
-
* has a direct tag index for.
|
|
35
|
-
*
|
|
36
|
-
* @remarks Both spellings answer here, so the index is not something one of them silently misses.
|
|
37
|
-
*
|
|
38
|
-
* @since 0.5.0-canary.9
|
|
39
|
-
*/
|
|
40
|
-
export function singleTagOnlyOf(options: ResolveOptions): BindingTag | undefined {
|
|
41
|
-
if (options.name !== undefined) {
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
const listed = options.tags;
|
|
45
|
-
const shorthand = options.tag;
|
|
46
|
-
if (shorthand !== undefined) {
|
|
47
|
-
// Both sources present means the request carries two tags, which no single-tag index can answer.
|
|
48
|
-
return listed === undefined || listed.length === 0 ? shorthand : undefined;
|
|
49
|
-
}
|
|
50
|
-
return listed !== undefined && listed.length === 1 ? listed[0] : undefined;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Shared core: build a ResolveOptions from already-normalised name + tags. */
|
|
54
|
-
function buildOptions(
|
|
55
|
-
name: string | undefined,
|
|
56
|
-
tags: ReadonlyArray<BindingTag> | undefined,
|
|
57
|
-
): ResolveOptions | undefined {
|
|
58
|
-
if (name === undefined && tags === undefined) {
|
|
59
|
-
return undefined;
|
|
60
|
-
}
|
|
61
|
-
const options: ResolveOptions = {};
|
|
62
|
-
if (name !== undefined) {
|
|
63
|
-
options.name = name;
|
|
64
|
-
}
|
|
65
|
-
if (tags !== undefined) {
|
|
66
|
-
options.tags = tags;
|
|
67
|
-
}
|
|
68
|
-
return options;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Builds a {@link ResolveOptions} safe for `exactOptionalPropertyTypes`:
|
|
73
|
-
* omits keys instead of assigning `undefined`.
|
|
74
|
-
*
|
|
75
|
-
* @since 0.3.16-canary.0
|
|
76
|
-
*/
|
|
77
|
-
export function injectionSlotToResolveOptions(
|
|
78
|
-
injectionSlot: Pick<DependencySlot, "name" | "tags">,
|
|
79
|
-
): ResolveOptions | undefined {
|
|
80
|
-
return buildOptions(injectionSlot.name, injectionSlot.tags);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Where a slot's derived options are memoized, so the same object is handed out every resolve. */
|
|
84
|
-
const MEMOIZED_RESOLVE_OPTIONS: unique symbol = Symbol("di:resolve-options");
|
|
85
|
-
|
|
86
|
-
interface SlotWithMemoizedOptions {
|
|
87
|
-
[MEMOIZED_RESOLVE_OPTIONS]?: ResolveOptions;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* The options a dependency resolves with — one object per slot, since a slot's criteria are fixed
|
|
92
|
-
* when it is declared.
|
|
93
|
-
*
|
|
94
|
-
* @remarks A slot carrying no criterion answers from its two fields, so the common shape never
|
|
95
|
-
* reaches the memo.
|
|
96
|
-
*
|
|
97
|
-
* @since 0.6.0
|
|
98
|
-
*/
|
|
99
|
-
export function resolveOptionsForSlot(injectionSlot: DependencySlot): ResolveOptions | undefined {
|
|
100
|
-
const { name, tags } = injectionSlot;
|
|
101
|
-
if (name === undefined && tags === undefined) {
|
|
102
|
-
return undefined;
|
|
103
|
-
}
|
|
104
|
-
const slot = injectionSlot as SlotWithMemoizedOptions;
|
|
105
|
-
const memoized = slot[MEMOIZED_RESOLVE_OPTIONS];
|
|
106
|
-
return memoized ?? memoizeResolveOptions(slot, name, tags);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Builds, freezes and stores a slot's options on first use.
|
|
111
|
-
*
|
|
112
|
-
* @remarks Frozen because one object answers every resolve of the slot and a constraint predicate is
|
|
113
|
-
* handed it. Split out for the `try`: in the caller it would cost the early return its inlining.
|
|
114
|
-
*/
|
|
115
|
-
function memoizeResolveOptions(
|
|
116
|
-
slot: SlotWithMemoizedOptions,
|
|
117
|
-
name: string | undefined,
|
|
118
|
-
tags: ReadonlyArray<BindingTag> | undefined,
|
|
119
|
-
): ResolveOptions {
|
|
120
|
-
const built = Object.freeze(buildOptions(name, tags) as ResolveOptions);
|
|
121
|
-
try {
|
|
122
|
-
slot[MEMOIZED_RESOLVE_OPTIONS] = built;
|
|
123
|
-
} catch {
|
|
124
|
-
// A frozen slot rebuilds on every hop rather than throwing.
|
|
125
|
-
}
|
|
126
|
-
return built;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Resolve options derived from a binding slot (tags may be empty; omits when nothing to match).
|
|
131
|
-
*
|
|
132
|
-
* @remarks Takes the slot structurally rather than as `BindingSlot`, so the slot on a public
|
|
133
|
-
* `BindingSnapshot` — where `name` is an optional property, not a required one holding `undefined` —
|
|
134
|
-
* is accepted by the same call.
|
|
135
|
-
*
|
|
136
|
-
* @since 0.3.16-canary.0
|
|
137
|
-
*/
|
|
138
|
-
export function bindingSlotToResolveOptions(bindingSlot: {
|
|
139
|
-
readonly name?: string | undefined;
|
|
140
|
-
readonly tags?: ReadonlyArray<BindingTag> | undefined;
|
|
141
|
-
}): ResolveOptions | undefined {
|
|
142
|
-
const tags = bindingSlot.tags;
|
|
143
|
-
return buildOptions(bindingSlot.name, tags !== undefined && tags.length > 0 ? tags : undefined);
|
|
144
|
-
}
|
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
import type { Binding } from "#/core/binding";
|
|
2
|
-
import { effectiveBindingScope } from "#/core/binding-scope";
|
|
3
|
-
import type { BindingRegistry } from "#/core/registry";
|
|
4
|
-
import type { Token } from "#/core/token";
|
|
5
|
-
import { tokenName } from "#/core/token";
|
|
6
|
-
import type { BindingKind, BindingScope, Constructor } from "#/core/types";
|
|
7
|
-
import type { DependencySlot } from "#/injection/resolve-options";
|
|
8
|
-
import { bindingSlotToResolveOptions } from "#/injection/resolve-options";
|
|
9
|
-
import type { MetadataReader } from "#/metadata/metadata-types";
|
|
10
|
-
import { matchesSlot } from "#/resolution/select/binding-select";
|
|
11
|
-
|
|
12
|
-
// ── Types ────────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @remarks `kind`/`scope` are `"unbound"` for the placeholder node an optional, currently
|
|
16
|
-
* unsatisfied dependency points at.
|
|
17
|
-
*
|
|
18
|
-
* @since 0.3.16-canary.0
|
|
19
|
-
*/
|
|
20
|
-
export interface GraphNode {
|
|
21
|
-
readonly id: string;
|
|
22
|
-
readonly tokenName: string;
|
|
23
|
-
/** Identifies the token itself, so bindings that share a display name stay distinguishable. */
|
|
24
|
-
readonly tokenKey: string;
|
|
25
|
-
readonly kind: BindingKind | "unbound";
|
|
26
|
-
readonly scope: BindingScope | "unbound";
|
|
27
|
-
readonly fromParent: boolean;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @remarks `label` is presentation, assembled for the adapters; read `optional` and `slotName`
|
|
32
|
-
* rather than parsing it.
|
|
33
|
-
*
|
|
34
|
-
* @since 0.3.16-canary.0
|
|
35
|
-
*/
|
|
36
|
-
export interface GraphEdge {
|
|
37
|
-
readonly from: string;
|
|
38
|
-
readonly to: string;
|
|
39
|
-
readonly label?: string;
|
|
40
|
-
readonly optional: boolean;
|
|
41
|
-
/** The named slot this edge resolves to, when the binding declares one. */
|
|
42
|
-
readonly slotName?: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* A container's dependency graph as plain JSON — nodes, edges, and whether the parent is included.
|
|
47
|
-
*
|
|
48
|
-
* @since 0.3.16-canary.0
|
|
49
|
-
*/
|
|
50
|
-
export interface ContainerGraphJson {
|
|
51
|
-
readonly nodes: ReadonlyArray<GraphNode>;
|
|
52
|
-
readonly edges: ReadonlyArray<GraphEdge>;
|
|
53
|
-
readonly includesParent: boolean;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Options controlling what a dependency graph includes.
|
|
58
|
-
*
|
|
59
|
-
* @since 0.3.16-canary.0
|
|
60
|
-
*/
|
|
61
|
-
export interface GraphOptions {
|
|
62
|
-
readonly includeParent?: boolean | undefined;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// ── Builder ──────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
66
|
-
|
|
67
|
-
// Tokens are compared by object identity, and a name is free to repeat, so the graph mints its
|
|
68
|
-
// own per-process key. Weakly held: a discarded token takes its key with it.
|
|
69
|
-
const tokenKeys = new WeakMap<object, string>();
|
|
70
|
-
let tokenKeySequence = 0;
|
|
71
|
-
|
|
72
|
-
function tokenKeyOf(dependency: Token<unknown> | Constructor): string {
|
|
73
|
-
const existing = tokenKeys.get(dependency);
|
|
74
|
-
|
|
75
|
-
if (existing !== undefined) {
|
|
76
|
-
return existing;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
tokenKeySequence += 1;
|
|
80
|
-
const key = `${tokenName(dependency)}@${String(tokenKeySequence)}`;
|
|
81
|
-
|
|
82
|
-
tokenKeys.set(dependency, key);
|
|
83
|
-
|
|
84
|
-
return key;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Mirrors filterBindings' slot semantics; predicates need a live resolution context, so the graph
|
|
88
|
-
// keeps every predicate-carrying candidate.
|
|
89
|
-
function matchingTargets(candidates: ReadonlyArray<Binding>, ref: DependencySlot): ReadonlyArray<Binding> {
|
|
90
|
-
const criterion = bindingSlotToResolveOptions(ref);
|
|
91
|
-
|
|
92
|
-
if (ref.multi && criterion === undefined) {
|
|
93
|
-
return candidates;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return candidates.filter((candidate) => matchesSlot(candidate.slot, criterion));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function edgeLabel(ref: DependencySlot, index: number): string {
|
|
100
|
-
const criterion =
|
|
101
|
-
ref.name !== undefined
|
|
102
|
-
? `name:${ref.name}`
|
|
103
|
-
: ref.tags !== undefined && ref.tags.length > 0
|
|
104
|
-
? `tag:${ref.tags[0]?.key.name}=${String(ref.tags[0]?.value)}`
|
|
105
|
-
: `[${index}]`;
|
|
106
|
-
|
|
107
|
-
return ref.optional ? `${criterion} optional` : criterion;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** The collections a graph walk fills, so each step of the walk can be a function of its own. */
|
|
111
|
-
interface GraphAccumulator {
|
|
112
|
-
readonly nodes: Array<GraphNode>;
|
|
113
|
-
readonly edges: Array<GraphEdge>;
|
|
114
|
-
// One placeholder node per optional-but-unbound token keeps the declared edge visible.
|
|
115
|
-
readonly unboundNodeIds: Map<string, string>;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/** Own bindings shadow the fallback, mirroring resolution's upward walk. */
|
|
119
|
-
type BindingLookup = (token: Token<unknown> | Constructor) => ReadonlyArray<Binding>;
|
|
120
|
-
|
|
121
|
-
function bindingLookup(sourceRegistry: BindingRegistry, fallbackRegistry: BindingRegistry | undefined): BindingLookup {
|
|
122
|
-
return (token) => {
|
|
123
|
-
const own = sourceRegistry.getAll(token);
|
|
124
|
-
|
|
125
|
-
if (own.length > 0 || fallbackRegistry === undefined) {
|
|
126
|
-
return own;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return fallbackRegistry.getAll(token);
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** The placeholder node an optional-but-unbound dependency points at, minted once per token. */
|
|
134
|
-
function unboundNodeIdFor(accumulator: GraphAccumulator, dependency: Token<unknown> | Constructor): string {
|
|
135
|
-
const key = tokenKeyOf(dependency);
|
|
136
|
-
const existing = accumulator.unboundNodeIds.get(key);
|
|
137
|
-
|
|
138
|
-
if (existing !== undefined) {
|
|
139
|
-
return existing;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const id = `unbound:${key}`;
|
|
143
|
-
|
|
144
|
-
accumulator.unboundNodeIds.set(key, id);
|
|
145
|
-
accumulator.nodes.push({
|
|
146
|
-
id,
|
|
147
|
-
tokenName: tokenName(dependency),
|
|
148
|
-
tokenKey: key,
|
|
149
|
-
kind: "unbound",
|
|
150
|
-
scope: "unbound",
|
|
151
|
-
fromParent: false,
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
return id;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function addDependencyEdges(
|
|
158
|
-
accumulator: GraphAccumulator,
|
|
159
|
-
from: string,
|
|
160
|
-
ref: DependencySlot,
|
|
161
|
-
index: number,
|
|
162
|
-
lookup: BindingLookup,
|
|
163
|
-
): void {
|
|
164
|
-
const targets = matchingTargets(lookup(ref.token), ref);
|
|
165
|
-
const label = edgeLabel(ref, index);
|
|
166
|
-
|
|
167
|
-
if (targets.length === 0) {
|
|
168
|
-
// A required-but-unbound dependency is validate()'s story, not the graph's.
|
|
169
|
-
if (!ref.optional) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
accumulator.edges.push({
|
|
174
|
-
from,
|
|
175
|
-
to: unboundNodeIdFor(accumulator, ref.token),
|
|
176
|
-
label,
|
|
177
|
-
optional: true,
|
|
178
|
-
...(ref.name !== undefined ? { slotName: ref.name } : {}),
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
for (const target of targets) {
|
|
185
|
-
// A multi dep with no criterion of its own fans out — each edge names the slot it hits.
|
|
186
|
-
const slotName = target.slot.name ?? ref.name;
|
|
187
|
-
const perTargetLabel =
|
|
188
|
-
ref.multi && ref.name === undefined && slotName !== undefined
|
|
189
|
-
? edgeLabel({ ...ref, name: slotName }, index)
|
|
190
|
-
: label;
|
|
191
|
-
|
|
192
|
-
accumulator.edges.push({
|
|
193
|
-
from,
|
|
194
|
-
to: target.id,
|
|
195
|
-
label: perTargetLabel,
|
|
196
|
-
optional: ref.optional,
|
|
197
|
-
...(slotName !== undefined ? { slotName } : {}),
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** What one binding declares up front — a class's params, a factory's descriptors, an alias's target. */
|
|
203
|
-
function addBindingEdges(
|
|
204
|
-
accumulator: GraphAccumulator,
|
|
205
|
-
binding: Binding,
|
|
206
|
-
metadataReader: MetadataReader,
|
|
207
|
-
lookup: BindingLookup,
|
|
208
|
-
): void {
|
|
209
|
-
if (binding.kind === "class") {
|
|
210
|
-
const meta = metadataReader.getConstructorMetadata(binding.target);
|
|
211
|
-
|
|
212
|
-
if (meta !== undefined) {
|
|
213
|
-
for (const [index, param] of meta.params.entries()) {
|
|
214
|
-
addDependencyEdges(accumulator, binding.id, param, index, lookup);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (binding.kind === "resolved" || binding.kind === "resolved-async") {
|
|
222
|
-
for (const [index, dependency] of binding.deps.entries()) {
|
|
223
|
-
addDependencyEdges(accumulator, binding.id, dependency, index, lookup);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (binding.kind === "alias") {
|
|
230
|
-
const aliasRef: DependencySlot = { token: binding.target, optional: false, multi: false };
|
|
231
|
-
|
|
232
|
-
for (const target of matchingTargets(lookup(binding.target), aliasRef)) {
|
|
233
|
-
accumulator.edges.push({ from: binding.id, to: target.id, label: "alias", optional: false });
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function addRegistryBindings(
|
|
239
|
-
accumulator: GraphAccumulator,
|
|
240
|
-
sourceRegistry: BindingRegistry,
|
|
241
|
-
metadataReader: MetadataReader,
|
|
242
|
-
fromParent: boolean,
|
|
243
|
-
fallbackRegistry?: BindingRegistry,
|
|
244
|
-
): void {
|
|
245
|
-
const lookup = bindingLookup(sourceRegistry, fallbackRegistry);
|
|
246
|
-
|
|
247
|
-
for (const binding of sourceRegistry.allBindings()) {
|
|
248
|
-
accumulator.nodes.push({
|
|
249
|
-
id: binding.id,
|
|
250
|
-
tokenName: tokenName(binding.token),
|
|
251
|
-
tokenKey: tokenKeyOf(binding.token),
|
|
252
|
-
kind: binding.kind,
|
|
253
|
-
scope: effectiveBindingScope(binding),
|
|
254
|
-
fromParent,
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
addBindingEdges(accumulator, binding, metadataReader, lookup);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Builds the JSON dependency graph of a registry's bindings, optionally including the parent's.
|
|
263
|
-
*
|
|
264
|
-
* @since 0.3.16-canary.0
|
|
265
|
-
*/
|
|
266
|
-
export function buildDependencyGraph(
|
|
267
|
-
registry: BindingRegistry,
|
|
268
|
-
metadataReader: MetadataReader,
|
|
269
|
-
options: GraphOptions | undefined,
|
|
270
|
-
parentRegistry?: BindingRegistry,
|
|
271
|
-
): ContainerGraphJson {
|
|
272
|
-
const accumulator: GraphAccumulator = { nodes: [], edges: [], unboundNodeIds: new Map() };
|
|
273
|
-
const includesParent = options?.includeParent === true;
|
|
274
|
-
|
|
275
|
-
addRegistryBindings(accumulator, registry, metadataReader, false, includesParent ? parentRegistry : undefined);
|
|
276
|
-
|
|
277
|
-
if (includesParent && parentRegistry !== undefined) {
|
|
278
|
-
addRegistryBindings(accumulator, parentRegistry, metadataReader, true);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return { nodes: accumulator.nodes, edges: accumulator.edges, includesParent };
|
|
282
|
-
}
|