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.

24 lines (23 loc) 731 B
import { hook } from './hook'; import { HooksRunner } from './HooksRunner'; export const onConstructHooksRunner = new HooksRunner('onConstruct'); export const onConstruct = (...fns) => hook('onConstruct', ...fns); export class AddOnConstructHookModule { onException; constructor(onException) { this.onException = onException; } applyTo(container) { container.addOnConstructHook((instance, scope) => { try { onConstructHooksRunner.execute(instance, { scope }); } catch (ex) { if (!this.onException) { throw ex; } this.onException(ex, { scope }); } }); } }