From d28b8e698fc7229bd824ec3b7fcde83631a16a51 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Thu, 14 Dec 2023 12:34:03 -0600 Subject: [PATCH] abstracting iti --- src/core/ioc/base.ts | 30 +++++++++++++++++++--------- src/core/ioc/container.ts | 5 +---- src/core/ioc/dependency-injection.ts | 1 - 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index c89ddc88..9a696689 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -4,6 +4,7 @@ import type { DependencyConfiguration } from '../../types/ioc'; import { CoreContainer } from './container'; import { Result } from 'ts-results-es' import { DefaultServices } from '../_internal'; +import { AnyFunction } from '../../types/utility'; //SIDE EFFECT: GLOBAL DI let containerSubject: CoreContainer>; @@ -23,8 +24,8 @@ export function useContainerRaw() { const dependencyBuilder = (container: any, excluded: string[]) => { type Insertable = - | ((container: CoreContainer) => { new(): unknown }) - | { new (): unknown } + | ((container: CoreContainer) => unknown ) + | Record return { /** * Insert a dependency into your container. @@ -47,11 +48,25 @@ const dependencyBuilder = (container: any, excluded: string[]) => { * @param v The dependency to swap out. * Swap out a preexisting dependency. */ - switch(key: keyof Dependencies, v: Insertable) { + swap(key: keyof Dependencies, v: Insertable) { Result .wrap(() => container.upsert({ [key]: v })) .expect("Failed to update " + key); }, + /** + * @param key the key of the dependency + * @param cleanup Provide cleanup for the dependency at key. First parameter is the dependency itself + * @example + * ```ts + * addDisposer('dbConnection', (dbConnection) => dbConnection.end()) + * ``` + * Swap out a preexisting dependency. + */ + addDisposer(key: keyof Dependencies, cleanup: AnyFunction) { + Result + .wrap(() => container.addDisposer({ [key] : cleanup })) + .expect("Failed to addDisposer for" + key); + } }; }; @@ -63,11 +78,10 @@ type ValidDependencyConfig = export const insertLogger = (containerSubject: CoreContainer) => { containerSubject - .upsert({'@sern/logger': () => DefaultServices.DefaultLogging}); + .upsert({'@sern/logger': () => new DefaultServices.DefaultLogging}); } -export async function makeDependencies( - conf: ValidDependencyConfig -) { +export async function makeDependencies +(conf: ValidDependencyConfig) { //Until there are more optional dependencies, just check if the logger exists //SIDE EFFECT containerSubject = new CoreContainer(); @@ -79,8 +93,6 @@ export async function makeDependencies( insertLogger(containerSubject); } containerSubject.ready(); - return useContainer(); - // todo } else { composeRoot(containerSubject, conf); } diff --git a/src/core/ioc/container.ts b/src/core/ioc/container.ts index cb19af58..779c392a 100644 --- a/src/core/ioc/container.ts +++ b/src/core/ioc/container.ts @@ -12,7 +12,6 @@ import * as Hooks from './hooks' */ export class CoreContainer> extends Container { private ready$ = new Subject(); - private excluded = new Set(); constructor() { super(); assert.ok(!this.isReady(), 'Listening for dispose & init should occur prior to sern being ready.'); @@ -46,9 +45,7 @@ export class CoreContainer> extends Container - 'dispose' in value - ? [key] - : []); + 'dispose' in value ? [key] : []); for(const key of otherDisposables) { this.addDisposer({ [key]: (dep: Disposable) => dep.dispose() } as never); diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index bb366e04..126efe3b 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -1,5 +1,4 @@ import type { CoreDependencies, DependencyConfiguration, IntoDependencies } from '../../types/ioc'; -import { DefaultServices } from '../_internal'; import { insertLogger, useContainerRaw } from './base'; import { CoreContainer } from './container';