Skip to content

Instantly share code, notes, and snippets.

@SuperOleg39
Created June 18, 2018 13:41
Show Gist options
  • Select an option

  • Save SuperOleg39/1e526885b12118c25cc35d29f995bbb6 to your computer and use it in GitHub Desktop.

Select an option

Save SuperOleg39/1e526885b12118c25cc35d29f995bbb6 to your computer and use it in GitHub Desktop.
/**
* Класс для сбора и отложенного выполнения команд.
*
* Кейс для использования - класс с динамическим импортом модуля,
* методы которого должны сработать после успешной загрузки этого модуля.
*/
export class CommandExecutor {
private ready = false;
private commands: Map<string, any[]> = new Map();
execute(command: string, ...options: any[]) {
if (this.ready) {
this.resolveCommand(command, options);
} else {
this.addCommand(command, options);
}
}
run() {
if (!this.ready) {
this.ready = true;
this.resolveCommands();
}
}
private addCommand(command: string, options: any[]) {
this.commands.set(command, options);
}
private resolveCommand(command: string, options: any[]) {
if (command in this && typeof this[command] === 'function') {
this[command](...options);
}
}
private resolveCommands() {
this.commands.forEach((value, key) => this.resolveCommand(key, value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment