Skip to content

Commit

Permalink
abstracting iti
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Dec 14, 2023
1 parent bd4aefb commit d28b8e6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
30 changes: 21 additions & 9 deletions src/core/ioc/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Partial<Dependencies>>;

Expand All @@ -23,8 +24,8 @@ export function useContainerRaw() {

const dependencyBuilder = (container: any, excluded: string[]) => {
type Insertable =
| ((container: CoreContainer<Dependencies>) => { new(): unknown })
| { new (): unknown }
| ((container: CoreContainer<Dependencies>) => unknown )
| Record<PropertyKey, unknown>
return {
/**
* Insert a dependency into your container.
Expand All @@ -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);
}
};
};

Expand All @@ -63,11 +78,10 @@ type ValidDependencyConfig =

export const insertLogger = (containerSubject: CoreContainer<any>) => {
containerSubject
.upsert({'@sern/logger': () => DefaultServices.DefaultLogging});
.upsert({'@sern/logger': () => new DefaultServices.DefaultLogging});
}
export async function makeDependencies<const T extends Dependencies>(
conf: ValidDependencyConfig
) {
export async function makeDependencies<const T extends Dependencies>
(conf: ValidDependencyConfig) {
//Until there are more optional dependencies, just check if the logger exists
//SIDE EFFECT
containerSubject = new CoreContainer();
Expand All @@ -79,8 +93,6 @@ export async function makeDependencies<const T extends Dependencies>(
insertLogger(containerSubject);
}
containerSubject.ready();
return useContainer<T>();
// todo
} else {
composeRoot(containerSubject, conf);
}
Expand Down
5 changes: 1 addition & 4 deletions src/core/ioc/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import * as Hooks from './hooks'
*/
export class CoreContainer<T extends Partial<Dependencies>> extends Container<T, {}> {
private ready$ = new Subject<void>();
private excluded = new Set();
constructor() {
super();
assert.ok(!this.isReady(), 'Listening for dispose & init should occur prior to sern being ready.');
Expand Down Expand Up @@ -46,9 +45,7 @@ export class CoreContainer<T extends Partial<Dependencies>> extends Container<T,
const otherDisposables = Object
.entries(this._context)
.flatMap(([key, value]) =>
'dispose' in value
? [key]
: []);
'dispose' in value ? [key] : []);

for(const key of otherDisposables) {
this.addDisposer({ [key]: (dep: Disposable) => dep.dispose() } as never);
Expand Down
1 change: 0 additions & 1 deletion src/core/ioc/dependency-injection.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down

0 comments on commit d28b8e6

Please sign in to comment.