proxydi
Version:
A typed hierarchical DI container that resolves circular dependencies via Proxy
87 lines (86 loc) • 3.56 kB
TypeScript
import { ProxyDiContainer } from './ProxyDiContainer';
export type DependencyId = string | symbol;
export type AliasId = string | symbol;
export type DependencyClass<T> = new (...args: any[]) => T;
export type Setter = (object: unknown, value: unknown) => void;
export declare enum ResolveScope {
Parent = 1,// 0b001
Current = 2,// 0b010
Children = 4,// 0b100
All = 7
}
export type SingleInjection = {
property: string | symbol;
dependencyId: DependencyId;
set: Setter;
};
export type AllInjection = SingleInjection & {
isAll: true;
scope: ResolveScope;
};
export type Injection = SingleInjection | AllInjection;
/**
* Type guard to check if injection is AllInjection
*/
export declare function isAllInjection(injection: Injection): injection is AllInjection;
export type IProxyDiContainer = {
id: number;
settings: Required<ContainerSettings>;
parent?: IProxyDiContainer;
isKnown: (dependencyId: DependencyId | DependencyClass<any>, scope?: ResolveScope) => boolean;
injectDependenciesTo: (dependency: any) => void;
register: (dependency: any, dependencyId?: DependencyId | DependencyClass<any>) => any;
resolve: <T>(dependencyId: DependencyId | DependencyClass<any>, scope?: ResolveScope) => T & ContainerizedDependency;
resolveAll<T>(dependencyId: DependencyId | DependencyClass<any>, scope?: ResolveScope): (T & ContainerizedDependency)[];
hasOwn: <T>(dependencyId: DependencyId | DependencyClass<any>) => boolean;
createChildContainer: () => IProxyDiContainer;
children: IProxyDiContainer[];
getChild(id: number): IProxyDiContainer;
remove: (dependencyId: DependencyId | ContainerizedDependency) => void;
bakeInjections(): void;
destroy: () => void;
};
export declare const INJECTIONS: unique symbol;
/**
* This symbol constant defines a property name.
* This property is present in each dependency instance that was registered in ProxyDiContainer.
* The property stores the dependency identifier that should be used to resolve dependency from the container where it was registered.
*/
export declare const DEPENDENCY_ID: unique symbol;
/**
* This symbol constant defines a property name.
* This property is present in each dependency instance that was registered in ProxyDiContainer.
* The property stores a reference to the ProxyDiContainer in which the dependency was registered.
*/
export declare const PROXYDI_CONTAINER: unique symbol;
export declare const ON_CONTAINERIZED: unique symbol;
export type OnContainerizedMethod = (container: ProxyDiContainer) => void;
export type Injections = Record<string | symbol, Injection>;
export type Dependency = {
[INJECTIONS]: Injections;
};
/**
* Respresent dependency instance that was registered in ProxyDi container
*/
export type ContainerizedDependency = Dependency & {
/**
* Unique identifier that could use to resolve this instance from container where it was registered
*/
[DEPENDENCY_ID]: DependencyId;
/**
* ProxyDi container in which this instance was registered
*/
[PROXYDI_CONTAINER]: IProxyDiContainer;
};
export type ContainerSettings = {
allowRewriteDependencies?: boolean;
resolveInContainerContext?: boolean;
};
export declare const IS_INJECTION_PROXY: unique symbol;
export declare const INJECTION_OWNER: unique symbol;
export declare const IS_INSTANCE_PROXY: unique symbol;
export type InjectionProxy = {
[IS_INJECTION_PROXY]: true;
[INJECTION_OWNER]: ContainerizedDependency;
[PROXYDI_CONTAINER]: IProxyDiContainer;
};