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) • 833 B
JavaScript
import { hook } from './hook.js';
import { HooksRunner } from './HooksRunner.js';
export const onConstructAsyncHooksRunner = new HooksRunner('onConstructAsync');
export const onConstructAsync = (...fns) => hook('onConstructAsync', ...fns);
export class AddOnConstructAsyncHookModule {
onException;
constructor(onException) {
this.onException = onException;
}
applyTo(container) {
container.addOnConstructHook((instance, scope) => {
if (!onConstructAsyncHooksRunner.hasHooks(instance)) {
return;
}
onConstructAsyncHooksRunner.executeAsync(instance, { scope }).catch((ex) => {
if (!this.onException) {
throw ex;
}
this.onException(ex, { scope });
});
});
}
}