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