feat: init project
This commit is contained in:
36
src/factory/AlveoApplication.ts
Normal file
36
src/factory/AlveoApplication.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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 onModuleInit hooks on all providers.
|
||||
* This is called automatically by AlveoFactory.create().
|
||||
*/
|
||||
public async init(): Promise<this> {
|
||||
await this.container.callLifecycleHook("onModuleInit");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully shuts down the application by calling onModuleDestroy hooks.
|
||||
*/
|
||||
public async close(): Promise<void> {
|
||||
await this.container.callLifecycleHook("onModuleDestroy");
|
||||
}
|
||||
}
|
||||
38
src/factory/AlveoFactory.ts
Normal file
38
src/factory/AlveoFactory.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Container } from "../container/Container";
|
||||
import type { Constructor } from "../types";
|
||||
import { AlveoApplication } from "./AlveoApplication";
|
||||
|
||||
/**
|
||||
* AlveoFactory is the entry point for creating Alveo application instances.
|
||||
* It handles the creation of the dependency injection container and module registration.
|
||||
*/
|
||||
export class AlveoFactoryStatic {
|
||||
/**
|
||||
* Creates an instance of an Alveo application.
|
||||
*
|
||||
* @param rootModule The root module of the application.
|
||||
* @returns A promise that resolves to an AlveoApplication instance.
|
||||
*/
|
||||
public async create(rootModule: Constructor): Promise<AlveoApplication> {
|
||||
const container = new Container();
|
||||
|
||||
// 1. Register the module tree starting from the root module
|
||||
container.registerRootModule(rootModule);
|
||||
|
||||
// 2. Resolve all providers (this builds the graph and instantiates singletons)
|
||||
await container.resolveAll();
|
||||
|
||||
// 3. Wrap in the application instance
|
||||
const app = new AlveoApplication(container);
|
||||
|
||||
// 4. Initialize lifecycle hooks
|
||||
await app.init();
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Static instance of AlveoFactory to be used by consumers.
|
||||
*/
|
||||
export const AlveoFactory = new AlveoFactoryStatic();
|
||||
Reference in New Issue
Block a user