feat: throttle updateTranslationBlock dispatch for improved performance (#6442)

- Introduced throttling to the updateTranslationBlock dispatch function to limit the frequency of updates, enhancing performance during message operations.
- Utilized lodash's throttle function to ensure efficient handling of accumulated text updates.
This commit is contained in:
MyPrototypeWhat 2025-05-25 23:37:59 +08:00 committed by GitHub
parent a47c8d4b7d
commit bf8f49f87e

View File

@ -23,6 +23,7 @@ import type { Assistant, Model, Topic } from '@renderer/types'
import type { Message, MessageBlock } from '@renderer/types/newMessage'
import { MessageBlockStatus, MessageBlockType } from '@renderer/types/newMessage'
import { abortCompletion } from '@renderer/utils/abortController'
import { throttle } from 'lodash'
import { useCallback } from 'react'
const selectMessagesState = (state: RootState) => state.messages
@ -243,9 +244,13 @@ export function useMessageOperations(topic: Topic) {
return null
}
return (accumulatedText: string, isComplete: boolean = false) => {
dispatch(updateTranslationBlockThunk(blockId!, accumulatedText, isComplete))
}
return throttle(
(accumulatedText: string, isComplete: boolean = false) => {
dispatch(updateTranslationBlockThunk(blockId!, accumulatedText, isComplete))
},
200,
{ leading: true, trailing: true }
)
},
[dispatch, topic.id]
)