blob: 02e25ce032ad8aaaaac5d647d0e3fe75fe132378 [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ínez7c941c72024-10-26 20:23:34 +02003import OptionsProviderAdapter from '../../../infrastructure/services/options/OptionsProvider.adapter';
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +02004import WorkflowsImport from '../../../features/workflows/core/communityConsole/import';
Adrià Vilanova Martínezedbb73e2024-11-09 19:26:19 +01005import StartupDataStorageAdapter from '../../../infrastructure/services/communityConsole/StartupDataStorage.adapter';
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +02006import ReportDialogColorThemeFix from '../../../features/ccDarkTheme/core/logic/reportDialog';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02007
8export const AutoRefreshDependency = 'autoRefresh';
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +02009export const ExtraInfoDependency = 'extraInfo';
Adrià Vilanova Martínezf7e86852024-05-11 14:16:38 +020010export const OptionsProviderDependency = 'optionsProvider';
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +020011export const ReportDialogColorThemeFixDependency =
12 'report-dialog-color-theme-fix';
Adrià Vilanova Martíneza2dda312024-05-18 00:35:51 +020013export const StartupDataStorageDependency = 'startupDataStorage';
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020014export const WorkflowsImportDependency = 'workflowsImport';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020015export const DependenciesToClass = {
16 [AutoRefreshDependency]: AutoRefresh,
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020017 [ExtraInfoDependency]: ExtraInfo,
Adrià Vilanova Martínez6ecaa0d2024-10-26 17:04:32 +020018 [OptionsProviderDependency]: OptionsProviderAdapter,
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +020019 [ReportDialogColorThemeFixDependency]: ReportDialogColorThemeFix,
Adrià Vilanova Martínezedbb73e2024-11-09 19:26:19 +010020 [StartupDataStorageDependency]: StartupDataStorageAdapter,
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020021 [WorkflowsImportDependency]: WorkflowsImport,
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020022};
23
24interface OurWindow extends Window {
25 TWPTDependencies?: Dependencies;
26}
27
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020028export type ClassFromDependency<T extends Dependency> = InstanceType<
29 (typeof DependenciesToClass)[T]
30>;
31
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020032type Dependencies = {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020033 [K in Dependency]?: ClassFromDependency<K>;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020034};
35
36export type Dependency = keyof typeof DependenciesToClass;
37
38class DependenciesProvider {
39 private dependencies: Dependencies;
40
41 constructor() {
42 const ourWindow = window as OurWindow;
43 if (!ourWindow.TWPTDependencies) {
44 ourWindow.TWPTDependencies = {};
45 }
46 this.dependencies = ourWindow.TWPTDependencies;
47 }
48
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020049 /**
50 * Gets an instance of a dependency, and creates it beforehand if it doesn't exist yet.
51 */
52 getDependency<T extends Dependency>(dependency: T): ClassFromDependency<T> {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020053 this.setUpDependency(dependency);
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020054 const dep = this.dependencies[dependency];
55 if (!dep) {
56 throw new Error(`Dependency ${dependency} not found.`);
57 }
58 return dep;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020059 }
60
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020061 setUpDependency<T extends Dependency>(dependency: T): void {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020062 if (!this.dependencies[dependency]) {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020063 const dependencyClass = DependenciesToClass[dependency];
64 this.dependencies[dependency] = new dependencyClass() as Dependencies[T];
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020065 }
66 }
67}
68
69export default class DependenciesProviderSingleton {
70 private static instance: DependenciesProvider;
71
72 /**
73 * @see {@link DependenciesProviderSingleton.getInstance}
74 */
75 private constructor() {}
76
77 public static getInstance(): DependenciesProvider {
78 if (!DependenciesProviderSingleton.instance) {
79 DependenciesProviderSingleton.instance = new DependenciesProvider();
80 }
81 return DependenciesProviderSingleton.instance;
82 }
83}