mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Merge branch '11.0.0' into chore/express-v5
This commit is contained in:
@@ -23,10 +23,7 @@ describe('BeforeApplicationShutdown', () => {
|
||||
it('should sort modules by distance (topological sort) - DESC order', async () => {
|
||||
@Injectable()
|
||||
class BB implements BeforeApplicationShutdown {
|
||||
public field: string;
|
||||
async beforeApplicationShutdown() {
|
||||
this.field = 'b-field';
|
||||
}
|
||||
beforeApplicationShutdown = Sinon.spy();
|
||||
}
|
||||
|
||||
@Module({
|
||||
@@ -37,12 +34,8 @@ describe('BeforeApplicationShutdown', () => {
|
||||
|
||||
@Injectable()
|
||||
class AA implements BeforeApplicationShutdown {
|
||||
public field: string;
|
||||
constructor(private bb: BB) {}
|
||||
|
||||
async beforeApplicationShutdown() {
|
||||
this.field = this.bb.field + '_a-field';
|
||||
}
|
||||
beforeApplicationShutdown = Sinon.spy();
|
||||
}
|
||||
@Module({
|
||||
imports: [B],
|
||||
@@ -58,7 +51,11 @@ describe('BeforeApplicationShutdown', () => {
|
||||
await app.init();
|
||||
await app.close();
|
||||
|
||||
const instance = module.get(AA);
|
||||
expect(instance.field).to.equal('b-field_a-field');
|
||||
const aa = module.get(AA);
|
||||
const bb = module.get(BB);
|
||||
Sinon.assert.callOrder(
|
||||
aa.beforeApplicationShutdown,
|
||||
bb.beforeApplicationShutdown,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,10 +23,7 @@ describe('OnApplicationShutdown', () => {
|
||||
it('should sort modules by distance (topological sort) - DESC order', async () => {
|
||||
@Injectable()
|
||||
class BB implements OnApplicationShutdown {
|
||||
public field: string;
|
||||
async onApplicationShutdown() {
|
||||
this.field = 'b-field';
|
||||
}
|
||||
onApplicationShutdown = Sinon.spy();
|
||||
}
|
||||
|
||||
@Module({
|
||||
@@ -37,12 +34,8 @@ describe('OnApplicationShutdown', () => {
|
||||
|
||||
@Injectable()
|
||||
class AA implements OnApplicationShutdown {
|
||||
public field: string;
|
||||
constructor(private bb: BB) {}
|
||||
|
||||
async onApplicationShutdown() {
|
||||
this.field = this.bb.field + '_a-field';
|
||||
}
|
||||
onApplicationShutdown = Sinon.spy();
|
||||
}
|
||||
@Module({
|
||||
imports: [B],
|
||||
@@ -58,7 +51,8 @@ describe('OnApplicationShutdown', () => {
|
||||
await app.init();
|
||||
await app.close();
|
||||
|
||||
const instance = module.get(AA);
|
||||
expect(instance.field).to.equal('b-field_a-field');
|
||||
const aa = module.get(AA);
|
||||
const bb = module.get(BB);
|
||||
Sinon.assert.callOrder(aa.onApplicationShutdown, bb.onApplicationShutdown);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -170,6 +170,10 @@ export class NestContainer {
|
||||
|
||||
if (this.isGlobalModule(type, dynamicMetadata)) {
|
||||
moduleRef.isGlobal = true;
|
||||
|
||||
// Set global module distance to -1 to ensure their lifecycle hooks
|
||||
// are always executed first (when initializing the application)
|
||||
moduleRef.distance = -1;
|
||||
this.addGlobalModule(moduleRef);
|
||||
}
|
||||
|
||||
|
||||
@@ -439,7 +439,10 @@ export class NestApplicationContext<
|
||||
* modules and children.
|
||||
*/
|
||||
protected async callShutdownHook(signal?: string): Promise<void> {
|
||||
const modulesSortedByDistance = this.getModulesToTriggerHooksOn();
|
||||
const modulesSortedByDistance = [
|
||||
...this.getModulesToTriggerHooksOn(),
|
||||
].reverse();
|
||||
|
||||
for (const module of modulesSortedByDistance) {
|
||||
await callAppShutdownHook(module, signal);
|
||||
}
|
||||
@@ -450,7 +453,10 @@ export class NestApplicationContext<
|
||||
* modules and children.
|
||||
*/
|
||||
protected async callBeforeShutdownHook(signal?: string): Promise<void> {
|
||||
const modulesSortedByDistance = this.getModulesToTriggerHooksOn();
|
||||
const modulesSortedByDistance = [
|
||||
...this.getModulesToTriggerHooksOn(),
|
||||
].reverse();
|
||||
|
||||
for (const module of modulesSortedByDistance) {
|
||||
await callBeforeAppShutdownHook(module, signal);
|
||||
}
|
||||
|
||||
@@ -93,9 +93,13 @@ export class DependenciesScanner {
|
||||
});
|
||||
await this.scanModulesForDependencies();
|
||||
this.addScopedEnhancersMetadata();
|
||||
this.container.bindGlobalScope();
|
||||
|
||||
// Modules distance calculation should be done after all modules are scanned
|
||||
// but before global modules are registered (linked to all modules).
|
||||
// Global modules have their distance set to -1 anyway.
|
||||
this.calculateModulesDistance();
|
||||
|
||||
this.container.bindGlobalScope();
|
||||
}
|
||||
|
||||
public async scanForModules({
|
||||
|
||||
Reference in New Issue
Block a user