Variablen nur in redux store und DevTools redux-toolkit
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
// utils/loadWindowVariables.js
|
||||
import { initializeDatabase } from "./indexedDB"; // Importiere initializeDatabase
|
||||
|
||||
export async function loadWindowVariables() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const requiredVars = [
|
||||
@@ -55,77 +53,21 @@ export async function loadWindowVariables() {
|
||||
});
|
||||
};
|
||||
|
||||
const saveToIndexedDB = async (key, value) => {
|
||||
const db = await initializeDatabase();
|
||||
const tx = db.transaction("cplVariables", "readwrite");
|
||||
const store = tx.objectStore("cplVariables");
|
||||
await store.put(value, key);
|
||||
return tx.done;
|
||||
};
|
||||
|
||||
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"];
|
||||
|
||||
scripts
|
||||
.reduce((promise, script) => {
|
||||
return promise.then(() => loadScript(script));
|
||||
}, Promise.resolve())
|
||||
.then(async () => {
|
||||
const checkVariables = () => {
|
||||
const missingVars = requiredVars.filter(
|
||||
(variable) => window[variable] === undefined
|
||||
);
|
||||
return missingVars;
|
||||
};
|
||||
|
||||
const initialMissingVars = checkVariables();
|
||||
|
||||
if (initialMissingVars.length === 0) {
|
||||
console.log("Alle Variablen von CPL geladen.");
|
||||
await saveVariables();
|
||||
resolve();
|
||||
} else {
|
||||
console.log(
|
||||
"Noch fehlende Variablen beim ersten Aufruf:",
|
||||
initialMissingVars
|
||||
);
|
||||
|
||||
const maxChecks = 10;
|
||||
let checkCount = 0;
|
||||
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);
|
||||
console.warn(
|
||||
"Einige Variablen wurden nicht geladen:",
|
||||
remainingMissingVars
|
||||
);
|
||||
reject(
|
||||
new Error(
|
||||
"Einige Variablen fehlen nach wiederholten Versuchen."
|
||||
)
|
||||
);
|
||||
} else {
|
||||
console.log("Noch fehlende Variablen:", remainingMissingVars);
|
||||
}
|
||||
checkCount++;
|
||||
}, 10000); // Überprüfung alle 10 Sekunden
|
||||
}
|
||||
.reduce(
|
||||
(promise, script) => promise.then(() => loadScript(script)),
|
||||
Promise.resolve()
|
||||
)
|
||||
.then(() => {
|
||||
const variablesObj = requiredVars.reduce((acc, variable) => {
|
||||
if (window[variable] !== undefined) {
|
||||
acc[variable] = window[variable];
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
resolve(variablesObj);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Fehler beim Laden eines Skripts:", error);
|
||||
|
||||
Reference in New Issue
Block a user