diff --git a/src/frontend/src/CustomNodes/hooks/use-handle-new-value.ts b/src/frontend/src/CustomNodes/hooks/use-handle-new-value.ts index 778dc5417..219116e10 100644 --- a/src/frontend/src/CustomNodes/hooks/use-handle-new-value.ts +++ b/src/frontend/src/CustomNodes/hooks/use-handle-new-value.ts @@ -1,3 +1,4 @@ +import { DEBOUNCE_FIELD_LIST } from "@/constants/constants"; import { usePostTemplateValue } from "@/controllers/API/queries/nodes/use-post-template-value"; import { track } from "@/customization/utils/analytics"; import useAlertStore from "@/stores/alertStore"; @@ -6,10 +7,12 @@ import useFlowsManagerStore from "@/stores/flowsManagerStore"; import { APIClassType, InputFieldType } from "@/types/api"; import { AllNodeType } from "@/types/flow"; import { useUpdateNodeInternals } from "@xyflow/react"; -import { cloneDeep } from "lodash"; -import { useCallback, useMemo } from "react"; +import { cloneDeep, debounce } from "lodash"; +import { useCallback, useMemo, useRef } from "react"; import { mutateTemplate } from "../helpers/mutate-template"; +const DEBOUNCE_TIME_1_SECOND = 1000; + export type handleOnNewValueType = ( changes: Partial, options?: { @@ -72,7 +75,8 @@ const useHandleOnNewValue = ({ [nodeId, setNode, updateNodeInternals], ); - // Memoize the handleOnNewValue function + const debouncedMutateRef = useRef(null); + const handleOnNewValue: handleOnNewValueType = useCallback( async (changes, options?) => { const newNode = cloneDeep(node); @@ -97,6 +101,10 @@ const useHandleOnNewValue = ({ return; } + const shouldDebounce = DEBOUNCE_FIELD_LIST.includes( + parameter?._input_type, + ); + if (!options?.skipSnapshot) takeSnapshot(); Object.entries(changes).forEach(([key, value]) => { @@ -111,7 +119,27 @@ const useHandleOnNewValue = ({ }; if (shouldUpdate && changes.value !== undefined) { - await mutateTemplate( + if (!debouncedMutateRef.current) { + debouncedMutateRef.current = debounce( + async ( + value, + node, + setNodeClassFn, + postTemplateFn, + setErrorDataFn, + ) => { + await mutateTemplate( + value, + node, + setNodeClassFn, + postTemplateFn, + setErrorDataFn, + ); + }, + shouldDebounce ? DEBOUNCE_TIME_1_SECOND : 0, + ); + } + debouncedMutateRef.current( changes.value, newNode, setNodeClass, diff --git a/src/frontend/src/constants/constants.ts b/src/frontend/src/constants/constants.ts index 524f9461b..3edf9c9bd 100644 --- a/src/frontend/src/constants/constants.ts +++ b/src/frontend/src/constants/constants.ts @@ -1037,3 +1037,14 @@ export const ALL_LANGUAGES = [ { value: "ar-SA", name: "Arabic" }, { value: "hi-IN", name: "Hindi" }, ]; + +export const DEBOUNCE_FIELD_LIST = [ + "SecretStrInput", + "MessageTextInput", + "TextInput", + "MultilineInput", + "SecretStrInput", + "IntInput", + "FloatInput", + "SliderInput", +]; diff --git a/src/frontend/src/controllers/API/queries/nodes/use-post-template-value.ts b/src/frontend/src/controllers/API/queries/nodes/use-post-template-value.ts index 010c24214..7c86f6318 100644 --- a/src/frontend/src/controllers/API/queries/nodes/use-post-template-value.ts +++ b/src/frontend/src/controllers/API/queries/nodes/use-post-template-value.ts @@ -54,7 +54,10 @@ export const usePostTemplateValue: useMutationFunctionType< > = mutate( ["usePostTemplateValue", { parameterId, nodeId }], postTemplateValueFn, - options, + { + ...options, + retry: 0, + }, ); return mutation;