blob: 105e6ac834baac05eb12632d555c168cf560bef5 [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';
Adrià Vilanova Martínezf7e86852024-05-11 14:16:38 +02003import OptionsProvider from '../../OptionsProvider';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02004
5export const AutoRefreshDependency = 'autoRefresh';
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +02006export const ExtraInfoDependency = 'extraInfo';
Adrià Vilanova Martínezf7e86852024-05-11 14:16:38 +02007export const OptionsProviderDependency = 'optionsProvider';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02008export const DependenciesToClass = {
9 [AutoRefreshDependency]: AutoRefresh,
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020010 [ExtraInfoDependency]: ExtraInfo,
Adrià Vilanova Martínezf7e86852024-05-11 14:16:38 +020011 [OptionsProviderDependency]: OptionsProvider,
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020012};
13
14interface OurWindow extends Window {
15 TWPTDependencies?: Dependencies;
16}
17
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020018export type ClassFromDependency<T extends Dependency> = InstanceType<
19 (typeof DependenciesToClass)[T]
20>;
21
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020022type Dependencies = {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020023 [K in Dependency]?: ClassFromDependency<K>;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020024};
25
26export type Dependency = keyof typeof DependenciesToClass;
27
28class DependenciesProvider {
29 private dependencies: Dependencies;
30
31 constructor() {
32 const ourWindow = window as OurWindow;
33 if (!ourWindow.TWPTDependencies) {
34 ourWindow.TWPTDependencies = {};
35 }
36 this.dependencies = ourWindow.TWPTDependencies;
37 }
38
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020039 /**
40 * Gets an instance of a dependency, and creates it beforehand if it doesn't exist yet.
41 */
42 getDependency<T extends Dependency>(dependency: T): ClassFromDependency<T> {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020043 this.setUpDependency(dependency);
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020044 const dep = this.dependencies[dependency];
45 if (!dep) {
46 throw new Error(`Dependency ${dependency} not found.`);
47 }
48 return dep;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020049 }
50
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020051 setUpDependency<T extends Dependency>(dependency: T): void {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020052 if (!this.dependencies[dependency]) {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020053 const dependencyClass = DependenciesToClass[dependency];
54 this.dependencies[dependency] = new dependencyClass() as Dependencies[T];
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020055 }
56 }
57}
58
59export default class DependenciesProviderSingleton {
60 private static instance: DependenciesProvider;
61
62 /**
63 * @see {@link DependenciesProviderSingleton.getInstance}
64 */
65 private constructor() {}
66
67 public static getInstance(): DependenciesProvider {
68 if (!DependenciesProviderSingleton.instance) {
69 DependenciesProviderSingleton.instance = new DependenciesProvider();
70 }
71 return DependenciesProviderSingleton.instance;
72 }
73}