diff --git a/src/core/id.ts b/src/core/id.ts index 28032c54..50a781ed 100644 --- a/src/core/id.ts +++ b/src/core/id.ts @@ -42,19 +42,10 @@ const TypeMap = new Map([ [CommandType.RoleSelect, ComponentType.RoleSelect], [CommandType.ChannelSelect, ComponentType.ChannelSelect]]); -/* - * Generates a number based on CommandType. - * This corresponds to an ApplicationCommandType or ComponentType - * TextCommands are 0 as they aren't either or. - */ -function apiType(t: CommandType | EventType) { - return TypeMap.get(t)!; -} - /* * Generates an id based on name and CommandType. * A is for any ApplicationCommand. C is for any ComponentCommand - * Then, another number generated by apiType function is appended + * Then, another number fetched from TypeMap */ export function create(name: string, type: CommandType | EventType) { if(type == CommandType.Text) { @@ -67,7 +58,7 @@ export function create(name: string, type: CommandType | EventType) { return `${name}_M`; } const am = (appBitField & type) !== 0 ? 'A' : 'C'; - return `${name}_${am}${apiType(type)}` + return `${name}_${am}${TypeMap.get(type)!}` } diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index eb26c9b0..493a8d64 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -22,7 +22,6 @@ export async function __swap_container(c: CoreContainer>) containerSubject = c; } /** - * @deprecated * Returns the underlying data structure holding all dependencies. * Exposes methods from iti * Use the Service API. The container should be readonly @@ -51,8 +50,14 @@ const dependencyBuilder = (container: any, excluded: string[] ) => { * Supply the correct key and dependency */ add(key: keyof Dependencies, v: Insertable) { - Result.wrap(() => container.add({ [key]: v})) - .expect("Failed to add " + key); + if(typeof v !== 'function') { + Result.wrap(() => container.add({ [key]: v})) + .expect("Failed to add " + key); + } else { + Result.wrap(() => + container.add((cntr: CoreContainer) => ({ [key]: v(cntr)} ))) + .expect("Failed to add " + key); + } }, /** * Exclude any dependencies from being added. @@ -68,8 +73,14 @@ const dependencyBuilder = (container: any, excluded: string[] ) => { * Swap out a preexisting dependency. */ swap(key: keyof Dependencies, v: Insertable) { - Result.wrap(() => container.upsert({ [key]: v })) - .expect("Failed to update " + key); + if(typeof v !== 'function') { + Result.wrap(() => container.upsert({ [key]: v})) + .expect("Failed to update " + key); + } else { + Result.wrap(() => + container.upsert((cntr: CoreContainer) => ({ [key]: v(cntr)}))) + .expect("Failed to update " + key); + } }, /** * @param key the key of the dependency diff --git a/src/core/ioc/container.ts b/src/core/ioc/container.ts index f8ef276d..4eef0727 100644 --- a/src/core/ioc/container.ts +++ b/src/core/ioc/container.ts @@ -51,8 +51,6 @@ export class CoreContainer> extends Container() { +function assignDefaults() { return map(({ module, absPath }) => { const processed = { name: module.name ?? Files.filename(absPath), @@ -126,7 +126,6 @@ function assignDefaults() { export function buildModules( input: ObservableInput, - moduleManager: ModuleManager, ) { return Files .buildModuleStream>(input) @@ -216,7 +215,7 @@ export function callInitPlugins>(sernEmitter: Emi }, onNext: (payload) => { sernEmitter.emit('module.register', resultPayload(PayloadType.Success, payload.module)); - return payload; + return payload as { module: T; metadata: CommandMeta }; }, }), ); diff --git a/src/handlers/ready-event.ts b/src/handlers/ready-event.ts index 3eeb9f84..60739b10 100644 --- a/src/handlers/ready-event.ts +++ b/src/handlers/ready-event.ts @@ -1,8 +1,8 @@ -import { ObservableInput, concat, first, fromEvent, ignoreElements, pipe } from 'rxjs'; +import { ObservableInput, concat, first, fromEvent, ignoreElements, pipe, tap } from 'rxjs'; import { CommandType } from '../core/structures'; import { SernError } from '../core/_internal'; import { Result } from 'ts-results-es'; -import { ModuleManager } from '../core/contracts'; +import { Logging, ModuleManager } from '../core/contracts'; import { buildModules, callInitPlugins } from './_internal'; import * as assert from 'node:assert'; import * as util from 'node:util'; @@ -10,20 +10,23 @@ import type { DependencyList } from '../types/ioc'; import type { AnyModule, CommandMeta, Processed } from '../types/core-modules'; export function readyHandler( - [sEmitter, , , moduleManager, client]: DependencyList, + [sEmitter, , log , moduleManager, client]: DependencyList, allPaths: ObservableInput, ) { - const ready$ = fromEvent(client!, 'ready').pipe(once()); + const ready$ = fromEvent(client!, 'ready').pipe(once(log)); - return concat(ready$, buildModules(allPaths, moduleManager)) + return concat(ready$, buildModules(allPaths)) .pipe(callInitPlugins(sEmitter)) .subscribe(({ module, metadata }) => { register(moduleManager, module, metadata) .expect(SernError.InvalidModuleType + ' ' + util.inspect(module)); + //TODO: TEST ALL MODULES AGAIN + console.log(module) }); } -const once = () => pipe( +const once = (log: Logging | undefined) => pipe( + tap(() => { log?.info({ message: "Waiting on discord client to be ready..." }) }), first(), ignoreElements()) @@ -31,9 +34,9 @@ const once = () => pipe( function register>( manager: ModuleManager, module: T, - metadata: unknown + metadata:CommandMeta ): Result { - manager.setMetadata(module, metadata as CommandMeta)!; + manager.setMetadata(module, metadata)!; const validModuleType = module.type >= 0 && module.type <= 1 << 10; assert.ok( @@ -48,6 +51,5 @@ function register>( module.alias?.forEach(a => manager.set(`${a}_T`, module)); } } - //@ts-ignore return Result.wrap(() => manager.set(metadata.id, module)); } diff --git a/src/handlers/user-defined-events.ts b/src/handlers/user-defined-events.ts index 95d6b4c0..3dd49894 100644 --- a/src/handlers/user-defined-events.ts +++ b/src/handlers/user-defined-events.ts @@ -23,7 +23,7 @@ export function eventsHandler( throw Error(SernError.InvalidModuleType + ' while creating event handler'); } }; - buildModules(allPaths, moduleManager) + buildModules(allPaths) .pipe( callInitPlugins(emitter), map(intoDispatcher), diff --git a/src/sern.ts b/src/sern.ts index e633be01..e88bd069 100644 --- a/src/sern.ts +++ b/src/sern.ts @@ -43,9 +43,10 @@ export function init(maybeWrapper: Wrapper | 'file') { //Ready event: load all modules and when finished, time should be taken and logged readyHandler(dependencies, Files.getFullPathTree(wrapper.commands)) .add(() => { + logger?.info({ message: "Client signaled ready, registering modules" }); const time = ((performance.now() - startTime) / 1000).toFixed(2); dependencies[0].emit('modulesLoaded'); - logger?.info({ message: `sern: registered all modules in ${time} s`, }); + logger?.info({ message: `sern: registered in ${time} s`, }); if(presencePath.exists) { const setPresence = async (p: any) => { return (dependencies[4] as Client).user?.setPresence(p);