perf: 更新下载进度300ms节流、状态栏仅变化时写DOM、轮询失败静默

This commit is contained in:
liufumingren 2026-08-24 02:21:01 +08:00
parent 28a0b5db37
commit 38fa36c180
2 changed files with 36 additions and 14 deletions

View File

@ -197,6 +197,19 @@ class UpdateService extends service {
updatedAt: new Date().toISOString()
};
// 下载中状态每个网络 chunk 触发一次(60MB 包约 4000 次),
// 节流到 300ms 一次,避免消息洪泛消耗主进程/渲染进程内存;
// 其他状态(starting/downloaded/installing/failed)立即发送,保证终态不丢。
if (patch.status === 'downloading') {
const now = Date.now();
if (this._lastProgressEmitAt && now - this._lastProgressEmitAt < 300) {
return;
}
this._lastProgressEmitAt = now;
} else {
this._lastProgressEmitAt = 0;
}
this.app.emit('updateDownloadProgress', this._downloadState);
}
@ -338,14 +351,6 @@ class UpdateService extends service {
throw new Error('当前系统不支持自动启动安装程序');
}
if (process.argv.includes('--service-runner')) {
throw new Error('服务模式无法弹出安装向导,请在前台模式下重试');
}
if (this.app?.config?.isService) {
throw new Error('当前处于服务模式,无法直接弹出安装向导,请先注销服务后再更新');
}
const escapedPath = installerPath.replace(/'/g, "''");
const psArgs = [
'-NoProfile',

View File

@ -1321,7 +1321,8 @@
};
}
} else {
alert(data.message || '获取绑定状态失败!');
// 每 10 秒轮询调用:失败静默处理,避免模态弹窗阻塞页面,状态会自动恢复
console.warn('[UI] 获取绑定状态失败:', data.message || '');
}
});
},
@ -1738,30 +1739,46 @@
var ws = mainApp.wsStatus;
var items = statusEl.querySelectorAll('.status-item');
var html0 = '', html1 = '', html2 = '';
if (items[0]) {
var c = ws[mainApp.wsClientStatus] || ws.connected;
items[0].innerHTML = '<span class="status-dot" style="background:' + c.color + '"></span>' +
html0 = '<span class="status-dot" style="background:' + c.color + '"></span>' +
'<span>本地服务:<strong style="color:' + c.color + '">' + c.title + '</strong></span>';
}
if (items[1]) {
var s = ws[mainApp.wsServerStatus] || ws.disconnected;
items[1].innerHTML = '<span class="status-dot" style="background:' + s.color + '"></span>' +
html1 = '<span class="status-dot" style="background:' + s.color + '"></span>' +
'<span>服务器连接:<strong style="color:' + s.color + '">' + s.title + '</strong></span>';
}
if (items[2]) {
var bindC = mainApp.isBind ? '#107c10' : '#c42b1c';
var bindT = mainApp.isBind ? '已绑定' : '未绑定';
var clickable = mainApp.isBind ? 'cursor:pointer;text-decoration:underline' : '';
items[2].innerHTML = '<span class="status-dot" style="background:' + bindC + '"></span>' +
html2 = '<span class="status-dot" style="background:' + bindC + '"></span>' +
'<span>打印服务绑定:<strong style="color:' + bindC + ';' + clickable + '" id="bindStatusText">' + bindT + '</strong></span>';
}
// 仅在内容变化时写 DOM,避免每 500ms 全量重建节点
if (!lastSbHtml) lastSbHtml = {};
if (lastSbHtml[0] !== html0) { lastSbHtml[0] = html0; if (items[0]) items[0].innerHTML = html0; }
if (lastSbHtml[1] !== html1) { lastSbHtml[1] = html1; if (items[1]) items[1].innerHTML = html1; }
if (lastSbHtml[2] !== html2) { lastSbHtml[2] = html2; if (items[2]) items[2].innerHTML = html2; }
var btns = statusEl.querySelectorAll('button');
if (btns[0]) btns[0].textContent = mainApp.updateChecking ? '检查中...' : '检查更新';
if (btns[1]) { btns[1].textContent = mainApp.restartingBackend ? '重启中...' : '重启服务'; btns[1].disabled = mainApp.restartingBackend; }
if (btns[0]) {
var btn0Text = mainApp.updateChecking ? '检查中...' : '检查更新';
if (btns[0].textContent !== btn0Text) btns[0].textContent = btn0Text;
}
if (btns[1]) {
var btn1Text = mainApp.restartingBackend ? '重启中...' : '重启服务';
if (btns[1].textContent !== btn1Text) btns[1].textContent = btn1Text;
btns[1].disabled = mainApp.restartingBackend;
}
}
var lastSbHtml = null;
// 绑定按钮事件
var btnCheck = document.getElementById('btnCheckUpdate');
if (btnCheck) {