From 86c4e45ad2d1055cadba87498f9d3cb8fc3350a8 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Tue, 19 Apr 2022 10:16:46 -0500 Subject: [PATCH] feat(observableHandling) : making match function a type predicate assertion --- src/handler/events/messageEvent.ts | 2 +- src/handler/events/observableHandling.ts | 17 ++++++++++------- src/handler/events/readyEvent.ts | 1 - src/handler/plugins/defaultPlugins.ts | 3 --- src/handler/plugins/plugin.ts | 2 +- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index a5239f2..2f759fb 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -18,7 +18,7 @@ export const onMessageCreate = (wrapper : Wrapper) => { const posMod = Files.Commands.get(prefix) ?? Files.Alias.get(prefix); const ctx = Context.wrap(m); return of( posMod ) - .pipe ( + .pipe( filterCorrectModule(CommandType.Text), filterTap(CommandType.Text, async (mod,plugins) => { const res = await Promise.all( diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index a81ce90..1e117d1 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -7,19 +7,22 @@ import { isNotFromBot } from '../utilities/messageHelpers'; import type { PluggedModule } from '../structures/modules/module'; import type { EventPlugin, SernPlugin } from '../plugins/plugin'; -export function match(plug: PluggedModule | undefined, type : CommandType) : boolean { + +export function match( + plug: PluggedModule | undefined, type : T +) : plug is { mod: ModuleDefs[T], plugins : SernPlugin[] } { return plug !== undefined && (plug.mod.type & type) != 0; } export function filterCorrectModule(cmdType : T) { return (src : Observable) => - new Observable( subscriber => { + new Observable<{cmdType : T, plug : PluggedModule}>( subscriber => { return src.subscribe({ - next(modul) { - if(match(modul, cmdType)) { - subscriber.next(modul); + next(plug) { + if(match(plug, cmdType)) { + subscriber.next({cmdType, plug}); } else { - if (modul === undefined) { + if (plug === undefined) { return throwError(() => SernError.UndefinedModule); } return throwError(() => SernError.MismatchModule); @@ -28,7 +31,7 @@ export function filterCorrectModule(cmdType : T) { error: (e) => subscriber.error(e), complete: () => subscriber.complete() }); - }); + }); } export function filterTap( diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 1cfbb09..c296634 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -8,7 +8,6 @@ import type { CommandPlugin, 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; diff --git a/src/handler/plugins/defaultPlugins.ts b/src/handler/plugins/defaultPlugins.ts index 5c5427a..b28b04f 100644 --- a/src/handler/plugins/defaultPlugins.ts +++ b/src/handler/plugins/defaultPlugins.ts @@ -1,6 +1,3 @@ -import type { Client } from "discord.js"; -import { Module, Sern } from "../.."; -import { CommandPlugin, Controller, PluginType } from "./plugin"; diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts index 5635574..802882c 100644 --- a/src/handler/plugins/plugin.ts +++ b/src/handler/plugins/plugin.ts @@ -48,7 +48,7 @@ export type EventPlugin = { modTy : T } & Override, controller: Controller ) => Awaitable> -} >; +}>; export type SernPlugin = CommandPlugin