blob: ae50e336a54172bec8b33710520f989083042d44 [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ínez4e847f42024-11-09 23:03:32 +01007import ThreadPageDesignWarning from '../../../features/threadPageDesignWarning/core/threadPageDesignWarning';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +02008
9export const AutoRefreshDependency = 'autoRefresh';
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020010export const ExtraInfoDependency = 'extraInfo';
Adrià Vilanova Martínezf7e86852024-05-11 14:16:38 +020011export const OptionsProviderDependency = 'optionsProvider';
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +020012export const ReportDialogColorThemeFixDependency =
13 'report-dialog-color-theme-fix';
Adrià Vilanova Martíneza2dda312024-05-18 00:35:51 +020014export const StartupDataStorageDependency = 'startupDataStorage';
Adrià Vilanova Martínez4e847f42024-11-09 23:03:32 +010015export const ThreadPageDesignWarningDependency = 'threadPageDesignWarning';
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020016export const WorkflowsImportDependency = 'workflowsImport';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020017export const DependenciesToClass = {
18 [AutoRefreshDependency]: AutoRefresh,
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020019 [ExtraInfoDependency]: ExtraInfo,
Adrià Vilanova Martínez6ecaa0d2024-10-26 17:04:32 +020020 [OptionsProviderDependency]: OptionsProviderAdapter,
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +020021 [ReportDialogColorThemeFixDependency]: ReportDialogColorThemeFix,
Adrià Vilanova Martínezedbb73e2024-11-09 19:26:19 +010022 [StartupDataStorageDependency]: StartupDataStorageAdapter,
Adrià Vilanova Martínez4e847f42024-11-09 23:03:32 +010023 [ThreadPageDesignWarningDependency]: ThreadPageDesignWarning,
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020024 [WorkflowsImportDependency]: WorkflowsImport,
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020025};
26
27interface OurWindow extends Window {
28 TWPTDependencies?: Dependencies;
29}
30
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020031export type ClassFromDependency<T extends Dependency> = InstanceType<
32 (typeof DependenciesToClass)[T]
33>;
34
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020035type Dependencies = {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020036 [K in Dependency]?: ClassFromDependency<K>;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020037};
38
39export type Dependency = keyof typeof DependenciesToClass;
40
41class DependenciesProvider {
42 private dependencies: Dependencies;
43
44 constructor() {
45 const ourWindow = window as OurWindow;
46 if (!ourWindow.TWPTDependencies) {
47 ourWindow.TWPTDependencies = {};
48 }
49 this.dependencies = ourWindow.TWPTDependencies;
50 }
51
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020052 /**
53 * Gets an instance of a dependency, and creates it beforehand if it doesn't exist yet.
54 */
55 getDependency<T extends Dependency>(dependency: T): ClassFromDependency<T> {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020056 this.setUpDependency(dependency);
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020057 const dep = this.dependencies[dependency];
58 if (!dep) {
59 throw new Error(`Dependency ${dependency} not found.`);
60 }
61 return dep;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020062 }
63
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020064 setUpDependency<T extends Dependency>(dependency: T): void {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020065 if (!this.dependencies[dependency]) {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020066 const dependencyClass = DependenciesToClass[dependency];
67 this.dependencies[dependency] = new dependencyClass() as Dependencies[T];
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020068 }
69 }
70}
71
72export default class DependenciesProviderSingleton {
73 private static instance: DependenciesProvider;
74
75 /**
76 * @see {@link DependenciesProviderSingleton.getInstance}
77 */
78 private constructor() {}
79
80 public static getInstance(): DependenciesProvider {
81 if (!DependenciesProviderSingleton.instance) {
82 DependenciesProviderSingleton.instance = new DependenciesProvider();
83 }
84 return DependenciesProviderSingleton.instance;
85 }
86}