Fix writeQueue ReferenceError in logger.ts

This commit is contained in:
2026-06-01 10:22:52 +02:00
parent 182c37a99c
commit 50df853ca8
+27 -13
View File
@@ -35,6 +35,12 @@ const processLogQueue = async () => {
isWriting = false; isWriting = false;
}; };
const waitForQueueDrain = async () => {
while (isWriting || logQueue.length > 0) {
await new Promise(resolve => setTimeout(resolve, 50));
}
};
const enqueueWrite = async (logLine: string) => { const enqueueWrite = async (logLine: string) => {
logQueue.push(logLine); logQueue.push(logLine);
processLogQueue(); // Intentionally unawaited to run in background processLogQueue(); // Intentionally unawaited to run in background
@@ -68,24 +74,32 @@ export const logger = {
readLogs: async (): Promise<string> => { readLogs: async (): Promise<string> => {
try { try {
// Ensure we wait for pending writes before reading // Wait for pending writes to finish
await writeQueue; await waitForQueueDrain();
const fileInfo = await FileSystem.getInfoAsync(logFileUri); isWriting = true; // lock the queue while reading
if (!fileInfo.exists) return 'Keine Logs vorhanden.'; try {
return await FileSystem.readAsStringAsync(logFileUri, { encoding: FileSystem.EncodingType.UTF8 }); const fileInfo = await FileSystem.getInfoAsync(logFileUri);
if (!fileInfo.exists) return 'Keine Logs vorhanden.';
return await FileSystem.readAsStringAsync(logFileUri, { encoding: FileSystem.EncodingType.UTF8 });
} finally {
isWriting = false;
processLogQueue();
}
} catch (e) { } catch (e) {
return `Fehler beim Lesen der Logs: ${e}`; return `Fehler beim Lesen der Logs: ${e}`;
} }
}, },
clearLogs: async () => { clearLogs: async () => {
writeQueue = writeQueue.then(async () => { await waitForQueueDrain();
try { isWriting = true; // lock the queue while clearing
await FileSystem.deleteAsync(logFileUri, { idempotent: true }); try {
} catch (e) { await FileSystem.deleteAsync(logFileUri, { idempotent: true });
console.warn('Failed to clear logs:', e); } catch (e) {
} console.warn('Failed to clear logs:', e);
}); } finally {
return writeQueue; isWriting = false;
processLogQueue();
}
} }
}; };