UNPKG

fluxject

Version:

A strongly-typed dependency injection library.

110 lines (109 loc) 4.56 kB
/** * Internal object used for the Host Service Provider * @template {Record<string, Types.Registration<any, any>>} TRegistrations * Registrations configured on the container. */ export class FluxjectHostServiceProvider<TRegistrations extends Record<string, Types.Registration<any, any>>> { /** * Construct a new `FluxjectHostServiceProvider` instance. * @param {TRegistrations} registrations * Registrations configured on the container. */ constructor(registrations: TRegistrations); /** * Create a new scoped service provider. All Scoped Services will * @returns {Types.Widen<FluxjectScopedServiceProvider<TRegistrations> & Types.InferInstanceTypes<TRegistrations>>} * A new Scoped Service Provider. */ createScope(): Types.Widen<FluxjectScopedServiceProvider<TRegistrations> & Types.InferInstanceTypes<TRegistrations>>; /** * Dispose of all services under this provider. * * This will also dispose of all scoped services that have been created by this provider. * * @returns {keyof {[K in keyof Types.InferInstanceTypes<TRegistrations, "singleton"|"transient"> as Types.InferInstanceTypes<TRegistrations, "singleton"|"transient">[K] extends { [Symbol.asyncDispose]: () => Promise<void> } ? K : never]: undefined} extends never ? void : Promise<void>} * Returns a Promise if any of the services have the `Symbol.asyncDispose` method defined. */ dispose(): keyof { [K in keyof Types.InferInstanceTypes<TRegistrations, "singleton" | "transient"> as Types.InferInstanceTypes<TRegistrations, "singleton" | "transient">[K] extends { [Symbol.asyncDispose]: () => Promise<void>; } ? K : never]: undefined; } extends never ? void : Promise<void>; #private; } /** * Internal object used for the Scoped Service Provider * @template {Record<string, Types.Registration<any, any>>} TRegistrations * Registrations configured on the container. */ export class FluxjectScopedServiceProvider<TRegistrations extends Record<string, Types.Registration<any, any>>> { /** * Construct a new `FluxjectScopedServiceProvider` instance. * @param {Record<string, LazyReference<any>|undefined>} references * References to Singletons and Transients from the Host Service Provider. * @param {TRegistrations} registrations * Registrations configured on the container. */ constructor(references: Record<string, LazyReference<any> | undefined>, registrations: TRegistrations); /** * Dispose of all Scoped services under this provider. * * This will also dispose of all scoped services that have been created by this provider. * * @returns {keyof {[K in keyof Types.InferInstanceTypes<TRegistrations, "scoped"> as Types.InferInstanceTypes<TRegistrations, "scoped">[K] extends { [Symbol.asyncDispose]: () => Promise<void> } ? K : never]: undefined} extends never ? void : Promise<void>} * Returns a Promise if any of the services have the `Symbol.asyncDispose` method defined. */ dispose(): keyof { [K in keyof Types.InferInstanceTypes<TRegistrations, "scoped"> as Types.InferInstanceTypes<TRegistrations, "scoped">[K] extends { [Symbol.asyncDispose]: () => Promise<void>; } ? K : never]: undefined; } extends never ? void : Promise<void>; #private; } /** * Thrown when a circular dependency is detected. * * This typically occurs when two services depend on each other inside their constructors. * * You can resolve this by deferring any de-reference of your dependencies until after the constructor has completed. * * @example * ```ts * // This would cause a circular dependency * class DependencyA { * test = 1; * constructor({ dependencyB }) { * dependencyB.test; * } * } * * class DependencyB { * test = 2; * constructor({ dependencyA }) { * dependencyA.test; * } * } * * // but this would be ok * class DependencyC { * test = 1; * #dependencyD; * constructor({ dependencyD }) { * this.#dependencyD = dependencyD; * } * } * * // You can still de-reference from one of the dependencies, though: * class DependencyD { * test = 2; * constructor({ dependencyC }) { * dependencyC.test; * } * } * ``` */ export class CircularDependencyError extends RangeError { /** * @param {string} serviceName * @param {any} stackTrace */ constructor(serviceName: string, stackTrace: any); } import type * as Types from "./types.js"; import { LazyReference } from "./lazy-reference.js";