@tsed/di 7.0.0-beta.12 → 7.0.0-beta.13
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/jest.config.js +14 -0
- package/lib/cjs/domain/Container.js +1 -2
- package/lib/cjs/domain/Container.js.map +1 -1
- package/lib/cjs/domain/ContextLogger.js +55 -30
- package/lib/cjs/domain/ContextLogger.js.map +1 -1
- package/lib/cjs/domain/DIContext.js +55 -26
- package/lib/cjs/domain/DIContext.js.map +1 -1
- package/lib/cjs/domain/Provider.js +11 -1
- package/lib/cjs/domain/Provider.js.map +1 -1
- package/lib/cjs/errors/InjectionError.js +3 -2
- package/lib/cjs/errors/InjectionError.js.map +1 -1
- package/lib/cjs/errors/UndefinedTokenError.js +1 -1
- package/lib/cjs/errors/UndefinedTokenError.js.map +1 -1
- package/lib/cjs/index.js +4 -0
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/AlterRunInContext.js +3 -0
- package/lib/cjs/interfaces/AlterRunInContext.js.map +1 -0
- package/lib/cjs/interfaces/DIConfigurationOptions.js.map +1 -1
- package/lib/cjs/interfaces/DILoggerOptions.js +3 -0
- package/lib/cjs/interfaces/DILoggerOptions.js.map +1 -0
- package/lib/cjs/interfaces/InterceptorMethods.js.map +1 -1
- package/lib/cjs/interfaces/TokenProvider.js.map +1 -1
- package/lib/cjs/registries/GlobalProviders.js +6 -12
- package/lib/cjs/registries/GlobalProviders.js.map +1 -1
- package/lib/cjs/services/DIConfiguration.js +51 -45
- package/lib/cjs/services/DIConfiguration.js.map +1 -1
- package/lib/cjs/services/DITest.js +4 -5
- package/lib/cjs/services/DITest.js.map +1 -1
- package/lib/cjs/services/InjectorService.js +56 -73
- package/lib/cjs/services/InjectorService.js.map +1 -1
- package/lib/cjs/utils/getConfiguration.js +3 -9
- package/lib/cjs/utils/getConfiguration.js.map +1 -1
- package/lib/cjs/utils/resolveControllers.js +1 -1
- package/lib/cjs/utils/resolveControllers.js.map +1 -1
- package/lib/cjs/utils/runInContext.js +9 -6
- package/lib/cjs/utils/runInContext.js.map +1 -1
- package/lib/cjs/utils/setLoggerConfiguration.js +11 -0
- package/lib/cjs/utils/setLoggerConfiguration.js.map +1 -0
- package/lib/cjs/utils/setLoggerFormat.js +33 -0
- package/lib/cjs/utils/setLoggerFormat.js.map +1 -0
- package/lib/cjs/utils/setLoggerLevel.js +1 -2
- package/lib/cjs/utils/setLoggerLevel.js.map +1 -1
- package/lib/esm/domain/Container.js +1 -2
- package/lib/esm/domain/Container.js.map +1 -1
- package/lib/esm/domain/ContextLogger.js +55 -30
- package/lib/esm/domain/ContextLogger.js.map +1 -1
- package/lib/esm/domain/DIContext.js +55 -26
- package/lib/esm/domain/DIContext.js.map +1 -1
- package/lib/esm/domain/Provider.js +11 -1
- package/lib/esm/domain/Provider.js.map +1 -1
- package/lib/esm/errors/InjectionError.js +3 -2
- package/lib/esm/errors/InjectionError.js.map +1 -1
- package/lib/esm/errors/UndefinedTokenError.js +1 -1
- package/lib/esm/errors/UndefinedTokenError.js.map +1 -1
- package/lib/esm/index.js +4 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/AlterRunInContext.js +2 -0
- package/lib/esm/interfaces/AlterRunInContext.js.map +1 -0
- package/lib/esm/interfaces/DIConfigurationOptions.js.map +1 -1
- package/lib/esm/interfaces/DILoggerOptions.js +2 -0
- package/lib/esm/interfaces/DILoggerOptions.js.map +1 -0
- package/lib/esm/interfaces/InterceptorMethods.js.map +1 -1
- package/lib/esm/interfaces/TokenProvider.js.map +1 -1
- package/lib/esm/registries/GlobalProviders.js +6 -12
- package/lib/esm/registries/GlobalProviders.js.map +1 -1
- package/lib/esm/services/DIConfiguration.js +52 -46
- package/lib/esm/services/DIConfiguration.js.map +1 -1
- package/lib/esm/services/DITest.js +4 -5
- package/lib/esm/services/DITest.js.map +1 -1
- package/lib/esm/services/InjectorService.js +57 -74
- package/lib/esm/services/InjectorService.js.map +1 -1
- package/lib/esm/utils/getConfiguration.js +3 -9
- package/lib/esm/utils/getConfiguration.js.map +1 -1
- package/lib/esm/utils/resolveControllers.js +1 -1
- package/lib/esm/utils/resolveControllers.js.map +1 -1
- package/lib/esm/utils/runInContext.js +8 -6
- package/lib/esm/utils/runInContext.js.map +1 -1
- package/lib/esm/utils/setLoggerConfiguration.js +7 -0
- package/lib/esm/utils/setLoggerConfiguration.js.map +1 -0
- package/lib/esm/utils/setLoggerFormat.js +29 -0
- package/lib/esm/utils/setLoggerFormat.js.map +1 -0
- package/lib/esm/utils/setLoggerLevel.js +1 -2
- package/lib/esm/utils/setLoggerLevel.js.map +1 -1
- package/lib/types/domain/ContextLogger.d.ts +15 -18
- package/lib/types/domain/DIContext.d.ts +15 -19
- package/lib/types/index.d.ts +4 -0
- package/lib/types/interfaces/AlterRunInContext.d.ts +4 -0
- package/lib/types/interfaces/DIConfigurationOptions.d.ts +1 -2
- package/lib/types/interfaces/DILoggerOptions.d.ts +44 -0
- package/lib/types/interfaces/InterceptorMethods.d.ts +1 -1
- package/lib/types/interfaces/TokenProvider.d.ts +1 -1
- package/lib/types/services/DIConfiguration.d.ts +17 -12
- package/lib/types/services/InjectorService.d.ts +11 -17
- package/lib/types/utils/runInContext.d.ts +2 -1
- package/lib/types/utils/setLoggerConfiguration.d.ts +2 -0
- package/lib/types/utils/setLoggerFormat.d.ts +6 -0
- package/lib/types/utils/setLoggerLevel.d.ts +1 -1
- package/package.json +10 -9
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalProviders.js","sourceRoot":"","sources":["../../../src/registries/GlobalProviders.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"GlobalProviders.js","sourceRoot":"","sources":["../../../src/registries/GlobalProviders.ts"],"names":[],"mappings":";;;AAAA,qCAAkD;AAClD,iDAA4C;AAC5C,yDAAoD;AAOpD,MAAa,sBAAuB,SAAQ,GAA4B;IACtE,SAAS,GAAkC,IAAI,GAAG,EAAE,CAAC;IAErD;;;;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GAAG,OAAO,CAAC,IAAI,IAAI,2BAAY,CAAC,QAAQ,CAAC;QAEnD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,MAAM,EAAC,KAAK,GAAG,mBAAQ,EAAC,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YAE1D,MAAM,IAAI,GAAG,IAAI,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAErC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;SACrB;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC;IACxB,CAAC;CACF;AAhID,wDAgIC;AAED;;;GAGG;AACH,0CAA0C;AAC7B,QAAA,eAAe,GAAG,IAAI,sBAAsB,EAAE,CAAC","sourcesContent":["import {getClassOrSymbol, Type} from \"@tsed/core\";\nimport {Provider} from \"../domain/Provider\";\nimport {ProviderType} from \"../domain/ProviderType\";\nimport type {InjectorService} from \"../services/InjectorService\";\nimport {ResolvedInvokeOptions} from \"../interfaces/ResolvedInvokeOptions\";\nimport {TokenProvider} from \"../interfaces/TokenProvider\";\nimport {RegistrySettings} from \"../interfaces/RegistrySettings\";\nimport {ProviderOpts} from \"../interfaces/ProviderOpts\";\n\nexport class GlobalProviderRegistry extends Map<TokenProvider, Provider> {\n #settings: Map<string, RegistrySettings> = new Map();\n\n /**\n * The get() method returns a specified element from a Map object.\n * @param key Required. The key of the element to return from the Map object.\n * @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.\n */\n get(key: TokenProvider): Provider | undefined {\n return super.get(getClassOrSymbol(key));\n }\n\n /**\n * The has() method returns a boolean indicating whether an element with the specified key exists or not.\n * @param key\n * @returns {boolean}\n */\n has(key: TokenProvider): boolean {\n return super.has(getClassOrSymbol(key));\n }\n\n /**\n * The set() method adds or updates an element with a specified key and value to a Map object.\n * @param key Required. The key of the element to add to the Map object.\n * @param metadata Required. The value of the element to add to the Map object.\n */\n set(key: TokenProvider, metadata: Provider): this {\n super.set(getClassOrSymbol(key), metadata);\n\n return this;\n }\n\n /**\n *\n * @param target\n * @param options\n */\n merge(target: TokenProvider, options: Partial<ProviderOpts>): void {\n const meta = this.createIfNotExists(target, options);\n\n Object.keys(options).forEach((key) => {\n meta[key] = (options as any)[key];\n });\n\n this.set(target, meta);\n }\n\n /**\n * The delete() method removes the specified element from a Map object.\n * @param key Required. The key of the element to remove from the Map object.\n * @returns {boolean} Returns true if an element in the Map object existed and has been removed, or false if the element does not exist.\n */\n delete(key: TokenProvider): boolean {\n return super.delete(getClassOrSymbol(key));\n }\n\n createRegistry(type: string, model: Type<Provider>, options: Partial<RegistrySettings> = {}) {\n const defaultOptions = this.getRegistrySettings(type);\n\n options = Object.assign(defaultOptions, {\n ...options,\n model\n });\n\n this.#settings.set(type, options);\n\n return this;\n }\n\n onInvoke(provider: Provider, locals: Map<TokenProvider, any>, options: ResolvedInvokeOptions & {injector: InjectorService}) {\n const settings = this.#settings.get(provider.type);\n\n if (settings?.onInvoke) {\n settings.onInvoke(provider, locals, options);\n }\n }\n\n getRegistrySettings(target: string | TokenProvider): RegistrySettings {\n let type: string = \"provider\";\n\n if (typeof target === \"string\") {\n type = target;\n } else {\n const provider = this.get(target);\n if (provider) {\n type = provider.type;\n }\n }\n\n return (\n this.#settings.get(type) || {\n model: Provider\n }\n );\n }\n\n createRegisterFn(type: string) {\n return (provider: any | ProviderOpts, instance?: any): void => {\n // istanbul ignore next\n if (!provider.provide) {\n provider = {\n provide: provider\n };\n }\n\n provider = Object.assign({instance}, provider, {type});\n this.merge(provider.provide, provider);\n };\n }\n\n /**\n *\n * @param key\n * @param options\n */\n protected createIfNotExists(key: TokenProvider, options: Partial<ProviderOpts>): Provider {\n const type = options.type || ProviderType.PROVIDER;\n\n if (!this.has(key)) {\n const {model = Provider} = this.#settings.get(type) || {};\n\n const item = new model(key, options);\n\n this.set(key, item);\n }\n\n return this.get(key)!;\n }\n}\n\n/**\n *\n * @type {GlobalProviders}\n */\n// tslint:disable-next-line: variable-name\nexport const GlobalProviders = new GlobalProviderRegistry();\n"]}
|
|
@@ -3,14 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.DIConfiguration = void 0;
|
|
4
4
|
const core_1 = require("@tsed/core");
|
|
5
5
|
class DIConfiguration {
|
|
6
|
+
default = new Map();
|
|
7
|
+
map = new Map();
|
|
6
8
|
constructor(initialProps = {}) {
|
|
7
|
-
this.default = new Map();
|
|
8
|
-
this.map = new Map();
|
|
9
9
|
Object.entries({
|
|
10
10
|
scopes: {},
|
|
11
11
|
resolvers: [],
|
|
12
12
|
imports: [],
|
|
13
13
|
routes: [],
|
|
14
|
+
logger: {},
|
|
14
15
|
...initialProps
|
|
15
16
|
}).forEach(([key, value]) => {
|
|
16
17
|
this.default.set(key, value);
|
|
@@ -21,29 +22,60 @@ class DIConfiguration {
|
|
|
21
22
|
}
|
|
22
23
|
});
|
|
23
24
|
}
|
|
25
|
+
get version() {
|
|
26
|
+
return this.get("version");
|
|
27
|
+
}
|
|
28
|
+
set version(v) {
|
|
29
|
+
this.map.set("version", v);
|
|
30
|
+
}
|
|
31
|
+
get rootDir() {
|
|
32
|
+
return this.get("rootDir");
|
|
33
|
+
}
|
|
34
|
+
set rootDir(value) {
|
|
35
|
+
this.map.set("rootDir", value);
|
|
36
|
+
}
|
|
37
|
+
get env() {
|
|
38
|
+
return this.map.get("env");
|
|
39
|
+
}
|
|
40
|
+
set env(value) {
|
|
41
|
+
this.map.set("env", value);
|
|
42
|
+
}
|
|
24
43
|
get scopes() {
|
|
25
|
-
return this.
|
|
44
|
+
return this.map.get("scopes");
|
|
26
45
|
}
|
|
27
46
|
set scopes(value) {
|
|
28
|
-
this.
|
|
47
|
+
this.map.set("scopes", value);
|
|
29
48
|
}
|
|
30
49
|
get resolvers() {
|
|
31
50
|
return this.getRaw("resolvers");
|
|
32
51
|
}
|
|
33
52
|
set resolvers(resolvers) {
|
|
34
|
-
this.
|
|
53
|
+
this.map.set("resolvers", resolvers);
|
|
35
54
|
}
|
|
36
55
|
get imports() {
|
|
37
|
-
return this.
|
|
56
|
+
return this.get("imports");
|
|
38
57
|
}
|
|
39
58
|
set imports(imports) {
|
|
40
|
-
this.
|
|
59
|
+
this.map.set("imports", imports);
|
|
41
60
|
}
|
|
42
61
|
get routes() {
|
|
43
|
-
return this.
|
|
62
|
+
return this.get("routes");
|
|
44
63
|
}
|
|
45
64
|
set routes(routes) {
|
|
46
|
-
this.
|
|
65
|
+
this.map.set("routes", routes);
|
|
66
|
+
}
|
|
67
|
+
get logger() {
|
|
68
|
+
return this.get("logger");
|
|
69
|
+
}
|
|
70
|
+
set logger(value) {
|
|
71
|
+
const logger = { ...this.logger, ...value };
|
|
72
|
+
this.map.set("logger", logger);
|
|
73
|
+
}
|
|
74
|
+
get debug() {
|
|
75
|
+
return this.logger.level === "debug";
|
|
76
|
+
}
|
|
77
|
+
set debug(debug) {
|
|
78
|
+
this.logger = { ...this.logger, level: debug ? "debug" : "info" };
|
|
47
79
|
}
|
|
48
80
|
/**
|
|
49
81
|
*
|
|
@@ -62,11 +94,17 @@ class DIConfiguration {
|
|
|
62
94
|
*/
|
|
63
95
|
set(propertyKey, value) {
|
|
64
96
|
if (typeof propertyKey === "string") {
|
|
65
|
-
|
|
97
|
+
if (propertyKey in this) {
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
this[propertyKey] = value;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
this.setRaw(propertyKey, value);
|
|
103
|
+
}
|
|
66
104
|
}
|
|
67
105
|
else {
|
|
68
106
|
Object.entries(propertyKey).forEach(([key, value]) => {
|
|
69
|
-
this
|
|
107
|
+
this.set(key, value);
|
|
70
108
|
});
|
|
71
109
|
}
|
|
72
110
|
return this;
|
|
@@ -82,7 +120,7 @@ class DIConfiguration {
|
|
|
82
120
|
* @returns {undefined|any}
|
|
83
121
|
*/
|
|
84
122
|
get(propertyKey, defaultValue) {
|
|
85
|
-
return this.
|
|
123
|
+
return this.getRaw(propertyKey, defaultValue);
|
|
86
124
|
}
|
|
87
125
|
getRaw(propertyKey, defaultValue) {
|
|
88
126
|
const value = (0, core_1.getValue)(this.map, propertyKey);
|
|
@@ -91,45 +129,13 @@ class DIConfiguration {
|
|
|
91
129
|
}
|
|
92
130
|
return (0, core_1.getValue)(this.default, propertyKey, defaultValue);
|
|
93
131
|
}
|
|
94
|
-
merge(obj) {
|
|
95
|
-
Object.entries(obj).forEach(([key, value]) => {
|
|
96
|
-
const descriptor = Object.getOwnPropertyDescriptor(DIConfiguration.prototype, key);
|
|
97
|
-
const originalValue = this.get(key);
|
|
98
|
-
value = (0, core_1.deepMerge)(value, originalValue);
|
|
99
|
-
if (descriptor && !["default", "set", "map", "get"].includes(key)) {
|
|
100
|
-
this[key] = value;
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
132
|
/**
|
|
105
133
|
*
|
|
106
134
|
* @param value
|
|
107
135
|
* @returns {any}
|
|
108
136
|
*/
|
|
109
137
|
resolve(value) {
|
|
110
|
-
|
|
111
|
-
if (![Array, Object].includes((0, core_1.classOf)(value))) {
|
|
112
|
-
return value;
|
|
113
|
-
}
|
|
114
|
-
return Object.entries(value).reduce((o, [k, v]) => {
|
|
115
|
-
// @ts-ignore
|
|
116
|
-
o[k] = this.resolve(v);
|
|
117
|
-
return o;
|
|
118
|
-
}, Array.isArray(value) ? [] : {});
|
|
119
|
-
}
|
|
120
|
-
if (typeof value === "string") {
|
|
121
|
-
const replacer = (match, key) => (0, core_1.getValue)(this.map, key);
|
|
122
|
-
return value
|
|
123
|
-
.replace(/\${([\w.]+)}/gi, replacer)
|
|
124
|
-
.replace(/<([\w.]+)>/gi, replacer)
|
|
125
|
-
.replace(/{{([\w.]+)}}/gi, replacer);
|
|
126
|
-
}
|
|
127
|
-
return value;
|
|
128
|
-
}
|
|
129
|
-
build() {
|
|
130
|
-
this.forEach((value, key) => this.map.set(key, this.resolve(value)));
|
|
131
|
-
this.set = this.setRaw;
|
|
132
|
-
this.get = this.getRaw = (propertyKey, defaultValue) => (0, core_1.getValue)(this.map, propertyKey, defaultValue);
|
|
138
|
+
return value.replace("${rootDir}", this.rootDir);
|
|
133
139
|
}
|
|
134
140
|
}
|
|
135
141
|
exports.DIConfiguration = DIConfiguration;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DIConfiguration.js","sourceRoot":"","sources":["../../../src/services/DIConfiguration.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"DIConfiguration.js","sourceRoot":"","sources":["../../../src/services/DIConfiguration.ts"],"names":[],"mappings":";;;AAAA,qCAA6E;AAO7E,MAAa,eAAe;IACjB,OAAO,GAAqB,IAAI,GAAG,EAAE,CAAC;IACrC,GAAG,GAAqB,IAAI,GAAG,EAAE,CAAC;IAE5C,YAAY,YAAY,GAAG,EAAE;QAC3B,MAAM,CAAC,OAAO,CAAC;YACb,MAAM,EAAE,EAAE;YACV,SAAS,EAAE,EAAE;YACb,OAAO,EAAE,EAAE;YACX,MAAM,EAAE,EAAE;YACV,MAAM,EAAE,EAAE;YACV,GAAG,YAAY;SAChB,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;YAC1B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,OAAO,IAAA,sBAAe,EAAkB,IAAI,EAAE;YAC5C,OAAO,CAAC,MAAM;gBACZ,OAAO,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;YAC1D,CAAC;SACF,CAAC,CAAC;IACL,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,SAAS,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IAAI,IAAI,EAAE;gBACvB,aAAa;gBACb,IAAI,CAAC,WAAW,CAAC,GAAG,KAAK,CAAC;aAC3B;iBAAM;gBACL,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;aACjC;SACF;aAAM;YACL,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;gBACnD,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACvB,CAAC,CAAC,CAAC;SACJ;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,CAAC,WAAmB,EAAE,KAAU;QACpC,IAAA,eAAQ,EAAC,IAAI,CAAC,GAAG,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC;QAEvC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAU,WAAmB,EAAE,YAAgB;QAChD,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;IAChD,CAAC;IAES,MAAM,CAAC,WAAmB,EAAE,YAAkB;QACtD,MAAM,KAAK,GAAG,IAAA,eAAQ,EAAC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;QAE9C,IAAI,KAAK,KAAK,SAAS,EAAE;YACvB,OAAO,KAAK,CAAC;SACd;QAED,OAAO,IAAA,eAAQ,EAAC,IAAI,CAAC,OAAO,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;IAC3D,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAC,KAAU;QAChB,OAAO,KAAK,CAAC,OAAO,CAAC,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACnD,CAAC;CACF;AAnKD,0CAmKC","sourcesContent":["import {classOf, Env, getValue, proxyDelegation, setValue} from \"@tsed/core\";\nimport type {ProviderScope} from \"../domain/ProviderScope\";\nimport type {DILoggerOptions} from \"../interfaces/DILoggerOptions\";\nimport type {DIResolver} from \"../interfaces/DIResolver\";\nimport type {TokenProvider} from \"../interfaces/TokenProvider\";\nimport type {TokenRoute} from \"../interfaces/TokenRoute\";\n\nexport class DIConfiguration {\n readonly default: Map<string, any> = new Map();\n protected map: Map<string, any> = new Map();\n\n constructor(initialProps = {}) {\n Object.entries({\n scopes: {},\n resolvers: [],\n imports: [],\n routes: [],\n logger: {},\n ...initialProps\n }).forEach(([key, value]) => {\n this.default.set(key, value);\n });\n\n return proxyDelegation<DIConfiguration>(this, {\n ownKeys(target) {\n return [...target.default.keys(), ...target.map.keys()];\n }\n });\n }\n\n get version() {\n return this.get(\"version\");\n }\n\n set version(v: string) {\n this.map.set(\"version\", v);\n }\n\n get rootDir() {\n return this.get(\"rootDir\");\n }\n\n set rootDir(value: string) {\n this.map.set(\"rootDir\", value);\n }\n\n get env(): Env {\n return this.map.get(\"env\");\n }\n\n set env(value: Env) {\n this.map.set(\"env\", value);\n }\n\n get scopes(): Record<string, ProviderScope> {\n return this.map.get(\"scopes\");\n }\n\n set scopes(value: Record<string, ProviderScope>) {\n this.map.set(\"scopes\", value);\n }\n\n get resolvers(): DIResolver[] {\n return this.getRaw(\"resolvers\");\n }\n\n set resolvers(resolvers: DIResolver[]) {\n this.map.set(\"resolvers\", resolvers);\n }\n\n get imports(): TokenProvider[] {\n return this.get(\"imports\");\n }\n\n set imports(imports: TokenProvider[]) {\n this.map.set(\"imports\", imports);\n }\n\n get routes(): TokenRoute[] {\n return this.get(\"routes\");\n }\n\n set routes(routes: TokenRoute[]) {\n this.map.set(\"routes\", routes);\n }\n\n get logger(): Partial<DILoggerOptions> {\n return this.get(\"logger\");\n }\n\n set logger(value: Partial<DILoggerOptions>) {\n const logger = {...this.logger, ...value};\n this.map.set(\"logger\", logger);\n }\n\n get debug(): boolean {\n return this.logger.level === \"debug\";\n }\n\n set debug(debug: boolean) {\n this.logger = {...this.logger, level: debug ? \"debug\" : \"info\"};\n }\n\n /**\n *\n * @param callbackfn\n * @param thisArg\n */\n forEach(callbackfn: (value: any, index: string, map: Map<string, any>) => void, thisArg?: any) {\n return new Set([...Array.from(this.default.keys()), ...Array.from(this.map.keys())]).forEach((key) => {\n callbackfn(this.getRaw(key), key, this.map);\n }, thisArg);\n }\n\n /**\n *\n * @param propertyKey\n * @param value\n */\n set(propertyKey: string | Partial<TsED.Configuration>, value?: any): this {\n if (typeof propertyKey === \"string\") {\n if (propertyKey in this) {\n // @ts-ignore\n this[propertyKey] = value;\n } else {\n this.setRaw(propertyKey, value);\n }\n } else {\n Object.entries(propertyKey).forEach(([key, value]) => {\n this.set(key, value);\n });\n }\n\n return this;\n }\n\n setRaw(propertyKey: string, value: any) {\n setValue(this.map, propertyKey, value);\n\n return this;\n }\n\n /**\n *\n * @param propertyKey\n * @param defaultValue\n * @returns {undefined|any}\n */\n get<T = any>(propertyKey: string, defaultValue?: T): T {\n return this.getRaw(propertyKey, defaultValue);\n }\n\n protected getRaw(propertyKey: string, defaultValue?: any): any {\n const value = getValue(this.map, propertyKey);\n\n if (value !== undefined) {\n return value;\n }\n\n return getValue(this.default, propertyKey, defaultValue);\n }\n\n /**\n *\n * @param value\n * @returns {any}\n */\n resolve(value: any) {\n return value.replace(\"${rootDir}\", this.rootDir);\n }\n}\n"]}
|
|
@@ -5,13 +5,14 @@ const core_1 = require("@tsed/core");
|
|
|
5
5
|
const logger_1 = require("@tsed/logger");
|
|
6
6
|
const createContainer_1 = require("../utils/createContainer");
|
|
7
7
|
const LocalsContainer_1 = require("../domain/LocalsContainer");
|
|
8
|
-
const
|
|
8
|
+
const setLoggerConfiguration_1 = require("../utils/setLoggerConfiguration");
|
|
9
9
|
const InjectorService_1 = require("./InjectorService");
|
|
10
10
|
const DIContext_1 = require("../domain/DIContext");
|
|
11
11
|
/**
|
|
12
12
|
* Tool to run test with lightweight DI sandbox.
|
|
13
13
|
*/
|
|
14
14
|
class DITest {
|
|
15
|
+
static _injector = null;
|
|
15
16
|
static get injector() {
|
|
16
17
|
if (DITest._injector) {
|
|
17
18
|
return DITest._injector;
|
|
@@ -35,9 +36,8 @@ class DITest {
|
|
|
35
36
|
await this.createContainer(settings);
|
|
36
37
|
}
|
|
37
38
|
static async createContainer(settings = {}) {
|
|
38
|
-
var _a;
|
|
39
39
|
const container = (0, createContainer_1.createContainer)();
|
|
40
|
-
|
|
40
|
+
settings.imports?.forEach(({ token, use }) => {
|
|
41
41
|
container.addProvider(token, {
|
|
42
42
|
useValue: use
|
|
43
43
|
});
|
|
@@ -52,7 +52,7 @@ class DITest {
|
|
|
52
52
|
injector.logger = logger_1.$log;
|
|
53
53
|
// @ts-ignore
|
|
54
54
|
injector.settings.set(DITest.configure(settings));
|
|
55
|
-
(0,
|
|
55
|
+
(0, setLoggerConfiguration_1.setLoggerConfiguration)(injector);
|
|
56
56
|
return injector;
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
@@ -112,5 +112,4 @@ class DITest {
|
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
exports.DITest = DITest;
|
|
115
|
-
DITest._injector = null;
|
|
116
115
|
//# sourceMappingURL=DITest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../../src/services/DITest.ts"],"names":[],"mappings":";;;AAAA,qCAAyC;AACzC,yCAAkC;AAClC,8DAAyD;AACzD,+DAA0D;AAG1D,
|
|
1
|
+
{"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../../src/services/DITest.ts"],"names":[],"mappings":";;;AAAA,qCAAyC;AACzC,yCAAkC;AAClC,8DAAyD;AACzD,+DAA0D;AAG1D,4EAAuE;AAEvE,uDAAkD;AAClD,mDAA8C;AAW9C;;GAEG;AACH,MAAa,MAAM;IACP,MAAM,CAAC,SAAS,GAA2B,IAAI,CAAC;IAE1D,MAAM,KAAK,QAAQ;QACjB,IAAI,MAAM,CAAC,SAAS,EAAE;YACpB,OAAO,MAAM,CAAC,SAAU,CAAC;SAC1B;QAED,0BA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AAM,CAAC,QAAQ;YACzB,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM;SAC/B,CAAC,CAAC;IACL,CAAC;IAES,MAAM,CAAC,SAAS,CAAC,WAAwC,EAAE;QACnE,OAAO;YACL,GAAG,QAAQ;YACX,GAAG,EAAE,IAAA,eAAQ,EAAC,QAAQ,EAAE,KAAK,EAAE,UAAG,CAAC,IAAI,CAAC;YACxC,MAAM,EAAE;gBACN,GAAG,IAAA,eAAQ,EAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE,CAAC;gBACnC,KAAK,EAAE,IAAA,eAAQ,EAAC,QAAQ,EAAE,cAAc,EAAE,KAAK,CAAC;aACjD;SACF,CAAC;IACJ,CAAC;;AA1HH,wBA2HC","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {$log} from \"@tsed/logger\";\nimport {createContainer} from \"../utils/createContainer\";\nimport {LocalsContainer} from \"../domain/LocalsContainer\";\nimport {OnInit} from \"../interfaces/OnInit\";\nimport {TokenProvider} from \"../interfaces/TokenProvider\";\nimport {setLoggerConfiguration} from \"../utils/setLoggerConfiguration\";\nimport {setLoggerLevel} from \"../utils/setLoggerLevel\";\nimport {InjectorService} from \"./InjectorService\";\nimport {DIContext} from \"../domain/DIContext\";\n\nexport interface DITestInvokeOptions {\n token?: TokenProvider;\n use: any;\n}\n\nexport interface DITestOptions extends Partial<TsED.Configuration> {\n imports?: DITestInvokeOptions[];\n}\n\n/**\n * Tool to run test with lightweight DI sandbox.\n */\nexport class DITest {\n protected static _injector: InjectorService | null = null;\n\n static get injector(): InjectorService {\n if (DITest._injector) {\n return DITest._injector!;\n }\n\n /* istanbul ignore next */\n throw new Error(\n \"PlatformTest.injector is not initialized. Use PlatformTest.create(): Promise before PlatformTest.invoke() or PlatformTest.injector.\\n\" +\n \"Example:\\n\" +\n \"before(async () => {\\n\" +\n \" await PlatformTest.create()\\n\" +\n \" await PlatformTest.invoke(MyService, [])\\n\" +\n \"})\"\n );\n }\n\n static set injector(injector: InjectorService) {\n DITest._injector = injector;\n }\n\n static hasInjector() {\n return !!DITest._injector;\n }\n\n static async create(settings: DITestOptions = {}) {\n DITest.injector = DITest.createInjector(settings);\n\n await this.createContainer(settings);\n }\n\n static async createContainer(settings: DITestOptions = {}) {\n const container = createContainer();\n\n settings.imports?.forEach(({token, use}) => {\n container.addProvider(token, {\n useValue: use\n });\n });\n\n await DITest.injector.load(container);\n }\n\n /**\n * Create a new injector with the right default services\n */\n static createInjector(settings: any = {}): InjectorService {\n const injector = new InjectorService();\n injector.logger = $log;\n\n // @ts-ignore\n injector.settings.set(DITest.configure(settings));\n\n setLoggerConfiguration(injector);\n\n return injector;\n }\n\n /**\n * Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.\n */\n static async reset() {\n if (DITest.hasInjector()) {\n await DITest.injector.destroy();\n DITest._injector = null;\n }\n }\n\n /**\n * Invoke a provider and return a fresh instance\n * @param target\n * @param providers\n */\n static invoke<T = any>(target: TokenProvider, providers: DITestInvokeOptions[] = []): T | Promise<T> {\n const locals = new LocalsContainer();\n providers.forEach((p) => {\n locals.set(p.token, p.use);\n });\n\n locals.set(InjectorService, DITest.injector);\n\n const instance: OnInit = DITest.injector.invoke(target, locals, {rebuild: true});\n\n if (instance && instance.$onInit) {\n // await instance.$onInit();\n const result = instance.$onInit();\n if (result instanceof Promise) {\n return result.then(() => instance as any);\n }\n }\n\n return instance as any;\n }\n\n /**\n * Return the instance from injector registry\n * @param target\n * @param options\n */\n static get<T = any>(target: TokenProvider, options: any = {}): T {\n return DITest.injector.get<T>(target, options)!;\n }\n\n static createDIContext() {\n return new DIContext({\n id: \"id\",\n injector: DITest.injector,\n logger: DITest.injector.logger\n });\n }\n\n protected static configure(settings: Partial<TsED.Configuration> = {}): Partial<TsED.Configuration> {\n return {\n ...settings,\n env: getValue(settings, \"env\", Env.TEST),\n logger: {\n ...getValue(settings, \"logger\", {}),\n level: getValue(settings, \"logger.level\", \"off\")\n }\n };\n }\n}\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var _InjectorService_cache;
|
|
3
2
|
var InjectorService_1;
|
|
4
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
4
|
exports.InjectorService = void 0;
|
|
@@ -9,16 +8,16 @@ const constants_1 = require("../constants/constants");
|
|
|
9
8
|
const configuration_1 = require("../decorators/configuration");
|
|
10
9
|
const injectable_1 = require("../decorators/injectable");
|
|
11
10
|
const Container_1 = require("../domain/Container");
|
|
11
|
+
const InjectablePropertyType_1 = require("../domain/InjectablePropertyType");
|
|
12
12
|
const LocalsContainer_1 = require("../domain/LocalsContainer");
|
|
13
13
|
const Provider_1 = require("../domain/Provider");
|
|
14
|
+
const ProviderScope_1 = require("../domain/ProviderScope");
|
|
14
15
|
const InjectionError_1 = require("../errors/InjectionError");
|
|
15
16
|
const UndefinedTokenError_1 = require("../errors/UndefinedTokenError");
|
|
16
17
|
const GlobalProviders_1 = require("../registries/GlobalProviders");
|
|
17
18
|
const createContainer_1 = require("../utils/createContainer");
|
|
18
19
|
const resolveControllers_1 = require("../utils/resolveControllers");
|
|
19
20
|
const DIConfiguration_1 = require("./DIConfiguration");
|
|
20
|
-
const ProviderScope_1 = require("../domain/ProviderScope");
|
|
21
|
-
const InjectablePropertyType_1 = require("../domain/InjectablePropertyType");
|
|
22
21
|
/**
|
|
23
22
|
* This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
|
|
24
23
|
*
|
|
@@ -41,13 +40,13 @@ const InjectablePropertyType_1 = require("../domain/InjectablePropertyType");
|
|
|
41
40
|
* ```
|
|
42
41
|
*/
|
|
43
42
|
let InjectorService = InjectorService_1 = class InjectorService extends Container_1.Container {
|
|
43
|
+
settings = new DIConfiguration_1.DIConfiguration();
|
|
44
|
+
logger = console;
|
|
45
|
+
resolvedConfiguration = false;
|
|
46
|
+
#cache = new LocalsContainer_1.LocalsContainer();
|
|
44
47
|
constructor() {
|
|
45
48
|
super();
|
|
46
|
-
this.
|
|
47
|
-
this.logger = console;
|
|
48
|
-
this.resolvedConfiguration = false;
|
|
49
|
-
_InjectorService_cache.set(this, new LocalsContainer_1.LocalsContainer());
|
|
50
|
-
tslib_1.__classPrivateFieldGet(this, _InjectorService_cache, "f").set(InjectorService_1, this);
|
|
49
|
+
this.#cache.set(InjectorService_1, this);
|
|
51
50
|
}
|
|
52
51
|
get resolvers() {
|
|
53
52
|
return this.settings.resolvers;
|
|
@@ -66,7 +65,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
66
65
|
* Return a list of instance build by the injector.
|
|
67
66
|
*/
|
|
68
67
|
toArray() {
|
|
69
|
-
return
|
|
68
|
+
return this.#cache.toArray();
|
|
70
69
|
}
|
|
71
70
|
/**
|
|
72
71
|
* Get a service or factory already constructed from his symbol or class.
|
|
@@ -117,7 +116,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
117
116
|
* @param token
|
|
118
117
|
*/
|
|
119
118
|
has(token) {
|
|
120
|
-
return
|
|
119
|
+
return this.#cache.get(token) !== undefined;
|
|
121
120
|
}
|
|
122
121
|
/**
|
|
123
122
|
* Invoke the class and inject all services that required by the class constructor.
|
|
@@ -140,73 +139,65 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
140
139
|
* @param options
|
|
141
140
|
* @returns {T} The class constructed.
|
|
142
141
|
*/
|
|
143
|
-
invoke(token, locals
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
142
|
+
invoke(token, locals, options = {}) {
|
|
143
|
+
let instance = locals ? locals.get(token) : undefined;
|
|
144
|
+
if (instance !== undefined) {
|
|
145
|
+
return instance;
|
|
146
|
+
}
|
|
147
|
+
if (token === configuration_1.Configuration) {
|
|
148
|
+
return this.settings;
|
|
149
|
+
}
|
|
150
|
+
instance = !options.rebuild ? this.getInstance(token) : undefined;
|
|
151
|
+
if (instance != undefined) {
|
|
152
|
+
return instance;
|
|
149
153
|
}
|
|
150
154
|
if (token === constants_1.DI_PARAM_OPTIONS) {
|
|
151
155
|
return {};
|
|
152
156
|
}
|
|
157
|
+
const provider = this.ensureProvider(token);
|
|
153
158
|
if (!provider || options.rebuild) {
|
|
154
159
|
instance = this.resolve(token, locals, options);
|
|
155
160
|
if (this.hasProvider(token)) {
|
|
156
|
-
|
|
161
|
+
this.#cache.set(token, instance);
|
|
157
162
|
}
|
|
158
163
|
return instance;
|
|
159
164
|
}
|
|
165
|
+
instance = this.resolve(token, locals, options);
|
|
160
166
|
switch (this.scopeOf(provider)) {
|
|
161
167
|
case ProviderScope_1.ProviderScope.SINGLETON:
|
|
162
|
-
if (!
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
tslib_1.__classPrivateFieldGet(this, _InjectorService_cache, "f").get(token).then((instance) => {
|
|
166
|
-
tslib_1.__classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, instance);
|
|
167
|
-
});
|
|
168
|
-
}
|
|
168
|
+
if (!provider.isAsync()) {
|
|
169
|
+
this.#cache.set(token, instance);
|
|
170
|
+
return instance;
|
|
169
171
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
+
// store promise to lock token in cache
|
|
173
|
+
this.#cache.set(token, instance);
|
|
174
|
+
instance = instance.then((instance) => {
|
|
175
|
+
this.#cache.set(token, instance);
|
|
176
|
+
return instance;
|
|
177
|
+
});
|
|
178
|
+
return instance;
|
|
172
179
|
case ProviderScope_1.ProviderScope.REQUEST:
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
break;
|
|
176
|
-
case ProviderScope_1.ProviderScope.INSTANCE:
|
|
177
|
-
instance = this.resolve(provider.provide, locals, options);
|
|
178
|
-
break;
|
|
180
|
+
locals && locals.set(token, instance);
|
|
181
|
+
return instance;
|
|
179
182
|
}
|
|
180
183
|
return instance;
|
|
181
184
|
}
|
|
182
185
|
/**
|
|
183
186
|
* Build only providers which are asynchronous.
|
|
184
187
|
*/
|
|
185
|
-
async loadAsync(
|
|
188
|
+
async loadAsync() {
|
|
186
189
|
for (const [, provider] of this) {
|
|
187
|
-
if (!
|
|
188
|
-
|
|
189
|
-
await this.invoke(provider.token, locals);
|
|
190
|
-
}
|
|
191
|
-
const instance = tslib_1.__classPrivateFieldGet(this, _InjectorService_cache, "f").get(provider.token);
|
|
192
|
-
if (instance !== undefined) {
|
|
193
|
-
locals.set(provider.token, instance);
|
|
194
|
-
}
|
|
190
|
+
if (!this.has(provider.token) && provider.isAsync()) {
|
|
191
|
+
await this.invoke(provider.token);
|
|
195
192
|
}
|
|
196
193
|
}
|
|
197
|
-
return locals;
|
|
198
194
|
}
|
|
199
|
-
loadSync(
|
|
195
|
+
loadSync() {
|
|
200
196
|
for (const [, provider] of this) {
|
|
201
|
-
if (!
|
|
202
|
-
this.invoke(provider.token
|
|
203
|
-
}
|
|
204
|
-
const instance = tslib_1.__classPrivateFieldGet(this, _InjectorService_cache, "f").get(provider.token);
|
|
205
|
-
if (instance !== undefined) {
|
|
206
|
-
locals.set(provider.token, instance);
|
|
197
|
+
if (!this.has(provider.token) && this.scopeOf(provider) === ProviderScope_1.ProviderScope.SINGLETON) {
|
|
198
|
+
this.invoke(provider.token);
|
|
207
199
|
}
|
|
208
200
|
}
|
|
209
|
-
return locals;
|
|
210
201
|
}
|
|
211
202
|
/**
|
|
212
203
|
* Boostrap injector from container and resolve configuration.
|
|
@@ -238,20 +229,15 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
238
229
|
* Build all providers from given container (or GlobalProviders) and emit `$onInit` event.
|
|
239
230
|
*
|
|
240
231
|
* @param container
|
|
241
|
-
* @param rootModule
|
|
242
232
|
*/
|
|
243
|
-
async load(container = (0, createContainer_1.createContainer)()
|
|
233
|
+
async load(container = (0, createContainer_1.createContainer)()) {
|
|
244
234
|
this.bootstrap(container);
|
|
245
235
|
// build async and sync provider
|
|
246
|
-
|
|
247
|
-
if (rootModule) {
|
|
248
|
-
await this.invoke(rootModule);
|
|
249
|
-
}
|
|
236
|
+
await this.loadAsync();
|
|
250
237
|
// load sync provider
|
|
251
|
-
|
|
252
|
-
await
|
|
253
|
-
await
|
|
254
|
-
return locals;
|
|
238
|
+
this.loadSync();
|
|
239
|
+
await this.emit("$beforeInit");
|
|
240
|
+
await this.emit("$onInit");
|
|
255
241
|
}
|
|
256
242
|
/**
|
|
257
243
|
* Load all configurations registered on providers
|
|
@@ -271,13 +257,12 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
271
257
|
});
|
|
272
258
|
}
|
|
273
259
|
if (provider.resolvers) {
|
|
274
|
-
this.resolvers.
|
|
260
|
+
this.settings.resolvers = this.settings.resolvers.concat(provider.resolvers);
|
|
275
261
|
}
|
|
276
262
|
});
|
|
277
263
|
mergedConfiguration.forEach((value, key) => {
|
|
278
|
-
this.settings
|
|
264
|
+
this.settings.set(key, (0, core_1.deepMerge)(value, this.settings.get(key)));
|
|
279
265
|
});
|
|
280
|
-
this.settings.build();
|
|
281
266
|
this.resolvedConfiguration = true;
|
|
282
267
|
}
|
|
283
268
|
/**
|
|
@@ -419,7 +404,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
419
404
|
let instance = this.getInstance(token);
|
|
420
405
|
if (!instance) {
|
|
421
406
|
instance = await this.invoke(token);
|
|
422
|
-
if ((0, core_1.isFunction)(instance
|
|
407
|
+
if ((0, core_1.isFunction)(instance?.$onInit)) {
|
|
423
408
|
await instance.$onInit();
|
|
424
409
|
}
|
|
425
410
|
}
|
|
@@ -432,7 +417,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
432
417
|
* @returns {Promise<any[]>} A list of promises.
|
|
433
418
|
*/
|
|
434
419
|
async emit(eventName, ...args) {
|
|
435
|
-
return
|
|
420
|
+
return this.#cache.emit(eventName, ...args);
|
|
436
421
|
}
|
|
437
422
|
/**
|
|
438
423
|
* @param eventName
|
|
@@ -440,7 +425,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
440
425
|
* @param args
|
|
441
426
|
*/
|
|
442
427
|
alter(eventName, value, ...args) {
|
|
443
|
-
return
|
|
428
|
+
return this.#cache.alter(eventName, value, ...args);
|
|
444
429
|
}
|
|
445
430
|
/**
|
|
446
431
|
* @param eventName
|
|
@@ -448,10 +433,10 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
448
433
|
* @param args
|
|
449
434
|
*/
|
|
450
435
|
async alterAsync(eventName, value, ...args) {
|
|
451
|
-
return
|
|
436
|
+
return this.#cache.alterAsync(eventName, value, ...args);
|
|
452
437
|
}
|
|
453
438
|
async destroy() {
|
|
454
|
-
await
|
|
439
|
+
await this.#cache.destroy();
|
|
455
440
|
}
|
|
456
441
|
ensureProvider(token) {
|
|
457
442
|
if (!this.hasProvider(token) && GlobalProviders_1.GlobalProviders.has(token)) {
|
|
@@ -460,7 +445,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
460
445
|
return this.getProvider(token);
|
|
461
446
|
}
|
|
462
447
|
getInstance(token) {
|
|
463
|
-
return
|
|
448
|
+
return this.#cache.get(token);
|
|
464
449
|
}
|
|
465
450
|
/**
|
|
466
451
|
* Invoke a class method and inject service.
|
|
@@ -479,7 +464,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
479
464
|
* @param options
|
|
480
465
|
* @private
|
|
481
466
|
*/
|
|
482
|
-
resolve(target, locals, options = {}) {
|
|
467
|
+
resolve(target, locals = new LocalsContainer_1.LocalsContainer(), options = {}) {
|
|
483
468
|
const resolvedOpts = this.mapInvokeOptions(target, locals, options);
|
|
484
469
|
const { token, deps, construct, imports, provider } = resolvedOpts;
|
|
485
470
|
if (provider) {
|
|
@@ -489,10 +474,9 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
489
474
|
let currentDependency = false;
|
|
490
475
|
try {
|
|
491
476
|
const invokeDependency = (parent) => (token, index) => {
|
|
492
|
-
var _a;
|
|
493
477
|
currentDependency = { token, index, deps };
|
|
494
478
|
if (token !== constants_1.DI_PARAM_OPTIONS) {
|
|
495
|
-
const options =
|
|
479
|
+
const options = provider?.store?.get(`${constants_1.DI_PARAM_OPTIONS}:${index}`);
|
|
496
480
|
locals.set(constants_1.DI_PARAM_OPTIONS, options || {});
|
|
497
481
|
}
|
|
498
482
|
return (0, core_1.isInheritedFrom)(token, Provider_1.Provider, 1) ? provider : this.invoke(token, locals, { parent });
|
|
@@ -577,7 +561,6 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
577
561
|
};
|
|
578
562
|
}
|
|
579
563
|
};
|
|
580
|
-
_InjectorService_cache = new WeakMap();
|
|
581
564
|
InjectorService = InjectorService_1 = tslib_1.__decorate([
|
|
582
565
|
(0, injectable_1.Injectable)({
|
|
583
566
|
scope: ProviderScope_1.ProviderScope.SINGLETON,
|