-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandRegistry.ts
More file actions
35 lines (29 loc) · 845 Bytes
/
Copy pathCommandRegistry.ts
File metadata and controls
35 lines (29 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export interface Command {
id: string;
name: string;
shortcut?: string;
category: string;
execute: () => void;
enabled?: () => boolean;
}
export default class CommandRegistry {
private commands: Map<string, Command> = new Map();
register(cmd: Command): void {
this.commands.set(cmd.id, cmd);
}
execute(id: string): void {
const cmd = this.commands.get(id);
if (cmd && (!cmd.enabled || cmd.enabled())) {
cmd.execute();
}
}
get(id: string): Command | undefined {
return this.commands.get(id);
}
getAll(): Command[] {
return Array.from(this.commands.values());
}
getByCategory(category: string): Command[] {
return this.getAll().filter(c => c.category === category);
}
}