feat(@nestjs/core): Add init function on NestApplicationContext to API

This commit is contained in:
Livio
2019-03-01 20:16:15 +01:00
parent d8c7ba3285
commit 6c5108bf1e
4 changed files with 20 additions and 2 deletions

View File

@@ -29,4 +29,12 @@ export interface INestApplicationContext {
* @returns {void}
*/
useLogger(logger: LoggerService);
/**
* Initalizes the Nest application.
* Calls the Nest lifecycle events.
*
* @returns {Promise<this>} The NestApplicationContext instance as Promise
*/
init(): Promise<this>;
}

View File

@@ -19,6 +19,7 @@ import { ModuleTokenFactory } from './injector/module-token-factory';
export class NestApplicationContext implements INestApplicationContext {
private readonly moduleTokenFactory = new ModuleTokenFactory();
private readonly containerScanner: ContainerScanner;
protected isInitialized: boolean = false;
constructor(
protected readonly container: NestContainer,
@@ -59,9 +60,20 @@ export class NestApplicationContext implements INestApplicationContext {
);
}
/**
* Initalizes the Nest application.
* Calls the Nest lifecycle events.
*
* @returns {Promise<this>} The NestApplicationContext instance as Promise
*/
public async init(): Promise<this> {
// Ignore if is already initialized
if (this.isInitialized) return;
await this.callInitHook();
await this.callBootstrapHook();
this.isInitialized = true;
return this;
}

View File

@@ -59,7 +59,6 @@ export class NestApplication extends NestApplicationContext
private readonly routesResolver: Resolver;
private readonly microservices = [];
private httpServer: http.Server;
private isInitialized = false;
constructor(
container: NestContainer,

View File

@@ -32,7 +32,6 @@ export class NestMicroservice extends NestApplicationContext
private microserviceConfig: MicroserviceOptions;
private server: Server & CustomTransportStrategy;
private isTerminated = false;
private isInitialized = false;
private isInitHookCalled = false;
constructor(