mirror of
https://github.com/bin456789/reinstall.git
synced 2025-12-10 07:28:56 +08:00
core: 日志显示使用缓冲区,避免卡顿
This commit is contained in:
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调度刷新
|
||||||
|
function scheduleFlush() {
|
||||||
|
if (!flushScheduled) {
|
||||||
|
flushScheduled = true;
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
setTimeout(flushBuffer, BUFFER_FLUSH_INTERVAL);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
// 错误
|
}
|
||||||
else if (event.data.includes('***** ERROR *****')) {
|
|
||||||
document.body.className = 'error'
|
// 添加消息到缓冲区
|
||||||
}
|
function bufferMessage(message) {
|
||||||
// 完成
|
messageBuffer.push(message);
|
||||||
else if (event.data.includes('***** DONE *****')) {
|
|
||||||
document.body.className = 'done'
|
// 如果缓冲区满了,立即刷新
|
||||||
|
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>
|
||||||
|
|||||||
Reference in New Issue
Block a user