From 3e0a5b3b3b51263a5d14e2d6cc944326cb9f4f02 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Wed, 20 Apr 2022 20:56:42 -0500 Subject: [PATCH] fix(handler) : turning readFiles to observable; refactor readyevent --- src/handler/events/readyEvent.ts | 49 +++++++++++++++++++------------ src/handler/utilities/readFile.ts | 16 +++++----- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index cfef13b..b2f1e2a 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -1,13 +1,14 @@ -import { from, fromEvent, map, take,concat, concatAll, mergeMap, skip, Observable, tap} from 'rxjs'; +import { from, fromEvent, map, take,concat, concatAll, mergeMap, skip, Observable, tap, of, concatMap} from 'rxjs'; import { basename } from 'path'; import * as Files from '../utilities/readFile'; import type Wrapper from '../structures/wrapper'; import type { HandlerCallback, ModuleHandlers, ModuleStates, ModuleType } from '../structures/modules/commands/moduleHandler'; import { CommandType } from '../sern'; -import type { CommandPlugin, SernPlugin } from '../plugins/plugin'; +import type { CommandPlugin, PluginType, SernPlugin } from '../plugins/plugin'; import { partition } from './observableHandling'; import { Err, Ok, Result } from 'ts-results'; import type { PluggedModule } from '../structures/modules/module'; +import type { Awaitable } from 'discord.js'; export const onReady = ( wrapper : Wrapper ) => { const { client, commands } = wrapper; @@ -19,23 +20,35 @@ export const onReady = ( wrapper : Wrapper ) => { return { mod: { name, ...plugged.mod }, plugins : plugged.plugins }; } return plugged; - })); - /** mergeMap(({ mod, plugins: allPlugins }) => { - const [ cmdPlugins, plugins ] = partition(allPlugins, isCmdPlugin); - return cmdPlugins.map(pl => { - const res = pl.execute(client, mod, { - next: () => Ok.EMPTY, - stop: () => Err.EMPTY - }) - return { res, plugged : { mod, plugins } } - }) - }) - **/ + })); + const processPlugins$ = processCommandFiles$.pipe( + mergeMap( ({mod, plugins:allPlugins}) => { + const [ cmdPlugins, plugins ] = partition(allPlugins, isCmdPlugin); + const cmdPluginsRes = cmdPlugins.map(plug => { return { + ...plug, + name: plug.name ?? 'Unnamed Plugin', + execute : plug.execute(client, mod, { + next : () => Ok.EMPTY, + stop : () => Err.EMPTY + }) + } + }); + return of({ plugged : { mod , plugins }, cmdPluginsRes }) + }) + ); - (concat(ready$,processCommandFiles$) as Observable< - PluggedModule - >) - .subscribe(console.log) + + (concat(ready$,processPlugins$) as Observable<{ + plugged: PluggedModule; + cmdPluginsRes: { + execute: Awaitable>; + type: PluginType.Command; + name: string; + description: string; + }[]; + }>).subscribe(({plugged, cmdPluginsRes }) => { + + }) /** ({ res, plugged: { mod, plugins }}) => { res.then( result => { diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index bbcede0..234bfc7 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -1,5 +1,6 @@ import { readdirSync, statSync } from 'fs'; import { join } from 'path'; +import { from, Observable } from 'rxjs'; import { SernError } from '../structures/errors'; import type { PluggedModule } from '../structures/modules/module'; @@ -35,18 +36,15 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3); * @returns {Promise<{ mod: PluggedModule; absPath: string; }[]>} data from command files */ -export async function buildData(commandDir: string ): Promise< +export function buildData(commandDir: string ): Observable< { plugged: PluggedModule; absPath: string; - }[]> { - return Promise.all( - getCommands(commandDir).map( async (absPath) => { - const plugged = (await import(absPath)).module; - if (plugged === undefined) throw Error(`${SernError.UndefinedModule} ${absPath}`); - return { plugged , absPath }; - }), - ); + }> { + return from(getCommands(commandDir).map(absPath => { + const plugged = ( require(absPath).module); + return { plugged, absPath } + })) } export function getCommands(dir: string): string[] {