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.
This commit is contained in:
@@ -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"];
|
const scripts = ["de.js", "kueData.js", "Start.js", "System.js"];
|
||||||
|
|
||||||
// Lade die Skripte nacheinander
|
// Lade die Skripte nacheinander
|
||||||
@@ -61,7 +93,7 @@ export async function loadWindowVariables() {
|
|||||||
.reduce((promise, script) => {
|
.reduce((promise, script) => {
|
||||||
return promise.then(() => loadScript(script));
|
return promise.then(() => loadScript(script));
|
||||||
}, Promise.resolve())
|
}, Promise.resolve())
|
||||||
.then(() => {
|
.then(async () => {
|
||||||
const checkVariables = () => {
|
const checkVariables = () => {
|
||||||
const missingVars = requiredVars.filter(
|
const missingVars = requiredVars.filter(
|
||||||
(variable) => window[variable] === undefined
|
(variable) => window[variable] === undefined
|
||||||
@@ -69,13 +101,11 @@ export async function loadWindowVariables() {
|
|||||||
return missingVars;
|
return missingVars;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Einmalige Überprüfung nach dem Laden aller Skripte
|
|
||||||
const initialMissingVars = checkVariables();
|
const initialMissingVars = checkVariables();
|
||||||
|
|
||||||
if (initialMissingVars.length === 0) {
|
if (initialMissingVars.length === 0) {
|
||||||
console.log(
|
console.log("Alle Variablen von CPL geladen.");
|
||||||
"Alle Variablen von CPL geladen. in loadWindowVariables.js"
|
await saveVariables();
|
||||||
);
|
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
@@ -83,15 +113,15 @@ export async function loadWindowVariables() {
|
|||||||
initialMissingVars
|
initialMissingVars
|
||||||
);
|
);
|
||||||
|
|
||||||
// Falls Variablen fehlen, starte ein Intervall zur wiederholten Überprüfung
|
|
||||||
const maxChecks = 10;
|
const maxChecks = 10;
|
||||||
let checkCount = 0;
|
let checkCount = 0;
|
||||||
const checkInterval = setInterval(() => {
|
const checkInterval = setInterval(async () => {
|
||||||
const remainingMissingVars = checkVariables();
|
const remainingMissingVars = checkVariables();
|
||||||
|
|
||||||
if (remainingMissingVars.length === 0) {
|
if (remainingMissingVars.length === 0) {
|
||||||
clearInterval(checkInterval);
|
clearInterval(checkInterval);
|
||||||
console.log("Alle fehlenden Systemvariablen sind jetzt geladen.");
|
console.log("Alle fehlenden Systemvariablen sind jetzt geladen.");
|
||||||
|
await saveVariables();
|
||||||
resolve();
|
resolve();
|
||||||
} else if (checkCount >= maxChecks) {
|
} else if (checkCount >= maxChecks) {
|
||||||
clearInterval(checkInterval);
|
clearInterval(checkInterval);
|
||||||
|
|||||||
Reference in New Issue
Block a user