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