First check-in of the code from the Obsidian Prj project.
This commit is contained in:
28
src/decorators/Register.ts
Normal file
28
src/decorators/Register.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { DIContainer } from '../DIContainer';
|
||||
|
||||
/**
|
||||
* A decorator to register a class in the DI (Dependency Injection) container.
|
||||
* @template TargetType The type of the class to be registered.
|
||||
* @param identifier The identifier used to register the class in the DI container.
|
||||
* @param deprecated If true, the dependency is deprecated => a warning
|
||||
* is logged when the dependency is resolved.
|
||||
* @returns A function that is applied as a decorator to the class.
|
||||
* @example
|
||||
* ```ts
|
||||
* \@Register('MyClassIdentifier')
|
||||
* class MyClass {
|
||||
* // ...
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function Register<
|
||||
TargetType extends new (...args: unknown[]) => InstanceType<TargetType>,
|
||||
>(identifier: string, deprecated?: boolean) {
|
||||
return function (constructor: TargetType, ...args: unknown[]): void {
|
||||
// Get the instance of the DI container
|
||||
const diContainer = DIContainer.getInstance();
|
||||
|
||||
// Register the class in the DI container
|
||||
diContainer.register(identifier, constructor, deprecated);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user