refactor and generic internal add

This commit is contained in:
jacob
2024-02-15 12:26:34 -06:00
parent 07b11b357b
commit 4efdbb21fb
7 changed files with 39 additions and 26 deletions

View File

@@ -21,6 +21,16 @@ export async function __swap_container(c: CoreContainer<Partial<Dependencies>>)
}
containerSubject = c;
}
/**
* @internal
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
* then it will swap
*/
export function __add_container(key: string,v : Insertable) {
containerSubject.add({ [key]: v });
}
/**
* Returns the underlying data structure holding all dependencies.
* Exposes methods from iti
@@ -39,11 +49,10 @@ export function disposeAll(logger: Logging|undefined) {
?.disposeAll()
.then(() => logger?.info({ message: 'Cleaning container and crashing' }));
}
const dependencyBuilder = (container: any, excluded: string[] ) => {
type Insertable =
| ((container: CoreContainer<Dependencies>) => unknown )
type Insertable =
| ((container: CoreContainer<Dependencies>) => unknown)
| object
const dependencyBuilder = (container: any, excluded: string[] ) => {
return {
/**
* Insert a dependency into your container.
@@ -104,11 +113,6 @@ type ValidDependencyConfig =
| CallbackBuilder
| DependencyConfiguration;
export const insertLogger = (containerSubject: CoreContainer<any>) => {
containerSubject
.upsert({'@sern/logger': () => new DefaultServices.DefaultLogging});
}
/**
* Given the user's conf, check for any excluded/included dependency keys.
@@ -123,7 +127,7 @@ function composeRoot(
//container should have no client or logger yet.
const hasLogger = conf.exclude?.has('@sern/logger');
if (!hasLogger) {
insertLogger(container);
__add_container('@sern/logger', new DefaultServices.DefaultLogging);
}
//Build the container based on the callback provided by the user
conf.build(container as CoreContainer<Omit<CoreDependencies, '@sern/client'>>);
@@ -141,13 +145,13 @@ export async function makeDependencies<const T extends Dependencies>
if(typeof conf === 'function') {
const excluded: string[] = [];
conf(dependencyBuilder(containerSubject, excluded));
//We only include logger if it does not exist
const includeLogger =
!excluded.includes('@sern/logger')
&& !containerSubject.getTokens()['@sern/logger'];
&& !containerSubject.hasKey('@sern/logger');
if(includeLogger) {
insertLogger(containerSubject);
__add_container('@sern/logger', new DefaultServices.DefaultLogging);
}
containerSubject.ready();

View File

@@ -2,7 +2,7 @@ import { Container } from 'iti';
import { Disposable } from '../';
import * as assert from 'node:assert';
import { Subject } from 'rxjs';
import { DefaultServices, ModuleStore } from '../_internal';
import { __Services, ModuleStore } from '../_internal';
import * as Hooks from './hooks';
import { EventEmitter } from 'node:events';
@@ -23,11 +23,11 @@ export class CoreContainer<T extends Partial<Dependencies>> extends Container<T,
.subscribe({ complete: unsubscribe });
(this as Container<{}, {}>)
.add({ '@sern/errors': () => new DefaultServices.DefaultErrorHandling,
.add({ '@sern/errors': () => new __Services.DefaultErrorHandling,
'@sern/emitter': () => new EventEmitter({ captureRejections: true }),
'@sern/store': () => new ModuleStore })
.add(ctx => {
return { '@sern/modules': new DefaultServices.DefaultModuleManager(ctx['@sern/store'])};
return { '@sern/modules': new __Services.DefaultModuleManager(ctx['@sern/store'])};
});
}