Created
June 18, 2018 13:41
-
-
Save SuperOleg39/1e526885b12118c25cc35d29f995bbb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Класс для сбора и отложенного выполнения команд. | |
| * | |
| * Кейс для использования - класс с динамическим импортом модуля, | |
| * методы которого должны сработать после успешной загрузки этого модуля. | |
| */ | |
| 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