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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 3x 3x 3x 22x 45x 22x 45x 2x 4x 17x 24x 24x 2x 22x 1x 21x 1x 1x 21x | import type { Inject } from '../decorators/Inject'; import type { Register } from '../decorators/Register'; import type { RegisterInstance } from '../decorators/RegisterInstance'; import type { register } from '../functions/register'; import type { resolve } from '../functions/resolve'; import { ImplementsStatic } from '../helper/ImplementsStatic'; import { DependencyResolutionError } from '../interfaces/Exceptions'; import { IDependency } from '../interfaces/IDependency'; import { ITSinjex, ITSinjex_ } from '../interfaces/ITSinjex'; import { Identifier } from '../types/Identifier'; /** * # TSinjex * The main class for the Dependency Injection Container **TSinjex**. * ### Decorators * @see {@link Register} for registering a class in the DI container. * @see {@link RegisterInstance} for registering an instance in the DI container. * @see {@link Inject} for injecting a dependency into a property. * --- * ### Functions * @see {@link register} for registering a dependency (class or instance) as a function. * @see {@link resolve} for resolving a dependency as a function. */ @ImplementsStatic<ITSinjex_>() export class TSinjex implements ITSinjex { /** * The singleton instance of the TSinjex class. */ private static _instance: TSinjex; /** * The dependencies map. */ private readonly _dependencies = new Map<Identifier, IDependency>(); /** * Private constructor to prevent direct instantiation. */ private constructor() {} //#region ITSinjex_ (Static) /** * Get the **singleton** TSInjex instance. * @returns The singleton instance. */ public static getInstance(): ITSinjex { if (this._instance == null) { this._instance = new TSinjex(); } return this._instance; } /** * Static implementation of {@link ITSinjex.register}. * @see {@link ITSinjex.register} * @inheritdoc */ public static register<T>( identifier: Identifier, dependency: T, deprecated = false, ): void { (TSinjex.getInstance() as TSinjex)._dependencies.set(identifier, { dependency: dependency, deprecated: deprecated, }); } /** * Static implementation of {@link ITSinjex.resolve}. * @see {@link ITSinjex.resolve} * @inheritdoc */ public static resolve<T>( identifier: Identifier, necessary = true, ): T | undefined { return (TSinjex.getInstance() as TSinjex).resolve<T>( identifier, necessary, ); } //#endregion //#region ITSinjex (Instance) /** * @inheritdoc */ public register<T>( identifier: Identifier, dependency: T, deprecated = false, ): void { this._dependencies.set(identifier, { dependency: dependency, deprecated: deprecated, }); } /** * @inheritdoc */ public resolve<T>(identifier: Identifier, necessary = true): T | undefined { const dependency = this._dependencies.get(identifier); if (necessary && !dependency) { throw new DependencyResolutionError(identifier); } else if (!dependency) { return undefined; } if (dependency.deprecated) { console.warn(`Dependency ${identifier.toString()} is deprecated`); // Remove the deprecation warning; it should only be logged once. dependency.deprecated = false; } return dependency.dependency as T; } //#endregion } |