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 | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 3 | import OptionsProvider from '../../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 | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 6 | |
| 7 | export const AutoRefreshDependency = 'autoRefresh'; |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 8 | export const ExtraInfoDependency = 'extraInfo'; |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 9 | export const OptionsProviderDependency = 'optionsProvider'; |
Adrià Vilanova Martínez | c78c3ad | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 10 | export const WorkflowsDependency = 'workflows'; |
| 11 | export const WorkflowsImportDependency = 'workflowsImport'; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 12 | export const DependenciesToClass = { |
| 13 | [AutoRefreshDependency]: AutoRefresh, |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 14 | [ExtraInfoDependency]: ExtraInfo, |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 15 | [OptionsProviderDependency]: OptionsProvider, |
Adrià Vilanova Martínez | c78c3ad | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 16 | [WorkflowsDependency]: Workflows, |
| 17 | [WorkflowsImportDependency]: WorkflowsImport, |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | interface OurWindow extends Window { |
| 21 | TWPTDependencies?: Dependencies; |
| 22 | } |
| 23 | |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 24 | export type ClassFromDependency<T extends Dependency> = InstanceType< |
| 25 | (typeof DependenciesToClass)[T] |
| 26 | >; |
| 27 | |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 28 | type Dependencies = { |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 29 | [K in Dependency]?: ClassFromDependency<K>; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | export type Dependency = keyof typeof DependenciesToClass; |
| 33 | |
| 34 | class DependenciesProvider { |
| 35 | private dependencies: Dependencies; |
| 36 | |
| 37 | constructor() { |
| 38 | const ourWindow = window as OurWindow; |
| 39 | if (!ourWindow.TWPTDependencies) { |
| 40 | ourWindow.TWPTDependencies = {}; |
| 41 | } |
| 42 | this.dependencies = ourWindow.TWPTDependencies; |
| 43 | } |
| 44 | |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 45 | /** |
| 46 | * Gets an instance of a dependency, and creates it beforehand if it doesn't exist yet. |
| 47 | */ |
| 48 | getDependency<T extends Dependency>(dependency: T): ClassFromDependency<T> { |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 49 | this.setUpDependency(dependency); |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 50 | const dep = this.dependencies[dependency]; |
| 51 | if (!dep) { |
| 52 | throw new Error(`Dependency ${dependency} not found.`); |
| 53 | } |
| 54 | return dep; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 57 | setUpDependency<T extends Dependency>(dependency: T): void { |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 58 | if (!this.dependencies[dependency]) { |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 59 | const dependencyClass = DependenciesToClass[dependency]; |
| 60 | this.dependencies[dependency] = new dependencyClass() as Dependencies[T]; |
Adrià Vilanova Martínez | 58cc67c | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | export default class DependenciesProviderSingleton { |
| 66 | private static instance: DependenciesProvider; |
| 67 | |
| 68 | /** |
| 69 | * @see {@link DependenciesProviderSingleton.getInstance} |
| 70 | */ |
| 71 | private constructor() {} |
| 72 | |
| 73 | public static getInstance(): DependenciesProvider { |
| 74 | if (!DependenciesProviderSingleton.instance) { |
| 75 | DependenciesProviderSingleton.instance = new DependenciesProvider(); |
| 76 | } |
| 77 | return DependenciesProviderSingleton.instance; |
| 78 | } |
| 79 | } |