histoire
Version:
A fast story book powered by Vite
24 lines (23 loc) • 902 B
JavaScript
import { TerminalError } from '@histoire/cli/protocol/core';
/**
* Builds the registry used by the adapter. Duplicate registrations are a
* programming error and throw immediately at startup.
* @param handlers Handlers contributed by domain modules.
*/
export function createCommandRegistry(handlers) {
const table = new Map();
for (const handler of handlers) {
if (table.has(handler.operation))
throw new Error(`Duplicate terminal operation: ${handler.operation}`);
table.set(handler.operation, handler);
}
return {
operations: () => [...table.keys()],
async dispatch(operation, args, context) {
const handler = table.get(operation);
if (!handler)
throw new TerminalError('unsupported', `Unsupported operation: ${operation}`);
return handler.handle(args, context);
},
};
}