Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 1 | import ExtraInfo from '../../../features/extraInfo/core'; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 2 | import AutoRefresh from '../../../features/autoRefresh/core/autoRefresh'; |
Adrià Vilanova Martínez | 7c941c7 | 2024-10-26 20:23:34 +0200 | [diff] [blame] | 3 | import OptionsProviderAdapter from '../../../infrastructure/services/options/OptionsProvider.adapter'; |
Adrià Vilanova Martínez | c78c3ad | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 4 | import WorkflowsImport from '../../../features/workflows/core/communityConsole/import'; |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 5 | import StartupDataStorageAdapter from '../../../infrastructure/services/communityConsole/StartupDataStorage.adapter'; |
Adrià Vilanova Martínez | 917797e | 2024-05-25 22:41:25 +0200 | [diff] [blame] | 6 | import ReportDialogColorThemeFix from '../../../features/ccDarkTheme/core/logic/reportDialog'; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 7 | |
| 8 | export const AutoRefreshDependency = 'autoRefresh'; |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 9 | export const ExtraInfoDependency = 'extraInfo'; |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 10 | export const OptionsProviderDependency = 'optionsProvider'; |
Adrià Vilanova Martínez | 917797e | 2024-05-25 22:41:25 +0200 | [diff] [blame] | 11 | export const ReportDialogColorThemeFixDependency = |
| 12 | 'report-dialog-color-theme-fix'; |
Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 13 | export const StartupDataStorageDependency = 'startupDataStorage'; |
Adrià Vilanova Martínez | c78c3ad | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 14 | export const WorkflowsImportDependency = 'workflowsImport'; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 15 | export const DependenciesToClass = { |
| 16 | [AutoRefreshDependency]: AutoRefresh, |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 17 | [ExtraInfoDependency]: ExtraInfo, |
Adrià Vilanova Martínez | 6ecaa0d | 2024-10-26 17:04:32 +0200 | [diff] [blame] | 18 | [OptionsProviderDependency]: OptionsProviderAdapter, |
Adrià Vilanova Martínez | 917797e | 2024-05-25 22:41:25 +0200 | [diff] [blame] | 19 | [ReportDialogColorThemeFixDependency]: ReportDialogColorThemeFix, |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 20 | [StartupDataStorageDependency]: StartupDataStorageAdapter, |
Adrià Vilanova Martínez | c78c3ad | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 21 | [WorkflowsImportDependency]: WorkflowsImport, |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | interface OurWindow extends Window { |
| 25 | TWPTDependencies?: Dependencies; |
| 26 | } |
| 27 | |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 28 | export type ClassFromDependency<T extends Dependency> = InstanceType< |
| 29 | (typeof DependenciesToClass)[T] |
| 30 | >; |
| 31 | |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 32 | type Dependencies = { |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 33 | [K in Dependency]?: ClassFromDependency<K>; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | export type Dependency = keyof typeof DependenciesToClass; |
| 37 | |
| 38 | class 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ínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 49 | /** |
| 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ínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 53 | this.setUpDependency(dependency); |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 54 | const dep = this.dependencies[dependency]; |
| 55 | if (!dep) { |
| 56 | throw new Error(`Dependency ${dependency} not found.`); |
| 57 | } |
| 58 | return dep; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 61 | setUpDependency<T extends Dependency>(dependency: T): void { |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 62 | if (!this.dependencies[dependency]) { |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 63 | const dependencyClass = DependenciesToClass[dependency]; |
| 64 | this.dependencies[dependency] = new dependencyClass() as Dependencies[T]; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | export 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 | } |