@evyweb/ioctopus
Version:
A simple IoC container for JavaScript and TypeScript for classes and functions.
47 lines (43 loc) • 2.19 kB
TypeScript
type DependencyKey = symbol | string;
type ModuleKey = symbol | string;
interface DependencyObject {
[key: string]: DependencyKey;
}
type DependencyArray = DependencyKey[];
type Scope = 'singleton' | 'transient' | 'scoped';
interface Container {
bind(key: DependencyKey): {
toValue: (value: unknown) => void;
toFunction: (fn: CallableFunction) => void;
toHigherOrderFunction: (fn: CallableFunction, dependencies?: DependencyArray | DependencyObject, scope?: Scope) => void;
toCurry: (fn: CallableFunction, dependencies?: DependencyArray | DependencyObject, scope?: Scope) => void;
toFactory: (factory: CallableFunction, scope?: Scope) => void;
toClass: <C>(constructor: new (...args: any[]) => C, dependencies?: DependencyArray | DependencyObject, scope?: Scope) => void;
};
load(moduleKey: ModuleKey, module: Module): void;
get<T>(key: DependencyKey): T;
unload(key: ModuleKey): void;
runInScope<T>(callback: () => T): T;
}
interface Module {
bind(key: DependencyKey): {
toValue: (value: unknown) => void;
toFunction: (fn: CallableFunction) => void;
toHigherOrderFunction: (fn: CallableFunction, dependencies?: DependencyArray | DependencyObject, scope?: Scope) => void;
toCurry: (fn: CallableFunction, dependencies?: DependencyArray | DependencyObject, scope?: Scope) => void;
toFactory: (factory: CallableFunction, scope?: Scope) => void;
toClass: <C>(constructor: new (...args: any[]) => C, dependencies?: DependencyArray | DependencyObject, scope?: Scope) => void;
};
bindings: Map<DependencyKey, Binding>;
}
interface InjectionTokens {
[key: string]: DependencyKey;
}
type ResolveFunction = (dep: DependencyKey) => unknown;
interface Binding {
factory: (resolve: (key: DependencyKey) => unknown) => unknown;
scope: Scope;
}
declare function createContainer(): Container;
declare function createModule(): Module;
export { type Binding, type Container, type DependencyArray, type DependencyKey, type DependencyObject, type InjectionTokens, type Module, type ModuleKey, type ResolveFunction, type Scope, createContainer, createModule };