feat(startup-data-storage): adapt to the new DI architecture
Bug: twpowertools:226
Change-Id: Id65533f9349cc0945bf46ec016c9973608ba7080
diff --git a/src/infrastructure/services/communityConsole/StartupDataStorage.adapter.ts b/src/infrastructure/services/communityConsole/StartupDataStorage.adapter.ts
new file mode 100644
index 0000000..39e6b90
--- /dev/null
+++ b/src/infrastructure/services/communityConsole/StartupDataStorage.adapter.ts
@@ -0,0 +1,57 @@
+import { Queue } from '@datastructures-js/queue';
+import StartupDataModel from '../../../models/StartupData';
+import {
+ StartupDataModification,
+ StartupDataStoragePort,
+} from '../../../services/communityConsole/StartupDataStorage.port';
+
+export default class StartupDataStorageAdapter
+ implements StartupDataStoragePort
+{
+ private isSetUp = false;
+ private startupData: StartupDataModel;
+ private modificationsQueue = new Queue<StartupDataModification>();
+
+ get(): StartupDataModel {
+ this.setUp();
+ return this.startupData;
+ }
+
+ enqueueModification(
+ modification: StartupDataModification,
+ ): StartupDataStorageAdapter {
+ this.modificationsQueue.enqueue(modification);
+ return this;
+ }
+
+ applyModifications(): StartupDataStorageAdapter {
+ this.setUp();
+ while (!this.modificationsQueue.isEmpty()) {
+ const modification = this.modificationsQueue.dequeue();
+ modification(this.startupData);
+ }
+ this.persistToDOM();
+ 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);
+ }
+
+ private getHtmlElement() {
+ const htmlElement = document.querySelector('html');
+ if (!htmlElement) {
+ throw new Error("StartupDataStorage: couldn't find the html element.");
+ }
+ return htmlElement;
+ }
+}