fix: clean up timer logic to prevent memory leaks
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Your Name 2026-01-31 22:33:55 +08:00
parent eca231575d
commit 258c4ce173

View File

@ -195,6 +195,10 @@ function PureMessages({
// 🆕 超时保护:如果占位图显示超过 3 分钟,强制隐藏(防止卡住)
const maxShowTime = 3 * 60 * 1000; // 3 分钟
// 用于存储需要清理的定时器
let delayTimer: ReturnType<typeof setTimeout> | null = null;
let timeoutTimer: ReturnType<typeof setTimeout> | null = null;
// 如果是 submitted 状态,立即显示
if (status === "submitted") {
setShowThinkingWithDelay(true);
@ -216,16 +220,13 @@ function PureMessages({
}
// 如果 AI 开始有内容了,延迟后隐藏
else if (status === "streaming" && lastMessage?.role === "assistant" && hasContent) {
const timer = setTimeout(() => {
delayTimer = setTimeout(() => {
setShowThinkingWithDelay(false);
thinkingStartTimeRef.current = null;
}, delayTime);
return () => clearTimeout(timer);
}
// 🆕 如果状态是 ready(生成完成),立即隐藏
else if (status === "ready") {
console.log("[占位图] status=ready,立即隐藏占位图");
setShowThinkingWithDelay(false);
thinkingStartTimeRef.current = null;
}
@ -235,16 +236,22 @@ function PureMessages({
thinkingStartTimeRef.current = null;
}
// 🆕 超时保护定时器
const timeoutTimer = setTimeout(() => {
if (thinkingStartTimeRef.current && Date.now() - thinkingStartTimeRef.current > maxShowTime) {
console.warn("[占位图] ⚠️ 超时保护触发:占位图显示超过 3 分钟,强制隐藏");
setShowThinkingWithDelay(false);
thinkingStartTimeRef.current = null;
}
}, maxShowTime);
// 🆕 超时保护定时器(只在显示占位图时启动)
if (status === "submitted" || status === "streaming") {
timeoutTimer = setTimeout(() => {
if (thinkingStartTimeRef.current && Date.now() - thinkingStartTimeRef.current > maxShowTime) {
console.warn("[占位图] ⚠️ 超时保护触发:占位图显示超过 3 分钟,强制隐藏");
setShowThinkingWithDelay(false);
thinkingStartTimeRef.current = null;
}
}, maxShowTime);
}
return () => clearTimeout(timeoutTimer);
// 清理所有定时器
return () => {
if (delayTimer) clearTimeout(delayTimer);
if (timeoutTimer) clearTimeout(timeoutTimer);
};
}, [status, messages, isGeminiModel, isQianwenVideoModel, isVolcanoVisionModel, isVideoModel, isAlibabaImageModel]);
// 🆕 监听新消息添加,当用户发送带图片的消息或添加加载消息时,强制滚动到底部