UNPKG

@mooncake/container

Version:

DI(dependency injection) container for JavaScript and TypeScript.

116 lines (115 loc) 3.32 kB
import { AsyncHookMap } from 'async-hooks-map'; import { Registion } from './registion'; import { BindOption, Constructor, ContainerBinder, ContainerResolver, Factory, ID } from './types'; export declare class Container implements ContainerResolver, ContainerBinder { protected _map: AsyncHookMap<any, Registion<any>>; protected _lazyBinds: { [scope: string]: Map<any, Registion<any>>; }; protected _autoBinds: Set<Function>; /** * alias of bindValue * * @template T * @param {ID<T>} id * @param {T} value * @memberof ContainerClass */ set<T>(id: ID<T>, value: T, opt?: { scope?: string; }): this; /** * bind with value, it will be singleton * * @template T * @param {ID<T>} id * @param {T} value * @memberof ContainerClass */ bindValue<T>(id: ID<T>, value: T, opt?: { scope?: string; }): this; /** * bind lazy * * @template T * @param {ID<T>} id * @param {() => T} func * @param {BindOption} [opt] * @memberof ContainerClass */ bind<T>(id: ID<T>, creater: () => T, opt?: BindOption): this; bindFactory<T>(id: Constructor<T>, factory: Factory<T> | Constructor<Factory<T>>, opt?: Partial<BindOption & { factorySingleton: boolean; }>): this; /** * * * @template T * @param {ID<T>} id * @param {Function} cls * @param {BindOption} [opt={}] * @memberof Container */ bindClassWithId(id: ID<any>, cls: Constructor<any>, opt?: BindOption): this; /** * bind a class, ignore the decoractors * * @template T * @param {Constructor<T>} cls * @param {BindOption} [opt] * @memberof ContainerClass */ bindClass<T>(cls: Constructor<T>, opt?: BindOption): this; bindAlias<T>(id: ID<T>, toId: ID<T>, opt?: Pick<BindOption, 'scope'>): this; /** * auto bind a class with decorectors * * @param {Function} target * @memberof Container */ autoBind(target: Function): this; /** * create name or alias a async scope * * @param {string} name * @memberof Container */ aliasScope(name: string): this; /** * detect a scope * * @param {string} name * @returns * @memberof Container */ hasScope(name: string): boolean; /** * fill a instance by it't prop decorectors * * @param {*} target * @param {string} [fromScope] * @memberof Container */ fill(target: any, fromScope?: string): void; /** * get instance with id or class * * @template T * @param {(Function | Constructor<T>)} id * @param {string} [fromScope] * @returns {T} * @memberof Container */ get<T>(id: Function | Constructor<T>, fromScope?: string): T; get<T = any>(id: string | symbol, fromScope?: string): T | undefined; /** * get the distance of scope which has the key * * @param {ID<any>} id * @returns * @memberof Container */ distance(id: ID<any>): number; protected _bindLazy(scope: string, id: any, registion: Registion<any>): void; }