Skip to content

Commit f973f38

Browse files
Merge branch 'master' of https://github.com/peawyoyoyin/nest into peawyoyoyin-master
2 parents afaf23e + 26800a1 commit f973f38

8 files changed

Lines changed: 21 additions & 5 deletions

File tree

packages/core/hooks/before-app-shutdown.hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function callBeforeAppShutdownHook(
5050
module: Module,
5151
signal?: string,
5252
): Promise<void> {
53-
const providers = [...module.providers];
53+
const providers = [...module.getNonAliasProviders()];
5454
const [_, { instance: moduleClassInstance }] = providers.shift();
5555
const instances = [...module.controllers, ...providers];
5656

packages/core/hooks/on-app-bootstrap.hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function callOperator(instances: InstanceWrapper[]): Promise<any>[] {
3939
* @param module The module which will be initialized
4040
*/
4141
export async function callModuleBootstrapHook(module: Module): Promise<any> {
42-
const providers = [...module.providers];
42+
const providers = [...module.getNonAliasProviders()];
4343
// Module (class) instance is the first element of the providers array
4444
// Lifecycle hook has to be called once all classes are properly initialized
4545
const [_, { instance: moduleClassInstance }] = providers.shift();

packages/core/hooks/on-app-shutdown.hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export async function callAppShutdownHook(
4747
module: Module,
4848
signal?: string,
4949
): Promise<any> {
50-
const providers = [...module.providers];
50+
const providers = [...module.getNonAliasProviders()];
5151
// Module (class) instance is the first element of the providers array
5252
// Lifecycle hook has to be called once all classes are properly initialized
5353
const [_, { instance: moduleClassInstance }] = providers.shift();

packages/core/hooks/on-module-destroy.hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function callOperator(instances: InstanceWrapper[]): Promise<any>[] {
3939
* @param module The module which will be initialized
4040
*/
4141
export async function callModuleDestroyHook(module: Module): Promise<any> {
42-
const providers = [...module.providers];
42+
const providers = [...module.getNonAliasProviders()];
4343
// Module (class) instance is the first element of the providers array
4444
// Lifecycle hook has to be called once all classes are properly destroyed
4545
const [_, { instance: moduleClassInstance }] = providers.shift();

packages/core/hooks/on-module-init.hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function callOperator(instances: InstanceWrapper[]): Promise<any>[] {
3535
* @param module The module which will be initialized
3636
*/
3737
export async function callModuleInitHook(module: Module): Promise<void> {
38-
const providers = [...module.providers];
38+
const providers = [...module.getNonAliasProviders()];
3939
// Module (class) instance is the first element of the providers array
4040
// Lifecycle hook has to be called once all classes are properly initialized
4141
const [_, { instance: moduleClassInstance }] = providers.shift();

packages/core/injector/instance-wrapper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export class InstanceWrapper<T = any> {
4242
public inject?: (string | symbol | Function | Type<any>)[];
4343
public forwardRef?: boolean;
4444

45+
public isAlias: Boolean = false;
46+
4547
private readonly values = new WeakMap<ContextId, InstancePerContext<T>>();
4648
private readonly [INSTANCE_METADATA_SYMBOL]: InstanceMetadataStore = {};
4749
private readonly [INSTANCE_ID_SYMBOL]: string;

packages/core/injector/module.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ export class Module {
351351
isResolved: false,
352352
inject: [useExisting],
353353
host: this,
354+
isAlias: true,
354355
}),
355356
);
356357
}
@@ -472,6 +473,18 @@ export class Module {
472473
return this._providers.get(name) as InstanceWrapper<T>;
473474
}
474475

476+
public getNonAliasProviders(): Map<string, InstanceWrapper<Injectable>> {
477+
const result = new Map<string, InstanceWrapper<Injectable>>();
478+
479+
this._providers.forEach((wrapper, key) => {
480+
if (!wrapper.isAlias) {
481+
result.set(key, wrapper);
482+
}
483+
});
484+
485+
return result;
486+
}
487+
475488
public createModuleReferenceType(): any {
476489
const self = this;
477490
return class extends ModuleRef {

packages/core/test/injector/module.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ describe('Module', () => {
262262
instance: null,
263263
inject: [provider.useExisting as any],
264264
isResolved: false,
265+
isAlias: true,
265266
}),
266267
),
267268
).to.be.true;

0 commit comments

Comments
 (0)