blob: b6af2a13b7ad1acc2090954830c90a1826d2a873 [file] [log] [blame]
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +02001import ExtraInfo from '../../../features/extraInfo/core';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02002import AutoRefresh from '../../../features/autoRefresh/core/autoRefresh';
3
4export const AutoRefreshDependency = 'autoRefresh';
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +02005export const ExtraInfoDependency = 'extraInfo';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02006export const DependenciesToClass = {
7 [AutoRefreshDependency]: AutoRefresh,
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +02008 [ExtraInfoDependency]: ExtraInfo,
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02009};
10
11interface OurWindow extends Window {
12 TWPTDependencies?: Dependencies;
13}
14
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020015export type ClassFromDependency<T extends Dependency> = InstanceType<
16 (typeof DependenciesToClass)[T]
17>;
18
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020019type Dependencies = {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020020 [K in Dependency]?: ClassFromDependency<K>;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020021};
22
23export type Dependency = keyof typeof DependenciesToClass;
24
25class DependenciesProvider {
26 private dependencies: Dependencies;
27
28 constructor() {
29 const ourWindow = window as OurWindow;
30 if (!ourWindow.TWPTDependencies) {
31 ourWindow.TWPTDependencies = {};
32 }
33 this.dependencies = ourWindow.TWPTDependencies;
34 }
35
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020036 /**
37 * Gets an instance of a dependency, and creates it beforehand if it doesn't exist yet.
38 */
39 getDependency<T extends Dependency>(dependency: T): ClassFromDependency<T> {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020040 this.setUpDependency(dependency);
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020041 const dep = this.dependencies[dependency];
42 if (!dep) {
43 throw new Error(`Dependency ${dependency} not found.`);
44 }
45 return dep;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020046 }
47
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020048 setUpDependency<T extends Dependency>(dependency: T): void {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020049 if (!this.dependencies[dependency]) {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020050 const dependencyClass = DependenciesToClass[dependency];
51 this.dependencies[dependency] = new dependencyClass() as Dependencies[T];
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020052 }
53 }
54}
55
56export default class DependenciesProviderSingleton {
57 private static instance: DependenciesProvider;
58
59 /**
60 * @see {@link DependenciesProviderSingleton.getInstance}
61 */
62 private constructor() {}
63
64 public static getInstance(): DependenciesProvider {
65 if (!DependenciesProviderSingleton.instance) {
66 DependenciesProviderSingleton.instance = new DependenciesProvider();
67 }
68 return DependenciesProviderSingleton.instance;
69 }
70}