/* * Created by Trevor Sears . * 3:36 PM -- October 21st, 2019. * Project: @command-socket/core */ import { CommandSocketCommandNotFoundError } from "../error/command-socket-command-not-found-error"; import { CommandSocket } from "../command-socket/command-socket"; import { CommandStructureInCommandSet, CommandNameInCommandSet, CommandSetStructure } from "../schema/command/command-set-structure"; import { IfAny } from "../util/any-types"; import { CommandStructureParameterType, CommandStructureReturnType } from "../schema/command/command-structure"; import { CommandExecutableFunction } from "../schema/command/command-executable-function"; import { Command } from "../schema/command/command"; type CommandMapKey = CommandNameInCommandSet; type CommandMapValue = CommandNameInCommandSet> = CommandExecutableFunction>; /** * A registry of {@link Command}s, accessible via their string identifiers. * * @author Trevor Sears * @version v0.1.0 * @since v0.1.0 */ export class CommandRegistry { /** * A map from Command identifiers to {@link Command}s. */ private commandMap: Map, CommandMapValue>; /** * Initializes a new CommandRegistry. */ public constructor() { this.commandMap = new Map(); } // DOC-ME [2/22/20 @ 1:55 PM] - Documentation required! protected static normalizeCommand( command: Command>): CommandExecutableFunction> { if (typeof command === "function") return command; else return command.execute; } // DOC-ME [2/22/20 @ 2:59 PM] - Documentation required! public addCommand>( commandName: CommandName, command: Command>): void { // TODO [1/16/20 @ 8:25 AM] - Add in handling for erroring on duplicated command names. this.commandMap.set(commandName, CommandRegistry.normalizeCommand(command)); } /** * Returns true if this CommandRegistry instance contains a {@link Command} with the provided identifier. * * @param command The string Command identifier to check for existence within this CommandRegistry instance. * @return true if this CommandRegistry instance contains a Command with the provided identifier. */ public hasCommand(command: string): boolean { return this.commandMap.has(command as CommandMapKey); } /** * Returns the {@link Command} associated with the provided Command identifier if such a Command exists, otherwise * returning undefined. * * @param command The string Command identifier for which to return a Command. * @return The Command associated with the provided Command identifier if such a Command exists, otherwise returning * undefined. * @see CommandRegistry#hasCommand */ public getCommand>(command: CommandName): IfAny | undefined, CommandExecutableFunction>> { return this.commandMap.get(command) as IfAny | undefined, CommandExecutableFunction>>; } // DOC-ME [1/13/20 @ 12:37 PM] - Documentation required! public async execute>( commandName: CommandName, params: CommandStructureParameterType>, context: CommandSocket): Promise>>> { if (this.hasCommand(commandName as string)) { let command: CommandExecutableFunction> = this.commandMap.get(commandName) as CommandExecutableFunction>; return await command(params, context) as unknown as IfAny>>; } else throw new CommandSocketCommandNotFoundError(commandName as string); } }