From c764de12ac38da320e8c8177994395961927ca1d Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Thu, 13 Jun 2024 12:26:37 -0500 Subject: [PATCH] finish ioc transition --- package.json | 1 + src/core/ioc/base.ts | 5 +-- src/core/ioc/container.ts | 77 --------------------------------------- src/core/ioc/global.ts | 2 +- src/core/ioc/index.ts | 2 +- yarn.lock | 8 ++++ 6 files changed, 13 insertions(+), 82 deletions(-) delete mode 100644 src/core/ioc/container.ts diff --git a/package.json b/package.json index b177954..88b081c 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "author": "SernDevs", "license": "MIT", "dependencies": { + "@sern/ioc": "^1.0.2", "callsites": "^3.1.0", "node-cron": "^3.0.3", "rxjs": "^7.8.0", diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index fcf878d..78db4b0 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -1,4 +1,4 @@ -import { Container } from './container'; +import { Container } from '@sern/ioc'; import * as __Services from '../structures/default-services'; import type { Logging } from '../interfaces'; import { __add_container, __add_wiredcontainer, __init_container, __swap_container, useContainerRaw } from './global'; @@ -34,8 +34,7 @@ const dependencyBuilder = (container: Container) => { * Swap out a preexisting dependency. */ swap(key: keyof Dependencies, v: Insertable) { - //todo in container - this.add(key, v); + this.swap(key, v); }, }; }; diff --git a/src/core/ioc/container.ts b/src/core/ioc/container.ts deleted file mode 100644 index 5f2e02a..0000000 --- a/src/core/ioc/container.ts +++ /dev/null @@ -1,77 +0,0 @@ -/** - * 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 - */ -function hasCallableMethod(obj: object, name: PropertyKey) { - //@ts-ignore - return typeof obj[name] == 'function'; -} -/** - * A Depedency injection container capable of adding singletons, firing hooks, and managing IOC within an application - */ -export class Container { - private __singletons = new Map(); - private hooks= new Map(); - private finished_init = false; - constructor(options: { autowire: boolean; path?: string }) { - if(options.autowire) { /* noop */ } - } - - addHook(name: string, callback: Function) { - if (!this.hooks.has(name)) { - this.hooks.set(name, []); - } - this.hooks.get(name)!.push(callback); - } - private registerHooks(hookname: string, insert: object) { - - if(hasCallableMethod(insert, hookname)) { - //@ts-ignore - 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)) { - this.registerHooks('init', insert) - this.registerHooks('dispose', insert) - this.__singletons.set(key, insert); - return true; - } - return false; - } - - addWiredSingleton(key: string, fn: (c: Record) => object) { - const insert = fn(this.deps()); - return this.addSingleton(key, insert); - } - - async disposeAll() { - await this.executeHooks('dispose'); - this.hooks.delete('dispose'); - } - - isReady() { return this.finished_init; } - hasKey(key: string) { return this.__singletons.has(key); } - get(key: PropertyKey) : T|undefined { return this.__singletons.get(key); } - - async ready() { - await this.executeHooks('init'); - this.hooks.delete('init'); - this.finished_init = true; - } - - deps>(): T { - return Object.fromEntries(this.__singletons) as T - } - - async executeHooks(name: string) { - const hookFunctions = this.hooks.get(name) || []; - for (const hookFunction of hookFunctions) { - await hookFunction(); - } - } -} diff --git a/src/core/ioc/global.ts b/src/core/ioc/global.ts index 766b8c5..2322293 100644 --- a/src/core/ioc/global.ts +++ b/src/core/ioc/global.ts @@ -1,5 +1,5 @@ +import { Container } from '@sern/ioc'; import { UnpackedDependencies } from '../../types/utility'; -import { Container } from './container'; //SIDE EFFECT: GLOBAL DI let containerSubject: Container; diff --git a/src/core/ioc/index.ts b/src/core/ioc/index.ts index e06055e..09cddad 100644 --- a/src/core/ioc/index.ts +++ b/src/core/ioc/index.ts @@ -1,4 +1,4 @@ -import { IntoDependencies } from '../../types/ioc'; +import type { IntoDependencies } from '../../types/ioc'; import { Service as __Service, Services as __Services } from './global' export { makeDependencies } from './base'; diff --git a/yarn.lock b/yarn.lock index ef8561e..af7dc4c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -553,6 +553,7 @@ __metadata: resolution: "@sern/handler@workspace:." dependencies: "@faker-js/faker": ^8.0.1 + "@sern/ioc": ^1.0.2 "@types/node": ^20.0.0 "@types/node-cron": ^3.0.11 "@typescript-eslint/eslint-plugin": 5.58.0 @@ -568,6 +569,13 @@ __metadata: languageName: unknown linkType: soft +"@sern/ioc@npm:^1.0.2": + version: 1.0.2 + resolution: "@sern/ioc@npm:1.0.2" + checksum: 7e127e07c3f7eb2eefa77eb5f7c48ba37a259f089cbadfd91e1f6d448a5083cd7f38d4d3c5a2c6efada1b62120d310a3854ba2ba6444fb6a7279a11e2a0b4705 + languageName: node + linkType: hard + "@sinclair/typebox@npm:^0.27.8": version: 0.27.8 resolution: "@sinclair/typebox@npm:0.27.8"