Fix writeQueue ReferenceError in logger.ts

This commit is contained in:
2026-06-01 10:22:52 +02:00
parent 182c37a99c
commit 50df853ca8
+19 -5
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();
isWriting = true; // lock the queue while reading
try {
const fileInfo = await FileSystem.getInfoAsync(logFileUri); const fileInfo = await FileSystem.getInfoAsync(logFileUri);
if (!fileInfo.exists) return 'Keine Logs vorhanden.'; if (!fileInfo.exists) return 'Keine Logs vorhanden.';
return await FileSystem.readAsStringAsync(logFileUri, { encoding: FileSystem.EncodingType.UTF8 }); 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();
isWriting = true; // lock the queue while clearing
try { try {
await FileSystem.deleteAsync(logFileUri, { idempotent: true }); await FileSystem.deleteAsync(logFileUri, { idempotent: true });
} catch (e) { } catch (e) {
console.warn('Failed to clear logs:', e); console.warn('Failed to clear logs:', e);
} finally {
isWriting = false;
processLogQueue();
} }
});
return writeQueue;
} }
}; };