Explorar el Código

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)
kiwina hace 10 meses
padre
commit
3f3825a219
Se han modificado 1 ficheros con 23 adiciones y 6 borrados
  1. 23 6
      webview-ui/src/components/chat/ChatView.tsx

+ 23 - 6
webview-ui/src/components/chat/ChatView.tsx

@@ -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()