@codefast/di 0.12.0 → 0.14.0
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 +192 -0
- package/README.md +142 -43
- package/dist/ambient-container.d.ts +46 -0
- package/dist/ambient-container.js +72 -1
- package/dist/container/binding-chain.js +3 -3
- package/dist/container/container.d.ts +41 -30
- package/dist/container/container.js +59 -243
- package/dist/container/introspect.d.ts +47 -0
- package/dist/container/introspect.js +53 -0
- package/dist/core/binding-builders.d.ts +27 -3
- package/dist/core/binding-declaration.d.ts +2 -2
- package/dist/core/binding-declaration.js +4 -4
- package/dist/core/module.d.ts +4 -25
- package/dist/core/module.js +3 -27
- package/dist/core/state-epoch.d.ts +1 -1
- package/dist/core/state-epoch.js +1 -1
- package/dist/core/types.d.ts +16 -7
- package/dist/decorators/inject.d.ts +3 -1
- package/dist/decorators/inject.js +27 -22
- package/dist/decorators/injectable.d.ts +14 -18
- package/dist/decorators/injectable.js +23 -2
- package/dist/decorators/lifecycle.d.ts +9 -3
- package/dist/decorators/lifecycle.js +5 -1
- package/dist/errors.d.ts +33 -15
- package/dist/errors.js +41 -21
- package/dist/index.d.ts +9 -8
- package/dist/index.js +4 -3
- package/dist/injection/dependency-slot.d.ts +17 -0
- package/dist/injection/dependency-slot.js +46 -0
- package/dist/injection/descriptor.d.ts +9 -3
- package/dist/injection/descriptor.js +10 -1
- package/dist/introspection/explanation.js +26 -8
- package/dist/introspection/validation.d.ts +28 -0
- package/dist/introspection/validation.js +244 -0
- package/dist/lifecycle/hooks.d.ts +13 -2
- package/dist/lifecycle/hooks.js +36 -18
- package/dist/lifecycle/scopes.d.ts +21 -2
- package/dist/lifecycle/scopes.js +86 -2
- package/dist/metadata/keys.d.ts +1 -1
- package/dist/metadata/keys.js +1 -1
- package/dist/resolution/cache/class-introspector.d.ts +7 -8
- package/dist/resolution/cache/class-introspector.js +40 -3
- package/dist/resolution/cache/lookup.d.ts +10 -0
- package/dist/resolution/cache/lookup.js +27 -0
- package/dist/resolution/context.d.ts +12 -12
- package/dist/resolution/path.d.ts +12 -2
- package/dist/resolution/path.js +22 -2
- package/dist/resolution/plan/codegen.d.ts +2 -1
- package/dist/resolution/plan/codegen.js +1 -1
- package/dist/resolution/plan/compiler.d.ts +6 -6
- package/dist/resolution/plan/compiler.js +152 -22
- package/dist/resolution/resolver.d.ts +6 -0
- package/dist/resolution/resolver.js +39 -84
- package/package.json +11 -3
|
@@ -36,6 +36,16 @@ export interface CollectionEntry {
|
|
|
36
36
|
activationVersion: number;
|
|
37
37
|
readBefore: boolean;
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* A collection's value list when every member answers with a fixed value, else `undefined`.
|
|
41
|
+
*
|
|
42
|
+
* @remarks A fixed value is a hook-free constant, or a hook-free singleton whose instance is cached — anything that
|
|
43
|
+
* changes either bumps a registry version the memo is keyed on. The list is kept unfrozen: a frozen array reads
|
|
44
|
+
* through a slow elements kind, and every read copies it anyway.
|
|
45
|
+
*
|
|
46
|
+
* @since 0.14.0
|
|
47
|
+
*/
|
|
48
|
+
export declare function stableCollectionValuesOf(candidates: ReadonlyArray<Binding>): Array<unknown> | undefined;
|
|
39
49
|
/**
|
|
40
50
|
* A version-stamped cache of binding lookups by token and criterion across the container chain.
|
|
41
51
|
*
|
|
@@ -1,5 +1,32 @@
|
|
|
1
|
+
import { NO_INSTANCE } from "#core/binding";
|
|
1
2
|
import { getOrInsertComputed } from "#core/map-upsert";
|
|
2
3
|
import { stateEpoch } from "#core/state-epoch";
|
|
4
|
+
/**
|
|
5
|
+
* A collection's value list when every member answers with a fixed value, else `undefined`.
|
|
6
|
+
*
|
|
7
|
+
* @remarks A fixed value is a hook-free constant, or a hook-free singleton whose instance is cached — anything that
|
|
8
|
+
* changes either bumps a registry version the memo is keyed on. The list is kept unfrozen: a frozen array reads
|
|
9
|
+
* through a slow elements kind, and every read copies it anyway.
|
|
10
|
+
*
|
|
11
|
+
* @since 0.14.0
|
|
12
|
+
*/
|
|
13
|
+
export function stableCollectionValuesOf(candidates) {
|
|
14
|
+
for (let index = 0; index < candidates.length; index += 1) {
|
|
15
|
+
if (!isStableCollectionMember(candidates[index])) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return candidates.map(stableMemberValue);
|
|
20
|
+
}
|
|
21
|
+
function isStableCollectionMember(binding) {
|
|
22
|
+
if (binding.kind === "alias" || binding.activationHook !== undefined) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return binding.kind === "constant" || (binding.scope === "singleton" && binding.instance !== NO_INSTANCE);
|
|
26
|
+
}
|
|
27
|
+
function stableMemberValue(binding) {
|
|
28
|
+
return binding.kind === "constant" ? binding.value : binding.instance;
|
|
29
|
+
}
|
|
3
30
|
const newTagToEntryMap = () => new Map();
|
|
4
31
|
const newNameToTagMap = () => new Map();
|
|
5
32
|
/**
|
|
@@ -27,12 +27,12 @@ export declare class DefaultResolutionContext implements ResolutionContext {
|
|
|
27
27
|
constructor(resolver: ResolverCallbacks, resolutionStack: Array<ResolutionFrame>, currentOptions: ResolveOptions | undefined);
|
|
28
28
|
get graph(): ConstraintContext;
|
|
29
29
|
reset(resolver: ResolverCallbacks, resolutionStack: Array<ResolutionFrame>, currentOptions: ResolveOptions | undefined): void;
|
|
30
|
-
resolve<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Value;
|
|
31
|
-
resolveAsync<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<Value>;
|
|
32
|
-
resolveOptional<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Value | undefined;
|
|
33
|
-
resolveOptionalAsync<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<Value | undefined>;
|
|
34
|
-
resolveAll<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): ReadonlyArray<Value>;
|
|
35
|
-
resolveAllAsync<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<ReadonlyArray<Value>>;
|
|
30
|
+
resolve<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Value;
|
|
31
|
+
resolveAsync<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Promise<Value>;
|
|
32
|
+
resolveOptional<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Value | undefined;
|
|
33
|
+
resolveOptionalAsync<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Promise<Value | undefined>;
|
|
34
|
+
resolveAll<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): ReadonlyArray<Value>;
|
|
35
|
+
resolveAllAsync<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Promise<ReadonlyArray<Value>>;
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
38
|
* One async level's resolution context, which is also its branch of the resolution stack.
|
|
@@ -55,12 +55,12 @@ export declare class AsyncLevelContext implements ResolutionContext {
|
|
|
55
55
|
get graph(): ConstraintContext;
|
|
56
56
|
/** This level's own path, fixed for its lifetime, for a caller that names it in an error. */
|
|
57
57
|
get ownPath(): ReadonlyArray<ResolutionFrame>;
|
|
58
|
-
resolve<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Value;
|
|
59
|
-
resolveAsync<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<Value>;
|
|
60
|
-
resolveOptional<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Value | undefined;
|
|
61
|
-
resolveOptionalAsync<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<Value | undefined>;
|
|
62
|
-
resolveAll<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): ReadonlyArray<Value>;
|
|
63
|
-
resolveAllAsync<Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<ReadonlyArray<Value>>;
|
|
58
|
+
resolve<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Value;
|
|
59
|
+
resolveAsync<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Promise<Value>;
|
|
60
|
+
resolveOptional<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Value | undefined;
|
|
61
|
+
resolveOptionalAsync<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Promise<Value | undefined>;
|
|
62
|
+
resolveAll<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): ReadonlyArray<Value>;
|
|
63
|
+
resolveAllAsync<Value, Names extends string = string>(token: Token<Value, Names> | Constructor<Value>, options?: NoInfer<ResolveOptions<Names>>): Promise<ReadonlyArray<Value>>;
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* The one constraint context every predicate reads, whatever lane built it.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Binding } from "#core/binding";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Token } from "#core/token";
|
|
3
|
+
import type { Constructor, ResolutionFrame } from "#core/types";
|
|
3
4
|
/**
|
|
4
5
|
* Creates the resolution-stack frame for one in-flight resolve of a binding.
|
|
5
6
|
*
|
|
@@ -17,11 +18,20 @@ export declare function buildResolutionFrame(binding: Binding, tokenName: string
|
|
|
17
18
|
*/
|
|
18
19
|
export declare function bindingsOf(frames: ReadonlyArray<ResolutionFrame>): Array<Binding>;
|
|
19
20
|
/**
|
|
20
|
-
* The token names along a stack plus the
|
|
21
|
+
* The token names along a stack plus the one ending the path, a cycle's closing frame or a miss — built only to throw.
|
|
21
22
|
*
|
|
22
23
|
* @since 0.6.0
|
|
23
24
|
*/
|
|
24
25
|
export declare function cycleNamesOf(resolutionStack: ReadonlyArray<ResolutionFrame>, closingName: string): Array<string>;
|
|
26
|
+
/**
|
|
27
|
+
* The token names from the outermost request to a lookup that selected nothing, its alias hops included.
|
|
28
|
+
*
|
|
29
|
+
* @remarks `aliasHops` is the set an alias walk keeps, which starts at `requested` — the name the path already
|
|
30
|
+
* ends on.
|
|
31
|
+
*
|
|
32
|
+
* @since 0.13.0
|
|
33
|
+
*/
|
|
34
|
+
export declare function missPathOf(resolutionStack: ReadonlyArray<ResolutionFrame>, requested: Token<unknown> | Constructor, aliasHops: ReadonlySet<Token<unknown> | Constructor> | undefined): Array<string>;
|
|
25
35
|
/**
|
|
26
36
|
* Marks a level in flight on a synchronous path and pushes its frame, throwing if its binding already is.
|
|
27
37
|
*
|
package/dist/resolution/path.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { tokenName } from "#core/token";
|
|
2
|
+
/** The resolution path: cycle bookkeeping carried on the stack itself, and the names an error prints. */
|
|
2
3
|
import { CircularDependencyError } from "#errors";
|
|
3
4
|
/** The key under which a resolver-built frame carries its binding, for marking a seeded path. */
|
|
4
5
|
const FRAME_BINDING = Symbol("di:frame-binding");
|
|
@@ -38,7 +39,7 @@ export function bindingsOf(frames) {
|
|
|
38
39
|
return bindings;
|
|
39
40
|
}
|
|
40
41
|
/**
|
|
41
|
-
* The token names along a stack plus the
|
|
42
|
+
* The token names along a stack plus the one ending the path, a cycle's closing frame or a miss — built only to throw.
|
|
42
43
|
*
|
|
43
44
|
* @since 0.6.0
|
|
44
45
|
*/
|
|
@@ -50,6 +51,25 @@ export function cycleNamesOf(resolutionStack, closingName) {
|
|
|
50
51
|
names[resolutionStack.length] = closingName;
|
|
51
52
|
return names;
|
|
52
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* The token names from the outermost request to a lookup that selected nothing, its alias hops included.
|
|
56
|
+
*
|
|
57
|
+
* @remarks `aliasHops` is the set an alias walk keeps, which starts at `requested` — the name the path already
|
|
58
|
+
* ends on.
|
|
59
|
+
*
|
|
60
|
+
* @since 0.13.0
|
|
61
|
+
*/
|
|
62
|
+
export function missPathOf(resolutionStack, requested, aliasHops) {
|
|
63
|
+
const path = cycleNamesOf(resolutionStack, tokenName(requested));
|
|
64
|
+
if (aliasHops !== undefined) {
|
|
65
|
+
for (const hop of aliasHops) {
|
|
66
|
+
if (hop !== requested) {
|
|
67
|
+
path.push(tokenName(hop));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return path;
|
|
72
|
+
}
|
|
53
73
|
/**
|
|
54
74
|
* Marks a level in flight on a synchronous path and pushes its frame, throwing if its binding already is.
|
|
55
75
|
*
|
|
@@ -26,8 +26,9 @@ export type PlanNode = {
|
|
|
26
26
|
readonly deps: ReadonlyArray<PlanNode>;
|
|
27
27
|
} | {
|
|
28
28
|
readonly kind: "accessors";
|
|
29
|
-
readonly construct: (deps: Array<unknown>) => unknown;
|
|
29
|
+
readonly construct: (deps: Array<unknown>, accessorValues: Array<unknown>) => unknown;
|
|
30
30
|
readonly deps: ReadonlyArray<PlanNode>;
|
|
31
|
+
readonly accessors: ReadonlyArray<PlanNode>;
|
|
31
32
|
} | {
|
|
32
33
|
readonly kind: "call";
|
|
33
34
|
readonly factory: (...args: Array<unknown>) => unknown;
|
|
@@ -106,7 +106,7 @@ class PlanEmitter {
|
|
|
106
106
|
case "construct":
|
|
107
107
|
return this.#define(`new ${this.#slot(node.target, "C")}(${this.#list(node.deps)})`);
|
|
108
108
|
case "accessors":
|
|
109
|
-
return this.#define(`${this.#slot(node.construct, "A")}([${this.#list(node.deps)}])`);
|
|
109
|
+
return this.#define(`${this.#slot(node.construct, "A")}([${this.#list(node.deps)}], [${this.#list(node.accessors)}])`);
|
|
110
110
|
case "call": {
|
|
111
111
|
// The settle only ever throws, so it runs on the promise branch alone and the plain result stands.
|
|
112
112
|
const local = this.#define(`${this.#slot(node.factory, "F")}(${this.#list(node.deps)})`);
|
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
* @remarks A dependency the compiler cannot inline escapes to a resolver callback instead; the
|
|
5
5
|
* closure must stay callable for it, which is what bounds what may be inlined.
|
|
6
6
|
*/
|
|
7
|
+
import type { AmbientResolution } from "#ambient-container";
|
|
7
8
|
import type { Binding } from "#core/binding";
|
|
8
9
|
import type { ConstructorInvocation } from "#core/constructor-type";
|
|
9
10
|
import type { Token } from "#core/token";
|
|
10
11
|
import type { Constructor, ResolutionFrame, ResolveOptions } from "#core/types";
|
|
12
|
+
import type { DependencySlot } from "#injection/dependency-slot";
|
|
11
13
|
import type { ConstructorMetadata } from "#metadata/types";
|
|
12
14
|
/**
|
|
13
15
|
* Compilation asked to retry later (class lifecycle metadata not discovered yet).
|
|
@@ -41,12 +43,10 @@ export interface InstantiationPlanHost {
|
|
|
41
43
|
hasActivationHandlers(binding: Binding): boolean;
|
|
42
44
|
/** Cached postConstruct presence — `undefined` until a runtime resolve discovers it. */
|
|
43
45
|
knownPostConstruct(target: Constructor): boolean | undefined;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*/
|
|
49
|
-
constructWithAccessors(binding: Binding, target: ConstructorInvocation, deps: Array<unknown>): unknown;
|
|
46
|
+
/** The slots of a class's `@inject` accessors, in the order their initializers run; empty when it has none. */
|
|
47
|
+
accessorSlots(target: Constructor): ReadonlyArray<DependencySlot>;
|
|
48
|
+
/** Constructs a class with the container ambient and `resolution` answering its accessors. */
|
|
49
|
+
constructWithAccessors(target: ConstructorInvocation, deps: Array<unknown>, resolution: AmbientResolution): unknown;
|
|
50
50
|
getConstructorMetadata(target: Constructor): ConstructorMetadata | undefined;
|
|
51
51
|
/** Options-less lookup with alias hops folded; `null` when the fast lane can't answer. */
|
|
52
52
|
lookupDependencyEntry(token: Token<unknown> | Constructor): InstantiationPlanDependencyEntry | null;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isPreparableSlot, PreparedAccessorResolution } from "#ambient-container";
|
|
1
2
|
import { NO_INSTANCE } from "#core/binding";
|
|
2
3
|
import { tokenName } from "#core/token";
|
|
3
4
|
import { AsyncResolutionError } from "#errors";
|
|
@@ -233,26 +234,22 @@ export class InstantiationPlanCompiler {
|
|
|
233
234
|
if (hasPostConstruct) {
|
|
234
235
|
return null;
|
|
235
236
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const withAccessors = this.#host.needsActiveContainer(target);
|
|
239
|
-
if (withAccessors && depth !== 0) {
|
|
237
|
+
const accessorSlots = this.#preparableAccessorSlots(target);
|
|
238
|
+
if (accessorSlots === null) {
|
|
240
239
|
return null;
|
|
241
240
|
}
|
|
242
241
|
const invokable = target;
|
|
243
242
|
const meta = this.#host.getConstructorMetadata(target);
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
return null;
|
|
248
|
-
}
|
|
249
|
-
return this.#classNode(binding, invokable, withAccessors, []);
|
|
243
|
+
// Metadata-less classes with required params throw on the runtime path — keep them there.
|
|
244
|
+
if (meta === undefined && target.length !== 0) {
|
|
245
|
+
return null;
|
|
250
246
|
}
|
|
251
|
-
const params = meta
|
|
252
|
-
if (params.length === 0) {
|
|
253
|
-
return this.#classNode(
|
|
247
|
+
const params = meta?.params ?? [];
|
|
248
|
+
if (params.length === 0 && accessorSlots.length === 0) {
|
|
249
|
+
return this.#classNode(invokable, []);
|
|
254
250
|
}
|
|
255
251
|
const deps = new Array(params.length);
|
|
252
|
+
const accessorDeps = new Array(accessorSlots.length);
|
|
256
253
|
const depAncestors = [...ancestors, binding];
|
|
257
254
|
compileStack.add(binding.identifier);
|
|
258
255
|
try {
|
|
@@ -263,21 +260,98 @@ export class InstantiationPlanCompiler {
|
|
|
263
260
|
}
|
|
264
261
|
deps[index] = dep;
|
|
265
262
|
}
|
|
263
|
+
for (let index = 0; index < accessorSlots.length; index += 1) {
|
|
264
|
+
const dep = this.#compileInjectionThunk(accessorSlots[index], compileStack, depth, depAncestors);
|
|
265
|
+
if (dep === PLAN_RETRY) {
|
|
266
|
+
return dep;
|
|
267
|
+
}
|
|
268
|
+
accessorDeps[index] = dep;
|
|
269
|
+
}
|
|
266
270
|
}
|
|
267
271
|
finally {
|
|
268
272
|
compileStack.delete(binding.identifier);
|
|
269
273
|
}
|
|
270
|
-
|
|
274
|
+
if (accessorSlots.length === 0) {
|
|
275
|
+
return this.#classNode(invokable, deps);
|
|
276
|
+
}
|
|
277
|
+
return this.#accessorClassNode(invokable, deps, accessorSlots, accessorDeps, depAncestors);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* A class whose accessors take values the plan resolves before `new`, as its params do.
|
|
281
|
+
*
|
|
282
|
+
* @remarks The fallback resolves an accessor the prepared values do not cover, seeded with the class
|
|
283
|
+
* on the path so a cycle back into it is still caught.
|
|
284
|
+
*/
|
|
285
|
+
#accessorClassNode(invokable, deps, accessorSlots, accessorDeps, ancestors) {
|
|
286
|
+
const host = this.#host;
|
|
287
|
+
const fallback = this.#seededResolution(ancestors);
|
|
288
|
+
const construct = (params, accessorValues) => host.constructWithAccessors(invokable, params, new PreparedAccessorResolution(fallback, accessorSlots, accessorValues));
|
|
289
|
+
const runs = deps.map((dep) => dep.run);
|
|
290
|
+
const accessorRuns = accessorDeps.map((dep) => dep.run);
|
|
291
|
+
const node = {
|
|
292
|
+
kind: "accessors",
|
|
293
|
+
construct,
|
|
294
|
+
deps: deps.map((dep) => dep.node),
|
|
295
|
+
accessors: accessorDeps.map((dep) => dep.node),
|
|
296
|
+
};
|
|
297
|
+
// The common shape — no constructor params, one accessor — is written out so it maps nothing.
|
|
298
|
+
if (runs.length === 0 && accessorRuns.length === 1) {
|
|
299
|
+
const accessor0 = accessorRuns[0];
|
|
300
|
+
return { run: () => construct([], [accessor0()]), node };
|
|
301
|
+
}
|
|
302
|
+
return {
|
|
303
|
+
run: () => construct(runs.map((run) => run()), accessorRuns.map((run) => run())),
|
|
304
|
+
node,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
/** A class's accessor slots, or `null` when one is a record no `@inject` initializer will ask for. */
|
|
308
|
+
#preparableAccessorSlots(target) {
|
|
309
|
+
const accessorSlots = this.#host.accessorSlots(target);
|
|
310
|
+
// A custom reader's own record would be resolved for nothing, so its class stays on the interpreted lane.
|
|
311
|
+
for (let index = 0; index < accessorSlots.length; index += 1) {
|
|
312
|
+
if (!isPreparableSlot(accessorSlots[index])) {
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return accessorSlots;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Constructs an accessor class from its settled params, resolving its accessors synchronously first.
|
|
320
|
+
*
|
|
321
|
+
* @remarks The async lane's construction, where the params may have been awaited but the accessors
|
|
322
|
+
* resolve on the sync lane, as their initializers would.
|
|
323
|
+
*/
|
|
324
|
+
#accessorConstruct(invokable, accessorSlots, accessorDeps, ancestors) {
|
|
325
|
+
const host = this.#host;
|
|
326
|
+
const fallback = this.#seededResolution(ancestors);
|
|
327
|
+
const accessorRuns = accessorDeps.map((dep) => dep.run);
|
|
328
|
+
if (accessorRuns.length === 1) {
|
|
329
|
+
const accessor0 = accessorRuns[0];
|
|
330
|
+
return (params) => host.constructWithAccessors(invokable, params, new PreparedAccessorResolution(fallback, accessorSlots, [accessor0()]));
|
|
331
|
+
}
|
|
332
|
+
return (params) => host.constructWithAccessors(invokable, params, new PreparedAccessorResolution(fallback, accessorSlots, accessorRuns.map((run) => run())));
|
|
333
|
+
}
|
|
334
|
+
/** A path-continuing resolution seeded with fixed ancestors, for a request no thunk was compiled for. */
|
|
335
|
+
#seededResolution(ancestors) {
|
|
336
|
+
const host = this.#host;
|
|
337
|
+
const frames = ancestors.map((ancestor) => host.getResolutionFrame(ancestor));
|
|
338
|
+
const escaped = (token, options, arity) => {
|
|
339
|
+
const alreadyInFlight = enterSeededPath(ancestors);
|
|
340
|
+
try {
|
|
341
|
+
return host.resolveEscaped(token, options, arity, [...frames]);
|
|
342
|
+
}
|
|
343
|
+
finally {
|
|
344
|
+
leaveSeededPath(ancestors, alreadyInFlight);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
return {
|
|
348
|
+
resolve: (token, options) => escaped(token, options, "single"),
|
|
349
|
+
resolveOptional: (token, options) => escaped(token, options, "optional"),
|
|
350
|
+
};
|
|
271
351
|
}
|
|
272
352
|
// The closure is hand-written per arity up to three so the common shapes spread nothing.
|
|
273
|
-
#classNode(
|
|
353
|
+
#classNode(invokable, deps) {
|
|
274
354
|
const nodes = deps.map((dep) => dep.node);
|
|
275
|
-
if (withAccessors) {
|
|
276
|
-
const host = this.#host;
|
|
277
|
-
const construct = (values) => host.constructWithAccessors(binding, invokable, values);
|
|
278
|
-
const runs = deps.map((dep) => dep.run);
|
|
279
|
-
return { run: () => construct(runs.map((run) => run())), node: { kind: "accessors", construct, deps: nodes } };
|
|
280
|
-
}
|
|
281
355
|
const node = { kind: "construct", target: invokable, deps: nodes };
|
|
282
356
|
switch (deps.length) {
|
|
283
357
|
case 0:
|
|
@@ -368,11 +442,18 @@ export class InstantiationPlanCompiler {
|
|
|
368
442
|
if (hasPostConstruct === undefined) {
|
|
369
443
|
return PLAN_RETRY;
|
|
370
444
|
}
|
|
371
|
-
if (hasPostConstruct
|
|
445
|
+
if (hasPostConstruct) {
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
const accessorSlots = this.#preparableAccessorSlots(target);
|
|
449
|
+
if (accessorSlots === null) {
|
|
372
450
|
return null;
|
|
373
451
|
}
|
|
374
452
|
const invokable = target;
|
|
375
453
|
const meta = this.#host.getConstructorMetadata(target);
|
|
454
|
+
if (accessorSlots.length > 0) {
|
|
455
|
+
return this.#compileAsyncAccessorClassNode(binding, invokable, meta, accessorSlots, compileStack, depth, ancestors);
|
|
456
|
+
}
|
|
376
457
|
const leaf = {
|
|
377
458
|
run: () => new invokable(),
|
|
378
459
|
promiseShape: "never",
|
|
@@ -428,6 +509,55 @@ export class InstantiationPlanCompiler {
|
|
|
428
509
|
node: { kind: "construct", target: invokable, deps: nodes, awaits: true },
|
|
429
510
|
};
|
|
430
511
|
}
|
|
512
|
+
/**
|
|
513
|
+
* An accessor class on the async lane: its params settle as any class's do, then its accessors
|
|
514
|
+
* resolve synchronously and the class constructs, the order the interpreted async lane keeps.
|
|
515
|
+
*/
|
|
516
|
+
#compileAsyncAccessorClassNode(binding, invokable, meta, accessorSlots, compileStack, depth, ancestors) {
|
|
517
|
+
// Metadata-less classes with required params throw on the runtime path — keep them there.
|
|
518
|
+
if (meta === undefined && binding.target.length !== 0) {
|
|
519
|
+
return null;
|
|
520
|
+
}
|
|
521
|
+
const params = meta?.params ?? [];
|
|
522
|
+
const depThunks = new Array(params.length);
|
|
523
|
+
const accessorDeps = new Array(accessorSlots.length);
|
|
524
|
+
const depAncestors = [...ancestors, binding];
|
|
525
|
+
compileStack.add(binding.identifier);
|
|
526
|
+
try {
|
|
527
|
+
for (let index = 0; index < params.length; index += 1) {
|
|
528
|
+
const thunk = this.#compileAsyncInjectionThunk(params[index], compileStack, depth, depAncestors);
|
|
529
|
+
if (thunk === PLAN_RETRY) {
|
|
530
|
+
return thunk;
|
|
531
|
+
}
|
|
532
|
+
depThunks[index] = thunk;
|
|
533
|
+
}
|
|
534
|
+
for (let index = 0; index < accessorSlots.length; index += 1) {
|
|
535
|
+
const dep = this.#compileInjectionThunk(accessorSlots[index], compileStack, depth, depAncestors);
|
|
536
|
+
if (dep === PLAN_RETRY) {
|
|
537
|
+
return dep;
|
|
538
|
+
}
|
|
539
|
+
accessorDeps[index] = dep;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
finally {
|
|
543
|
+
compileStack.delete(binding.identifier);
|
|
544
|
+
}
|
|
545
|
+
const construct = this.#accessorConstruct(invokable, accessorSlots, accessorDeps, depAncestors);
|
|
546
|
+
const factory = (...values) => construct(values);
|
|
547
|
+
const nodes = depThunks.map((thunk) => thunk.node);
|
|
548
|
+
if (allSynchronous(depThunks)) {
|
|
549
|
+
return {
|
|
550
|
+
run: () => construct(depThunks.map((thunk) => thunk.run())),
|
|
551
|
+
promiseShape: "never",
|
|
552
|
+
node: { kind: "call", factory, deps: nodes, awaits: false },
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
run: settleThenApply(depThunks, construct),
|
|
557
|
+
promiseShape: "always",
|
|
558
|
+
node: { kind: "call", factory, deps: nodes, awaits: true },
|
|
559
|
+
};
|
|
560
|
+
}
|
|
431
561
|
#compileAsyncResolvedNode(binding, compileStack, depth, ancestors) {
|
|
432
562
|
if (binding.activationHook !== undefined || this.#host.hasActivationHandlers(binding)) {
|
|
433
563
|
return null;
|
|
@@ -39,6 +39,12 @@ export declare class DependencyResolver implements ResolverCallbacks {
|
|
|
39
39
|
* Mirrors {@link DependencyResolver.resolveAll} candidate selection only (no instantiation).
|
|
40
40
|
*/
|
|
41
41
|
peekCandidateBindingsForValidate(token: Token<unknown> | Constructor, options: ResolveOptions | undefined): ReadonlyArray<Binding>;
|
|
42
|
+
/** Every binding the chain holds under a token, nearest container first — for `Container.validate`. */
|
|
43
|
+
bindingsForValidate(token: Token<unknown> | Constructor): ReadonlyArray<Binding>;
|
|
44
|
+
/** The resolver whose chain a binding this one can see resolves its dependencies from, as a singleton does. */
|
|
45
|
+
ownerForValidate(binding: Binding): DependencyResolver;
|
|
46
|
+
/** Deactivates a scoped instance of a binding this resolver owns, with its hooks and reader, when it owes a teardown. */
|
|
47
|
+
deactivateScoped(binding: Binding, instance: unknown): Promise<void> | undefined;
|
|
42
48
|
resolveFromContext<Value>(token: Token<Value> | Constructor<Value>, resolutionStack: Array<ResolutionFrame>): Value;
|
|
43
49
|
resolve<Value>(token: Token<Value> | Constructor<Value>, options: ResolveOptions | undefined, resolutionStack: Array<ResolutionFrame>, precomputedCriterion?: BindingTag | null): Value;
|
|
44
50
|
resolveOptional<Value>(token: Token<Value> | Constructor<Value>, options: ResolveOptions | undefined, resolutionStack: Array<ResolutionFrame>, precomputedCriterion?: BindingTag | null): Value | undefined;
|