perf(安装日志): 优化安装日志处理性能

添加日志缓冲和批量发送机制以减少IPC通信次数
限制前端日志条目数量防止内存泄漏
This commit is contained in:
2026-03-29 16:36:58 +08:00
parent a8d462395a
commit 94f4307783
2 changed files with 31 additions and 3 deletions

View File

@@ -561,20 +561,42 @@ async function processNextInQueue() {
let stdout = "";
let stderr = "";
let logBuffer = "";
let logBufferTimer: NodeJS.Timeout | null = null;
const LOG_FLUSH_MS = 100;
const flushLogBuffer = () => {
if (logBuffer.length > 0) {
sendLog(logBuffer);
logBuffer = "";
}
logBufferTimer = null;
};
const bufferedSendLog = (message: string) => {
logBuffer += message;
if (!logBufferTimer) {
logBufferTimer = setTimeout(flushLogBuffer, LOG_FLUSH_MS);
}
};
child.stdout.on("data", (d) => {
const s = d.toString();
stdout += s;
sendLog(s);
bufferedSendLog(s);
});
child.stderr.on("data", (d) => {
const s = d.toString();
stderr += s;
sendLog(s);
bufferedSendLog(s);
});
child.on("close", (code) => {
if (logBufferTimer) {
clearTimeout(logBufferTimer);
flushLogBuffer();
}
if (task.cancelled) {
reject(new Error("安装已取消"));
return;

View File

@@ -183,13 +183,19 @@ window.ipcRenderer.on(
},
);
const MAX_LOG_ENTRIES = 500;
window.ipcRenderer.on("install-log", (_event, log: InstallLog) => {
const downloadObj = downloads.value.find((d) => d.id === log.id);
if (downloadObj)
if (downloadObj) {
downloadObj.logs.push({
time: log.time,
message: log.message,
});
if (downloadObj.logs.length > MAX_LOG_ENTRIES) {
downloadObj.logs.splice(0, downloadObj.logs.length - MAX_LOG_ENTRIES);
}
}
});
window.ipcRenderer.on("install-complete", (_event, log: DownloadResult) => {