awilixify 3.0.0 → 3.1.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/README.md +5 -2
- package/dist/index.d.ts +2 -1
- package/dist/lib/devtools/devtools.types.d.ts +63 -0
- package/dist/lib/devtools/devtools.types.js +1 -0
- package/dist/lib/devtools/helpers.d.ts +2 -0
- package/dist/lib/devtools/helpers.js +18 -0
- package/dist/lib/devtools/index.d.ts +4 -0
- package/dist/lib/devtools/index.js +3 -0
- package/dist/lib/di/contexts/container-context-base.d.ts +13 -3
- package/dist/lib/di/contexts/container-context-base.js +45 -7
- package/dist/lib/di/contexts/di-context-async.js +15 -9
- package/dist/lib/di/contexts/di-context-base.js +5 -4
- package/dist/lib/di/contexts/module-graph-ensurer.d.ts +1 -0
- package/dist/lib/di/contexts/module-graph-ensurer.js +5 -0
- package/dist/lib/di/errors.d.ts +3 -0
- package/dist/lib/di/errors.js +6 -0
- package/dist/lib/di/modules/module-factories.js +9 -1
- package/dist/lib/di/modules/runtime-module.types.d.ts +7 -0
- package/dist/lib/di/processors/handler-processor.d.ts +5 -1
- package/dist/lib/di/processors/handler-processor.js +41 -7
- package/dist/lib/di/processors/initializer-processor.d.ts +3 -2
- package/dist/lib/di/processors/initializer-processor.js +33 -29
- package/dist/lib/di/processors/interceptor-processor.d.ts +4 -1
- package/dist/lib/di/processors/interceptor-processor.js +18 -2
- package/dist/lib/di/processors/keyed-feature-registrar.js +1 -1
- package/dist/lib/di/processors/lifecycle-processor.d.ts +0 -1
- package/dist/lib/di/processors/lifecycle-processor.js +1 -4
- package/dist/lib/di/providers/provider-resolver.d.ts +7 -3
- package/dist/lib/di/providers/provider-resolver.js +56 -16
- package/dist/lib/di/providers/types/provider.types.d.ts +5 -5
- package/dist/lib/mediator/mediator.d.ts +4 -1
- package/dist/lib/mediator/mediator.js +13 -2
- package/dist/lib/react/index.d.ts +4 -0
- package/dist/lib/react/index.js +4 -0
- package/dist/lib/react/react-di-context.d.ts +2 -7
- package/dist/lib/react/react-di-context.js +5 -8
- package/dist/lib/react/react-module.types.d.ts +5 -19
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -3
- package/dist/lib/di/contexts/context-overrides.d.ts +0 -22
- package/dist/lib/di/contexts/context-overrides.js +0 -93
- package/dist/lib/di/contexts/module-overrides.d.ts +0 -70
- package/dist/lib/di/contexts/module-overrides.js +0 -3
- package/dist/lib/di/contexts/override-value.types.d.ts +0 -6
- package/dist/lib/di/contexts/override-value.types.js +0 -1
- package/dist/lib/di/contexts/overrides-processor.d.ts +0 -22
- package/dist/lib/di/contexts/overrides-processor.js +0 -93
- package/dist/lib/di/contexts/provider-overrides.types.d.ts +0 -19
- package/dist/lib/di/contexts/provider-overrides.types.js +0 -1
- package/dist/lib/di/modules/provider-overrides.types.d.ts +0 -19
- package/dist/lib/di/modules/provider-overrides.types.js +0 -1
- package/dist/lib/di/providers/basic-provider-resolver.d.ts +0 -35
- package/dist/lib/di/providers/basic-provider-resolver.js +0 -88
- package/dist/react.d.ts +0 -4
- package/dist/react.js +0 -4
|
@@ -1,25 +1,48 @@
|
|
|
1
1
|
import { resolveDecoratorState } from "../../decorators/decorator-state.js";
|
|
2
|
+
import { getControllerMethodNames } from "../../devtools/helpers.js";
|
|
2
3
|
import * as ERRORS from "../errors.js";
|
|
3
4
|
import { runInRequestScopeContext } from "../request-scope-context.js";
|
|
4
5
|
import { KeyedFeatureRegistrar } from "./keyed-feature-registrar.js";
|
|
5
6
|
export class InitializerProcessor {
|
|
7
|
+
devtoolsProcessorRef;
|
|
6
8
|
keyedFeatureRegistrar = new KeyedFeatureRegistrar({});
|
|
7
9
|
resolversByModule = new WeakMap();
|
|
10
|
+
constructor(devtoolsProcessorRef) {
|
|
11
|
+
this.devtoolsProcessorRef = devtoolsProcessorRef;
|
|
12
|
+
}
|
|
8
13
|
collectInitializers(m, scope, importedModulesWithScope, controllers) {
|
|
9
|
-
this.
|
|
14
|
+
this.resolversByModule.set(m, this.keyedFeatureRegistrar.register({
|
|
15
|
+
featureKind: "initializers",
|
|
16
|
+
module: m,
|
|
17
|
+
scope,
|
|
18
|
+
importedModulesWithScope,
|
|
19
|
+
}));
|
|
10
20
|
const initializers = [
|
|
11
21
|
...(this.resolversByModule.get(m) ??
|
|
12
22
|
new Map()).entries(),
|
|
13
23
|
];
|
|
14
24
|
if (initializers.length === 0 || controllers.length === 0)
|
|
15
25
|
return null;
|
|
26
|
+
this.devtoolsProcessorRef.current?.graphCollector.collectModuleRoutes({
|
|
27
|
+
module: m,
|
|
28
|
+
controllers,
|
|
29
|
+
initializers,
|
|
30
|
+
});
|
|
16
31
|
return async (options) => {
|
|
17
32
|
const activeInitializers = this.filterActiveInitializers(initializers, options);
|
|
18
33
|
if (activeInitializers.length === 0)
|
|
19
34
|
return;
|
|
20
35
|
for (const controller of controllers) {
|
|
21
|
-
for (const methodName of
|
|
22
|
-
const invoke = (...args) =>
|
|
36
|
+
for (const methodName of getControllerMethodNames(controller.controllerClass)) {
|
|
37
|
+
const invoke = (...args) => this.devtoolsProcessorRef.current?.tracer.traceInitializer({
|
|
38
|
+
args,
|
|
39
|
+
callback: () => runInRequestScopeContext(() => controller.resolve()[methodName](...args)),
|
|
40
|
+
controllerName: controller.controllerClass.name,
|
|
41
|
+
getStatusCode: () => getStatusCode(args),
|
|
42
|
+
methodName: String(methodName),
|
|
43
|
+
moduleName: m.name,
|
|
44
|
+
}) ??
|
|
45
|
+
runInRequestScopeContext(() => controller.resolve()[methodName](...args));
|
|
23
46
|
this.ensureNoMultiInvokableInitializersPerMethod(m.name, methodName, controller, activeInitializers);
|
|
24
47
|
for (const [, resolveInitializer] of activeInitializers) {
|
|
25
48
|
const initializer = resolveInitializer();
|
|
@@ -62,30 +85,11 @@ export class InitializerProcessor {
|
|
|
62
85
|
const excludedKeys = new Set(options.excludeInitializers);
|
|
63
86
|
return initializers.filter(([key]) => !excludedKeys.has(key));
|
|
64
87
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
getControllerMethodNames(controllerClass) {
|
|
74
|
-
const collected = new Set();
|
|
75
|
-
let proto = controllerClass.prototype;
|
|
76
|
-
while (proto && proto !== Object.prototype) {
|
|
77
|
-
Object.getOwnPropertyNames(proto)
|
|
78
|
-
.filter((name) => name !== "constructor" && typeof proto[name] === "function")
|
|
79
|
-
.forEach((name) => {
|
|
80
|
-
collected.add(name);
|
|
81
|
-
});
|
|
82
|
-
Object.getOwnPropertySymbols(proto)
|
|
83
|
-
.filter((symbol) => typeof proto[symbol] === "function")
|
|
84
|
-
.forEach((symbol) => {
|
|
85
|
-
collected.add(symbol);
|
|
86
|
-
});
|
|
87
|
-
proto = Object.getPrototypeOf(proto);
|
|
88
|
-
}
|
|
89
|
-
return [...collected];
|
|
90
|
-
}
|
|
88
|
+
}
|
|
89
|
+
function getStatusCode(args) {
|
|
90
|
+
const reply = args[1];
|
|
91
|
+
if (!reply || typeof reply !== "object")
|
|
92
|
+
return undefined;
|
|
93
|
+
const statusCode = reply.statusCode;
|
|
94
|
+
return typeof statusCode === "number" ? statusCode : undefined;
|
|
91
95
|
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import * as Awilix from "awilix";
|
|
2
|
+
import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
|
|
2
3
|
import type { RegisteredModuleScope } from "../contexts/container-context-base.js";
|
|
3
4
|
import type { DiContextOptions } from "../contexts/di-context-base.js";
|
|
4
5
|
import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
|
|
5
6
|
export declare class InterceptorProcessor {
|
|
7
|
+
private readonly devtoolsProcessorRef;
|
|
6
8
|
private readonly keyedFeatureRegistrar;
|
|
7
9
|
private readonly resolversByModule;
|
|
8
|
-
constructor(providerOptions: DiContextOptions["providerOptions"]);
|
|
10
|
+
constructor(providerOptions: DiContextOptions["providerOptions"], devtoolsProcessorRef: DevtoolsProcessorRef);
|
|
9
11
|
processInterceptors(m: M, scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[]): void;
|
|
10
12
|
createInterceptedProviderResolver({ module, useClass, options, resolver, }: {
|
|
11
13
|
module: M;
|
|
@@ -15,4 +17,5 @@ export declare class InterceptorProcessor {
|
|
|
15
17
|
}): Awilix.Resolver<any>;
|
|
16
18
|
private createInterceptedProviderInstance;
|
|
17
19
|
private callWithInterceptorChain;
|
|
20
|
+
private get devtoolsTracer();
|
|
18
21
|
}
|
|
@@ -2,9 +2,11 @@ import * as Awilix from "awilix";
|
|
|
2
2
|
import { hasDecoratorMethodMetadata, resolveDecoratorState, } from "../../decorators/decorator-state.js";
|
|
3
3
|
import { KeyedFeatureRegistrar } from "./keyed-feature-registrar.js";
|
|
4
4
|
export class InterceptorProcessor {
|
|
5
|
+
devtoolsProcessorRef;
|
|
5
6
|
keyedFeatureRegistrar;
|
|
6
7
|
resolversByModule = new WeakMap();
|
|
7
|
-
constructor(providerOptions) {
|
|
8
|
+
constructor(providerOptions, devtoolsProcessorRef) {
|
|
9
|
+
this.devtoolsProcessorRef = devtoolsProcessorRef;
|
|
8
10
|
this.keyedFeatureRegistrar = new KeyedFeatureRegistrar(providerOptions ?? {});
|
|
9
11
|
}
|
|
10
12
|
processInterceptors(m, scope, importedModulesWithScope) {
|
|
@@ -86,7 +88,8 @@ export class InterceptorProcessor {
|
|
|
86
88
|
if (metadata === undefined) {
|
|
87
89
|
return invoke(index + 1, next);
|
|
88
90
|
}
|
|
89
|
-
|
|
91
|
+
const interceptorName = current.constructor.name;
|
|
92
|
+
const callInterceptor = () => current.intercept({
|
|
90
93
|
target,
|
|
91
94
|
methodName,
|
|
92
95
|
args,
|
|
@@ -95,7 +98,20 @@ export class InterceptorProcessor {
|
|
|
95
98
|
decoratorState: metadata.state,
|
|
96
99
|
proceed: () => invoke(index + 1, next),
|
|
97
100
|
});
|
|
101
|
+
if (!this.devtoolsTracer)
|
|
102
|
+
return callInterceptor();
|
|
103
|
+
return this.devtoolsTracer.recordSpan({
|
|
104
|
+
kind: "interceptor",
|
|
105
|
+
moduleName,
|
|
106
|
+
providerKey: interceptorName,
|
|
107
|
+
methodName: String(methodName),
|
|
108
|
+
args,
|
|
109
|
+
callback: callInterceptor,
|
|
110
|
+
});
|
|
98
111
|
};
|
|
99
112
|
return invoke(0, proceed);
|
|
100
113
|
}
|
|
114
|
+
get devtoolsTracer() {
|
|
115
|
+
return this.devtoolsProcessorRef.current?.tracer;
|
|
116
|
+
}
|
|
101
117
|
}
|
|
@@ -30,7 +30,7 @@ export class KeyedFeatureRegistrar {
|
|
|
30
30
|
const resolverMap = new Map();
|
|
31
31
|
const ownerByInitializerToken = new Map();
|
|
32
32
|
const config = this.featureConfig[featureKind];
|
|
33
|
-
const providerResolver = new ProviderResolver(undefined, this.getProviderOptions(featureKind), getOrCreateRequestScope);
|
|
33
|
+
const providerResolver = new ProviderResolver(undefined, this.getProviderOptions(featureKind), {}, getOrCreateRequestScope);
|
|
34
34
|
for (const { module: importedModule, moduleScope: importedModuleScope, } of importedModulesWithScope) {
|
|
35
35
|
for (const key of importedModule[config.exportKey] ?? []) {
|
|
36
36
|
const feature = importedModule[featureKind]?.[key];
|
|
@@ -113,7 +113,7 @@ export class LifecycleProcessor {
|
|
|
113
113
|
sortEagerProviderRefs() {
|
|
114
114
|
const nodes = this.eagerProviderRefs.map((ref) => ({
|
|
115
115
|
...ref,
|
|
116
|
-
id:
|
|
116
|
+
id: `${ref.module.name}:${ref.key}`,
|
|
117
117
|
initAfter: this.getProviderInitAfter(ref.module, ref.key),
|
|
118
118
|
}));
|
|
119
119
|
const nodeById = new Map(nodes.map((node) => [node.id, node]));
|
|
@@ -172,7 +172,4 @@ export class LifecycleProcessor {
|
|
|
172
172
|
}
|
|
173
173
|
return nodeByKey;
|
|
174
174
|
}
|
|
175
|
-
static createEagerProviderId(moduleName, key) {
|
|
176
|
-
return `${moduleName}:${key}`;
|
|
177
|
-
}
|
|
178
175
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import * as Awilix from "awilix";
|
|
2
|
+
import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
|
|
2
3
|
import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
|
|
3
4
|
import type { InterceptorProcessor } from "../processors/interceptor-processor.js";
|
|
4
5
|
import type { AnyProvider } from "./provider.types.js";
|
|
5
6
|
export type RequestScopeResolver = (scope: Awilix.AwilixContainer) => Awilix.AwilixContainer;
|
|
6
7
|
type ResolveProviderParams = {
|
|
7
8
|
key: string;
|
|
9
|
+
moduleId?: string;
|
|
8
10
|
provider: AnyProvider;
|
|
9
11
|
resolutionScope: Awilix.AwilixContainer;
|
|
10
12
|
module: M;
|
|
@@ -19,13 +21,15 @@ type ResolveClassProviderParams = {
|
|
|
19
21
|
export declare class ProviderResolver {
|
|
20
22
|
private readonly interceptorProcessor;
|
|
21
23
|
private readonly providerOptions;
|
|
24
|
+
private readonly devtoolsProcessorRef;
|
|
22
25
|
private readonly requestScopeResolver?;
|
|
23
|
-
constructor(interceptorProcessor: InterceptorProcessor | undefined, providerOptions: Partial<Awilix.BuildResolverOptions<any>>, requestScopeResolver?: RequestScopeResolver | undefined);
|
|
26
|
+
constructor(interceptorProcessor: InterceptorProcessor | undefined, providerOptions: Partial<Awilix.BuildResolverOptions<any>>, devtoolsProcessorRef: DevtoolsProcessorRef, requestScopeResolver?: RequestScopeResolver | undefined);
|
|
24
27
|
static mergeResolverOptions(module: M, providerOptions: Partial<Awilix.BuildResolverOptions<any>>, options?: Partial<Awilix.BuildResolverOptions<any>>): Awilix.BuildResolverOptions<any>;
|
|
25
28
|
resolveClassProvider({ provider, resolutionScope, module, wrapForExport, }: ResolveClassProviderParams): Awilix.Resolver<any>;
|
|
26
|
-
resolveProvider({ key, provider, resolutionScope, module, }: ResolveProviderParams): Awilix.Resolver<any>;
|
|
27
|
-
resolveProviderAsync({ key, provider, resolutionScope, module, }: ResolveProviderParams): Promise<Awilix.Resolver<any>>;
|
|
29
|
+
resolveProvider({ key, moduleId, provider, resolutionScope, module, }: ResolveProviderParams): Awilix.Resolver<any>;
|
|
30
|
+
resolveProviderAsync({ key, moduleId, provider, resolutionScope, module, }: ResolveProviderParams): Promise<Awilix.Resolver<any>>;
|
|
28
31
|
private extractResolverOptions;
|
|
29
32
|
private createProxyResolver;
|
|
33
|
+
private get devtoolsTracer();
|
|
30
34
|
}
|
|
31
35
|
export {};
|
|
@@ -4,10 +4,12 @@ import * as GUARGS from "../type-guards.js";
|
|
|
4
4
|
export class ProviderResolver {
|
|
5
5
|
interceptorProcessor;
|
|
6
6
|
providerOptions;
|
|
7
|
+
devtoolsProcessorRef;
|
|
7
8
|
requestScopeResolver;
|
|
8
|
-
constructor(interceptorProcessor, providerOptions, requestScopeResolver) {
|
|
9
|
+
constructor(interceptorProcessor, providerOptions, devtoolsProcessorRef, requestScopeResolver) {
|
|
9
10
|
this.interceptorProcessor = interceptorProcessor;
|
|
10
11
|
this.providerOptions = providerOptions;
|
|
12
|
+
this.devtoolsProcessorRef = devtoolsProcessorRef;
|
|
11
13
|
this.requestScopeResolver = requestScopeResolver;
|
|
12
14
|
}
|
|
13
15
|
static mergeResolverOptions(module, providerOptions, options) {
|
|
@@ -35,7 +37,7 @@ export class ProviderResolver {
|
|
|
35
37
|
? this.requestScopeResolver(resolutionScope)
|
|
36
38
|
: resolutionScope), resolverOptions);
|
|
37
39
|
}
|
|
38
|
-
resolveProvider({ key, provider, resolutionScope, module, }) {
|
|
40
|
+
resolveProvider({ key, moduleId, provider, resolutionScope, module, }) {
|
|
39
41
|
if (GUARGS.isPrimitive(provider)) {
|
|
40
42
|
return Awilix.asValue(provider);
|
|
41
43
|
}
|
|
@@ -51,13 +53,22 @@ export class ProviderResolver {
|
|
|
51
53
|
}
|
|
52
54
|
const resolverOptions = this.extractResolverOptions(module, provider);
|
|
53
55
|
if (GUARGS.isCostructorProvider(provider)) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
const resolver = !this.interceptorProcessor
|
|
57
|
+
? Awilix.asClass(provider, resolverOptions)
|
|
58
|
+
: this.interceptorProcessor.createInterceptedProviderResolver({
|
|
59
|
+
module,
|
|
60
|
+
useClass: provider,
|
|
61
|
+
options: resolverOptions,
|
|
62
|
+
});
|
|
63
|
+
if (!this.devtoolsTracer)
|
|
64
|
+
return resolver;
|
|
65
|
+
return this.devtoolsTracer.wrapResolver({
|
|
66
|
+
kind: "provider",
|
|
58
67
|
module,
|
|
59
|
-
|
|
68
|
+
moduleId,
|
|
60
69
|
options: resolverOptions,
|
|
70
|
+
providerKey: key,
|
|
71
|
+
resolver,
|
|
61
72
|
});
|
|
62
73
|
}
|
|
63
74
|
if (GUARGS.isFactoryProvider(provider)) {
|
|
@@ -70,26 +81,52 @@ export class ProviderResolver {
|
|
|
70
81
|
throw new ERRORS.AsyncFactoryRequiresAsyncCreateError(module.name, key ?? "unknown");
|
|
71
82
|
}
|
|
72
83
|
const factoryDeps = (provider.inject || []).map((k) => resolutionScope.registrations[k].resolve(resolutionScope));
|
|
73
|
-
|
|
84
|
+
const resolver = Awilix.asFunction(() => provider.useFactory(...factoryDeps), resolverOptions);
|
|
85
|
+
if (!this.devtoolsTracer)
|
|
86
|
+
return resolver;
|
|
87
|
+
return this.devtoolsTracer.wrapResolver({
|
|
88
|
+
kind: "provider",
|
|
89
|
+
module,
|
|
90
|
+
moduleId,
|
|
91
|
+
options: resolverOptions,
|
|
92
|
+
providerKey: key,
|
|
93
|
+
isFactory: true,
|
|
94
|
+
resolver,
|
|
95
|
+
});
|
|
74
96
|
}
|
|
75
97
|
const baseResolver = Awilix.asClass(provider.useClass, resolverOptions);
|
|
76
98
|
const classResolver = provider.allowCircular
|
|
77
99
|
? this.createProxyResolver(baseResolver, resolverOptions)
|
|
78
100
|
: baseResolver;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
101
|
+
const resolver = !this.interceptorProcessor
|
|
102
|
+
? classResolver
|
|
103
|
+
: this.interceptorProcessor.createInterceptedProviderResolver({
|
|
104
|
+
module,
|
|
105
|
+
useClass: provider.useClass,
|
|
106
|
+
options: resolverOptions,
|
|
107
|
+
resolver: classResolver,
|
|
108
|
+
});
|
|
109
|
+
if (!this.devtoolsTracer)
|
|
110
|
+
return resolver;
|
|
111
|
+
return this.devtoolsTracer.wrapResolver({
|
|
112
|
+
kind: "provider",
|
|
83
113
|
module,
|
|
84
|
-
|
|
114
|
+
moduleId,
|
|
85
115
|
options: resolverOptions,
|
|
86
|
-
|
|
116
|
+
providerKey: key,
|
|
117
|
+
resolver,
|
|
87
118
|
});
|
|
88
119
|
}
|
|
89
|
-
async resolveProviderAsync({ key, provider, resolutionScope, module, }) {
|
|
120
|
+
async resolveProviderAsync({ key, moduleId, provider, resolutionScope, module, }) {
|
|
90
121
|
if (!GUARGS.isFactoryProvider(provider) ||
|
|
91
122
|
!GUARGS.isAsyncFactoryProvider(provider)) {
|
|
92
|
-
return this.resolveProvider({
|
|
123
|
+
return this.resolveProvider({
|
|
124
|
+
provider,
|
|
125
|
+
resolutionScope,
|
|
126
|
+
module,
|
|
127
|
+
key,
|
|
128
|
+
moduleId,
|
|
129
|
+
});
|
|
93
130
|
}
|
|
94
131
|
const resolverOptions = this.extractResolverOptions(module, provider);
|
|
95
132
|
if (resolverOptions.lifetime !== Awilix.Lifetime.SINGLETON) {
|
|
@@ -125,4 +162,7 @@ export class ProviderResolver {
|
|
|
125
162
|
},
|
|
126
163
|
});
|
|
127
164
|
}
|
|
165
|
+
get devtoolsTracer() {
|
|
166
|
+
return this.devtoolsProcessorRef.current?.tracer;
|
|
167
|
+
}
|
|
128
168
|
}
|
|
@@ -3,22 +3,22 @@ export type DefProviderMap = Record<string, object | string | boolean | number>;
|
|
|
3
3
|
export type ConstructorProvider<T extends object = object> = Constructor<T>;
|
|
4
4
|
export type PrimitiveProvider = string | number | boolean | symbol | bigint;
|
|
5
5
|
export type FunctionProvider = (...args: any[]) => any;
|
|
6
|
-
export type FactoryProvider<T extends object, DepsMap extends Record<string, unknown>, Keys extends readonly Extract<keyof DepsMap, string>[], Strict extends boolean = true
|
|
6
|
+
export type FactoryProvider<T extends object, DepsMap extends Record<string, unknown>, Keys extends readonly Extract<keyof DepsMap, string>[], Strict extends boolean = true, ResolverOptions extends object = BuildResolverOptions<T>> = {
|
|
7
7
|
inject?: Keys;
|
|
8
8
|
initAfter?: readonly Extract<keyof DepsMap, string>[];
|
|
9
9
|
eager?: boolean;
|
|
10
10
|
useFactory: Strict extends true ? (...args: MapKeysToValues<DepsMap, Keys>) => T | Promise<T> : (...args: any[]) => T | Promise<T>;
|
|
11
|
-
} &
|
|
12
|
-
export type ClassProvider<T extends object, DepsMap extends Record<string, unknown> = Record<string, unknown>> = {
|
|
11
|
+
} & ResolverOptions;
|
|
12
|
+
export type ClassProvider<T extends object, DepsMap extends Record<string, unknown> = Record<string, unknown>, ResolverOptions extends object = BuildResolverOptions<T>> = {
|
|
13
13
|
useClass: Constructor<T>;
|
|
14
14
|
allowCircular?: boolean;
|
|
15
15
|
eager?: boolean;
|
|
16
16
|
initAfter?: readonly Extract<keyof DepsMap, string>[];
|
|
17
|
-
} &
|
|
17
|
+
} & ResolverOptions;
|
|
18
18
|
export interface ProviderInit {
|
|
19
19
|
init(): void | Promise<void>;
|
|
20
20
|
}
|
|
21
|
-
export type Provider<T extends object, DepsMap extends Record<string, unknown> = Record<string, unknown>> = FactoryProvider<T, DepsMap, readonly Extract<keyof DepsMap, string>[], false> | ClassProvider<T, DepsMap> | ConstructorProvider<T>;
|
|
21
|
+
export type Provider<T extends object, DepsMap extends Record<string, unknown> = Record<string, unknown>, ResolverOptions extends object = BuildResolverOptions<T>> = FactoryProvider<T, DepsMap, readonly Extract<keyof DepsMap, string>[], false> | ClassProvider<T, DepsMap, ResolverOptions> | ConstructorProvider<T>;
|
|
22
22
|
export type AnyProvider = FactoryProvider<any, any, readonly string[], false> | ClassProvider<any> | ConstructorProvider<any> | PrimitiveProvider | FunctionProvider | object;
|
|
23
23
|
type MapKeysToValues<DepsMap extends Record<string, unknown>, Keys extends readonly Extract<keyof DepsMap, string>[]> = {
|
|
24
24
|
[K in keyof Keys]: Keys[K] extends keyof DepsMap ? DepsMap[Keys[K]] : never;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DevtoolsProcessorRef } from "../devtools/devtools.types.js";
|
|
1
2
|
import type { AnyContract } from "./contract.types.js";
|
|
2
3
|
import type { Executor, ExtractPayload } from "./handler.types.js";
|
|
3
4
|
import type { ExecuteArgs, ExecuteResponse, ExecuteResponseByScenario, ExecuteRuntimeOptions, ExtractScenarioName } from "./mediator.types.js";
|
|
@@ -5,8 +6,10 @@ import type { MiddlewareResolverMap } from "./middleware.types.js";
|
|
|
5
6
|
export declare class Mediator<C extends AnyContract> {
|
|
6
7
|
private handlers;
|
|
7
8
|
private middlewareResolvers;
|
|
9
|
+
private handlerType;
|
|
8
10
|
private moduleName;
|
|
9
|
-
|
|
11
|
+
private devtoolsProcessorRef;
|
|
12
|
+
constructor(middlewareResolvers: MiddlewareResolverMap, moduleName: string, handlerType: string | undefined, devtoolsProcessorRef: DevtoolsProcessorRef);
|
|
10
13
|
register(key: string, executor: Executor): void;
|
|
11
14
|
execute<K extends C["key"], Name extends ExtractScenarioName<C, K>>(key: K, payload: ExtractPayload<C, K>, options: ExecuteRuntimeOptions<C, K> & {
|
|
12
15
|
scenario: Name;
|
|
@@ -4,10 +4,14 @@ import { Result } from "./result.js";
|
|
|
4
4
|
export class Mediator {
|
|
5
5
|
handlers = new Map();
|
|
6
6
|
middlewareResolvers;
|
|
7
|
+
handlerType;
|
|
7
8
|
moduleName;
|
|
8
|
-
|
|
9
|
+
devtoolsProcessorRef;
|
|
10
|
+
constructor(middlewareResolvers, moduleName, handlerType = "handler", devtoolsProcessorRef) {
|
|
11
|
+
this.handlerType = handlerType;
|
|
9
12
|
this.moduleName = moduleName;
|
|
10
13
|
this.middlewareResolvers = middlewareResolvers;
|
|
14
|
+
this.devtoolsProcessorRef = devtoolsProcessorRef;
|
|
11
15
|
}
|
|
12
16
|
register(key, executor) {
|
|
13
17
|
const keyStr = String(key);
|
|
@@ -45,7 +49,14 @@ export class Mediator {
|
|
|
45
49
|
let context = {};
|
|
46
50
|
let hasResultMiddleware = false;
|
|
47
51
|
for (const [middlewareKey, middleware] of middlewares) {
|
|
48
|
-
const result = await
|
|
52
|
+
const result = await (this.devtoolsProcessorRef.current?.tracer.recordSpan({
|
|
53
|
+
kind: "prehandler",
|
|
54
|
+
moduleName: this.moduleName,
|
|
55
|
+
providerKey: `${this.handlerType}:prehandler:${middlewareKey}`,
|
|
56
|
+
methodName: "execute",
|
|
57
|
+
args: [payload, context, executionContext],
|
|
58
|
+
callback: () => middleware.execute(payload, context, executionContext),
|
|
59
|
+
}) ?? middleware.execute(payload, context, executionContext));
|
|
49
60
|
if (this.isResult(result)) {
|
|
50
61
|
hasResultMiddleware = true;
|
|
51
62
|
}
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import * as Awilix from "awilix";
|
|
2
2
|
import { ContainerContextBase, type ContainerContextOptions, type ModuleScope, type RegisteredModuleScope } from "../di/contexts/container-context-base.js";
|
|
3
3
|
import type { InternalModuleLike as M } from "../di/modules/runtime-module.types.js";
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
type NonScopedResolverOptions = Omit<Partial<Awilix.BuildResolverOptions<any>>, "lifetime"> & {
|
|
8
|
-
lifetime?: Exclude<Awilix.LifetimeType, "SCOPED">;
|
|
9
|
-
};
|
|
4
|
+
import type { NonScopedResolverOptions } from "./react-module.types.js";
|
|
5
|
+
type ReactContainerContextOptions = ContainerContextOptions<NonScopedResolverOptions<any>>;
|
|
10
6
|
export declare class ReactDIContext extends ContainerContextBase {
|
|
11
7
|
private readonly componentProcessor;
|
|
12
8
|
private constructor();
|
|
@@ -15,7 +11,6 @@ export declare class ReactDIContext extends ContainerContextBase {
|
|
|
15
11
|
protected ensureAdditionalNameConflicts(m: M, resolvedImports: M[]): void;
|
|
16
12
|
protected beforeRegisterProviders(m: M): void;
|
|
17
13
|
protected afterRegisterProviders(m: M, scope: Awilix.AwilixContainer): void;
|
|
18
|
-
private ensureProviderOptionsDoNotUseScopedLifetime;
|
|
19
14
|
private hasScopedProviderLifetime;
|
|
20
15
|
}
|
|
21
16
|
export {};
|
|
@@ -7,8 +7,7 @@ export class ReactDIContext extends ContainerContextBase {
|
|
|
7
7
|
componentProcessor = new ReactComponentProcessor();
|
|
8
8
|
constructor(options) {
|
|
9
9
|
super(options);
|
|
10
|
-
this.
|
|
11
|
-
this.providerResolver = new ProviderResolver(undefined, this.options.providerOptions || {});
|
|
10
|
+
this.providerResolver = new ProviderResolver(undefined, this.options.providerOptions || {}, {});
|
|
12
11
|
}
|
|
13
12
|
static create(module, options) {
|
|
14
13
|
return new ReactDIContext(options ?? {}).bootstrapModule(module);
|
|
@@ -30,7 +29,10 @@ export class ReactDIContext extends ContainerContextBase {
|
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
beforeRegisterProviders(m) {
|
|
33
|
-
|
|
32
|
+
if (m.providerOptions?.lifetime === Awilix.Lifetime.SCOPED ||
|
|
33
|
+
this.options.providerOptions?.lifetime === Awilix.Lifetime.SCOPED) {
|
|
34
|
+
throw new REACT_ERRORS.ScopedProviderLifetimeError(m.name, "providerOptions");
|
|
35
|
+
}
|
|
34
36
|
for (const [key, provider] of Object.entries(m.providers || {})) {
|
|
35
37
|
if (this.hasScopedProviderLifetime(provider)) {
|
|
36
38
|
throw new REACT_ERRORS.ScopedProviderLifetimeError(m.name, key);
|
|
@@ -40,11 +42,6 @@ export class ReactDIContext extends ContainerContextBase {
|
|
|
40
42
|
afterRegisterProviders(m, scope) {
|
|
41
43
|
this.componentProcessor.processComponents(m, scope);
|
|
42
44
|
}
|
|
43
|
-
ensureProviderOptionsDoNotUseScopedLifetime(moduleName, providerOptions) {
|
|
44
|
-
if (providerOptions?.lifetime !== Awilix.Lifetime.SCOPED)
|
|
45
|
-
return;
|
|
46
|
-
throw new REACT_ERRORS.ScopedProviderLifetimeError(moduleName, "providerOptions");
|
|
47
|
-
}
|
|
48
45
|
hasScopedProviderLifetime(provider) {
|
|
49
46
|
return (typeof provider === "object" &&
|
|
50
47
|
provider !== null &&
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { BuildResolverOptions,
|
|
1
|
+
import type { BuildResolverOptions, ContainerOptions, LifetimeType } from "awilix";
|
|
2
2
|
import type { EmptyObject } from "../di/common.types.js";
|
|
3
3
|
import type { ModuleImport } from "../di/modules/module.types.js";
|
|
4
4
|
import type { GlobalDependencies, NormalizeGlobalDependencies } from "../di/modules/module-def.types.js";
|
|
5
|
-
import type { DefProviderMap } from "../di/providers/provider.types.js";
|
|
5
|
+
import type { DefProviderMap, Provider } from "../di/providers/provider.types.js";
|
|
6
6
|
export type ReactComponent<TProps = any, TResult = any> = (props: TProps) => TResult;
|
|
7
7
|
declare const reactImportModuleDefMarker: unique symbol;
|
|
8
8
|
export type ReactImportModule<TDef extends ModuleDefinition = ModuleDefinition> = {
|
|
@@ -136,27 +136,13 @@ type WithProviders<TProviders extends DefProviderMap | EmptyObject, TDeps extend
|
|
|
136
136
|
providers: ToReactProviderMap<TProviders, TDeps>;
|
|
137
137
|
};
|
|
138
138
|
type ToReactProviderMap<T extends DefProviderMap, DepsMap extends object = Record<string, unknown>> = [keyof T] extends [never] ? EmptyObject : {
|
|
139
|
-
[K in keyof T]: T[K] extends object ? (
|
|
139
|
+
[K in keyof T]: T[K] extends object ? (Provider<T[K], KnownStringKeyMap<DepsMap>, NonScopedResolverOptions<T>> & {
|
|
140
140
|
initAfter?: readonly Extract<keyof DepsMap, string>[];
|
|
141
141
|
}) | T[K] : T[K];
|
|
142
142
|
};
|
|
143
|
-
type
|
|
144
|
-
|
|
145
|
-
lifetime?: NonScopedLifetime;
|
|
143
|
+
export type NonScopedResolverOptions<T> = Omit<BuildResolverOptions<T>, "lifetime"> & {
|
|
144
|
+
lifetime?: Exclude<LifetimeType, "SCOPED">;
|
|
146
145
|
};
|
|
147
|
-
type ReactFactoryProvider<T extends object, DepsMap extends Record<string, unknown>> = {
|
|
148
|
-
inject?: readonly Extract<keyof DepsMap, string>[];
|
|
149
|
-
initAfter?: readonly Extract<keyof DepsMap, string>[];
|
|
150
|
-
eager?: boolean;
|
|
151
|
-
useFactory: (...args: any[]) => T | Promise<T>;
|
|
152
|
-
} & NonScopedResolverOptions<T>;
|
|
153
|
-
type ReactClassProvider<T extends object, DepsMap extends Record<string, unknown> = Record<string, unknown>> = {
|
|
154
|
-
useClass: Constructor<T>;
|
|
155
|
-
allowCircular?: boolean;
|
|
156
|
-
eager?: boolean;
|
|
157
|
-
initAfter?: readonly Extract<keyof DepsMap, string>[];
|
|
158
|
-
} & NonScopedResolverOptions<T>;
|
|
159
|
-
type ReactProvider<T extends object, DepsMap extends Record<string, unknown> = Record<string, unknown>> = ReactFactoryProvider<T, DepsMap> | ReactClassProvider<T, DepsMap> | Constructor<T>;
|
|
160
146
|
type KnownStringKeyMap<T extends object> = {
|
|
161
147
|
[K in Extract<keyof T, string>]: T[K];
|
|
162
148
|
};
|