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