diff --git a/packages/ioc/src/container.ts b/packages/ioc/src/container.ts index 8bdce6c..557d7e4 100644 --- a/packages/ioc/src/container.ts +++ b/packages/ioc/src/container.ts @@ -1,19 +1,16 @@ -import assert from "node:assert"; +import assert from "assert"; import { hasCallableMethod } from "./hooks"; import { } from 'node:fs/promises' /** - * 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 + * A Depedency injection container capable of adding singletons, firing hooks, and managing IOC within an application */ -export class CoreContainer { +export class Container { private __singletons = new Map(); private hooks= new Map(); private finished_init = false; constructor(options: { autowire: boolean; path?: string }) { - if(options.autowire) { - - } + if(options.autowire) { /* noop */ } } addHook(name: string, callback: Function) { @@ -23,10 +20,10 @@ export class CoreContainer { this.hooks.get(name)!.push(callback); } private registerHooks(hookname: string, insert: object) { - if(hasCallableMethod(insert, hookname)) { - //@ts-ignore - this.addHook('init', async () => await insert[hookname]()) - } + if(hasCallableMethod(insert, hookname)) { + //@ts-ignore + this.addHook('init', async () => await insert[hookname]()) + } } addSingleton(key: string, insert: object) { assert(typeof insert === 'object') @@ -39,7 +36,7 @@ export class CoreContainer { return false; } - addWiredSingleton(key: string, fn: (c: CoreContainer) => object) { + addWiredSingleton(key: string, fn: (c: Container) => object) { const insert = fn(this); assert(typeof insert === 'object') if(!this.__singletons.has(key)){ diff --git a/packages/ioc/src/dependency-injection.ts b/packages/ioc/src/dependency-injection.ts deleted file mode 100644 index 96b1184..0000000 --- a/packages/ioc/src/dependency-injection.ts +++ /dev/null @@ -1,29 +0,0 @@ -import assert from 'node:assert'; -import { useContainerRaw } from './base'; - - -/** - * The Service api, retrieve from the globally init'ed container - * Note: this method only works AFTER your container has been initiated - * @since 3.0.0 - * @example - * ```ts - * const client = Service('@sern/client'); - * ``` - * @param key a key that corresponds to a dependency registered. - * - */ -export function Service(key: PropertyKey) { - const dep = useContainerRaw().get(key)!; - assert(dep, "Requested key " + String(key) + " returned undefined"); - return dep; -} -/** - * @since 3.0.0 - * The plural version of {@link Service} - * @returns array of dependencies, in the same order of keys provided - */ -export function Services(...keys: [...T]) { - const container = useContainerRaw(); - return keys.map(k => container.get(k)!) as V; -} diff --git a/packages/ioc/src/base.ts b/packages/ioc/src/global.ts similarity index 56% rename from packages/ioc/src/base.ts rename to packages/ioc/src/global.ts index 550516e..edfebff 100644 --- a/packages/ioc/src/base.ts +++ b/packages/ioc/src/global.ts @@ -1,11 +1,10 @@ -import * as assert from 'assert'; +import assert from 'assert'; import { CoreContainer } from './container'; //SIDE EFFECT: GLOBAL DI let containerSubject: CoreContainer; /** - * @internal * Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything * then it will swap */ @@ -17,7 +16,6 @@ export async function __swap_container(c: CoreContainer) { } /** - * @internal * Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything * then it will swap */ @@ -25,12 +23,17 @@ export function __add_container(key: string, v: object) { containerSubject.addSingleton(key, v); } +/** + * Initiates the global api. + * Once this is finished, the Service api and the other global api is available + */ export function __init_container(options: { autowire: boolean; path?: string | undefined; }) { containerSubject = new CoreContainer(options); } + /** * Returns the underlying data structure holding all dependencies. * Exposes methods from iti @@ -44,4 +47,28 @@ export function useContainerRaw() { return containerSubject; } - +/** + * The Service api, retrieve from the globally init'ed container + * Note: this method only works AFTER your container has been initiated + * @since 3.0.0 + * @example + * ```ts + * const client = Service('@sern/client'); + * ``` + * @param key a key that corresponds to a dependency registered. + * + */ +export function Service(key: PropertyKey) { + const dep = useContainerRaw().get(key)!; + assert(dep, "Requested key " + String(key) + " returned undefined"); + return dep; +} +/** + * @since 3.0.0 + * The plural version of {@link Service} + * @returns array of dependencies, in the same order of keys provided + */ +export function Services(...keys: [...T]) { + const container = useContainerRaw(); + return keys.map(k => container.get(k)!) as V; +} diff --git a/packages/ioc/src/index.ts b/packages/ioc/src/index.ts index 5676d56..6430851 100644 --- a/packages/ioc/src/index.ts +++ b/packages/ioc/src/index.ts @@ -1,2 +1,2 @@ -export { Service, Services } from './dependency-injection'; -export { CoreContainer } from './container' +export { Service, Services, __init_container, __swap_container, __add_container } from './global'; +export { Container } from './container'