23 lines
559 B
TypeScript
23 lines
559 B
TypeScript
import type { Command } from "./command";
|
|
|
|
export type CommandRegistry = {
|
|
get(id: string): Command<unknown> | undefined;
|
|
list(): Command<unknown>[];
|
|
};
|
|
|
|
export function createCommandRegistry(commands: Command<unknown>[]): CommandRegistry {
|
|
const byId = new Map<string, Command<unknown>>();
|
|
|
|
for (const command of commands) {
|
|
if (byId.has(command.id)) {
|
|
throw new Error(`Duplicate command id: ${command.id}`);
|
|
}
|
|
byId.set(command.id, command);
|
|
}
|
|
|
|
return {
|
|
get: (id) => byId.get(id),
|
|
list: () => [...byId.values()],
|
|
};
|
|
}
|