Files
core/src/factory/AlveoApplication.ts
M1000fr 0c166ab54c feat: implement full lifecycle hooks
Adds onApplicationBootstrap, beforeApplicationShutdown, and onApplicationShutdown hooks with signal support.
2026-01-11 17:20:14 +01:00

45 lines
1.4 KiB
TypeScript

import type { Container } from "../container/Container";
import type { ProviderToken } from "../container/types";
/**
* AlveoApplication is the main class representing an Alveo application instance.
* It provides methods to interact with the dependency injection container and manage the application lifecycle.
*/
export class AlveoApplication {
constructor(private readonly container: Container) {}
/**
* Retrieves an instance of a provider from the application container.
*
* @param token The provider token (Class, String, or Symbol)
* @returns A promise resolving to the instance of the provider
*/
public async get<T>(token: ProviderToken<T>): Promise<T> {
return this.container.get(token);
}
/**
* Initializes the application by calling lifecycle hooks on all providers.
* This is called automatically by AlveoFactory.create().
*/
public async init(): Promise<this> {
await this.container.callLifecycleHook("onModuleInit");
await this.container.callLifecycleHook("onApplicationBootstrap");
return this;
}
/**
* Gracefully shuts down the application by calling lifecycle hooks.
*
* @param signal The signal that triggered the shutdown
*/
public async close(signal?: string): Promise<void> {
await this.container.callLifecycleHook("onModuleDestroy", signal);
await this.container.callLifecycleHook(
"beforeApplicationShutdown",
signal,
);
await this.container.callLifecycleHook("onApplicationShutdown", signal);
}
}