Merge branch 'master' of https://github.com/peawyoyoyin/nest into peawyoyoyin-master

This commit is contained in:
Kamil Myśliwiec
2019-12-27 12:59:46 +01:00
8 changed files with 21 additions and 5 deletions

View File

@@ -50,7 +50,7 @@ export async function callBeforeAppShutdownHook(
module: Module,
signal?: string,
): Promise<void> {
const providers = [...module.providers];
const providers = [...module.getNonAliasProviders()];
const [_, { instance: moduleClassInstance }] = providers.shift();
const instances = [...module.controllers, ...providers];

View File

@@ -39,7 +39,7 @@ function callOperator(instances: InstanceWrapper[]): Promise<any>[] {
* @param module The module which will be initialized
*/
export async function callModuleBootstrapHook(module: Module): Promise<any> {
const providers = [...module.providers];
const providers = [...module.getNonAliasProviders()];
// Module (class) instance is the first element of the providers array
// Lifecycle hook has to be called once all classes are properly initialized
const [_, { instance: moduleClassInstance }] = providers.shift();

View File

@@ -47,7 +47,7 @@ export async function callAppShutdownHook(
module: Module,
signal?: string,
): Promise<any> {
const providers = [...module.providers];
const providers = [...module.getNonAliasProviders()];
// Module (class) instance is the first element of the providers array
// Lifecycle hook has to be called once all classes are properly initialized
const [_, { instance: moduleClassInstance }] = providers.shift();

View File

@@ -39,7 +39,7 @@ function callOperator(instances: InstanceWrapper[]): Promise<any>[] {
* @param module The module which will be initialized
*/
export async function callModuleDestroyHook(module: Module): Promise<any> {
const providers = [...module.providers];
const providers = [...module.getNonAliasProviders()];
// Module (class) instance is the first element of the providers array
// Lifecycle hook has to be called once all classes are properly destroyed
const [_, { instance: moduleClassInstance }] = providers.shift();

View File

@@ -35,7 +35,7 @@ function callOperator(instances: InstanceWrapper[]): Promise<any>[] {
* @param module The module which will be initialized
*/
export async function callModuleInitHook(module: Module): Promise<void> {
const providers = [...module.providers];
const providers = [...module.getNonAliasProviders()];
// Module (class) instance is the first element of the providers array
// Lifecycle hook has to be called once all classes are properly initialized
const [_, { instance: moduleClassInstance }] = providers.shift();

View File

@@ -42,6 +42,8 @@ export class InstanceWrapper<T = any> {
public inject?: (string | symbol | Function | Type<any>)[];
public forwardRef?: boolean;
public isAlias: Boolean = false;
private readonly values = new WeakMap<ContextId, InstancePerContext<T>>();
private readonly [INSTANCE_METADATA_SYMBOL]: InstanceMetadataStore = {};
private readonly [INSTANCE_ID_SYMBOL]: string;

View File

@@ -351,6 +351,7 @@ export class Module {
isResolved: false,
inject: [useExisting],
host: this,
isAlias: true,
}),
);
}
@@ -472,6 +473,18 @@ export class Module {
return this._providers.get(name) as InstanceWrapper<T>;
}
public getNonAliasProviders(): Map<string, InstanceWrapper<Injectable>> {
const result = new Map<string, InstanceWrapper<Injectable>>();
this._providers.forEach((wrapper, key) => {
if (!wrapper.isAlias) {
result.set(key, wrapper);
}
});
return result;
}
public createModuleReferenceType(): any {
const self = this;
return class extends ModuleRef {

View File

@@ -262,6 +262,7 @@ describe('Module', () => {
instance: null,
inject: [provider.useExisting as any],
isResolved: false,
isAlias: true,
}),
),
).to.be.true;