feat(commands): add app state dispatcher foundation
This commit is contained in:
22
commands/registry.ts
Normal file
22
commands/registry.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
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()],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user