fix: Address memory leaks in ChatView component (ChatView_1113, ChatView_1236) (#4248)

* chore: Staging ChatView.tsx after reset to main

* fix: Address memory leaks in ChatView (ChatView_1113, ChatView_1236)
This commit is contained in:
kiwina 2025-06-05 05:34:34 +08:00 committed by GitHub
parent 8cc89149b4
commit 3f3825a219
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -60,6 +60,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
{ isHidden, showAnnouncement, hideAnnouncement },
ref,
) => {
const isMountedRef = useRef(true)
const [audioBaseUri] = useState(() => {
const w = window as any
return w.AUDIO_BASE_URI || ""
@ -158,6 +159,13 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
clineAskRef.current = clineAsk
}, [clineAsk])
useEffect(() => {
isMountedRef.current = true
return () => {
isMountedRef.current = false
}
}, [])
const isProfileDisabled = useMemo(
() => !!apiConfiguration && !ProfileValidator.isProfileAllowed(apiConfiguration, organizationAllowList),
[apiConfiguration, organizationAllowList],
@ -1109,10 +1117,14 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
)
useEffect(() => {
let timerId: NodeJS.Timeout | undefined
if (!disableAutoScrollRef.current) {
setTimeout(() => scrollToBottomSmooth(), 50)
// Don't cleanup since if visibleMessages.length changes it cancels.
// return () => clearTimeout(timer)
timerId = setTimeout(() => scrollToBottomSmooth(), 50)
}
return () => {
if (timerId) {
clearTimeout(timerId)
}
}
}, [groupedMessages.length, scrollToBottomSmooth])
@ -1234,6 +1246,9 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
// Add delay for write operations.
if (lastMessage.ask === "tool" && isWriteToolAction(lastMessage)) {
await new Promise((resolve) => setTimeout(resolve, writeDelayMs))
if (!isMountedRef.current) {
return
}
}
vscode.postMessage({ type: "askResponse", askResponse: "yesButtonClicked" })
@ -1241,9 +1256,11 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
// This is copied from `handlePrimaryButtonClick`, which we used
// to call from `autoApprove`. I'm not sure how many of these
// things are actually needed.
setSendingDisabled(true)
setClineAsk(undefined)
setEnableButtons(false)
if (isMountedRef.current) {
setSendingDisabled(true)
setClineAsk(undefined)
setEnableButtons(false)
}
}
}
autoApprove()