diff --git a/package.json b/package.json index 88b081c..1ed4016 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "author": "SernDevs", "license": "MIT", "dependencies": { - "@sern/ioc": "^1.0.2", + "@sern/ioc": "^1.0.3", "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 deleted file mode 100644 index 78db4b0..0000000 --- a/src/core/ioc/base.ts +++ /dev/null @@ -1,75 +0,0 @@ -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'; -import { EventEmitter } from 'node:events'; - -export function disposeAll(logger: Logging|undefined) { - useContainerRaw() - ?.disposeAll() - .then(() => logger?.info({ message: 'Cleaning container and crashing' })); -} - -type Insertable = - | ((container: Dependencies) => object) - | object - -const dependencyBuilder = (container: Container) => { - return { - /** - * Insert a dependency into your container. - * Supply the correct key and dependency - */ - add(key: keyof Dependencies, v: Insertable) { - if(typeof v !== 'function') { - container.addSingleton(key, v) - } else { - //@ts-ignore - container.addWiredSingleton(key, (cntr) => v(cntr)) - } - }, - /** - * @param key the key of the dependency - * @param v The dependency to swap out. - * Swap out a preexisting dependency. - */ - swap(key: keyof Dependencies, v: Insertable) { - this.swap(key, v); - }, - }; -}; - -/** - * - * - * - */ -type ValidDependencyConfig = - | ((c: ReturnType) => any) - -/** - * makeDependencies constructs a dependency injection container for sern handler to use. - * This is required to start the handler, and is to be called before Sern.init. - * @example - * ```ts - * await makeDependencies(({ add }) => { - * add('@sern/client', new Client({ intents, partials }) - * }) - * ``` - */ -export async function makeDependencies (conf: ValidDependencyConfig) { - const container = await __init_container({ autowire: false }); - //We only include logger if it does not exist - const includeLogger = !container.hasKey('@sern/logger'); - - if(includeLogger) { - __add_container('@sern/logger', new __Services.DefaultLogging); - } - __add_container('@sern/errors', new __Services.DefaultErrorHandling); - __add_container('@sern/modules', new Map) - __add_container('@sern/emitter', new EventEmitter) - __add_wiredcontainer('@sern/cron', deps => new __Services.Cron(deps)) - conf(dependencyBuilder(container)); - await container.ready(); -} - diff --git a/src/core/ioc/global.ts b/src/core/ioc/global.ts deleted file mode 100644 index 2322293..0000000 --- a/src/core/ioc/global.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { Container } from '@sern/ioc'; -import { UnpackedDependencies } from '../../types/utility'; - -//SIDE EFFECT: GLOBAL DI -let containerSubject: Container; - -/** - * Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything - * then it will swap - */ -export async function __swap_container(c: Container) { - if(containerSubject) { - await containerSubject.disposeAll() - } - containerSubject = c; -} - -/** - * Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything - * then it will swap - */ -export function __add_container(key: string, v: object) { - containerSubject.addSingleton(key, v); -} - -/** - * Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything - * then it will swap - */ -export function __add_wiredcontainer(key: string, depfn : (deps: UnpackedDependencies) => object) { - //@ts-ignore - containerSubject.addWiredSingleton(key, depfn); -} -/** - * Initiates the global api. - * Once this is finished, the Service api and the other global api is available - */ -export async function __init_container(options: { - autowire: boolean; - path?: string | undefined; -}) { - containerSubject = new Container(options); - await containerSubject.ready() - return containerSubject -} - -/** - * Returns the underlying data structure holding all dependencies. - * Exposes methods from iti - * Use the Service API. The container should be readonly - */ -export function useContainerRaw() { - if (!(containerSubject && containerSubject.isReady())) { - throw new Error("Container wasn't ready or init'd. Please ensure container is ready()"); - } - - 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)!; - if(!dep) { - throw Error("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/src/core/ioc/index.ts b/src/core/ioc/index.ts deleted file mode 100644 index 09cddad..0000000 --- a/src/core/ioc/index.ts +++ /dev/null @@ -1,51 +0,0 @@ -import type { IntoDependencies } from '../../types/ioc'; -import { Service as __Service, Services as __Services } from './global' -export { makeDependencies } from './base'; - - -/** - * The new Service api, a cleaner alternative to useContainer - * To obtain intellisense, ensure a .d.ts file exists in the root of compilation. - * Usually our scaffolding tool takes care of this. - * 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: T) { - return __Service(key) as Dependencies[T] -} -/** - * @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]) { - return __Services>(...keys) -} - -/** - * @deprecated - * Creates a singleton object. - * @param cb - */ -export function single(cb: () => T) { - console.log('The `single` function is deprecated and has no effect') - return cb(); -} - -/** - * @deprecated - * @since 2.0.0 - * Creates a transient object - * @param cb - */ -export function transient(cb: () => () => T) { - console.log('The `transient` function is deprecated and has no effect') - return cb()(); -} - diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index 813f6ed..f835281 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -15,7 +15,7 @@ import * as assert from 'node:assert'; import { Context } from '../core/structures/context'; import { CommandType } from '../core/structures/enums' import { inspect } from 'node:util' -import { disposeAll } from '../core/ioc/base'; +import { disposeAll } from '../core/ioc'; import { resultPayload, isAutocomplete, treeSearch, fmt } from '../core/functions' function handleError(crashHandler: ErrorHandling, emitter: Emitter, logging?: Logging) { diff --git a/src/index.ts b/src/index.ts index 8c9088b..7639692 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,7 +53,7 @@ export type Controller = typeof controller export * from './core/create-plugins'; export { CommandType, PluginType, PayloadType, EventType } from './core/structures/enums'; export { Context } from './core/structures/context'; -export * from './core/ioc'; +export { makeDependencies, single, transient, Service, Services } from './core/ioc'; diff --git a/src/sern.ts b/src/sern.ts index 3cd067d..2fabe7a 100644 --- a/src/sern.ts +++ b/src/sern.ts @@ -1,3 +1,6 @@ +//side effect: global container +import { useContainerRaw } from '@sern/ioc/global'; + import callsites from 'callsites'; import * as Files from './core/module-loading'; import { merge } from 'rxjs'; @@ -7,7 +10,6 @@ import messageHandler from './handlers/message'; import interactionHandler from './handlers/interaction'; import { presenceHandler } from './handlers/presence'; import { handleCrash } from './handlers/event-utils'; -import { useContainerRaw } from './core/ioc/global'; import { UnpackedDependencies } from './types/utility'; import type { PresenceResult } from './core/presences'; diff --git a/src/types/ioc.ts b/src/types/ioc.ts index 3803a64..6e543ac 100644 --- a/src/types/ioc.ts +++ b/src/types/ioc.ts @@ -1,4 +1,4 @@ -import type { Container } from '../core/ioc/container'; +import type { Container } from '@sern/ioc'; import * as Contracts from '../core/interfaces'; import type { UnpackFunction } from './utility' import type { Client } from 'discord.js' diff --git a/yarn.lock b/yarn.lock index af7dc4c..6ffc01f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -553,7 +553,7 @@ __metadata: resolution: "@sern/handler@workspace:." dependencies: "@faker-js/faker": ^8.0.1 - "@sern/ioc": ^1.0.2 + "@sern/ioc": ^1.0.3 "@types/node": ^20.0.0 "@types/node-cron": ^3.0.11 "@typescript-eslint/eslint-plugin": 5.58.0 @@ -569,10 +569,10 @@ __metadata: languageName: unknown linkType: soft -"@sern/ioc@npm:^1.0.2": - version: 1.0.2 - resolution: "@sern/ioc@npm:1.0.2" - checksum: 7e127e07c3f7eb2eefa77eb5f7c48ba37a259f089cbadfd91e1f6d448a5083cd7f38d4d3c5a2c6efada1b62120d310a3854ba2ba6444fb6a7279a11e2a0b4705 +"@sern/ioc@npm:^1.0.3": + version: 1.0.3 + resolution: "@sern/ioc@npm:1.0.3" + checksum: 2c640cabbf3927d923b57233e660be1d6d4da1cd376a4ba77b118fd9aa5ef81c465d287357a15a7cd7827306dd614c73bde68751978d2030c3a4877dcbb0d02d languageName: node linkType: hard