blob: 98a4b9682b78bea34b1f44509ecea7c7c4aca892 [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ínez58cc67c2024-04-21 16:43:01 +02007import AutoRefreshFeature from './autoRefresh/autoRefresh.feature';
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +02008import InfiniteScrollFeature from './infiniteScroll/infiniteScroll.feature';
9
10export type ConcreteFeatureClass = { new (): Feature };
11
12export interface Context {
13 page: ScriptPage;
14 environment: ScriptEnvironment;
15 runPhase: ScriptRunPhase;
16}
17
18export default class Features {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020019 private features: ConcreteFeatureClass[] = [
20 AutoRefreshFeature,
21 InfiniteScrollFeature,
22 ];
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +020023 private initializedFeatures: Feature[];
24
25 runScripts(context: Context) {
26 const scripts = this.getScripts(context).sort((a, b) =>
27 a.priority < b.priority ? -1 : 1,
28 );
29 for (const script of scripts) {
30 script.execute();
31 }
32 }
33
34 getScripts(context: Context) {
35 const features = this.getFeatures();
36 const allScripts = features.map((feature) => feature.getScripts()).flat(1);
37 return allScripts.filter(
38 (script) =>
39 script.page === context.page &&
40 script.environment === context.environment &&
41 script.runPhase === context.runPhase,
42 );
43 }
44
45 private getFeatures() {
46 if (this.initializedFeatures === undefined) {
47 this.initializedFeatures = this.features.map((feature) => new feature());
48 }
49 return this.initializedFeatures;
50 }
51}