Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 1x 1x 1x 1x 6x 1x 6x 1x 2x 1x 3x 3x 1x 2x 2x 2x | import { ImplementsStatic } from './helper/ImplementsStatic';
import { DependencyResolutionError } from './interfaces/Exceptions';
import { IDependency } from './interfaces/IDependency';
import { ITSinjex, ITSinjex_ } from './interfaces/ITSinjex';
/**
* **TSInjex**: Dependency Injection Container
*/
@ImplementsStatic<ITSinjex_>()
export class TSinjex implements ITSinjex {
private static _instance: TSinjex;
private readonly _dependencies = new Map<string, IDependency>();
/**
* Private constructor to prevent direct instantiation.
*/
private constructor() {}
//#region IDIContainer_
/**
* Retrieves the singleton instance of DependencyRegistry.
* @returns The singleton instance.
*/
public static getInstance(): ITSinjex {
if (this._instance == null) {
this._instance = new TSinjex();
}
return this._instance;
}
/**
* @inheritdoc
* @see {@link ITSinjex.register}
*/
public static register<T>(
identifier: string,
dependency: T,
deprecated = false,
): void {
(TSinjex.getInstance() as TSinjex)._dependencies.set(identifier, {
dependency: dependency,
deprecated: deprecated,
});
}
/**
* @inheritdoc
* @see {@link ITSinjex.resolve}
*/
public static resolve<T>(
identifier: string,
necessary = true,
): T | undefined {
return (TSinjex.getInstance() as TSinjex).resolve<T>(
identifier,
necessary,
);
}
//#endregion
//#region IDIContainer
/**
* @inheritdoc
*/
public register<T>(
identifier: string,
dependency: T,
deprecated = false,
): void {
this._dependencies.set(identifier, {
dependency: dependency,
deprecated: deprecated,
});
}
/**
* @inheritdoc
*/
public resolve<T>(identifier: string, necessary = true): T | undefined {
const dependency = this._dependencies.get(identifier);
if (necessary && !dependency) {
throw new DependencyResolutionError(identifier);
} else Iif (!dependency) {
return undefined;
}
Iif (dependency.deprecated) {
// eslint-disable-next-line no-console
console.warn(`Dependency ${identifier} is deprecated`);
// Remove the deprecation warning; it should only be logged once.
dependency.deprecated = false;
}
return dependency.dependency as T;
}
//#endregion
}
|