Skip to content

Commit

Permalink
fix(lifecycle-hooks): only call hooks once
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Oct 13, 2022
1 parent 0918ffd commit e298ac8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
24 changes: 12 additions & 12 deletions spec/lifecycle-hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import { Controller } from '../src/router/controller/decorator.ts';
Deno.test('Lifecycle hooks', async (testContext) => {
@Injectable({ scope: SCOPE.GLOBAL })
class InjectableWithHook implements OnAppBootstrap, OnAppClose {
public appBoostrapCalled = false;
public appCloseCalled = false;
public appBoostrapCalled = 0;
public appCloseCalled = 0;
onAppBootstrap() {
this.appBoostrapCalled = true;
this.appBoostrapCalled += 1;
}
onAppClose() {
this.appCloseCalled = true;
this.appCloseCalled += 1;
}
}

@Controller('second-controller/')
class ControllerWithHook implements OnAppBootstrap, OnAppClose {
public appBoostrapCalled = false;
public appCloseCalled = false;
public appBoostrapCalled = 0;
public appCloseCalled = 0;
onAppBootstrap(): void | Promise<void> {
this.appBoostrapCalled = true;
this.appBoostrapCalled += 1;
}
onAppClose() {
this.appCloseCalled = true;
this.appCloseCalled += 1;
}

constructor(
Expand All @@ -50,15 +50,15 @@ Deno.test('Lifecycle hooks', async (testContext) => {
'call global injectables onAppBootstrap hook',
async () => {
const injectableWithHook = await app.get(InjectableWithHook);
assertEquals(injectableWithHook.appBoostrapCalled, true);
assertEquals(injectableWithHook.appBoostrapCalled, 1);
},
);

await testContext.step(
'call global controller onAppBootstrap hook',
async () => {
const controllerWithHook = await app.get(ControllerWithHook);
assertEquals(controllerWithHook.appBoostrapCalled, true);
assertEquals(controllerWithHook.appBoostrapCalled, 1);
},
);

Expand All @@ -68,8 +68,8 @@ Deno.test('Lifecycle hooks', async (testContext) => {
await app.close();
const injectableWithHook = await app.get(ControllerWithHook);
const controllerWithHook = await app.get(InjectableWithHook);
assertEquals(controllerWithHook.appCloseCalled, true);
assertEquals(injectableWithHook.appCloseCalled, true);
assertEquals(controllerWithHook.appCloseCalled, 1);
assertEquals(injectableWithHook.appCloseCalled, 1);
},
);
});
1 change: 0 additions & 1 deletion src/injector/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export class Injector {
if (controllers) {
await this.resolveControllers(controllers);
}
await this.executeOnAppBoostrapHook(controllers, injectables);
}

public addAvailableInjectable(injectables: InjectableConstructor[]) {
Expand Down

0 comments on commit e298ac8

Please sign in to comment.