boardgame-core/src/utils/command/command-runner.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2026-04-02 15:36:32 +08:00
import type { Command, CommandSchema } from './types';
2026-04-02 08:58:11 +08:00
export type PromptEvent = {
schema: CommandSchema;
resolve: (command: Command) => void;
reject: (error: Error) => void;
};
export type CommandRunnerEvents = {
prompt: PromptEvent;
};
export type CommandResult<T=unknown> = {
success: true;
result: T;
} | {
success: false;
error: string;
}
2026-04-02 08:58:11 +08:00
export type CommandRunnerContext<TContext> = {
context: TContext;
run: <T=unknown>(input: string) => Promise<CommandResult<T>>;
2026-04-02 08:58:11 +08:00
runParsed: (command: Command) => Promise<{ success: true; result: unknown } | { success: false; error: string }>;
prompt: (schema: CommandSchema | string) => Promise<Command>;
on: <T extends keyof CommandRunnerEvents>(event: T, listener: (e: CommandRunnerEvents[T]) => void) => void;
off: <T extends keyof CommandRunnerEvents>(event: T, listener: (e: CommandRunnerEvents[T]) => void) => void;
2026-04-02 08:58:11 +08:00
};
export type CommandRunnerHandler<TContext, TResult> = (
this: CommandRunnerContext<TContext>,
command: Command
) => Promise<TResult>;
export type CommandRunner<TContext, TResult = unknown> = {
schema: CommandSchema;
run: CommandRunnerHandler<TContext, TResult>;
};