blob: 7b563bd0ae1bdf3bc4cb0ded7dd8ff78dafc524f [file] [log] [blame]
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +02001import Feature from '../common/architecture/features/Feature';
Adrià Vilanova Martínezab4c80e2024-05-04 19:53:33 +02002import ScriptRunner from '../common/architecture/scripts/ScriptRunner';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02003import AutoRefreshFeature from './autoRefresh/autoRefresh.feature';
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +02004import InfiniteScrollFeature from './infiniteScroll/infiniteScroll.feature';
Adrià Vilanova Martínez104f8ef2024-05-04 17:22:11 +02005import { Context } from '../common/architecture/entrypoint/Context';
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +02006
7export type ConcreteFeatureClass = { new (): Feature };
8
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +02009export default class Features {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020010 private features: ConcreteFeatureClass[] = [
11 AutoRefreshFeature,
12 InfiniteScrollFeature,
13 ];
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +020014 private initializedFeatures: Feature[];
15
Adrià Vilanova Martínezab4c80e2024-05-04 19:53:33 +020016 getScriptRunner(context: Context) {
17 const scripts = this.getScripts(context);
18 const scriptRunner = new ScriptRunner();
19 scriptRunner.add(...scripts);
20 return scriptRunner;
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +020021 }
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}