refactor: even tighter api

This commit is contained in:
2026-04-02 14:46:34 +08:00
parent b2b35c3a99
commit e945d28fc3
3 changed files with 134 additions and 97 deletions
+16
View File
@@ -3,6 +3,7 @@ import type {CommandResult, CommandRunner, CommandRunnerContext, PromptEvent} fr
import { parseCommand } from './command-parse.js';
import { applyCommandSchema } from './command-validate.js';
import { parseCommandSchema } from './schema-parse.js';
import {AsyncQueue} from "../async-queue";
export type CommandRegistry<TContext> = Map<string, CommandRunner<TContext, unknown>>;
@@ -42,6 +43,7 @@ type Listener = (e: PromptEvent) => void;
export type CommandRunnerContextExport<TContext> = CommandRunnerContext<TContext> & {
registry: CommandRegistry<TContext>;
promptQueue: AsyncQueue<PromptEvent>;
_activePrompt: PromptEvent | null;
_resolvePrompt: (command: Command) => void;
_rejectPrompt: (error: Error) => void;
@@ -101,11 +103,25 @@ export function createCommandRunnerContext<TContext>(
_resolvePrompt: resolvePrompt,
_rejectPrompt: rejectPrompt,
_pendingInput: null,
promptQueue: null!
};
Object.defineProperty(runnerCtx, '_activePrompt', {
get: () => activePrompt,
});
let promptQueue: AsyncQueue<PromptEvent>;
Object.defineProperty(runnerCtx, 'promptQueue', {
get(){
if (!promptQueue) {
promptQueue = new AsyncQueue();
listeners.add(async (event) => {
promptQueue.push(event);
});
}
return promptQueue;
}
});
return runnerCtx;
}