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

31 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-04-02 08:58:11 +08:00
import type { Command, CommandSchema } from './types.js';
export type PromptEvent = {
schema: CommandSchema;
resolve: (command: Command) => void;
reject: (error: Error) => void;
};
export type CommandRunnerEvents = {
prompt: PromptEvent;
};
2026-04-02 08:58:11 +08:00
export type CommandRunnerContext<TContext> = {
context: TContext;
run: (input: string) => Promise<{ success: true; result: unknown } | { success: false; error: string }>;
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>;
};