diff --git a/package.json b/package.json index a999f1e..da50b36 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "license": "MIT", "dependencies": { "callsites": "^3.1.0", - "iti": "^0.6.0", "rxjs": "^7.8.0", "ts-results-es": "^4.1.0" }, diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index f8cc0d9..69a6d6b 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -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('@sern/logger') - ?.info({ message: 'All dependencies loaded successfully.' }); + container.get('@sern/logger') + ?.info({ message: 'All dependencies loaded successfully.' }); } container.ready(); } diff --git a/src/core/ioc/container.ts b/src/core/ioc/container.ts index 528629d..91a66a0 100644 --- a/src/core/ioc/container.ts +++ b/src/core/ioc/container.ts @@ -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); diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts deleted file mode 100644 index 9b9373c..0000000 --- a/src/core/ioc/dependency-injection.ts +++ /dev/null @@ -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(cb: () => T) { return cb; } - -/** - * @__PURE__ - * @since 2.0.0 - * Creates a transient object - * @param cb - */ -export function transient(cb: () => () => T) { return cb; } - diff --git a/src/core/ioc/index.ts b/src/core/ioc/index.ts index 5d48b6a..1a03a8e 100644 --- a/src/core/ioc/index.ts +++ b/src/core/ioc/index.ts @@ -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(key: T) { export function Services(...keys: [...T]) { return __Services>(...keys) } + +/** + * @since 2.0.0. + * Creates a singleton object. + * @param cb + */ +export function single(cb: () => T) { return cb; } + +/** + * @__PURE__ + * @since 2.0.0 + * Creates a transient object + * @param cb + */ +export function transient(cb: () => () => T) { return cb; } + diff --git a/yarn.lock b/yarn.lock index d0c2ed0..c380662 100644 --- a/yarn.lock +++ b/yarn.lock @@ -241,7 +241,6 @@ __metadata: callsites: ^3.1.0 discord.js: ^14.11.0 eslint: 8.39.0 - iti: ^0.6.0 prettier: 2.8.8 rxjs: ^7.8.0 ts-results-es: ^4.1.0 @@ -1048,15 +1047,6 @@ __metadata: languageName: node linkType: hard -"iti@npm:^0.6.0": - version: 0.6.0 - resolution: "iti@npm:0.6.0" - dependencies: - utility-types: ^3.10.0 - checksum: 19e484aa8b00bf57642c73c56b658d06d70d7b5acf5725a6aca9948c6b3c8d1fab18d71fb25f482a13d8c6acac137799fa80e7dbdc97cc24ed5afc94f03811e3 - languageName: node - linkType: hard - "js-sdsl@npm:^4.1.4": version: 4.4.2 resolution: "js-sdsl@npm:4.4.2" @@ -1534,13 +1524,6 @@ __metadata: languageName: node linkType: hard -"utility-types@npm:^3.10.0": - version: 3.10.0 - resolution: "utility-types@npm:3.10.0" - checksum: 8f274415c6196ab62883b8bd98c9d2f8829b58016e4269aaa1ebd84184ac5dda7dc2ca45800c0d5e0e0650966ba063bf9a412aaeaea6850ca4440a391283d5c8 - languageName: node - linkType: hard - "which@npm:^2.0.1": version: 2.0.2 resolution: "which@npm:2.0.2"