blob: ae50e336a54172bec8b33710520f989083042d44 [file] [log] [blame]
import ExtraInfo from '../../../features/extraInfo/core';
import AutoRefresh from '../../../features/autoRefresh/core/autoRefresh';
import OptionsProviderAdapter from '../../../infrastructure/services/options/OptionsProvider.adapter';
import WorkflowsImport from '../../../features/workflows/core/communityConsole/import';
import StartupDataStorageAdapter from '../../../infrastructure/services/communityConsole/StartupDataStorage.adapter';
import ReportDialogColorThemeFix from '../../../features/ccDarkTheme/core/logic/reportDialog';
import ThreadPageDesignWarning from '../../../features/threadPageDesignWarning/core/threadPageDesignWarning';
export const AutoRefreshDependency = 'autoRefresh';
export const ExtraInfoDependency = 'extraInfo';
export const OptionsProviderDependency = 'optionsProvider';
export const ReportDialogColorThemeFixDependency =
'report-dialog-color-theme-fix';
export const StartupDataStorageDependency = 'startupDataStorage';
export const ThreadPageDesignWarningDependency = 'threadPageDesignWarning';
export const WorkflowsImportDependency = 'workflowsImport';
export const DependenciesToClass = {
[AutoRefreshDependency]: AutoRefresh,
[ExtraInfoDependency]: ExtraInfo,
[OptionsProviderDependency]: OptionsProviderAdapter,
[ReportDialogColorThemeFixDependency]: ReportDialogColorThemeFix,
[StartupDataStorageDependency]: StartupDataStorageAdapter,
[ThreadPageDesignWarningDependency]: ThreadPageDesignWarning,
[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;
}
}