All files registry.ts

83.33% Statements 30/36
70.96% Branches 22/31
80% Functions 4/5
87.5% Lines 28/32

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186  1x 1x                                                                                                                                                                                                               1x 3x                           24x 24x 6x       6x   24x   24x 1x   23x 23x 5x 18x                     9x   9x 9x         9x   9x 9x                   11x   11x 11x           11x 6x   11x 11x      
import { InjectableDef, InjectableOptions } from "./types";
import { InjectablesKeys } from "./constants";
import { getInjectKey } from "./utils";
 
/**
 * @description Type representing either a class constructor or an instance.
 * @summary Defines an Injectable type that can be either a class constructor or an instance of a class.
 * @template T The type of the injectable object
 * @typedef {function(any): T | T} Injectable
 * @memberOf module:injectable-decorators
 */
export type Injectable<T> = { new (...args: any[]): T } | T;
 
/**
 * @description Contract for a registry that manages injectable objects.
 * @summary Interface for an injectable registry that provides methods for retrieving, registering, and building injectable objects.
 * @template T Type parameter used in the interface methods
 * @interface InjectablesRegistry
 * @memberOf module:injectable-decorators
 */
export interface InjectablesRegistry {
  /**
   * @description Fetches an injectable instance by its registered name.
   * @summary Retrieves an {@link Injectable} from the registry by name, optionally passing constructor arguments.
   * @template T Type of the injectable object to retrieve
   * @param {symbol} name The registered name of the injectable to retrieve
   * @param {any[]} args Constructor arguments to pass when instantiating the injectable
   * @return {Injectable<T> | undefined} The injectable instance or undefined if not found
   * @memberOf module:injectable-decorators
   */
  get<T>(
    name: symbol | string | { new (...args: any[]): T },
    ...args: any[]
  ): T | undefined;
 
  /**
   * @description Adds a class or object to the injectable registry.
   * @summary Registers an injectable constructor or instance with the registry, making it available for injection.
   * @template T Type of the injectable object to register
   * @param {Injectable<T>} constructor The class constructor or object instance to register
   * @param options
   * @param {any[]} args Additional arguments for registration (category, singleton flag, etc.)
   * @return {void}
   * @memberOf module:injectable-decorators
   */
  register<T>(
    constructor: Injectable<T>,
    category: symbol | undefined,
    options: InjectableOptions<T>,
    ...args: any[]
  ): void;
 
  /**
   * @description Creates a new instance of an injectable class.
   * @summary Instantiates an injectable class using its constructor and the provided arguments.
   * @template T Type of the object to build
   * @param {symbol} name Object containing the name of the injectable to build
   * @param {any[]} args Constructor arguments to pass when instantiating the injectable
   * @return {T} The newly created instance
   * @memberOf module:injectable-decorators
   */
  build<T>(name: symbol, ...args: any[]): T;
}
 
/**
 * @description Default implementation of the InjectablesRegistry interface.
 * @summary Holds the various {@link Injectable}s in a cache and provides methods to register, retrieve, and build them.
 * @template T Type parameter used in the class methods
 *
 * @class InjectableRegistryImp
 * @implements InjectablesRegistry
 *
 * @memberOf module:injectable-decorators
 *
 * @example
 * // Create a new registry
 * const registry = new InjectableRegistryImp();
 *
 * // Register a class
 * class MyService {
 *   doSomething() {
 *     return 'Hello World';
 *   }
 * }
 * registry.register(MyService, 'MyService', true);
 *
 * // Get the instance
 * const service = registry.get('MyService');
 * service.doSomething(); // 'Hello World'
 *
 * @mermaid
 * sequenceDiagram
 *   participant Client
 *   participant Registry
 *
 *   Client->>Registry: register(MyService)
 *   Registry->>Registry: Store in cache
 *
 *   Client->>Registry: get("MyService")
 *   alt Instance exists and is singleton
 *     Registry-->>Client: Return cached instance
 *   else No instance or not singleton
 *     Registry->>Registry: build(name)
 *     Registry-->>Client: Return new instance
 *   end
 */
export class InjectableRegistryImp implements InjectablesRegistry {
  private cache: Record<symbol, InjectableDef> = {};
 
  has<T>(name: symbol | { new (...args: any[]): T }): boolean {
    Iif (typeof name === "symbol") return name in this.cache;
    return Symbol.for(name.toString()) in this.cache;
  }
 
  /**
   * @inheritDoc
   */
  get<T>(
    name: symbol | string | { new (...args: any[]): T },
    ...args: any[]
  ): T | undefined {
    if (typeof name === "string") name = Symbol.for(name);
    if (typeof name !== "symbol") {
      const meta = Reflect.getMetadata(
        getInjectKey(InjectablesKeys.INJECTABLE),
        name
      );
      name = (meta?.symbol as symbol) || Symbol.for(name.toString());
    }
    Iif (!name) throw new Error(`Injectable ${name} not found`);
 
    if (!((name as symbol) in this.cache)) {
      return undefined;
    }
    const cache = this.cache[name];
    if (!cache.options.singleton && !cache.instance)
      return this.build<T>(name, ...args);
    return cache.instance || this.build<T>(name, ...args);
  }
  /**
   * @inheritDoc
   */
  register<T>(
    obj: Injectable<T>,
    category: symbol | undefined,
    options: InjectableOptions<T>,
    force: boolean = false
  ): void {
    const castObj: Record<string, any> = obj as Record<string, any>;
 
    const constructor = !castObj.name && castObj.constructor;
    Iif (typeof castObj !== "function" && !constructor)
      throw new Error(
        `Injectable registering failed. Missing Class name or constructor`
      );
 
    const name = category || Symbol.for((obj as any).toString());
 
    if (!this.cache[name] || force)
      this.cache[name] = {
        instance: options.singleton && constructor ? obj : undefined,
        constructor: !constructor ? obj : (obj as any).constructor,
        options: options,
      };
  }
  /**
   * @inheritDoc
   */
  build<T>(name: symbol, ...args: any[]): T {
    const { constructor, options } = this.cache[name];
    let instance: T;
    try {
      instance = new constructor(...args);
    } catch (e: unknown) {
      throw new Error(
        `failed to build ${name.toString()} with args ${args}: ${e}`
      );
    }
    if (options.singleton) {
      this.cache[name].instance = instance;
    }
    if (options.callback) instance = options.callback(instance, ...args);
    return instance;
  }
}