From a72d0192cb59481160c4fb41b3646833a85e4f24 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Wed, 7 Jul 2021 11:41:12 +0200 Subject: [PATCH] improve debounce behavior (#104421) --- .../lens/public/shared_components/debounced_value.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/lens/public/shared_components/debounced_value.ts b/x-pack/plugins/lens/public/shared_components/debounced_value.ts index 5525f6b16b31..54696e672ccb 100644 --- a/x-pack/plugins/lens/public/shared_components/debounced_value.ts +++ b/x-pack/plugins/lens/public/shared_components/debounced_value.ts @@ -30,12 +30,20 @@ export const useDebouncedValue = ( // Save the initial value const initialValue = useRef(value); + const flushChangesTimeout = useRef(); + const onChangeDebounced = useMemo(() => { const callback = debounce((val: T) => { onChange(val); - unflushedChanges.current = false; + // do not reset unflushed flag right away, wait a bit for upstream to pick it up + flushChangesTimeout.current = setTimeout(() => { + unflushedChanges.current = false; + }, 256); }, 256); return (val: T) => { + if (flushChangesTimeout.current) { + clearTimeout(flushChangesTimeout.current); + } unflushedChanges.current = true; callback(val); };