forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnest-application-context.ts
More file actions
175 lines (154 loc) · 5.81 KB
/
Copy pathnest-application-context.ts
File metadata and controls
175 lines (154 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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 iterate from 'iterare';
import { UnknownModuleException } from './errors/exceptions/unknown-module.exception';
import { NestContainer } from './injector/container';
import { Module } from './injector/module';
import { ModuleRef } from './injector/module-ref';
import { ModuleTokenFactory } from './injector/module-token-factory';
export class NestApplicationContext extends ModuleRef
implements INestApplicationContext {
private readonly moduleTokenFactory = new ModuleTokenFactory();
constructor(
container: NestContainer,
private readonly scope: Type<any>[],
protected contextModule: Module,
) {
super(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 as any, 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 this.callModuleInitHook(module);
}
}
protected async callModuleInitHook(module: Module): Promise<any> {
const components = [...module.components];
// The Module (class) instance is the first element of the components array
// Lifecycle hook has to be called once all classes are properly initialized
const [_, { instance: moduleClassInstance }] = components.shift();
const instances = [...module.routes, ...components];
await Promise.all(
iterate(instances)
.map(([key, { instance }]) => instance)
.filter(instance => !isNil(instance))
.filter(this.hasOnModuleInitHook)
.map(async instance => await (instance as OnModuleInit).onModuleInit()),
);
if (moduleClassInstance && this.hasOnModuleInitHook(moduleClassInstance)) {
await (moduleClassInstance as OnModuleInit).onModuleInit();
}
}
protected hasOnModuleInitHook(instance: any): instance is OnModuleInit {
return !isUndefined((instance as OnModuleInit).onModuleInit);
}
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 components = [...module.components];
// The Module (class) instance is the first element of the components array
// Lifecycle hook has to be called once all classes are properly destroyed
const [_, { instance: moduleClassInstance }] = components.shift();
const instances = [...module.routes, ...components];
await Promise.all(
iterate(instances)
.map(([key, { instance }]) => instance)
.filter(instance => !isNil(instance))
.filter(this.hasOnModuleDestroyHook)
.map(
async instance =>
await (instance as OnModuleDestroy).onModuleDestroy(),
),
);
if (
moduleClassInstance &&
this.hasOnModuleDestroyHook(moduleClassInstance)
) {
await (moduleClassInstance as OnModuleDestroy).onModuleDestroy();
}
}
protected hasOnModuleDestroyHook(instance): 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 this.callModuleBootstrapHook(module);
}
}
protected async callModuleBootstrapHook(module: Module): Promise<any> {
const components = [...module.components];
const [_, { instance: moduleClassInstance }] = components.shift();
const instances = [...module.routes, ...components];
await Promise.all(
iterate(instances)
.map(([key, { instance }]) => instance)
.filter(instance => !isNil(instance))
.filter(this.hasOnAppBotstrapHook)
.map(
async instance =>
await (instance as OnApplicationBootstrap).onApplicationBootstrap(),
),
);
if (moduleClassInstance && this.hasOnAppBotstrapHook(moduleClassInstance)) {
await (moduleClassInstance as OnApplicationBootstrap).onApplicationBootstrap();
}
}
protected hasOnAppBotstrapHook(
instance: any,
): instance is OnApplicationBootstrap {
return !isUndefined(
(instance as OnApplicationBootstrap).onApplicationBootstrap,
);
}
}