From 4102c7102ddeb5f90a741ec4cd5b11ceb4e6bdc1 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Wed, 16 Mar 2022 00:34:05 -0500 Subject: [PATCH] feat(commands) create module handler to deal with incoming data --- src/handler/structures/commands/module.ts | 12 +++--- .../structures/commands/moduleHandler.ts | 42 ++++++++----------- src/handler/structures/structxports.ts | 4 +- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/src/handler/structures/commands/module.ts b/src/handler/structures/commands/module.ts index bff59e8..a07d4ba 100644 --- a/src/handler/structures/commands/module.ts +++ b/src/handler/structures/commands/module.ts @@ -4,19 +4,18 @@ import type { CommandType } from "../../sern"; -interface BaseModule { +export interface BaseModule { name? : string; description : string; execute() : Awaitable } -export type Text= { type : CommandType.TEXT; alias : string[] | [] }; -export type Slash={ type : CommandType.SLASH; options : ApplicationCommandOptionData[] | [] }; -export type Both= { type : CommandType.BOTH; alias : string[] | []; options : ApplicationCommandOptionData[] | [] } +export type Text = { type : CommandType.TEXT; alias : string[] | [] }; +export type Slash = { type : CommandType.SLASH; options : ApplicationCommandOptionData[] | [] }; +export type Both = { type : CommandType.BOTH; alias : string[] | []; options : ApplicationCommandOptionData[] | [] } export type Module = - BaseModule - & Slash | Both | Text; + (BaseModule & Slash) | (BaseModule & Both) | (BaseModule & Text); @@ -26,4 +25,3 @@ export type Module = - diff --git a/src/handler/structures/commands/moduleHandler.ts b/src/handler/structures/commands/moduleHandler.ts index 55b708b..00992c0 100644 --- a/src/handler/structures/commands/moduleHandler.ts +++ b/src/handler/structures/commands/moduleHandler.ts @@ -1,30 +1,22 @@ import { CommandType } from "../../sern"; -import { SernError } from "../errors"; -import type { Modules } from "../structxports"; +import type { Text, Both, Slash, BaseModule } from "./module"; -export type TextAction = ( mod : Modules.Text ) => unknown; -export type BothAction = ( mod : Modules.Both) => unknown; -export type SlashAction= ( mod : Modules.Slash) => unknown; - -export type Action = - TextAction - | BothAction - | SlashAction; - - -export function onModule ( mod: T, action : Action ) : unknown { - switch (mod.type) { - case CommandType.TEXT : { - return (action as TextAction)(mod); - } - case CommandType.SLASH : { - return (action as SlashAction)(mod); - } - case CommandType.BOTH : { - return (action as BothAction)(mod); - } - default : throw Error(SernError.NOT_VALID_MOD_TYPE); - } +//https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union +// Explicit Module Definitions for mapping +export type ModuleDefs = { + [CommandType.TEXT] : Text & BaseModule, + [CommandType.SLASH] : Slash & BaseModule, + [CommandType.BOTH] : Both & BaseModule, } +//Keys of ModuleDefs +export type ModuleType = keyof ModuleDefs; +// The keys mapped to a constructed union with its type +export type ModuleStates = { [ K in ModuleType ] : { type : K } & ModuleDefs[K] }; +// A handler callback that is called on each ModuleDef +export type HandlerCallback= ( params : ModuleStates[K] ) => unknown; +//An object that acts as the mapped object to handler +export type ModuleHandlers = { [K in ModuleType] : HandlerCallback }; + + diff --git a/src/handler/structures/structxports.ts b/src/handler/structures/structxports.ts index 704feed..1c9ea05 100644 --- a/src/handler/structures/structxports.ts +++ b/src/handler/structures/structxports.ts @@ -1,5 +1,5 @@ import Context from './context'; -import * as ModuleTypes from './commands/module'; +import * as Modules from './commands/module'; import type Wrapper from './wrapper'; -export { Context, ModuleTypes, Wrapper }; +export { Context, Modules, Wrapper };