blob: 221cc51175094d74c8b92cdebb75f63b113264da [file] [log] [blame]
import ExtraInfo from '../../../features/extraInfo/core';
import AutoRefresh from '../../../features/autoRefresh/core/autoRefresh';
import OptionsProvider from '../../OptionsProvider';
import WorkflowsImport from '../../../features/workflows/core/communityConsole/import';
import Workflows from '../../../features/workflows/core/communityConsole/workflows';
import StartupDataStorage from '../../../contentScripts/communityConsole/utils/StartupDataStorage';
export const AutoRefreshDependency = 'autoRefresh';
export const ExtraInfoDependency = 'extraInfo';
export const OptionsProviderDependency = 'optionsProvider';
export const StartupDataStorageDependency = 'startupDataStorage';
export const WorkflowsDependency = 'workflows';
export const WorkflowsImportDependency = 'workflowsImport';
export const DependenciesToClass = {
[AutoRefreshDependency]: AutoRefresh,
[ExtraInfoDependency]: ExtraInfo,
[OptionsProviderDependency]: OptionsProvider,
[StartupDataStorageDependency]: StartupDataStorage,
[WorkflowsDependency]: Workflows,
[WorkflowsImportDependency]: WorkflowsImport,
};
interface OurWindow extends Window {
TWPTDependencies?: Dependencies;
}
export type ClassFromDependency<T extends Dependency> = InstanceType<
(typeof DependenciesToClass)[T]
>;
type Dependencies = {
[K in Dependency]?: ClassFromDependency<K>;
};
export type Dependency = keyof typeof DependenciesToClass;
class DependenciesProvider {
private dependencies: Dependencies;
constructor() {
const ourWindow = window as OurWindow;
if (!ourWindow.TWPTDependencies) {
ourWindow.TWPTDependencies = {};
}
this.dependencies = ourWindow.TWPTDependencies;
}
/**
* Gets an instance of a dependency, and creates it beforehand if it doesn't exist yet.
*/
getDependency<T extends Dependency>(dependency: T): ClassFromDependency<T> {
this.setUpDependency(dependency);
const dep = this.dependencies[dependency];
if (!dep) {
throw new Error(`Dependency ${dependency} not found.`);
}
return dep;
}
setUpDependency<T extends Dependency>(dependency: T): void {
if (!this.dependencies[dependency]) {
const dependencyClass = DependenciesToClass[dependency];
this.dependencies[dependency] = new dependencyClass() as Dependencies[T];
}
}
}
export default class DependenciesProviderSingleton {
private static instance: DependenciesProvider;
/**
* @see {@link DependenciesProviderSingleton.getInstance}
*/
private constructor() {}
public static getInstance(): DependenciesProvider {
if (!DependenciesProviderSingleton.instance) {
DependenciesProviderSingleton.instance = new DependenciesProvider();
}
return DependenciesProviderSingleton.instance;
}
}