removeiti

This commit is contained in:
Jacob Nguyen
2024-05-03 18:50:47 -05:00
parent cd92b54839
commit f762033504
6 changed files with 27 additions and 72 deletions

View File

@@ -1,8 +1,7 @@
import type { DependencyConfiguration } from '../../types/ioc';
import { Container } from './container';
import { Result } from 'ts-results-es';
import * as __Services from '../structures/default-services';
import { AnyFunction, UnpackFunction } from '../../types/utility';
import { UnpackFunction } from '../../types/utility';
import type { Logging } from '../interfaces';
import { __add_container, __swap_container, useContainerRaw } from './global';
@@ -19,7 +18,7 @@ type UnpackedDependencies = {
type Insertable =
| ((container: UnpackedDependencies) => unknown)
| object
const dependencyBuilder = (container: any, excluded: string[] ) => {
const dependencyBuilder = (container: Container, excluded: string[] ) => {
return {
/**
* Insert a dependency into your container.
@@ -27,12 +26,9 @@ const dependencyBuilder = (container: any, excluded: string[] ) => {
*/
add(key: keyof Dependencies, v: Insertable) {
if(typeof v !== 'function') {
Result.wrap(() => container.add({ [key]: v}))
.expect("Failed to add " + key);
container.addSingleton(key, v)
} else {
Result.wrap(() =>
container.add((cntr: UnpackedDependencies) => ({ [key]: v(cntr)} )))
.expect("Failed to add " + key);
container.addWiredSingleton(key, (cntr: UnpackedDependencies) => v(cntr))
}
},
/**
@@ -49,28 +45,9 @@ const dependencyBuilder = (container: any, excluded: string[] ) => {
* Swap out a preexisting dependency.
*/
swap(key: keyof Dependencies, v: Insertable) {
if(typeof v !== 'function') {
Result.wrap(() => container.upsert({ [key]: v}))
.expect("Failed to update " + key);
} else {
Result.wrap(() =>
container.upsert((cntr: UnpackedDependencies) => ({ [key]: v(cntr)})))
.expect("Failed to update " + key);
}
//todo in container
this.add(key, v);
},
/**
* @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);
}
};
};
@@ -99,9 +76,8 @@ async function composeRoot(
conf.build(container as Container);
if (!hasLogger) {
container
.get<Logging>('@sern/logger')
?.info({ message: 'All dependencies loaded successfully.' });
container.get<Logging>('@sern/logger')
?.info({ message: 'All dependencies loaded successfully.' });
}
container.ready();
}

View File

@@ -1,7 +1,5 @@
import type { Disposable } from '../interfaces';
import * as __Services from '../structures/default-services';
/**
* A semi-generic container that provides error handling, emitter, and module store.
* For the handler to operate correctly, The only user provided dependency needs to be @sern/client
@@ -19,6 +17,7 @@ export class Container {
private finished_init = false;
constructor(options: { autowire: boolean; path?: string }) {
if(options.autowire) { /* noop */ }
}
addHook(name: string, callback: Function) {
@@ -31,14 +30,14 @@ export class Container {
if(hasCallableMethod(insert, hookname)) {
console.log(hookname)
//@ts-ignore
this.addHook(hookname, async () => await insert[hookname]())
this.addHook(hookname, () => insert[hookname]())
}
}
addSingleton(key: string, insert: object) {
if(typeof insert !== 'object') {
throw Error("Inserted object must be an object");
}
if(!this.__singletons.has(key)){
if(!this.__singletons.has(key)) {
this.registerHooks('init', insert)
this.registerHooks('dispose', insert)
this.__singletons.set(key, insert);

View File

@@ -1,17 +0,0 @@
import type { IntoDependencies } from '../../types/ioc';
import { Service as __Service, Services as __Services } from './global'
/**
* @since 2.0.0.
* Creates a singleton object.
* @param cb
*/
export function single<T>(cb: () => T) { return cb; }
/**
* @__PURE__
* @since 2.0.0
* Creates a transient object
* @param cb
*/
export function transient<T>(cb: () => () => T) { return cb; }

View File

@@ -1,7 +1,6 @@
import { IntoDependencies } from '../../types/ioc';
import { Service as __Service, Services as __Services } from './global'
export { makeDependencies } from './base';
export { single, transient } from './dependency-injection';
/**
@@ -28,3 +27,19 @@ export function Service<const T extends keyof Dependencies>(key: T) {
export function Services<const T extends (keyof Dependencies)[]>(...keys: [...T]) {
return __Services<T, IntoDependencies<T>>(...keys)
}
/**
* @since 2.0.0.
* Creates a singleton object.
* @param cb
*/
export function single<T>(cb: () => T) { return cb; }
/**
* @__PURE__
* @since 2.0.0
* Creates a transient object
* @param cb
*/
export function transient<T>(cb: () => () => T) { return cb; }