/** * Type representing a function that returns an injected value. * @template T - The type of the injected value. * @param instance - Optional instance context for the injected value. * @returns The injected value of type T. */ export type Injected = (instance?: unknown) => T; /** * Type representing an injectable function. * @template T - The return type of the function. * @template TArgs - The argument types of the function. */ export type Injectable = (...args: TArgs) => T; /** * Type representing an injectable constructor. * @template T - The instance type created by the constructor. * @template TArgs - The argument types passed to the constructor. */ export type InjectableConstructor = new (...args: TArgs) => T; /** * Type representing an injectable function with a typed 'this' context. * @template T - The return type of the function. * @template U - The type of the 'this' context. * @template TArgs - The argument types of the function. */ export type InjectableWithTypedThis = (this: U, ...args: TArgs) => T; /** * Type representing an injectable asynchronous function. * @template T - The resolved type of the promise. * @template TArgs - The argument types of the function. */ export type InjectableAsync = Injectable, TArgs>; /** * Type representing an injectable asynchronous function with a typed 'this' context. * @template T - The resolved type of the promise. * @template U - The type of the 'this' context. * @template TArgs - The argument types of the function. */ export type InjectableAsyncWithTypedThis = (this: U, ...args: TArgs) => PromiseLike; /** * Type representing a parameter with its index in the argument list. * @template T - The type of the parameter value. */ export type InjectedParameter = { index: number; value: T; }; /** * Type representing a parameter map for dependency injection. * @template T - The type of the object being mapped. */ export type InjectMap = T extends object ? { [key in keyof T]: Resolvable; } : never; /** * Type representing a resolvable parameter value. * @template T - The type of the resolved value. */ type SimpleResolvable = InjectMap | string | symbol; export type ResolvableArray = SimpleResolvable[] | [ICustomResolver, ...SimpleResolvable[]]; export type Resolvable = ResolvableArray | SimpleResolvable; export declare const injectorLog: import("../logging/index.browser.js").LoggerWrapper; /** * Converts a constructor to a function that creates new instances. * @template T - The parameter types of the constructor. * @template TResult - The instance type created by the constructor. * @param {new (...args: T) => TResult} ctor - The constructor to convert. * @returns {(...parameters: T) => TResult} A function that creates new instances of the constructor. */ export declare function ctorToFunction(ctor: new (...args: T) => TResult): (...parameters: T) => TResult; export declare const customResolve: unique symbol; export interface ICustomResolver { [customResolve](param: Resolvable): T; } export declare function isCustomResolver(x: unknown): x is ICustomResolver; export type SpecificInjector = Injector & { resolve(param: Resolvable): T; }; /** * The `Injector` abstract class provides a framework for dependency injection, * allowing the resolution and injection of parameters, functions, and constructors. * It supports synchronous and asynchronous resolution of dependencies, as well as * advanced features like nested property resolution and fallback mechanisms. * * Key Features: * - Resolves parameters and injects them into functions or constructors. * - Supports both synchronous and asynchronous dependency resolution. * - Handles nested property resolution and fallback mechanisms for unresolved keys. * - Provides utilities for merging arguments, collecting parameter maps, and applying them. * - Allows injection of new instances of constructors or asynchronous functions. * * Usage: * Extend this class to implement custom dependency injection logic by overriding * the abstract methods `resolve`, `onResolve`, and `inspect`. * * Example: * ```typescript * class MyInjector extends Injector { * resolve(param: Resolvable): T { * // Custom resolution logic * } * onResolve(name: Resolvable): PromiseLike { * // Custom asynchronous resolution logic * } * inspect(): void { * // Custom inspection logic * } * } * ``` * * @template T - The type of the resolved value. */ export declare abstract class Injector implements ICustomResolver { static readonly customResolve: symbol; [customResolve](param: Resolvable): T; /** * Applies a collected map to resolved values. * @param {InjectMap} param - The parameter map. * @param {{ [k: string | symbol]: any }} resolved - The resolved values. * @returns {T} The applied map. */ static applyCollectedMap(param: InjectMap | ArrayLike, resolved: { [k: string | symbol]: any; }): T | Promise; /** * Collects a map of parameters. * @param {InjectMap} param - The parameter map. * @returns {(string | symbol)[]} The collected map. */ static collectMap(param: InjectMap): Resolvable[]; /** * Merges arrays of resolved arguments and other arguments. * @param {InjectedParameter[]} resolvedArgs - The resolved arguments. * @param {...unknown[]} otherArgs - The other arguments. * @returns {unknown[]} The merged array. */ static mergeArrays(resolvedArgs: InjectedParameter[], ...otherArgs: unknown[]): { promisedArgs: Promise; args: any[]; } | { args: any[]; promisedArgs?: undefined; }; /** * Gets the arguments to inject. * @param {(Resolvable)[]} toInject - The resolvable parameters to inject. * @returns {InjectedParameter[]} The injected parameters. */ protected getArguments(toInject: (Resolvable)[]): InjectedParameter[]; /** * Resolves a series of keys on a given source object, traversing through nested properties. * If the resolution encounters an `Injector` instance, it delegates the resolution to the `Injector`. * If the resolution encounters a promise-like object, it resolves the promise and continues the resolution. * If a key cannot be resolved and a fallback function is provided, the fallback is invoked with the remaining keys. * * @template T - The type of the resolved value. * @param source - The initial object to resolve the keys from. * @param keys - An array of keys (strings or symbols) to resolve on the source object. * @param fallback - A function to handle unresolved keys, invoked with the remaining keys if resolution fails. * @returns The resolved value of type `T`, or the result of the fallback function if provided. */ static resolveKeys(source: unknown, keys: ResolvableArray, fallback?: (keys: Resolvable[]) => T): T; /** * Resolves a parameter asynchronously and returns a promise. * @param name - The name of the parameter to resolve. * @returns A promise resolving to the resolved value. */ abstract onResolve(name: Resolvable): PromiseLike; /** * Resolves a parameter asynchronously and invokes a handler with the result. * @param name - The name of the parameter to resolve. * @param handler - Callback to execute with the resolved value. */ abstract onResolve(name: Resolvable, handler: (value: T) => void): void; /** * Injects a function or property. * @param {Injectable | Resolvable} a - The function or resolvable parameter. * @param {...Resolvable[]} b - Additional resolvable parameters. * @returns {Injected | ((b: TypedPropertyDescriptor>) => void)} The injected function or property. */ inject(a: Injectable): Injected; inject(...a: (Resolvable)[]): (b: TypedPropertyDescriptor>) => void; inject(a: Injectable | Resolvable, ...b: (Resolvable)[]): Injected | ((b: TypedPropertyDescriptor>) => void); /** * Injects an asynchronous function or property. * @param {Injectable | Resolvable} a - The function or resolvable parameter. * @param {...Resolvable[]} b - Additional resolvable parameters. * @returns {Injected | ((b: TypedPropertyDescriptor>) => void)} The injected function or property. */ injectAsync(a: Injectable): Injected; injectAsync(...a: (Resolvable)[]): Injectable; /** * Injects a new instance of a constructor. * @param {InjectableConstructor} ctor - The constructor to inject. * @returns {Injected} The injected instance. */ injectNew(ctor: InjectableConstructor): Injected; /** * Resolves a parameter. * @param {Resolvable} param - The parameter to resolve. * @returns {T} The resolved parameter. */ abstract resolve(param: Resolvable): T; /** * Inspects the injector. */ abstract inspect(): void; /** * Injects a new instance of a constructor with specified parameters. * @param {Resolvable[]} toInject - The parameters to inject. * @param {InjectableConstructor} ctor - The constructor to inject. * @returns {Injected} The injected instance. */ injectNewWithName(toInject: Resolvable[], ctor: InjectableConstructor): Injected; /** * Injects an asynchronous function with specified parameters. * @param {Resolvable[]} toInject - The parameters to inject. * @param {InjectableAsync | Injectable} a - The function to inject. * @returns {Promise} The injected function. */ injectWithNameAsync(toInject: (Resolvable)[], a: Injectable, TArgs>): Injected>; /** * Injects a function with specified parameters. * @param {Resolvable[]} toInject - The parameters to inject. * @param {Injectable} a - The function to inject. * @returns {Injected} The injected function. */ injectWithName(toInject: Resolvable[], a: Injectable): Injected; /** * Executes a function with specified parameters. * @param {...Resolvable[]} toInject - The parameters to inject. * @returns {(f: Injectable) => T} The executed function. */ exec(...toInject: (Resolvable)[]): (f: Injectable) => T; } export declare abstract class LocalInjector extends Injector { protected parent?: Injector | null; constructor(parent?: Injector | null); /** * Unregisters a parameter. * @param {string | symbol} name - The name of the parameter to unregister. */ abstract unregister(name: string | symbol): void; /** * Registers a parameter with a value. * @param {string | symbol} name - The name of the parameter to register. * @param {T} value - The value to register. * @param {boolean} [override] - Whether to override the existing value. * @returns {T} The registered value. */ register(name: string | symbol, value: T, override?: boolean): T; /** * Registers a factory function. * @param {string} name - The name of the factory. * @param {(() => unknown)} value - The factory function. * @param {boolean} [override] - Whether to override the existing value. * @returns {(() => unknown)} The registered factory function. */ registerFactory(name: string | symbol, value: (() => unknown), override?: boolean): (() => unknown); /** * Creates a factory function. * @param {string} name - The name of the factory. * @param {boolean} [override] - Whether to override the existing value. * @returns {(fact: (() => unknown)) => void} The factory function. */ factory(name: string, override?: boolean): (fact: (() => unknown)) => void; /** * Registers a service. * @param {string | symbol} name - The name of the service. * @param {...Resolvable[]} toInject - The parameters to inject. */ service(name: string | symbol, ...toInject: Resolvable[]): any; service(name: string | symbol, override?: boolean, ...toInject: Resolvable[]): any; /** * Registers a descriptor. * @param {string | symbol} name - The name of the descriptor. * @param {PropertyDescriptor} value - The descriptor value. * @param {boolean} [override] - Whether to override the existing value. */ abstract registerDescriptor(name: string | symbol, value: PropertyDescriptor, override?: boolean): void; } export declare class InjectorMap extends Injector { private map; constructor(map: ((Resolvable: any) => any)); onResolve(name: Resolvable): PromiseLike; onResolve(name: Resolvable, handler: (value: T) => void): void; resolve(param: Resolvable): T; inspect(): void; } export {};