Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 1 | import { Queue } from '@datastructures-js/queue'; |
| 2 | import StartupDataModel from '../../../models/StartupData'; |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 3 | import { |
| 4 | StartupDataModification, |
| 5 | StartupDataStoragePort, |
| 6 | } from '../../../services/communityConsole/StartupDataStorage.port'; |
Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 7 | |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 8 | export default class StartupDataStorageAdapter |
| 9 | implements StartupDataStoragePort |
| 10 | { |
| 11 | private isSetUp = false; |
Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 12 | private startupData: StartupDataModel; |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 13 | private modificationsQueue = new Queue<StartupDataModification>(); |
Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 14 | |
| 15 | get(): StartupDataModel { |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 16 | this.setUp(); |
Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 17 | return this.startupData; |
| 18 | } |
| 19 | |
| 20 | enqueueModification( |
| 21 | modification: StartupDataModification, |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 22 | ): StartupDataStorageAdapter { |
Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 23 | this.modificationsQueue.enqueue(modification); |
| 24 | return this; |
| 25 | } |
| 26 | |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 27 | applyModifications(): StartupDataStorageAdapter { |
| 28 | this.setUp(); |
Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 29 | while (!this.modificationsQueue.isEmpty()) { |
| 30 | const modification = this.modificationsQueue.dequeue(); |
| 31 | modification(this.startupData); |
| 32 | } |
| 33 | this.persistToDOM(); |
| 34 | return this; |
| 35 | } |
| 36 | |
Adrià Vilanova Martínez | edbb73e | 2024-11-09 19:26:19 +0100 | [diff] [blame] | 37 | private setUp() { |
| 38 | if (this.isSetUp) return; |
| 39 | |
| 40 | this.isSetUp = true; |
| 41 | const rawData = this.getHtmlElement().getAttribute('data-startup'); |
| 42 | this.startupData = new StartupDataModel(rawData ? JSON.parse(rawData) : {}); |
| 43 | } |
| 44 | |
Adrià Vilanova Martínez | a2dda31 | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 45 | private persistToDOM() { |
| 46 | const serializedData = JSON.stringify(this.startupData.data); |
| 47 | this.getHtmlElement().setAttribute('data-startup', serializedData); |
| 48 | } |
| 49 | |
| 50 | private getHtmlElement() { |
| 51 | const htmlElement = document.querySelector('html'); |
| 52 | if (!htmlElement) { |
| 53 | throw new Error("StartupDataStorage: couldn't find the html element."); |
| 54 | } |
| 55 | return htmlElement; |
| 56 | } |
| 57 | } |