Skip to content

Commit

Permalink
improve debounce behavior (#104421)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Jul 7, 2021
1 parent f5c4a05 commit a72d019
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion x-pack/plugins/lens/public/shared_components/debounced_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ export const useDebouncedValue = <T>(
// Save the initial value
const initialValue = useRef(value);

const flushChangesTimeout = useRef<NodeJS.Timeout | undefined>();

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);
};
Expand Down

0 comments on commit a72d019

Please sign in to comment.