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