Adrià Vilanova MartÃnez | 18d03c4 | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 1 | import NodeWatcherSingleton from '../../../nodeWatcher/NodeWatcher'; |
| 2 | import { ConcreteNodeWatcherScriptHandler } from './handlers/NodeWatcherScriptHandler'; |
| 3 | import Script from '../Script'; |
| 4 | |
| 5 | export default abstract class NodeWatcherScript<Options> extends Script { |
| 6 | public abstract handlers: Map< |
| 7 | string, |
| 8 | ConcreteNodeWatcherScriptHandler<Options> |
| 9 | >; |
| 10 | |
| 11 | /** |
| 12 | * Resolves to the options when the script is executed. |
| 13 | * |
| 14 | * This is so we can defer retrieving dependencies until the script is |
| 15 | * executed, to prevent loading unnecessary things if they aren't needed |
| 16 | * after all. |
| 17 | */ |
| 18 | protected abstract optionsFactory(): Options; |
| 19 | |
| 20 | execute() { |
| 21 | const nodeWatcher = NodeWatcherSingleton.getInstance(); |
| 22 | const options = this.optionsFactory(); |
| 23 | |
| 24 | for (const [key, handlerClass] of this.handlers) { |
| 25 | const handler = new handlerClass(options); |
| 26 | nodeWatcher.setHandler(key, handler); |
| 27 | } |
| 28 | } |
| 29 | } |