blob: 4f01b82011128841cbb3b5edf7d895a0904929b9 [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ínez6ecaa0d2024-10-26 17:04:32 +02003import OptionsProviderAdapter from '../../options/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íneza2dda312024-05-18 00:35:51 +02006import StartupDataStorage from '../../../contentScripts/communityConsole/utils/StartupDataStorage';
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +02007import ReportDialogColorThemeFix from '../../../features/ccDarkTheme/core/logic/reportDialog';
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ínezc78c3ad2024-05-18 00:35:51 +020015export const WorkflowsDependency = 'workflows';
16export 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íneza2dda312024-05-18 00:35:51 +020022 [StartupDataStorageDependency]: StartupDataStorage,
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020023 [WorkflowsDependency]: Workflows,
24 [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}