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