From 47d687b1518ca458ac5217860f5e50aa252ad2d8 Mon Sep 17 00:00:00 2001 From: Siddhant Rai Date: Sat, 16 Nov 2024 17:13:15 +0530 Subject: [PATCH] fix: old streaming answer gets appended to new conversation --- frontend/src/Navigation.tsx | 2 ++ frontend/src/conversation/conversationSlice.ts | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/frontend/src/Navigation.tsx b/frontend/src/Navigation.tsx index 324d5aa0..242efb1a 100644 --- a/frontend/src/Navigation.tsx +++ b/frontend/src/Navigation.tsx @@ -18,6 +18,7 @@ import SourceDropdown from './components/SourceDropdown'; import { setConversation, updateConversationId, + handleAbort, } from './conversation/conversationSlice'; import ConversationTile from './conversation/ConversationTile'; import { useDarkTheme, useMediaQuery, useOutsideAlerter } from './hooks'; @@ -180,6 +181,7 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) { }; const resetConversation = () => { + handleAbort(); dispatch(setConversation([])); dispatch( updateConversationId({ diff --git a/frontend/src/conversation/conversationSlice.ts b/frontend/src/conversation/conversationSlice.ts index 3ca05c7b..5a78e014 100644 --- a/frontend/src/conversation/conversationSlice.ts +++ b/frontend/src/conversation/conversationSlice.ts @@ -17,9 +17,23 @@ const initialState: ConversationState = { const API_STREAMING = import.meta.env.VITE_API_STREAMING === 'true'; +let abortController: AbortController | null = null; +export function handleAbort() { + if (abortController) { + abortController.abort(); + abortController = null; + } +} + export const fetchAnswer = createAsyncThunk( 'fetchAnswer', - async ({ question }, { dispatch, getState, signal }) => { + async ({ question }, { dispatch, getState }) => { + if (abortController) { + abortController.abort(); + } + abortController = new AbortController(); + const { signal } = abortController; + let isSourceUpdated = false; const state = getState() as RootState; if (state.preference) {