@tsed/di 8.0.0-rc.5 → 8.0.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/lib/browser/di.umd.min.js +1 -1
- package/lib/browser/di.umd.min.js.map +1 -1
- package/lib/esm/common/decorators/autoInjectable.js +1 -1
- package/lib/esm/common/decorators/autoInjectable.js.map +1 -1
- package/lib/esm/common/decorators/configuration.js +7 -1
- package/lib/esm/common/decorators/configuration.js.map +1 -1
- package/lib/esm/common/decorators/controller.js +1 -1
- package/lib/esm/common/decorators/injectable.js +3 -3
- package/lib/esm/common/decorators/injectable.js.map +1 -1
- package/lib/esm/common/decorators/module.js +2 -4
- package/lib/esm/common/decorators/module.js.map +1 -1
- package/lib/esm/common/domain/Container.js +2 -2
- package/lib/esm/common/domain/Container.js.map +1 -1
- package/lib/esm/common/domain/ControllerProvider.js +2 -2
- package/lib/esm/common/domain/ControllerProvider.js.map +1 -1
- package/lib/esm/common/domain/LocalsContainer.js +1 -1
- package/lib/esm/common/domain/LocalsContainer.js.map +1 -1
- package/lib/esm/common/domain/Provider.js +44 -29
- package/lib/esm/common/domain/Provider.js.map +1 -1
- package/lib/esm/common/fn/inject.js +3 -2
- package/lib/esm/common/fn/inject.js.map +1 -1
- package/lib/esm/common/fn/injectMany.js +10 -1
- package/lib/esm/common/fn/injectMany.js.map +1 -1
- package/lib/esm/common/fn/injectable.js +2 -48
- package/lib/esm/common/fn/injectable.js.map +1 -1
- package/lib/esm/common/fn/injector.js +5 -17
- package/lib/esm/common/fn/injector.js.map +1 -1
- package/lib/esm/common/fn/lazyInject.js +4 -4
- package/lib/esm/common/fn/lazyInject.js.map +1 -1
- package/lib/esm/common/index.js +2 -3
- package/lib/esm/common/index.js.map +1 -1
- package/lib/esm/common/registries/GlobalProviders.js +8 -14
- package/lib/esm/common/registries/GlobalProviders.js.map +1 -1
- package/lib/esm/common/registries/ProviderRegistry.js +7 -3
- package/lib/esm/common/registries/ProviderRegistry.js.map +1 -1
- package/lib/esm/common/services/DIConfiguration.js +1 -14
- package/lib/esm/common/services/DIConfiguration.js.map +1 -1
- package/lib/esm/common/services/DILogger.js +3 -9
- package/lib/esm/common/services/DILogger.js.map +1 -1
- package/lib/esm/common/services/InjectorService.js +157 -157
- package/lib/esm/common/services/InjectorService.js.map +1 -1
- package/lib/esm/common/utils/discoverHooks.js +14 -0
- package/lib/esm/common/utils/discoverHooks.js.map +1 -0
- package/lib/esm/common/utils/providerBuilder.js +55 -0
- package/lib/esm/common/utils/providerBuilder.js.map +1 -0
- package/lib/esm/node/domain/ContextLogger.js +1 -1
- package/lib/esm/node/domain/ContextLogger.js.map +1 -1
- package/lib/esm/node/domain/DIContext.js.map +1 -1
- package/lib/esm/node/services/DITest.js +9 -14
- package/lib/esm/node/services/DITest.js.map +1 -1
- package/lib/esm/node/utils/attachLogger.js +4 -3
- package/lib/esm/node/utils/attachLogger.js.map +1 -1
- package/lib/esm/node/utils/setLoggerConfiguration.js +3 -3
- package/lib/esm/node/utils/setLoggerConfiguration.js.map +1 -1
- package/lib/esm/node/utils/setLoggerFormat.js +8 -7
- package/lib/esm/node/utils/setLoggerFormat.js.map +1 -1
- package/lib/esm/node/utils/setLoggerLevel.js +4 -4
- package/lib/esm/node/utils/setLoggerLevel.js.map +1 -1
- package/lib/types/common/decorators/controller.d.ts +1 -1
- package/lib/types/common/decorators/injectable.d.ts +1 -1
- package/lib/types/common/decorators/module.d.ts +0 -6
- package/lib/types/common/domain/ControllerProvider.d.ts +1 -1
- package/lib/types/common/domain/LocalsContainer.d.ts +1 -1
- package/lib/types/common/domain/Provider.d.ts +26 -7
- package/lib/types/common/fn/injectMany.d.ts +9 -0
- package/lib/types/common/fn/injectable.d.ts +403 -18
- package/lib/types/common/fn/injector.d.ts +2 -9
- package/lib/types/common/index.d.ts +2 -3
- package/lib/types/common/interfaces/DIConfigurationOptions.d.ts +0 -5
- package/lib/types/common/interfaces/InvokeOptions.d.ts +0 -5
- package/lib/types/common/interfaces/ProviderOpts.d.ts +1 -6
- package/lib/types/common/interfaces/RegistrySettings.d.ts +0 -12
- package/lib/types/common/interfaces/ResolvedInvokeOptions.d.ts +2 -2
- package/lib/types/common/registries/GlobalProviders.d.ts +1 -7
- package/lib/types/common/registries/ProviderRegistry.d.ts +12 -2
- package/lib/types/common/services/DIConfiguration.d.ts +1 -6
- package/lib/types/common/services/InjectorService.d.ts +50 -38
- package/lib/types/common/utils/discoverHooks.d.ts +2 -0
- package/lib/types/common/utils/providerBuilder.d.ts +17 -0
- package/lib/types/node/domain/ContextLogger.d.ts +1 -1
- package/lib/types/node/services/DITest.d.ts +1 -2
- package/lib/types/node/utils/attachLogger.d.ts +2 -2
- package/lib/types/node/utils/setLoggerConfiguration.d.ts +1 -2
- package/lib/types/node/utils/setLoggerFormat.d.ts +1 -3
- package/lib/types/node/utils/setLoggerLevel.d.ts +1 -3
- package/package.json +13 -11
- package/vitest.config.mts +4 -4
- package/lib/esm/common/fn/events.js +0 -31
- package/lib/esm/common/fn/events.js.map +0 -1
- package/lib/esm/common/interfaces/DIResolver.js +0 -2
- package/lib/esm/common/interfaces/DIResolver.js.map +0 -1
- package/lib/esm/common/utils/resolveControllers.js +0 -42
- package/lib/esm/common/utils/resolveControllers.js.map +0 -1
- package/lib/types/common/fn/events.d.ts +0 -23
- package/lib/types/common/interfaces/DIResolver.d.ts +0 -5
- package/lib/types/common/utils/resolveControllers.d.ts +0 -12
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { ControllerProvider } from "../domain/ControllerProvider.js";
|
|
2
2
|
import { ProviderType } from "../domain/ProviderType.js";
|
|
3
|
+
import { injectable } from "../fn/injectable.js";
|
|
3
4
|
import { GlobalProviders } from "./GlobalProviders.js";
|
|
4
5
|
GlobalProviders.createRegistry(ProviderType.CONTROLLER, ControllerProvider);
|
|
5
6
|
/**
|
|
6
7
|
* Register a provider configuration.
|
|
7
|
-
* @
|
|
8
|
+
* @deprecated Since v8. Use injectable() function instead.
|
|
8
9
|
*/
|
|
9
|
-
export function registerProvider(
|
|
10
|
-
return
|
|
10
|
+
export function registerProvider({ token, provide, ...opts }) {
|
|
11
|
+
return injectable(token || provide, {
|
|
12
|
+
...opts,
|
|
13
|
+
token: token || provide
|
|
14
|
+
}).inspect();
|
|
11
15
|
}
|
|
12
16
|
//# sourceMappingURL=ProviderRegistry.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProviderRegistry.js","sourceRoot":"","sources":["../../../../src/common/registries/ProviderRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAC,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAC,YAAY,EAAC,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"ProviderRegistry.js","sourceRoot":"","sources":["../../../../src/common/registries/ProviderRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAC,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAC,YAAY,EAAC,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAC,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAG/C,OAAO,EAAC,eAAe,EAAC,MAAM,sBAAsB,CAAC;AAErD,eAAe,CAAC,cAAc,CAAC,YAAY,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;AAe5E;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAa,EAAC,KAAK,EAAE,OAAO,EAAE,GAAG,IAAI,EAAa;IAChF,OAAO,UAAU,CAAC,KAAK,IAAI,OAAO,EAAE;QAClC,GAAG,IAAI;QACP,KAAK,EAAE,KAAK,IAAI,OAAO;KACY,CAAC,CAAC,OAAO,EAAE,CAAC;AACnD,CAAC"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { getValue, setValue } from "@tsed/core";
|
|
2
|
+
export const CONFIGURATION = Symbol.for("CONFIGURATION");
|
|
2
3
|
export class DIConfiguration {
|
|
3
4
|
constructor(initialProps = {}) {
|
|
4
5
|
this.default = new Map();
|
|
5
6
|
this.map = new Map();
|
|
6
7
|
Object.entries({
|
|
7
|
-
scopes: {},
|
|
8
|
-
resolvers: [],
|
|
9
8
|
imports: [],
|
|
10
9
|
routes: [],
|
|
11
10
|
logger: {},
|
|
@@ -32,18 +31,6 @@ export class DIConfiguration {
|
|
|
32
31
|
set env(value) {
|
|
33
32
|
this.map.set("env", value);
|
|
34
33
|
}
|
|
35
|
-
get scopes() {
|
|
36
|
-
return this.map.get("scopes");
|
|
37
|
-
}
|
|
38
|
-
set scopes(value) {
|
|
39
|
-
this.map.set("scopes", value);
|
|
40
|
-
}
|
|
41
|
-
get resolvers() {
|
|
42
|
-
return this.getRaw("resolvers");
|
|
43
|
-
}
|
|
44
|
-
set resolvers(resolvers) {
|
|
45
|
-
this.map.set("resolvers", resolvers);
|
|
46
|
-
}
|
|
47
34
|
get imports() {
|
|
48
35
|
return this.get("imports");
|
|
49
36
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DIConfiguration.js","sourceRoot":"","sources":["../../../../src/common/services/DIConfiguration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAM,QAAQ,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"DIConfiguration.js","sourceRoot":"","sources":["../../../../src/common/services/DIConfiguration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAM,QAAQ,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AAOnD,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;AAEzD,MAAM,OAAO,eAAe;IAI1B,YAAY,YAAY,GAAG,EAAE;QAHpB,YAAO,GAAqB,IAAI,GAAG,EAAE,CAAC;QACrC,QAAG,GAAqB,IAAI,GAAG,EAAE,CAAC;QAG1C,MAAM,CAAC,OAAO,CAAC;YACb,OAAO,EAAE,EAAE;YACX,MAAM,EAAE,EAAE;YACV,MAAM,EAA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BAAM,CAAC;gBACN,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;YAClC,CAAC;QACH,CAAC;aAAM,CAAC;YACN,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;QACL,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,CAAC,WAAmB,EAAE,KAAU;QACpC,QAAQ,CAAC,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,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;QAE9C,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;IAC3D,CAAC;CACF"}
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { injectable } from "../fn/injectable.js";
|
|
2
|
+
import { injector } from "../fn/injector.js";
|
|
3
3
|
export const LOGGER = Symbol.for("LOGGER");
|
|
4
|
-
|
|
5
|
-
provide: LOGGER,
|
|
6
|
-
deps: [InjectorService],
|
|
7
|
-
useFactory(injector) {
|
|
8
|
-
return injector.logger;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
4
|
+
injectable(LOGGER).factory(() => injector().logger);
|
|
11
5
|
//# sourceMappingURL=DILogger.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DILogger.js","sourceRoot":"","sources":["../../../../src/common/services/DILogger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DILogger.js","sourceRoot":"","sources":["../../../../src/common/services/DILogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAC,QAAQ,EAAC,MAAM,mBAAmB,CAAC;AAG3C,MAAM,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;AAG3C,UAAU,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { classOf, deepClone, deepMerge, Hooks, isArray, isClass, isFunction, isInheritedFrom, isObject, isPromise, nameOf, Store } from "@tsed/core";
|
|
1
|
+
import { classOf, deepClone, deepMerge, isArray, isClass, isFunction, isInheritedFrom, isObject, isPromise, nameOf } from "@tsed/core";
|
|
2
|
+
import { $alter, $asyncAlter, $asyncEmit, $emit, $off, $on } from "@tsed/hooks";
|
|
4
3
|
import { DI_INVOKE_OPTIONS, DI_USE_PARAM_OPTIONS } from "../constants/constants.js";
|
|
5
|
-
import { Configuration } from "../decorators/configuration.js";
|
|
6
|
-
import { Injectable } from "../decorators/injectable.js";
|
|
7
4
|
import { Container } from "../domain/Container.js";
|
|
8
5
|
import { LocalsContainer } from "../domain/LocalsContainer.js";
|
|
9
6
|
import { Provider } from "../domain/Provider.js";
|
|
@@ -12,8 +9,8 @@ import { InjectionError } from "../errors/InjectionError.js";
|
|
|
12
9
|
import { GlobalProviders } from "../registries/GlobalProviders.js";
|
|
13
10
|
import { createContainer } from "../utils/createContainer.js";
|
|
14
11
|
import { getConstructorDependencies } from "../utils/getConstructorDependencies.js";
|
|
15
|
-
import { resolveControllers } from "../utils/resolveControllers.js";
|
|
16
12
|
import { DIConfiguration } from "./DIConfiguration.js";
|
|
13
|
+
const EXCLUDED_CONFIGURATION_KEYS = ["mount", "imports"];
|
|
17
14
|
/**
|
|
18
15
|
* This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
|
|
19
16
|
*
|
|
@@ -28,36 +25,32 @@ import { DIConfiguration } from "./DIConfiguration.js";
|
|
|
28
25
|
* import MyService3 from "./services/service3.js";
|
|
29
26
|
*
|
|
30
27
|
* // When all services are imported, you can load InjectorService.
|
|
31
|
-
* const injector = new InjectorService()
|
|
32
28
|
*
|
|
33
|
-
* await injector.load();
|
|
29
|
+
* await injector().load();
|
|
34
30
|
*
|
|
35
|
-
* const myService1 = injector.get<MyService1>(
|
|
31
|
+
* const myService1 = injector.get<MyService1>(MyService1);
|
|
36
32
|
* ```
|
|
37
33
|
*/
|
|
38
|
-
|
|
34
|
+
export class InjectorService extends Container {
|
|
39
35
|
#cache;
|
|
36
|
+
#loaded;
|
|
40
37
|
constructor() {
|
|
41
38
|
super();
|
|
42
|
-
this.settings = new DIConfiguration();
|
|
43
39
|
this.logger = console;
|
|
44
|
-
this.hooks = new Hooks();
|
|
45
40
|
this.resolvedConfiguration = false;
|
|
46
41
|
this.#cache = new LocalsContainer();
|
|
47
|
-
this.#
|
|
42
|
+
this.#loaded = false;
|
|
43
|
+
this.#cache.set(InjectorService, this);
|
|
44
|
+
this.#cache.set(DIConfiguration, new DIConfiguration());
|
|
48
45
|
}
|
|
49
|
-
get
|
|
50
|
-
return this.
|
|
46
|
+
get settings() {
|
|
47
|
+
return this.#cache.get(DIConfiguration);
|
|
51
48
|
}
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
set settings(settings) {
|
|
50
|
+
this.#cache.set(DIConfiguration, settings);
|
|
54
51
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
* @param provider
|
|
58
|
-
*/
|
|
59
|
-
scopeOf(provider) {
|
|
60
|
-
return provider.scope || this.scopes[String(provider.type)] || ProviderScope.SINGLETON;
|
|
52
|
+
isLoaded() {
|
|
53
|
+
return this.#loaded;
|
|
61
54
|
}
|
|
62
55
|
/**
|
|
63
56
|
* Return a list of instance build by the injector.
|
|
@@ -84,26 +77,18 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
84
77
|
* @param token The class or symbol registered in InjectorService.
|
|
85
78
|
* @param options
|
|
86
79
|
*/
|
|
87
|
-
get(token, options
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return instance;
|
|
91
|
-
}
|
|
92
|
-
if (!this.hasProvider(token)) {
|
|
93
|
-
for (const resolver of this.resolvers) {
|
|
94
|
-
const result = resolver.get(token, options);
|
|
95
|
-
if (result !== undefined) {
|
|
96
|
-
return result;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
80
|
+
get(token, options) {
|
|
81
|
+
if (!this.has(token)) {
|
|
82
|
+
return this.resolve(token, options);
|
|
99
83
|
}
|
|
84
|
+
return this.#cache.get(token);
|
|
100
85
|
}
|
|
101
86
|
/**
|
|
102
87
|
* Return all instance of the same provider type
|
|
103
88
|
*/
|
|
104
|
-
getMany(type,
|
|
89
|
+
getMany(type, options) {
|
|
105
90
|
return this.getProviders(type).map((provider) => {
|
|
106
|
-
return this.
|
|
91
|
+
return this.resolve(provider.token, options);
|
|
107
92
|
});
|
|
108
93
|
}
|
|
109
94
|
/**
|
|
@@ -120,7 +105,9 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
120
105
|
return this;
|
|
121
106
|
}
|
|
122
107
|
/**
|
|
123
|
-
*
|
|
108
|
+
* Resolve the token depending on his provider configuration.
|
|
109
|
+
*
|
|
110
|
+
* If the token isn't cached, the injector will invoke the provider and cache the result.
|
|
124
111
|
*
|
|
125
112
|
* #### Example
|
|
126
113
|
*
|
|
@@ -136,75 +123,79 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
136
123
|
* ```
|
|
137
124
|
*
|
|
138
125
|
* @param token The injectable class to invoke. Class parameters are injected according constructor signature.
|
|
139
|
-
* @param
|
|
140
|
-
* @param options
|
|
126
|
+
* @param options {InvokeOptions} Optional options to invoke the class.
|
|
141
127
|
* @returns {Type} The class constructed.
|
|
142
128
|
*/
|
|
143
|
-
|
|
144
|
-
let instance = locals ? locals.get(token) : undefined;
|
|
129
|
+
resolve(token, options = {}) {
|
|
130
|
+
let instance = options.locals ? options.locals.get(token) : undefined;
|
|
145
131
|
if (instance !== undefined) {
|
|
146
132
|
return instance;
|
|
147
133
|
}
|
|
148
|
-
if (token ===
|
|
149
|
-
return
|
|
134
|
+
if (token === DI_USE_PARAM_OPTIONS) {
|
|
135
|
+
return options.useOpts;
|
|
150
136
|
}
|
|
151
|
-
instance = !options.rebuild ? this.
|
|
137
|
+
instance = !options.rebuild ? this.#cache.get(token) : undefined;
|
|
152
138
|
if (instance != undefined) {
|
|
153
139
|
return instance;
|
|
154
140
|
}
|
|
155
|
-
if (token === DI_USE_PARAM_OPTIONS) {
|
|
156
|
-
return options.useOpts;
|
|
157
|
-
}
|
|
158
141
|
const provider = this.ensureProvider(token);
|
|
159
|
-
|
|
160
|
-
this.#cache.set(token, instance);
|
|
161
|
-
provider?.alias && this.alias(token, provider.alias);
|
|
162
|
-
};
|
|
142
|
+
// maybe not necessary
|
|
163
143
|
if (!provider || options.rebuild) {
|
|
164
|
-
instance = this.
|
|
165
|
-
if (
|
|
166
|
-
|
|
144
|
+
instance = this.invokeToken(token, options);
|
|
145
|
+
if (provider) {
|
|
146
|
+
return this.setToCache(provider, instance);
|
|
167
147
|
}
|
|
168
148
|
return instance;
|
|
169
149
|
}
|
|
170
|
-
instance = this.
|
|
171
|
-
switch (
|
|
150
|
+
instance = this.invokeToken(token, options);
|
|
151
|
+
switch (provider.scope) {
|
|
172
152
|
case ProviderScope.SINGLETON:
|
|
173
|
-
if (
|
|
174
|
-
this.registerHooks(provider,
|
|
153
|
+
if (!options.rebuild) {
|
|
154
|
+
this.registerHooks(provider, options);
|
|
175
155
|
}
|
|
176
|
-
|
|
177
|
-
set(instance);
|
|
178
|
-
// locals?.delete(DI_USE_PARAM_OPTIONS);
|
|
179
|
-
return instance;
|
|
180
|
-
}
|
|
181
|
-
// store promise to lock token in cache
|
|
182
|
-
set(instance);
|
|
183
|
-
instance = instance.then((instance) => {
|
|
184
|
-
set(instance);
|
|
185
|
-
return instance;
|
|
186
|
-
});
|
|
187
|
-
// locals?.delete(DI_USE_PARAM_OPTIONS);
|
|
188
|
-
return instance;
|
|
156
|
+
return this.setToCache(provider, instance);
|
|
189
157
|
case ProviderScope.REQUEST:
|
|
190
|
-
if (locals) {
|
|
191
|
-
locals.set(token, instance);
|
|
192
|
-
|
|
193
|
-
locals.hooks.on("$onDestroy", (...args) => provider.hooks.$onDestroy(instance, ...args));
|
|
194
|
-
}
|
|
158
|
+
if (options.locals) {
|
|
159
|
+
options.locals.set(provider.token, instance);
|
|
160
|
+
this.registerHooks(provider, options);
|
|
195
161
|
}
|
|
196
|
-
// locals?.delete(DI_USE_PARAM_OPTIONS);
|
|
197
162
|
return instance;
|
|
198
163
|
}
|
|
199
164
|
return instance;
|
|
200
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Resolve the token depending on his provider configuration.
|
|
168
|
+
*
|
|
169
|
+
* If the token isn't cached, the injector will invoke the provider and cache the result.
|
|
170
|
+
*
|
|
171
|
+
* #### Example
|
|
172
|
+
*
|
|
173
|
+
* ```typescript
|
|
174
|
+
* import {InjectorService} from "@tsed/di";
|
|
175
|
+
* import MyService from "./services.js";
|
|
176
|
+
*
|
|
177
|
+
* class OtherService {
|
|
178
|
+
* constructor(injectorService: InjectorService) {
|
|
179
|
+
* const myService = injectorService.invoke<MyService>(MyService);
|
|
180
|
+
* }
|
|
181
|
+
* }
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* @param token The injectable class to invoke. Class parameters are injected according constructor signature.
|
|
185
|
+
* @param options {InvokeOptions} Optional options to invoke the class.
|
|
186
|
+
* @returns {Type} The class constructed.
|
|
187
|
+
* @alias InjectorService.resolve
|
|
188
|
+
*/
|
|
189
|
+
invoke(token, options = {}) {
|
|
190
|
+
return this.resolve(token, options);
|
|
191
|
+
}
|
|
201
192
|
/**
|
|
202
193
|
* Build only providers which are asynchronous.
|
|
203
194
|
*/
|
|
204
195
|
async loadAsync() {
|
|
205
196
|
for (const [, provider] of this) {
|
|
206
197
|
if (!this.has(provider.token) && provider.isAsync()) {
|
|
207
|
-
await this.
|
|
198
|
+
await this.resolve(provider.token);
|
|
208
199
|
}
|
|
209
200
|
}
|
|
210
201
|
}
|
|
@@ -213,55 +204,32 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
213
204
|
*/
|
|
214
205
|
loadSync() {
|
|
215
206
|
for (const [, provider] of this) {
|
|
216
|
-
|
|
217
|
-
|
|
207
|
+
// TODO try to lazy provider instead initiate all providers (&& provider.hasRegisteredHooks())
|
|
208
|
+
if (!this.has(provider.token) && provider.scope === ProviderScope.SINGLETON) {
|
|
209
|
+
this.resolve(provider.token);
|
|
218
210
|
}
|
|
219
211
|
}
|
|
220
212
|
}
|
|
221
|
-
/**
|
|
222
|
-
* Boostrap injector from container and resolve configuration.
|
|
223
|
-
*
|
|
224
|
-
* @param container
|
|
225
|
-
*/
|
|
226
|
-
bootstrap(container = createContainer()) {
|
|
227
|
-
// Clone all providers in the container
|
|
228
|
-
this.addProviders(container);
|
|
229
|
-
// Resolve all configuration
|
|
230
|
-
this.resolveConfiguration();
|
|
231
|
-
// allow mocking or changing provider instance before loading injector
|
|
232
|
-
this.resolveImportsProviders();
|
|
233
|
-
return this;
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Load injector from a given module
|
|
237
|
-
* @param rootModule
|
|
238
|
-
*/
|
|
239
|
-
loadModule(rootModule) {
|
|
240
|
-
this.settings.routes = this.settings.routes.concat(resolveControllers(this.settings));
|
|
241
|
-
const container = createContainer();
|
|
242
|
-
container.delete(rootModule);
|
|
243
|
-
container.addProvider(rootModule, {
|
|
244
|
-
type: "server:module",
|
|
245
|
-
scope: ProviderScope.SINGLETON
|
|
246
|
-
});
|
|
247
|
-
return this.load(container);
|
|
248
|
-
}
|
|
249
213
|
/**
|
|
250
214
|
* Build all providers from given container (or GlobalProviders) and emit `$onInit` event.
|
|
251
215
|
*
|
|
252
216
|
* @param container
|
|
253
217
|
*/
|
|
254
218
|
async load(container = createContainer()) {
|
|
219
|
+
// avoid provider registration in the GlobalContainer during the loading phase
|
|
220
|
+
// using injectable() or providerBuilder()
|
|
221
|
+
this.#loaded = true;
|
|
222
|
+
await $asyncEmit("$beforeInit");
|
|
255
223
|
this.bootstrap(container);
|
|
256
224
|
// build async and sync provider
|
|
257
225
|
await this.loadAsync();
|
|
258
226
|
// load sync provider
|
|
259
227
|
this.loadSync();
|
|
260
|
-
await
|
|
261
|
-
await this.emit("$onInit");
|
|
228
|
+
await $asyncEmit("$onInit");
|
|
262
229
|
}
|
|
263
230
|
/**
|
|
264
|
-
* Load all configurations registered on providers
|
|
231
|
+
* Load all configurations registered on providers via @Configuration decorator.
|
|
232
|
+
* It inspects for each provider some fields like imports, mount, etc. to resolve the configuration.
|
|
265
233
|
*/
|
|
266
234
|
resolveConfiguration() {
|
|
267
235
|
if (this.resolvedConfiguration) {
|
|
@@ -271,15 +239,12 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
271
239
|
super.forEach((provider) => {
|
|
272
240
|
if (provider.configuration && provider.type !== "server:module") {
|
|
273
241
|
Object.entries(provider.configuration).forEach(([key, value]) => {
|
|
274
|
-
if (!
|
|
242
|
+
if (!EXCLUDED_CONFIGURATION_KEYS.includes(key)) {
|
|
275
243
|
value = mergedConfiguration.has(key) ? deepMerge(mergedConfiguration.get(key), value) : deepClone(value);
|
|
276
244
|
mergedConfiguration.set(key, value);
|
|
277
245
|
}
|
|
278
246
|
});
|
|
279
247
|
}
|
|
280
|
-
if (provider.resolvers) {
|
|
281
|
-
this.settings.resolvers = this.settings.resolvers.concat(provider.resolvers);
|
|
282
|
-
}
|
|
283
248
|
});
|
|
284
249
|
mergedConfiguration.forEach((value, key) => {
|
|
285
250
|
this.settings.set(key, deepMerge(value, this.settings.get(key)));
|
|
@@ -291,33 +256,53 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
291
256
|
* @param eventName The event name to emit at all services.
|
|
292
257
|
* @param args List of the parameters to give to each service.
|
|
293
258
|
* @returns A list of promises.
|
|
259
|
+
* @deprecated use $asyncEmit instead
|
|
294
260
|
*/
|
|
295
261
|
emit(eventName, ...args) {
|
|
296
|
-
return
|
|
262
|
+
return $asyncEmit(eventName, args);
|
|
297
263
|
}
|
|
298
264
|
/**
|
|
299
265
|
* Alter value attached to an event.
|
|
300
266
|
* @param eventName
|
|
301
267
|
* @param value
|
|
302
268
|
* @param args
|
|
269
|
+
* @deprecated use $alter instead
|
|
303
270
|
*/
|
|
304
271
|
alter(eventName, value, ...args) {
|
|
305
|
-
return
|
|
272
|
+
return $alter(eventName, value, args);
|
|
306
273
|
}
|
|
307
274
|
/**
|
|
308
275
|
* Alter value attached to an event asynchronously.
|
|
309
276
|
* @param eventName
|
|
310
277
|
* @param value
|
|
311
278
|
* @param args
|
|
279
|
+
* @deprecated use $asyncAlter instead
|
|
312
280
|
*/
|
|
313
281
|
alterAsync(eventName, value, ...args) {
|
|
314
|
-
return
|
|
282
|
+
return $asyncAlter(eventName, value, args);
|
|
315
283
|
}
|
|
316
284
|
/**
|
|
317
285
|
* Destroy the injector and all services.
|
|
318
286
|
*/
|
|
319
287
|
async destroy() {
|
|
320
|
-
await
|
|
288
|
+
await $asyncEmit("$onDestroy");
|
|
289
|
+
this.#cache.forEach((_, token) => {
|
|
290
|
+
$off(token);
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Bootstrap injector from container, resolve configuration and providers.
|
|
295
|
+
*
|
|
296
|
+
* @param container
|
|
297
|
+
*/
|
|
298
|
+
bootstrap(container = createContainer()) {
|
|
299
|
+
// Clone all providers in the container
|
|
300
|
+
this.addProviders(container);
|
|
301
|
+
// Resolve all configuration
|
|
302
|
+
this.resolveConfiguration();
|
|
303
|
+
// allow mocking or changing provider instance before loading injector
|
|
304
|
+
this.resolveImportsProviders();
|
|
305
|
+
return this;
|
|
321
306
|
}
|
|
322
307
|
ensureProvider(token, force = false) {
|
|
323
308
|
if (!this.hasProvider(token) && (GlobalProviders.has(token) || force)) {
|
|
@@ -325,9 +310,6 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
325
310
|
}
|
|
326
311
|
return this.getProvider(token);
|
|
327
312
|
}
|
|
328
|
-
getInstance(token) {
|
|
329
|
-
return this.#cache.get(token);
|
|
330
|
-
}
|
|
331
313
|
/**
|
|
332
314
|
* Invoke a class method and inject service.
|
|
333
315
|
*
|
|
@@ -341,29 +323,32 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
341
323
|
* #### Example
|
|
342
324
|
*
|
|
343
325
|
* @param target
|
|
344
|
-
* @param locals
|
|
345
326
|
* @param options
|
|
346
327
|
* @private
|
|
347
328
|
*/
|
|
348
|
-
|
|
349
|
-
const resolvedOpts = this.mapInvokeOptions(target,
|
|
329
|
+
invokeToken(target, options = {}) {
|
|
330
|
+
const resolvedOpts = this.mapInvokeOptions(target, options);
|
|
350
331
|
if (!resolvedOpts) {
|
|
351
332
|
return undefined;
|
|
352
333
|
}
|
|
353
334
|
const { token, deps, construct, imports, provider } = resolvedOpts;
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
335
|
+
$emit("$beforeInvoke", token, [resolvedOpts]);
|
|
336
|
+
$emit(`$beforeInvoke:${String(provider.type)}`, [resolvedOpts]);
|
|
357
337
|
let instance;
|
|
358
338
|
let currentDependency = false;
|
|
359
339
|
try {
|
|
360
340
|
const invokeDependency = (parent) => (token, index) => {
|
|
361
341
|
currentDependency = { token, index, deps };
|
|
362
342
|
if (isArray(token)) {
|
|
363
|
-
return this.getMany(token[0],
|
|
343
|
+
return this.getMany(token[0], options);
|
|
364
344
|
}
|
|
365
|
-
|
|
366
|
-
|
|
345
|
+
return isInheritedFrom(token, Provider, 1)
|
|
346
|
+
? provider
|
|
347
|
+
: this.resolve(token, {
|
|
348
|
+
parent,
|
|
349
|
+
locals: options.locals,
|
|
350
|
+
useOpts: provider?.getArgOpts(index) || options.useOpts
|
|
351
|
+
});
|
|
367
352
|
};
|
|
368
353
|
// Invoke manually imported providers
|
|
369
354
|
imports.forEach(invokeDependency());
|
|
@@ -380,14 +365,15 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
380
365
|
}
|
|
381
366
|
if (instance && isClass(classOf(instance))) {
|
|
382
367
|
Reflect.defineProperty(instance, DI_INVOKE_OPTIONS, {
|
|
383
|
-
get: () => ({ rebuild: options.rebuild, locals })
|
|
368
|
+
get: () => ({ rebuild: options.rebuild, locals: options.locals })
|
|
384
369
|
});
|
|
385
370
|
}
|
|
371
|
+
$emit("$afterInvoke", token, [instance, resolvedOpts]);
|
|
386
372
|
return instance;
|
|
387
373
|
}
|
|
388
374
|
resolveImportsProviders() {
|
|
389
375
|
this.settings.imports = this.settings.imports
|
|
390
|
-
?.filter((meta) => isObject(meta) && "token" in meta && meta.token !==
|
|
376
|
+
?.filter((meta) => isObject(meta) && "token" in meta && meta.token !== InjectorService)
|
|
391
377
|
.map((meta) => {
|
|
392
378
|
if (isObject(meta) && "token" in meta) {
|
|
393
379
|
const { token, ...props } = meta;
|
|
@@ -421,13 +407,13 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
421
407
|
/**
|
|
422
408
|
* Create options to invoke a provider or class.
|
|
423
409
|
* @param token
|
|
424
|
-
* @param locals
|
|
425
410
|
* @param options
|
|
426
411
|
*/
|
|
427
|
-
mapInvokeOptions(token,
|
|
412
|
+
mapInvokeOptions(token, options) {
|
|
413
|
+
const locals = options.locals || new LocalsContainer();
|
|
414
|
+
options.locals = locals;
|
|
428
415
|
let imports = options.imports;
|
|
429
416
|
let deps = options.deps;
|
|
430
|
-
let scope = options.scope;
|
|
431
417
|
let construct;
|
|
432
418
|
if (!token || token === Object) {
|
|
433
419
|
throw new Error("Given token is undefined. Could mean a circular dependency problem. Try to use @Inject(() => Token) to solve it.");
|
|
@@ -435,17 +421,10 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
435
421
|
let provider;
|
|
436
422
|
if (!this.hasProvider(token)) {
|
|
437
423
|
provider = new Provider(token);
|
|
438
|
-
this.resolvers.forEach((resolver) => {
|
|
439
|
-
const result = resolver.get(token, locals.get(DI_USE_PARAM_OPTIONS));
|
|
440
|
-
if (result !== undefined) {
|
|
441
|
-
provider.useFactory = () => result;
|
|
442
|
-
}
|
|
443
|
-
});
|
|
444
424
|
}
|
|
445
425
|
else {
|
|
446
426
|
provider = this.getProvider(token);
|
|
447
427
|
}
|
|
448
|
-
scope = scope || this.scopeOf(provider);
|
|
449
428
|
deps = deps || provider.deps;
|
|
450
429
|
imports = imports || provider.imports;
|
|
451
430
|
if (provider.useValue !== undefined) {
|
|
@@ -470,27 +449,48 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
470
449
|
}
|
|
471
450
|
return {
|
|
472
451
|
token,
|
|
473
|
-
scope: scope || Store.from(token).get("scope") || ProviderScope.SINGLETON,
|
|
474
452
|
deps: deps || [],
|
|
475
453
|
imports: imports || [],
|
|
476
454
|
construct,
|
|
477
|
-
provider
|
|
455
|
+
provider,
|
|
456
|
+
locals
|
|
478
457
|
};
|
|
479
458
|
}
|
|
480
|
-
registerHooks(provider,
|
|
459
|
+
registerHooks(provider, options) {
|
|
481
460
|
if (provider.hooks) {
|
|
461
|
+
if (provider.scope === ProviderScope.REQUEST) {
|
|
462
|
+
if (options.locals && provider.hooks?.$onDestroy) {
|
|
463
|
+
const { locals } = options;
|
|
464
|
+
options.locals.hooks.on("$onDestroy", (...args) => {
|
|
465
|
+
return provider.hooks?.$onDestroy?.(locals.get(provider.token), ...args);
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
482
470
|
Object.entries(provider.hooks).forEach(([event, cb]) => {
|
|
483
|
-
const callback = (...args) =>
|
|
484
|
-
|
|
471
|
+
const callback = (...args) => {
|
|
472
|
+
return cb(this.#cache.get(provider.token), ...args);
|
|
473
|
+
};
|
|
474
|
+
$on(event, provider.token, callback);
|
|
485
475
|
});
|
|
486
476
|
}
|
|
487
477
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
478
|
+
setToCache(provider, instance) {
|
|
479
|
+
const set = (instance) => {
|
|
480
|
+
this.#cache.set(provider.token, instance);
|
|
481
|
+
provider?.alias && this.alias(provider.token, provider.alias);
|
|
482
|
+
};
|
|
483
|
+
if ("isAsync" in provider && !provider.isAsync() && !isPromise(instance)) {
|
|
484
|
+
set(instance);
|
|
485
|
+
return instance;
|
|
486
|
+
}
|
|
487
|
+
// store promise to lock token in cache
|
|
488
|
+
set(instance);
|
|
489
|
+
instance = instance.then((instance) => {
|
|
490
|
+
set(instance);
|
|
491
|
+
return instance;
|
|
492
|
+
});
|
|
493
|
+
return instance;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
496
|
//# sourceMappingURL=InjectorService.js.map
|