Adrià Vilanova Martínez | 18d03c4 | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 1 | import Feature from '../common/architecture/features/Feature'; |
| 2 | import { |
| 3 | ScriptEnvironment, |
| 4 | ScriptPage, |
| 5 | ScriptRunPhase, |
| 6 | } from '../common/architecture/scripts/Script'; |
Adrià Vilanova Martínez | ab4c80e | 2024-05-04 19:53:33 +0200 | [diff] [blame^] | 7 | import ScriptRunner from '../common/architecture/scripts/ScriptRunner'; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 8 | import AutoRefreshFeature from './autoRefresh/autoRefresh.feature'; |
Adrià Vilanova Martínez | 18d03c4 | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 9 | import InfiniteScrollFeature from './infiniteScroll/infiniteScroll.feature'; |
| 10 | |
| 11 | export type ConcreteFeatureClass = { new (): Feature }; |
| 12 | |
| 13 | export interface Context { |
| 14 | page: ScriptPage; |
| 15 | environment: ScriptEnvironment; |
| 16 | runPhase: ScriptRunPhase; |
| 17 | } |
| 18 | |
| 19 | export default class Features { |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 20 | private features: ConcreteFeatureClass[] = [ |
| 21 | AutoRefreshFeature, |
| 22 | InfiniteScrollFeature, |
| 23 | ]; |
Adrià Vilanova Martínez | 18d03c4 | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 24 | private initializedFeatures: Feature[]; |
| 25 | |
Adrià Vilanova Martínez | ab4c80e | 2024-05-04 19:53:33 +0200 | [diff] [blame^] | 26 | getScriptRunner(context: Context) { |
| 27 | const scripts = this.getScripts(context); |
| 28 | const scriptRunner = new ScriptRunner(); |
| 29 | scriptRunner.add(...scripts); |
| 30 | return scriptRunner; |
Adrià Vilanova Martínez | 18d03c4 | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | getScripts(context: Context) { |
| 34 | const features = this.getFeatures(); |
| 35 | const allScripts = features.map((feature) => feature.getScripts()).flat(1); |
| 36 | return allScripts.filter( |
| 37 | (script) => |
| 38 | script.page === context.page && |
| 39 | script.environment === context.environment && |
| 40 | script.runPhase === context.runPhase, |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | private getFeatures() { |
| 45 | if (this.initializedFeatures === undefined) { |
| 46 | this.initializedFeatures = this.features.map((feature) => new feature()); |
| 47 | } |
| 48 | return this.initializedFeatures; |
| 49 | } |
| 50 | } |