blob: 8a13700ed24a4947a24b605160eb84e5e638da0d [file] [log] [blame]
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +02001import Feature from '../common/architecture/features/Feature';
2import {
3 ScriptEnvironment,
4 ScriptPage,
5 ScriptRunPhase,
6} from '../common/architecture/scripts/Script';
Adrià Vilanova Martínezab4c80e2024-05-04 19:53:33 +02007import ScriptRunner from '../common/architecture/scripts/ScriptRunner';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02008import AutoRefreshFeature from './autoRefresh/autoRefresh.feature';
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +02009import InfiniteScrollFeature from './infiniteScroll/infiniteScroll.feature';
10
11export type ConcreteFeatureClass = { new (): Feature };
12
13export interface Context {
14 page: ScriptPage;
15 environment: ScriptEnvironment;
16 runPhase: ScriptRunPhase;
17}
18
19export default class Features {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020020 private features: ConcreteFeatureClass[] = [
21 AutoRefreshFeature,
22 InfiniteScrollFeature,
23 ];
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +020024 private initializedFeatures: Feature[];
25
Adrià Vilanova Martínezab4c80e2024-05-04 19:53:33 +020026 getScriptRunner(context: Context) {
27 const scripts = this.getScripts(context);
28 const scriptRunner = new ScriptRunner();
29 scriptRunner.add(...scripts);
30 return scriptRunner;
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +020031 }
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}