Simplify ConsoleRuntimeProvider to batch-send logs at completion only

This commit is contained in:
Mario Zechner 2025-10-09 20:16:32 +02:00
parent af0297cd16
commit 6983ad4eaa

View file

@ -33,8 +33,8 @@ export class ConsoleRuntimeProvider implements SandboxRuntimeProvider {
info: console.info, info: console.info,
}; };
// Track pending logs (not yet confirmed sent) // Collect logs locally, send at completion
const pendingLogs: Array<{ method: string; text: string; args: any[] }> = []; const collectedLogs: Array<{ method: string; text: string; args: any[] }> = [];
["log", "error", "warn", "info"].forEach((method) => { ["log", "error", "warn", "info"].forEach((method) => {
(console as any)[method] = (...args: any[]) => { (console as any)[method] = (...args: any[]) => {
@ -48,44 +48,21 @@ export class ConsoleRuntimeProvider implements SandboxRuntimeProvider {
}) })
.join(" "); .join(" ");
const logEntry = { method, text, args }; // Collect log for batch send at completion
collectedLogs.push({ method, text, args });
// Add to pending logs
pendingLogs.push(logEntry);
// Try to send immediately (fire-and-forget)
if ((window as any).sendRuntimeMessage) {
(window as any)
.sendRuntimeMessage({
type: "console",
method,
text,
args, // Send raw args for provider collection
})
.then(() => {
// Remove from pending on successful send
const index = pendingLogs.indexOf(logEntry);
if (index !== -1) {
pendingLogs.splice(index, 1);
}
})
.catch(() => {
// Keep in pending array if send fails
});
}
// Always log locally too // Always log locally too
(originalConsole as any)[method].apply(console, args); (originalConsole as any)[method].apply(console, args);
}; };
}); });
// Register completion callback to send any remaining logs // Register completion callback to send all collected logs
if ((window as any).onCompleted) { if ((window as any).onCompleted) {
(window as any).onCompleted(async (_success: boolean) => { (window as any).onCompleted(async (_success: boolean) => {
// Send any logs that haven't been sent yet // Send all collected logs
if (pendingLogs.length > 0 && (window as any).sendRuntimeMessage) { if (collectedLogs.length > 0 && (window as any).sendRuntimeMessage) {
await Promise.all( await Promise.all(
pendingLogs.map((logEntry) => collectedLogs.map((logEntry) =>
(window as any).sendRuntimeMessage({ (window as any).sendRuntimeMessage({
type: "console", type: "console",
method: logEntry.method, method: logEntry.method,