1+ /**
2+ * Contains a generic implementation of typed commands.
3+ *
4+ * This allows different parts of the extension to register commands with a certain type,
5+ * and then allow other parts to call those commands in a well-typed manner.
6+ */
7+
18export interface Disposable {
29 dispose ( ) : void ;
310}
411
12+ /**
13+ * A command function is a completely untyped command.
14+ */
515export type CommandFunction = ( ...args : any [ ] ) => Promise < unknown > ;
616
17+ /**
18+ * The command manager basically takes a single input, the type
19+ * of all the known commands. The second parameter is provided by
20+ * default (and should not be needed by the caller) it is a
21+ * technicality to allow the type system to look up commands.
22+ */
723export class CommandManager <
824 Commands extends Record < string , CommandFunction > ,
925 CommandName extends keyof Commands & string = keyof Commands & string ,
@@ -24,20 +40,29 @@ export class CommandManager<
2440 ) => Promise < Awaited < ReturnType < Commands [ T ] > > > ,
2541 ) { }
2642
43+ /**
44+ * Register a command with the specified name and implementation.
45+ */
2746 registerCommand < T extends CommandName > (
2847 commandName : T ,
2948 definition : Commands [ T ] ,
3049 ) : void {
3150 this . commands . push ( this . commandRegister ( commandName , definition ) ) ;
3251 }
3352
53+ /**
54+ * Execute a command with the specified name and the provided arguments.
55+ */
3456 executeCommand < T extends CommandName > (
3557 commandName : T ,
3658 ...args : Parameters < Commands [ T ] >
3759 ) : Promise < Awaited < ReturnType < Commands [ T ] > > > {
3860 return this . commandExecute ( commandName , ...args ) ;
3961 }
4062
63+ /**
64+ * Dispose the manager, disposing all the registered commands.
65+ */
4166 dispose ( ) : void {
4267 this . commands . forEach ( ( cmd ) => {
4368 cmd . dispose ( ) ;
0 commit comments