Skip to content

Commit d7b3020

Browse files
committed
core: 日志显示使用缓冲区,避免卡顿
1 parent e099324 commit d7b3020

File tree

1 file changed

+65
-18
lines changed

1 file changed

+65
-18
lines changed

logviewer.html

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@
6666
const scrollToBottomButton = document.getElementById('scroll-to-bottom');
6767
let shouldScrollToBottom = true;
6868

69+
// 缓冲区相关
70+
let messageBuffer = [];
71+
let flushScheduled = false;
72+
const BUFFER_FLUSH_INTERVAL = 100; // 毫秒
73+
const BUFFER_MAX_SIZE = 50; // 最大缓冲消息数
74+
6975
scrollToBottomButton.addEventListener('click', () => {
7076
logContainer.scrollTop = logContainer.scrollHeight;
7177
});
@@ -81,30 +87,71 @@
8187
shouldScrollToBottom = isAtBottom;
8288
});
8389

90+
// 刷新缓冲区到 DOM
91+
function flushBuffer() {
92+
if (messageBuffer.length === 0) {
93+
flushScheduled = false;
94+
return;
95+
}
96+
97+
// 批量更新文本内容
98+
const batchText = messageBuffer.join('\n');
99+
logContainer.textContent += '\n' + batchText;
100+
101+
// 检查状态变化(优先级:error > done > start)
102+
if (batchText.includes('***** ERROR *****')) {
103+
document.body.className = 'error';
104+
} else if (batchText.includes('***** DONE *****')) {
105+
document.body.className = 'done';
106+
} else if (batchText.includes('***** START TRANS *****')) {
107+
document.body.className = '';
108+
}
109+
110+
// 自动滚动
111+
if (shouldScrollToBottom) {
112+
logContainer.scrollTop = logContainer.scrollHeight;
113+
}
114+
115+
// 清空缓冲区
116+
messageBuffer = [];
117+
flushScheduled = false;
118+
}
119+
120+
// 调度刷新
121+
function scheduleFlush() {
122+
if (!flushScheduled) {
123+
flushScheduled = true;
124+
requestAnimationFrame(() => {
125+
setTimeout(flushBuffer, BUFFER_FLUSH_INTERVAL);
126+
});
127+
}
128+
}
129+
130+
// 添加消息到缓冲区
131+
function bufferMessage(message) {
132+
messageBuffer.push(message);
133+
134+
// 如果缓冲区满了,立即刷新
135+
if (messageBuffer.length >= BUFFER_MAX_SIZE) {
136+
if (flushScheduled) {
137+
// 取消之前的调度,立即刷新
138+
flushScheduled = false;
139+
}
140+
flushBuffer();
141+
} else {
142+
scheduleFlush();
143+
}
144+
}
145+
84146
var ws = new ReconnectingWebSocket('ws://' + location.host + '/');
85147
ws.onopen = function () {
86-
logContainer.textContent += '\nWebSocket Connected.';
148+
bufferMessage('WebSocket Connected.');
87149
};
88150
ws.onclose = function () {
89-
logContainer.textContent += '\nWebSocket Disconnected.';
151+
bufferMessage('WebSocket Disconnected.');
90152
};
91153
ws.onmessage = function (event) {
92-
logContainer.textContent += '\n' + event.data;
93-
if (shouldScrollToBottom) {
94-
logContainer.scrollTop = logContainer.scrollHeight;
95-
}
96-
// 开始/重新开始
97-
if (event.data.includes('***** START TRANS *****')) {
98-
document.body.className = ''
99-
}
100-
// 错误
101-
else if (event.data.includes('***** ERROR *****')) {
102-
document.body.className = 'error'
103-
}
104-
// 完成
105-
else if (event.data.includes('***** DONE *****')) {
106-
document.body.className = 'done'
107-
}
154+
bufferMessage(event.data);
108155
};
109156
</script>
110157
</body>

0 commit comments

Comments
 (0)