refactor: even tighter api
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user