refactor: migrate autorefresh feature to the new architecture
Bug: twpowertools:176
Change-Id: If000d8617e9a4d66bdc7f91c1037867a4cb9fbf2
diff --git a/src/common/architecture/dependenciesProvider/DependenciesProvider.ts b/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
new file mode 100644
index 0000000..132a67c
--- /dev/null
+++ b/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
@@ -0,0 +1,55 @@
+import AutoRefresh from '../../../features/autoRefresh/core/autoRefresh';
+
+export const AutoRefreshDependency = 'autoRefresh';
+export const DependenciesToClass = {
+ [AutoRefreshDependency]: AutoRefresh,
+};
+
+interface OurWindow extends Window {
+ TWPTDependencies?: Dependencies;
+}
+
+type Dependencies = {
+ [K in Dependency]?: InstanceType<(typeof DependenciesToClass)[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;
+ }
+
+ getDependency(dependency: Dependency) {
+ this.setUpDependency(dependency);
+ return this.dependencies[dependency];
+ }
+
+ setUpDependency(dependency: Dependency): void {
+ if (!this.dependencies[dependency]) {
+ this.dependencies[dependency] = new DependenciesToClass[dependency]();
+ }
+ }
+}
+
+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;
+ }
+}
diff --git a/src/common/architecture/scripts/setUpDependencies/SetUpDependenciesScript.ts b/src/common/architecture/scripts/setUpDependencies/SetUpDependenciesScript.ts
new file mode 100644
index 0000000..b5ff6f0
--- /dev/null
+++ b/src/common/architecture/scripts/setUpDependencies/SetUpDependenciesScript.ts
@@ -0,0 +1,13 @@
+import DependenciesProviderSingleton, { Dependency } from "../../dependenciesProvider/DependenciesProvider";
+import Script from "../Script";
+
+export default abstract class SetUpDependenciesScript extends Script {
+ abstract dependencies: Dependency[];
+
+ execute() {
+ const dependenciesProvider = DependenciesProviderSingleton.getInstance();
+ this.dependencies.forEach(dependency => {
+ dependenciesProvider.setUpDependency(dependency);
+ });
+ }
+}