From b963f51a3689643dd588a17ed57a3ac52931e9e7 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Tue, 19 Apr 2022 17:24:18 -0500 Subject: [PATCH] refactor(messageEvent) : cleaning up observables --- src/handler/events/messageEvent.ts | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index 5bab360..02e934d 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -1,5 +1,5 @@ import type { Message } from 'discord.js'; -import { fromEvent, Observable, of, concatMap, mergeMap, map, from } from 'rxjs'; +import { fromEvent, Observable, of, concatMap, mergeMap, map, from, every, concatAll, concat } from 'rxjs'; import { Err, Ok } from 'ts-results'; import type { Args } from '../..'; import type { EventPlugin } from '../plugins/plugin'; @@ -25,23 +25,20 @@ export const onMessageCreate = (wrapper : Wrapper) => { })); const ensureModuleType$ = processMessage$.pipe( concatMap(payload => of(payload.mod) - .pipe( - filterCorrectModule(CommandType.Text), - map( textCommand => ({ ...payload, textCommand })) + .pipe( + filterCorrectModule(CommandType.Text), + map( textCommand => ({ ...payload, textCommand })) ))); const processPlugins$ = ensureModuleType$.pipe( mergeMap( ({ctx, args, textCommand}) => { - const pluginRes = textCommand.plugins.map( ePlug => { - return { - name : ePlug.name, - description : ePlug.description, - res : from((ePlug).execute([ctx, args], { + const res = from(textCommand.plugins.map(ePlug => { + return from((ePlug).execute([ctx, args], { next : () => Ok.EMPTY, stop : () => Err.EMPTY - })) - } - }); - return of( { ctx, args, mod: textCommand.mod, pluginRes }) - }) - ); + })) + })); + return res.pipe(concatAll(), every(res => res.ok)) + }) + ); + };