Variablen nur in redux store und DevTools redux-toolkit

This commit is contained in:
ISA
2024-11-01 07:24:43 +01:00
parent 9c21cd271c
commit 5c5f816744
4 changed files with 16 additions and 74 deletions

View File

@@ -12,9 +12,8 @@ import store from "../store/store";
function MyApp({ Component, pageProps }) {
useEffect(() => {
const loadAndStoreVariables = async () => {
await loadWindowVariables();
const variables = {}; // Hier Variablen laden und in variables speichern
store.dispatch(setVariables(variables));
const variables = await loadWindowVariables();
store.dispatch(setVariables(variables)); // Speichere die geladenen Variablen im Redux Store
};
if (typeof window !== "undefined") {

View File

@@ -1,6 +1,6 @@
//Modul vorhanden 1 = vorhanden, 0 = nicht vorhanden
var kueOnline = [
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,
];
//---------------------------------------------------

View File

@@ -6,6 +6,7 @@ const store = configureStore({
reducer: {
variables: variablesReducer,
},
devTools: process.env.NODE_ENV !== "production", // Aktiviert DevTools nur in der Entwicklung
});
export default store;

View File

@@ -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);