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