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 }) { function MyApp({ Component, pageProps }) {
useEffect(() => { useEffect(() => {
const loadAndStoreVariables = async () => { const loadAndStoreVariables = async () => {
await loadWindowVariables(); const variables = await loadWindowVariables();
const variables = {}; // Hier Variablen laden und in variables speichern store.dispatch(setVariables(variables)); // Speichere die geladenen Variablen im Redux Store
store.dispatch(setVariables(variables));
}; };
if (typeof window !== "undefined") { if (typeof window !== "undefined") {

View File

@@ -1,6 +1,6 @@
//Modul vorhanden 1 = vorhanden, 0 = nicht vorhanden //Modul vorhanden 1 = vorhanden, 0 = nicht vorhanden
var kueOnline = [ 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, 1, 1, 1, 1, 1, 1,
]; ];
//--------------------------------------------------- //---------------------------------------------------

View File

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

View File

@@ -1,6 +1,4 @@
// utils/loadWindowVariables.js // utils/loadWindowVariables.js
import { initializeDatabase } from "./indexedDB"; // Importiere initializeDatabase
export async function loadWindowVariables() { export async function loadWindowVariables() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const requiredVars = [ 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"]; const scripts = ["de.js", "kueData.js", "Start.js", "System.js"];
scripts scripts
.reduce((promise, script) => { .reduce(
return promise.then(() => loadScript(script)); (promise, script) => promise.then(() => loadScript(script)),
}, Promise.resolve()) Promise.resolve()
.then(async () => { )
const checkVariables = () => { .then(() => {
const missingVars = requiredVars.filter( const variablesObj = requiredVars.reduce((acc, variable) => {
(variable) => window[variable] === undefined if (window[variable] !== undefined) {
); acc[variable] = window[variable];
return missingVars; }
}; return acc;
}, {});
const initialMissingVars = checkVariables(); resolve(variablesObj);
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
}
}) })
.catch((error) => { .catch((error) => {
console.error("Fehler beim Laden eines Skripts:", error); console.error("Fehler beim Laden eines Skripts:", error);