From 2fad7c0ff671b913fb158a698836f20277b7c39c Mon Sep 17 00:00:00 2001 From: MyPrototypeWhat <43230886+MyPrototypeWhat@users.noreply.github.com> Date: Fri, 4 Jul 2025 16:07:13 +0800 Subject: [PATCH] refactor(messageThunk): streamline loading state management for topics (#7809) * refactor(messageThunk): streamline loading state management for topics - Reintroduced the handleChangeLoadingOfTopic function to manage loading states more effectively. - Updated thunk implementations to ensure loading state is correctly set after message processing. - Removed commented-out code for clarity and maintainability. * fix(messageThunk): ensure loading state is managed correctly after message sending - Added a finally block to guarantee that the loading state is updated after the sendMessage thunk execution. - Removed commented-out code for improved clarity and maintainability. --- src/renderer/src/store/thunk/messageThunk.ts | 31 +++++++++----------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index 0360022a54..5537ed0b13 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -35,6 +35,7 @@ import { } from '@renderer/utils/messageUtils/create' import { getMainTextContent } from '@renderer/utils/messageUtils/find' import { getTopicQueue } from '@renderer/utils/queue' +import { waitForTopicQueue } from '@renderer/utils/queue' import { isOnHomePage } from '@renderer/utils/window' import { t } from 'i18next' import { isEmpty, throttle } from 'lodash' @@ -44,10 +45,10 @@ import type { AppDispatch, RootState } from '../index' import { removeManyBlocks, updateOneBlock, upsertManyBlocks, upsertOneBlock } from '../messageBlock' import { newMessagesActions, selectMessagesForTopic } from '../newMessage' -// const handleChangeLoadingOfTopic = async (topicId: string) => { -// await waitForTopicQueue(topicId) -// store.dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) -// } +const handleChangeLoadingOfTopic = async (topicId: string) => { + await waitForTopicQueue(topicId) + store.dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) +} // TODO: 后续可以将db操作移到Listener Middleware中 export const saveMessageAndBlocksToDB = async (message: Message, blocks: MessageBlock[], messageIndex: number = -1) => { try { @@ -801,7 +802,7 @@ const fetchAndProcessAssistantResponseImpl = async ( const usage = await estimateMessagesUsage({ assistant, messages: finalContextWithAssistant }) response.usage = usage } - dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) + // dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) } if (response && response.metrics) { if (response.metrics.completion_tokens === 0 && response.usage?.completion_tokens) { @@ -887,10 +888,9 @@ export const sendMessage = } } catch (error) { console.error('Error in sendMessage thunk:', error) + } finally { + handleChangeLoadingOfTopic(topicId) } - // finally { - // handleChangeLoadingOfTopic(topicId) - // } } /** @@ -1136,10 +1136,9 @@ export const resendMessageThunk = } } catch (error) { console.error(`[resendMessageThunk] Error resending user message ${userMessageToResend.id}:`, error) + } finally { + handleChangeLoadingOfTopic(topicId) } - // finally { - // handleChangeLoadingOfTopic(topicId) - // } } /** @@ -1271,10 +1270,9 @@ export const regenerateAssistantResponseThunk = error ) // dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) + } finally { + handleChangeLoadingOfTopic(topicId) } - // finally { - // handleChangeLoadingOfTopic(topicId) - // } } // --- Thunk to initiate translation and create the initial block --- @@ -1447,10 +1445,9 @@ export const appendAssistantResponseThunk = console.error(`[appendAssistantResponseThunk] Error appending assistant response:`, error) // Optionally dispatch an error action or notification // Resetting loading state should be handled by the underlying fetchAndProcessAssistantResponseImpl + } finally { + handleChangeLoadingOfTopic(topicId) } - // finally { - // handleChangeLoadingOfTopic(topicId) - // } } /**