From cbfd86532347708648a968a8fe879244131e880b Mon Sep 17 00:00:00 2001 From: ISA Date: Wed, 30 Oct 2024 08:04:33 +0100 Subject: [PATCH] feat: Optimized variable loading and storage in IndexedDB and localStorage - Added parallel saving of required variables to both IndexedDB and localStorage using Promise.all for improved performance. - Implemented a recursive check for missing variables with a retry mechanism for reliable loading. - Enhanced error handling for script loading and storage processes. - Streamlined loading scripts sequentially and storing in IndexedDB and localStorage upon successful loading. --- utils/loadWindowVariables.js | 44 ++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/utils/loadWindowVariables.js b/utils/loadWindowVariables.js index 2bfa4ae..e375fc5 100644 --- a/utils/loadWindowVariables.js +++ b/utils/loadWindowVariables.js @@ -54,6 +54,38 @@ export async function loadWindowVariables() { }); }; + const saveToIndexedDB = async (key, value) => { + const request = indexedDB.open("CPLDatabase", 1); + request.onupgradeneeded = () => { + const db = request.result; + if (!db.objectStoreNames.contains("cplVariables")) { + db.createObjectStore("cplVariables"); + } + }; + + return new Promise((resolve, reject) => { + request.onsuccess = () => { + const db = request.result; + const tx = db.transaction("cplVariables", "readwrite"); + const store = tx.objectStore("cplVariables"); + store.put(value, key); + tx.oncomplete = resolve; + tx.onerror = reject; + }; + request.onerror = reject; + }); + }; + + const saveVariables = async () => { + const savePromises = requiredVars.map(async (variable) => { + if (window[variable] !== undefined) { + localStorage.setItem(variable, window[variable]); + await saveToIndexedDB(variable, window[variable]); + } + }); + await Promise.all(savePromises); + }; + const scripts = ["de.js", "kueData.js", "Start.js", "System.js"]; // Lade die Skripte nacheinander @@ -61,7 +93,7 @@ export async function loadWindowVariables() { .reduce((promise, script) => { return promise.then(() => loadScript(script)); }, Promise.resolve()) - .then(() => { + .then(async () => { const checkVariables = () => { const missingVars = requiredVars.filter( (variable) => window[variable] === undefined @@ -69,13 +101,11 @@ export async function loadWindowVariables() { return missingVars; }; - // Einmalige Überprüfung nach dem Laden aller Skripte const initialMissingVars = checkVariables(); if (initialMissingVars.length === 0) { - console.log( - "Alle Variablen von CPL geladen. in loadWindowVariables.js" - ); + console.log("Alle Variablen von CPL geladen."); + await saveVariables(); resolve(); } else { console.log( @@ -83,15 +113,15 @@ export async function loadWindowVariables() { initialMissingVars ); - // Falls Variablen fehlen, starte ein Intervall zur wiederholten Überprüfung const maxChecks = 10; let checkCount = 0; - const checkInterval = setInterval(() => { + const checkInterval = setInterval(async () => { const remainingMissingVars = checkVariables(); if (remainingMissingVars.length === 0) { clearInterval(checkInterval); console.log("Alle fehlenden Systemvariablen sind jetzt geladen."); + await saveVariables(); resolve(); } else if (checkCount >= maxChecks) { clearInterval(checkInterval);