From 58bc2ee52c3676c64c04930169d560c42da1d731 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Fri, 14 Jun 2024 20:07:12 -0500 Subject: [PATCH] remove uneeded container class --- packages/ioc/src/container.ts | 75 ----------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 packages/ioc/src/container.ts diff --git a/packages/ioc/src/container.ts b/packages/ioc/src/container.ts deleted file mode 100644 index 61b5393..0000000 --- a/packages/ioc/src/container.ts +++ /dev/null @@ -1,75 +0,0 @@ - -function hasCallableMethod(obj: object, name: PropertyKey) { - // object will always be defined - // @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)) { - console.log(hookname) - //@ts-ignore - this.addHook(hookname, async () => await 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: Container) => object) { - const insert = fn(this); - 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(); - } - } -} -