mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
152 lines
5.0 KiB
TypeScript
152 lines
5.0 KiB
TypeScript
import {
|
|
INestApplicationContext,
|
|
Logger,
|
|
LoggerService,
|
|
OnApplicationBootstrap,
|
|
OnModuleDestroy,
|
|
OnModuleInit,
|
|
} from '@nestjs/common';
|
|
import { Type } from '@nestjs/common/interfaces/type.interface';
|
|
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
|
|
import { InstanceWrapper } from 'injector/instance-wrapper';
|
|
import iterate from 'iterare';
|
|
import { UnknownModuleException } from './errors/exceptions/unknown-module.exception';
|
|
import { NestContainer } from './injector/container';
|
|
import { ContainerScanner } from './injector/container-scanner';
|
|
import { Module } from './injector/module';
|
|
import { ModuleTokenFactory } from './injector/module-token-factory';
|
|
import { callModuleInitHook } from './hooks/on-module-init.hook';
|
|
import { callModuleBootstrapHook } from 'hooks/on-app-bootstrap.hook';
|
|
|
|
export class NestApplicationContext implements INestApplicationContext {
|
|
private readonly moduleTokenFactory = new ModuleTokenFactory();
|
|
private readonly containerScanner: ContainerScanner;
|
|
|
|
constructor(
|
|
protected readonly container: NestContainer,
|
|
private readonly scope: Type<any>[] = [],
|
|
private contextModule: Module = null,
|
|
) {
|
|
this.containerScanner = new ContainerScanner(container);
|
|
}
|
|
|
|
public selectContextModule() {
|
|
const modules = this.container.getModules().values();
|
|
this.contextModule = modules.next().value;
|
|
}
|
|
|
|
public select<T>(module: Type<T>): INestApplicationContext {
|
|
const modules = this.container.getModules();
|
|
const moduleMetatype = this.contextModule.metatype;
|
|
const scope = this.scope.concat(moduleMetatype);
|
|
|
|
const token = this.moduleTokenFactory.create(module, scope);
|
|
const selectedModule = modules.get(token);
|
|
if (!selectedModule) {
|
|
throw new UnknownModuleException();
|
|
}
|
|
return new NestApplicationContext(this.container, scope, selectedModule);
|
|
}
|
|
|
|
public get<TInput = any, TResult = TInput>(
|
|
typeOrToken: Type<TInput> | string | symbol,
|
|
options: { strict: boolean } = { strict: false },
|
|
): TResult {
|
|
if (!(options && options.strict)) {
|
|
return this.find<TInput, TResult>(typeOrToken);
|
|
}
|
|
return this.findInstanceByPrototypeOrToken<TInput, TResult>(
|
|
typeOrToken,
|
|
this.contextModule,
|
|
);
|
|
}
|
|
|
|
public async init(): Promise<this> {
|
|
await this.callInitHook();
|
|
await this.callBootstrapHook();
|
|
return this;
|
|
}
|
|
|
|
public async close(): Promise<void> {
|
|
await this.callDestroyHook();
|
|
}
|
|
|
|
public useLogger(logger: LoggerService) {
|
|
Logger.overrideLogger(logger);
|
|
}
|
|
|
|
protected async callInitHook(): Promise<any> {
|
|
const modulesContainer = this.container.getModules();
|
|
for (const module of [...modulesContainer.values()].reverse()) {
|
|
await callModuleInitHook(module);
|
|
}
|
|
}
|
|
|
|
protected async callDestroyHook(): Promise<any> {
|
|
const modulesContainer = this.container.getModules();
|
|
for (const module of modulesContainer.values()) {
|
|
await this.callModuleDestroyHook(module);
|
|
}
|
|
}
|
|
|
|
protected async callModuleDestroyHook(module: Module): Promise<any> {
|
|
const providers = [...module.providers];
|
|
// 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();
|
|
const instances = [...module.controllers, ...providers];
|
|
const callOperator = (list: any) =>
|
|
list
|
|
.filter(instance => !isNil(instance))
|
|
.filter(this.hasOnModuleDestroyHook)
|
|
.map(async instance => (instance as OnModuleDestroy).onModuleDestroy());
|
|
|
|
await Promise.all(
|
|
callOperator(
|
|
iterate(instances)
|
|
.filter(
|
|
([key, wrapper]) =>
|
|
wrapper.isDependencyTreeStatic() && !wrapper.isTransient,
|
|
)
|
|
.map(([key, { instance }]) => instance),
|
|
),
|
|
);
|
|
const transientInstances = this.getTransientInstances(instances);
|
|
await Promise.all(callOperator(iterate(transientInstances)));
|
|
|
|
if (
|
|
moduleClassInstance &&
|
|
this.hasOnModuleDestroyHook(moduleClassInstance)
|
|
) {
|
|
await (moduleClassInstance as OnModuleDestroy).onModuleDestroy();
|
|
}
|
|
}
|
|
|
|
protected hasOnModuleDestroyHook(instance: any): instance is OnModuleDestroy {
|
|
return !isUndefined((instance as OnModuleDestroy).onModuleDestroy);
|
|
}
|
|
|
|
protected async callBootstrapHook(): Promise<any> {
|
|
const modulesContainer = this.container.getModules();
|
|
for (const module of [...modulesContainer.values()].reverse()) {
|
|
await callModuleBootstrapHook(module);
|
|
}
|
|
}
|
|
|
|
protected find<TInput = any, TResult = TInput>(
|
|
typeOrToken: Type<TInput> | string | symbol,
|
|
): TResult {
|
|
return this.containerScanner.find<TInput, TResult>(typeOrToken);
|
|
}
|
|
|
|
protected findInstanceByPrototypeOrToken<TInput = any, TResult = TInput>(
|
|
metatypeOrToken: Type<TInput> | string | symbol,
|
|
contextModule: Partial<Module>,
|
|
): TResult {
|
|
return this.containerScanner.findInstanceByPrototypeOrToken<
|
|
TInput,
|
|
TResult
|
|
>(metatypeOrToken, contextModule);
|
|
}
|
|
}
|