blob: 68f77fd9b94aa91f9dde0bb57fb7748119731777 [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ínezb523be92024-05-25 19:14:19 +02003import OptionsProvider 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í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íneza2dda312024-05-18 00:35:51 +020011export const StartupDataStorageDependency = 'startupDataStorage';
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020012export const WorkflowsDependency = 'workflows';
13export const WorkflowsImportDependency = 'workflowsImport';
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020014export const DependenciesToClass = {
15 [AutoRefreshDependency]: AutoRefresh,
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020016 [ExtraInfoDependency]: ExtraInfo,
Adrià Vilanova Martínezf7e86852024-05-11 14:16:38 +020017 [OptionsProviderDependency]: OptionsProvider,
Adrià Vilanova Martíneza2dda312024-05-18 00:35:51 +020018 [StartupDataStorageDependency]: StartupDataStorage,
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020019 [WorkflowsDependency]: Workflows,
20 [WorkflowsImportDependency]: WorkflowsImport,
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020021};
22
23interface OurWindow extends Window {
24 TWPTDependencies?: Dependencies;
25}
26
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020027export type ClassFromDependency<T extends Dependency> = InstanceType<
28 (typeof DependenciesToClass)[T]
29>;
30
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020031type Dependencies = {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020032 [K in Dependency]?: ClassFromDependency<K>;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020033};
34
35export type Dependency = keyof typeof DependenciesToClass;
36
37class DependenciesProvider {
38 private dependencies: Dependencies;
39
40 constructor() {
41 const ourWindow = window as OurWindow;
42 if (!ourWindow.TWPTDependencies) {
43 ourWindow.TWPTDependencies = {};
44 }
45 this.dependencies = ourWindow.TWPTDependencies;
46 }
47
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020048 /**
49 * Gets an instance of a dependency, and creates it beforehand if it doesn't exist yet.
50 */
51 getDependency<T extends Dependency>(dependency: T): ClassFromDependency<T> {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020052 this.setUpDependency(dependency);
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020053 const dep = this.dependencies[dependency];
54 if (!dep) {
55 throw new Error(`Dependency ${dependency} not found.`);
56 }
57 return dep;
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020058 }
59
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020060 setUpDependency<T extends Dependency>(dependency: T): void {
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020061 if (!this.dependencies[dependency]) {
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020062 const dependencyClass = DependenciesToClass[dependency];
63 this.dependencies[dependency] = new dependencyClass() as Dependencies[T];
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020064 }
65 }
66}
67
68export default class DependenciesProviderSingleton {
69 private static instance: DependenciesProvider;
70
71 /**
72 * @see {@link DependenciesProviderSingleton.getInstance}
73 */
74 private constructor() {}
75
76 public static getInstance(): DependenciesProvider {
77 if (!DependenciesProviderSingleton.instance) {
78 DependenciesProviderSingleton.instance = new DependenciesProvider();
79 }
80 return DependenciesProviderSingleton.instance;
81 }
82}