UNPKG

ts-ioc-container

Version:

Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.

37 lines (36 loc) 1.32 kB
import { getProxyTarget, isProxy } from '../utils/proxy.js'; import { resolveArgs } from '../injector/MetadataInjector.js'; export class HookContext { scope; methodName; instance; initialArgs = []; constructor(instance, scope, methodName) { this.scope = scope; this.methodName = methodName; this.instance = isProxy(instance) ? getProxyTarget(instance) : instance; } resolveArgs(...args) { return resolveArgs(this.instance.constructor, this.methodName)(this.scope, { args: [...this.initialArgs, ...args], }); } invokeMethod({ args = this.resolveArgs() } = {}) { return this.instance[this.methodName](...args); } setProperty(fn) { this.instance[this.methodName] = fn.resolve(this.scope); } getProperty() { return this.instance[this.methodName]; } setInitialArgs(...args) { this.initialArgs = args; return this; } getInitialArgs() { return this.initialArgs; } } export const createHookContext = (Target, scope, methodName = 'constructor') => new HookContext(Target, scope, methodName); export const createHookContextFactory = ({ args = [] } = {}) => (Target, scope, methodName) => createHookContext(Target, scope, methodName).setInitialArgs(...args);