core: 日志显示使用缓冲区,避免卡顿

This commit is contained in:
bin456789
2025-10-31 07:50:45 +08:00
parent 8b838f8871
commit 7c19559d86

View File

@ -66,6 +66,12 @@
const scrollToBottomButton = document.getElementById('scroll-to-bottom'); const scrollToBottomButton = document.getElementById('scroll-to-bottom');
let shouldScrollToBottom = true; let shouldScrollToBottom = true;
// 缓冲区相关
let messageBuffer = [];
let flushScheduled = false;
const BUFFER_FLUSH_INTERVAL = 100; // 毫秒
const BUFFER_MAX_SIZE = 50; // 最大缓冲消息数
scrollToBottomButton.addEventListener('click', () => { scrollToBottomButton.addEventListener('click', () => {
logContainer.scrollTop = logContainer.scrollHeight; logContainer.scrollTop = logContainer.scrollHeight;
}); });
@ -81,30 +87,71 @@
shouldScrollToBottom = isAtBottom; shouldScrollToBottom = isAtBottom;
}); });
var ws = new ReconnectingWebSocket('ws://' + location.host + '/'); // 刷新缓冲区到 DOM
ws.onopen = function () { function flushBuffer() {
logContainer.textContent += '\nWebSocket Connected.'; if (messageBuffer.length === 0) {
}; flushScheduled = false;
ws.onclose = function () { return;
logContainer.textContent += '\nWebSocket Disconnected.'; }
};
ws.onmessage = function (event) { // 批量更新文本内容
logContainer.textContent += '\n' + event.data; const batchText = messageBuffer.join('\n');
logContainer.textContent += '\n' + batchText;
// 检查状态变化优先级error > done > start
if (batchText.includes('***** ERROR *****')) {
document.body.className = 'error';
} else if (batchText.includes('***** DONE *****')) {
document.body.className = 'done';
} else if (batchText.includes('***** START TRANS *****')) {
document.body.className = '';
}
// 自动滚动
if (shouldScrollToBottom) { if (shouldScrollToBottom) {
logContainer.scrollTop = logContainer.scrollHeight; logContainer.scrollTop = logContainer.scrollHeight;
} }
// 开始/重新开始
if (event.data.includes('***** START TRANS *****')) { // 清空缓冲区
document.body.className = '' messageBuffer = [];
flushScheduled = false;
} }
// 错误
else if (event.data.includes('***** ERROR *****')) { // 调度刷新
document.body.className = 'error' function scheduleFlush() {
if (!flushScheduled) {
flushScheduled = true;
requestAnimationFrame(() => {
setTimeout(flushBuffer, BUFFER_FLUSH_INTERVAL);
});
} }
// 完成
else if (event.data.includes('***** DONE *****')) {
document.body.className = 'done'
} }
// 添加消息到缓冲区
function bufferMessage(message) {
messageBuffer.push(message);
// 如果缓冲区满了,立即刷新
if (messageBuffer.length >= BUFFER_MAX_SIZE) {
if (flushScheduled) {
// 取消之前的调度,立即刷新
flushScheduled = false;
}
flushBuffer();
} else {
scheduleFlush();
}
}
var ws = new ReconnectingWebSocket('ws://' + location.host + '/');
ws.onopen = function () {
bufferMessage('WebSocket Connected.');
};
ws.onclose = function () {
bufferMessage('WebSocket Disconnected.');
};
ws.onmessage = function (event) {
bufferMessage(event.data);
}; };
</script> </script>
</body> </body>