From 0256a381872f6a27fec6021ed6349302269d8ea8 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Wed, 10 May 2023 21:40:40 -0500 Subject: [PATCH] revert: readd module store and add contract --- src/core/contracts/error-handling.ts | 1 + src/core/contracts/index.ts | 7 +-- src/core/contracts/module-store.ts | 4 ++ src/core/structures/container.ts | 51 ++++++++++++------- src/core/structures/core-context.ts | 2 +- src/core/structures/index.ts | 1 + src/core/structures/module-store.ts | 9 ++++ .../structures/services/error-handling.ts | 1 + src/core/structures/services/logger.ts | 1 + .../structures/services/module-manager.ts | 9 ++-- 10 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 src/core/contracts/module-store.ts create mode 100644 src/core/structures/module-store.ts diff --git a/src/core/contracts/error-handling.ts b/src/core/contracts/error-handling.ts index 82d686e..107d664 100644 --- a/src/core/contracts/error-handling.ts +++ b/src/core/contracts/error-handling.ts @@ -18,5 +18,6 @@ export interface ErrorHandling { * @param error */ updateAlive(error: Error): void; + } diff --git a/src/core/contracts/index.ts b/src/core/contracts/index.ts index 649bab1..ae2e677 100644 --- a/src/core/contracts/index.ts +++ b/src/core/contracts/index.ts @@ -1,3 +1,4 @@ -export { type ErrorHandling } from './error-handling'; -export type { Logging, LogPayload } from './logging'; -export { type ModuleManager } from './module-manager'; +export * from './error-handling'; +export * from './logging'; +export * from './module-manager'; +export * from './module-store' diff --git a/src/core/contracts/module-store.ts b/src/core/contracts/module-store.ts new file mode 100644 index 0000000..bd689db --- /dev/null +++ b/src/core/contracts/module-store.ts @@ -0,0 +1,4 @@ + +export interface CoreModuleStore { + commands : Map; +} diff --git a/src/core/structures/container.ts b/src/core/structures/container.ts index 1c3b474..6f20596 100644 --- a/src/core/structures/container.ts +++ b/src/core/structures/container.ts @@ -2,44 +2,59 @@ import { Container } from "iti"; import { DefaultErrorHandling, DefaultModuleManager, SernEmitter } from "../"; import { isAsyncFunction} from "node:util/types"; import * as assert from 'node:assert' -import { Dependencies } from "../ioc/types"; +import { Subject } from "rxjs"; +import { ModuleStore } from "./module-store"; + /** * Provides all the defaults for sern to function properly. * The only user provided dependency needs to be @sern/client */ export class CoreContainer> extends Container { - private _ready = false; + private ready$ = new Subject(); constructor() { super(); + + this.listenForInsertions(); + (this as Container<{}, {}>) .add({ '@sern/errors': () => new DefaultErrorHandling(), '@sern/emitter': () => new SernEmitter(), - '@sern/modules': () => new DefaultModuleManager(new Map()) + '@sern/store': () => new ModuleStore(), + }).add(ctx => { + return { '@sern/modules': () => new DefaultModuleManager(ctx["@sern/store"]) }; }) } + + private listenForInsertions() { + assert.ok(this.isReady(), "listening for init functions should only occur prior to sern being ready.") - async withInit(...keys: Keys[]) { - if(this.isReady()) { - throw Error("You cannot call this method after sern has started"); + const unsubscriber = this.on('containerUpserted', this.callInitHooks); + this.ready$.subscribe({ + complete: unsubscriber + }); + } + + private async callInitHooks(e: { key: keyof T, newContainer: T[keyof T]|null }) { + + const dep = e.newContainer; + assert.ok(dep); + + //Ignore any dependencies that are not objects or array + if(typeof(dep) !== 'object' || Array.isArray(dep)) { + return; } - for await (const k of keys) { - const dep = this.get(k); - assert.ok(dep !== undefined); - if('init' in dep && typeof dep.init === 'function') { - isAsyncFunction(dep.init) + if('init' in dep && typeof dep.init === 'function') { + isAsyncFunction(dep.init) ? await dep.init() : dep.init() - } else { - throw Error(`called withInit with key ${k} but found nothing to init`) - } - } - return this; + } } + isReady() { - return this._ready; + return this.ready$.closed; } ready() { - this._ready = true; + this.ready$.unsubscribe(); } } diff --git a/src/core/structures/core-context.ts b/src/core/structures/core-context.ts index 4fdb011..b011e28 100644 --- a/src/core/structures/core-context.ts +++ b/src/core/structures/core-context.ts @@ -17,7 +17,7 @@ export abstract class CoreContext { } public isMessage(): this is CoreContext { - return this.ctx.map(() => true).unwrapOr(false); + return this.ctx.ok; } public isSlash(): this is CoreContext { diff --git a/src/core/structures/index.ts b/src/core/structures/index.ts index b269be9..391c18e 100644 --- a/src/core/structures/index.ts +++ b/src/core/structures/index.ts @@ -2,3 +2,4 @@ export * from './enums'; export * from './context'; export * from './sern-emitter'; export * from './services' +export * from './module-store' diff --git a/src/core/structures/module-store.ts b/src/core/structures/module-store.ts new file mode 100644 index 0000000..bc81fe9 --- /dev/null +++ b/src/core/structures/module-store.ts @@ -0,0 +1,9 @@ +import { CoreModuleStore } from "../contracts"; + +/* + * @internal + * Version 4.0.0 will internalize this api. Please refrain from using ModuleStore! + */ +export class ModuleStore implements CoreModuleStore { + commands = new Map(); +} diff --git a/src/core/structures/services/error-handling.ts b/src/core/structures/services/error-handling.ts index cde0d4b..3e04d39 100644 --- a/src/core/structures/services/error-handling.ts +++ b/src/core/structures/services/error-handling.ts @@ -1,6 +1,7 @@ import { ErrorHandling } from "../../contracts"; /** + * @internal * @since 2.0.0 */ export class DefaultErrorHandling implements ErrorHandling { diff --git a/src/core/structures/services/logger.ts b/src/core/structures/services/logger.ts index 39056b3..60597c3 100644 --- a/src/core/structures/services/logger.ts +++ b/src/core/structures/services/logger.ts @@ -1,6 +1,7 @@ import { LogPayload, Logging } from "../../contracts"; /** + * @internal * @since 2.0.0 */ export class DefaultLogging implements Logging { diff --git a/src/core/structures/services/module-manager.ts b/src/core/structures/services/module-manager.ts index 307c99f..19550ad 100644 --- a/src/core/structures/services/module-manager.ts +++ b/src/core/structures/services/module-manager.ts @@ -1,9 +1,10 @@ -import { ModuleStore } from "../../../shared"; import { ModuleManager } from "../../contracts"; import { importModule } from "../../module-loading"; import { CommandModule } from "../../types/modules"; +import { ModuleStore } from "../module-store"; /** +* @internal * @since 2.0.0 */ export class DefaultModuleManager implements ModuleManager { @@ -14,14 +15,14 @@ export class DefaultModuleManager implements ModuleManager { } get(id: string) { - return this.moduleStore.get(id); + return this.moduleStore.commands.get(id); } set(id: string, path: string): void { - this.moduleStore.set(id, path); + this.moduleStore.commands.set(id, path); } //not tested getPublishableCommands(): Promise { - const entries = this.moduleStore.entries(); + const entries = this.moduleStore.commands.entries(); const publishable = 0b000000110; return Promise.all( Array.from(entries)