From bf8f49f87e556048a2b20f52a7cce01e3e92a18e Mon Sep 17 00:00:00 2001 From: MyPrototypeWhat <43230886+MyPrototypeWhat@users.noreply.github.com> Date: Sun, 25 May 2025 23:37:59 +0800 Subject: [PATCH] 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. --- src/renderer/src/hooks/useMessageOperations.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/hooks/useMessageOperations.ts b/src/renderer/src/hooks/useMessageOperations.ts index 5418893463..ef0ea6a2e0 100644 --- a/src/renderer/src/hooks/useMessageOperations.ts +++ b/src/renderer/src/hooks/useMessageOperations.ts @@ -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] )