feat(startup-data-storage): adapt to the new DI architecture
Bug: twpowertools:226
Change-Id: Id65533f9349cc0945bf46ec016c9973608ba7080
diff --git a/src/common/architecture/dependenciesProvider/DependenciesProvider.ts b/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
index 61a7425..8bbe0b8 100644
--- a/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
+++ b/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
@@ -3,7 +3,7 @@
import OptionsProviderAdapter from '../../../infrastructure/services/options/OptionsProvider.adapter';
import WorkflowsImport from '../../../features/workflows/core/communityConsole/import';
import Workflows from '../../../features/workflows/core/communityConsole/workflows';
-import StartupDataStorage from '../../../contentScripts/communityConsole/utils/StartupDataStorage';
+import StartupDataStorageAdapter from '../../../infrastructure/services/communityConsole/StartupDataStorage.adapter';
import ReportDialogColorThemeFix from '../../../features/ccDarkTheme/core/logic/reportDialog';
export const AutoRefreshDependency = 'autoRefresh';
@@ -19,7 +19,7 @@
[ExtraInfoDependency]: ExtraInfo,
[OptionsProviderDependency]: OptionsProviderAdapter,
[ReportDialogColorThemeFixDependency]: ReportDialogColorThemeFix,
- [StartupDataStorageDependency]: StartupDataStorage,
+ [StartupDataStorageDependency]: StartupDataStorageAdapter,
[WorkflowsDependency]: Workflows,
[WorkflowsImportDependency]: WorkflowsImport,
};
diff --git a/src/contentScripts/communityConsole/utils/StartupDataStorage.ts b/src/infrastructure/services/communityConsole/StartupDataStorage.adapter.ts
similarity index 67%
rename from src/contentScripts/communityConsole/utils/StartupDataStorage.ts
rename to src/infrastructure/services/communityConsole/StartupDataStorage.adapter.ts
index 6e67a5d..39e6b90 100644
--- a/src/contentScripts/communityConsole/utils/StartupDataStorage.ts
+++ b/src/infrastructure/services/communityConsole/StartupDataStorage.adapter.ts
@@ -1,30 +1,31 @@
import { Queue } from '@datastructures-js/queue';
import StartupDataModel from '../../../models/StartupData';
+import {
+ StartupDataModification,
+ StartupDataStoragePort,
+} from '../../../services/communityConsole/StartupDataStorage.port';
-type StartupDataModification = (startupData: StartupDataModel) => void;
-
-export default class StartupDataStorage {
+export default class StartupDataStorageAdapter
+ implements StartupDataStoragePort
+{
+ private isSetUp = false;
private startupData: StartupDataModel;
- private modificationsQueue: Queue<StartupDataModification>;
-
- constructor() {
- const rawData = this.getHtmlElement().getAttribute('data-startup');
- this.startupData = new StartupDataModel(rawData ? JSON.parse(rawData) : {});
- this.modificationsQueue = new Queue();
- }
+ private modificationsQueue = new Queue<StartupDataModification>();
get(): StartupDataModel {
+ this.setUp();
return this.startupData;
}
enqueueModification(
modification: StartupDataModification,
- ): StartupDataStorage {
+ ): StartupDataStorageAdapter {
this.modificationsQueue.enqueue(modification);
return this;
}
- applyModifications(): StartupDataStorage {
+ applyModifications(): StartupDataStorageAdapter {
+ this.setUp();
while (!this.modificationsQueue.isEmpty()) {
const modification = this.modificationsQueue.dequeue();
modification(this.startupData);
@@ -33,6 +34,14 @@
return this;
}
+ private setUp() {
+ if (this.isSetUp) return;
+
+ this.isSetUp = true;
+ const rawData = this.getHtmlElement().getAttribute('data-startup');
+ this.startupData = new StartupDataModel(rawData ? JSON.parse(rawData) : {});
+ }
+
private persistToDOM() {
const serializedData = JSON.stringify(this.startupData.data);
this.getHtmlElement().setAttribute('data-startup', serializedData);
diff --git a/src/services/communityConsole/StartupDataStorage.port.ts b/src/services/communityConsole/StartupDataStorage.port.ts
new file mode 100644
index 0000000..827cd36
--- /dev/null
+++ b/src/services/communityConsole/StartupDataStorage.port.ts
@@ -0,0 +1,22 @@
+import StartupDataModel from "../../models/StartupData";
+
+export interface StartupDataStoragePort {
+ /**
+ * Get the startup data as stored in this class (without having applied any
+ * queued modification).
+ *
+ * NOTE: This might not be synced with the live startup data which exists in the
+ * DOM.
+ */
+ get(): StartupDataModel;
+
+ /* Enqueue a function which includes a modification to the startup data. */
+ enqueueModification(
+ modification: StartupDataModification,
+ ): StartupDataStoragePort;
+
+ /* Apply all the queued modifications and persist the result to the DOM. */
+ applyModifications(): StartupDataStoragePort;
+}
+
+export type StartupDataModification = (startupData: StartupDataModel) => void;