feat(observableHandling) : making match function a type predicate

assertion
This commit is contained in:
Jacob Nguyen
2022-04-19 10:16:46 -05:00
parent eeabecb4e2
commit 86c4e45ad2
5 changed files with 12 additions and 13 deletions

View File

@@ -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(

View File

@@ -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<T extends keyof ModuleDefs>(
plug: PluggedModule | undefined, type : T
) : plug is { mod: ModuleDefs[T], plugins : SernPlugin[] } {
return plug !== undefined && (plug.mod.type & type) != 0;
}
export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType : T) {
return (src : Observable<PluggedModule|undefined>) =>
new Observable<PluggedModule>( 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<T extends keyof ModuleDefs>(cmdType : T) {
error: (e) => subscriber.error(e),
complete: () => subscriber.complete()
});
});
});
}
export function filterTap<T extends keyof ModuleDefs>(

View File

@@ -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;

View File

@@ -1,6 +1,3 @@
import type { Client } from "discord.js";
import { Module, Sern } from "../..";
import { CommandPlugin, Controller, PluginType } from "./plugin";

View File

@@ -48,7 +48,7 @@ export type EventPlugin<T extends CommandType = 1> = {
modTy : T
} & Override<BasePlugin, {
execute : ( event : Parameters<ModuleDefs[T]['execute']>, controller: Controller ) => Awaitable<Result<void,void>>
} >;
}>;
export type SernPlugin =
CommandPlugin