blob: 5c7ec33c9af477efde88a96f00e3496fec730238 [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ínezc78c3ad2024-05-18 00:35:51 +02004import WorkflowsImport from '../../../features/workflows/core/communityConsole/import';
5import Workflows from '../../../features/workflows/core/communityConsole/workflows';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02006
7export const AutoRefreshDependency = 'autoRefresh';
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +02008export const ExtraInfoDependency = 'extraInfo';
Adrià Vilanova Martínezf7e86852024-05-11 14:16:38 +02009export const OptionsProviderDependency = 'optionsProvider';
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020010export const WorkflowsDependency = 'workflows';
11export const WorkflowsImportDependency = 'workflowsImport';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020012export const DependenciesToClass = {
13 [AutoRefreshDependency]: AutoRefresh,
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020014 [ExtraInfoDependency]: ExtraInfo,
Adrià Vilanova Martínezf7e86852024-05-11 14:16:38 +020015 [OptionsProviderDependency]: OptionsProvider,
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020016 [WorkflowsDependency]: Workflows,
17 [WorkflowsImportDependency]: WorkflowsImport,
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020018};
19
20interface OurWindow extends Window {
21 TWPTDependencies?: Dependencies;
22}
23
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020024export type ClassFromDependency<T extends Dependency> = InstanceType<
25 (typeof DependenciesToClass)[T]
26>;
27
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020028type Dependencies = {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020029 [K in Dependency]?: ClassFromDependency<K>;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020030};
31
32export type Dependency = keyof typeof DependenciesToClass;
33
34class DependenciesProvider {
35 private dependencies: Dependencies;
36
37 constructor() {
38 const ourWindow = window as OurWindow;
39 if (!ourWindow.TWPTDependencies) {
40 ourWindow.TWPTDependencies = {};
41 }
42 this.dependencies = ourWindow.TWPTDependencies;
43 }
44
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020045 /**
46 * Gets an instance of a dependency, and creates it beforehand if it doesn't exist yet.
47 */
48 getDependency<T extends Dependency>(dependency: T): ClassFromDependency<T> {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020049 this.setUpDependency(dependency);
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020050 const dep = this.dependencies[dependency];
51 if (!dep) {
52 throw new Error(`Dependency ${dependency} not found.`);
53 }
54 return dep;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020055 }
56
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020057 setUpDependency<T extends Dependency>(dependency: T): void {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020058 if (!this.dependencies[dependency]) {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020059 const dependencyClass = DependenciesToClass[dependency];
60 this.dependencies[dependency] = new dependencyClass() as Dependencies[T];
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020061 }
62 }
63}
64
65export default class DependenciesProviderSingleton {
66 private static instance: DependenciesProvider;
67
68 /**
69 * @see {@link DependenciesProviderSingleton.getInstance}
70 */
71 private constructor() {}
72
73 public static getInstance(): DependenciesProvider {
74 if (!DependenciesProviderSingleton.instance) {
75 DependenciesProviderSingleton.instance = new DependenciesProvider();
76 }
77 return DependenciesProviderSingleton.instance;
78 }
79}