Merge branch '11.0.0' into chore/express-v5

This commit is contained in:
Kamil Myśliwiec
2024-12-05 14:15:45 +01:00
5 changed files with 30 additions and 25 deletions

View File

@@ -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,
);
});
});

View File

@@ -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);
});
});

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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({