Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debounce for input material controls #1813

Merged
merged 1 commit into from
Oct 8, 2021

Conversation

eneufeld
Copy link
Member

@eneufeld eneufeld commented Oct 6, 2021

  • add a custom hook which handles debounce
  • add example to test form performance

Fix #1645

@eneufeld eneufeld requested a review from sdirix October 6, 2021 10:15
Copy link
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not test the changes yet but conceptionally this looks as expected.

However we don't need any of the useRef, useMemo and useCallbacks. They complicate the code and don't prevent any rerenderings.

The renderers are already memoized, so if nothing changed they should not even render. If something changed they need to rerender anyway, so memoizing some of the props for their children will not prevent their rerender as something else changes too.

The only exception is the debounced_setter. This needs to be placed in a useMemo, or even better in a useState to guarantee it's always the same and debouncing works.

Edit: useState is not a good choice as the setter depends on handleChange and needs to react on changes there. So useMemo is best.

packages/material/src/mui-controls/MuiInputInteger.tsx Outdated Show resolved Hide resolved
packages/material/src/mui-controls/MuiInputNumber.tsx Outdated Show resolved Hide resolved
const eventToValue = (ev: any) => ev.target.value;
export const useDebouncedChange = (handleChange: (path: string, value: any) => void, defaultValue: any, data: any, path: string, eventToValueFunction: (ev: any) => any = eventToValue, timeout = 300): [any, React.ChangeEventHandler, () => void] => {
const [input, setInput] = useState(data || defaultValue);
const debouncedUpdate = useCallback(debounce((newValue: string) => handleChange(path, newValue), timeout), [handleChange]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a useMemo as we are calculating a function and not creating the function ourselves in place. A proper ESLint setup would complain here ;)

Suggested change
const debouncedUpdate = useCallback(debounce((newValue: string) => handleChange(path, newValue), timeout), [handleChange]);
const debouncedUpdate = useMemo(() => debounce((newValue: string) => handleChange(path, newValue), timeout), [handleChange]);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are actually creating a function, a function that is used to call handleChange

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's true, and technically that's completely fine.

I just noticed the pattern because ESLint usually complaints about it ("exhaustive-deps") as it can't properly analyze the function and therefore doesn't give you the usual guarantees. As a workaround we can use useMemo. We just don't have ESLint yet configured. I'm fine leaving it like this, so we can also change this later when we finally use ESLint.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're talking about it: timeout is missing in the dependencies as well as path.

- add a custom hook which handles debounce
- add example to test form performance

Fix eclipsesource#1645
@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 88.988% when pulling 8a24b09 on eneufeld:fix/1645 into d9fa0c3 on eclipsesource:master.

@eneufeld eneufeld requested a review from sdirix October 7, 2021 07:41
Copy link
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great

@sdirix sdirix merged commit be5c82c into eclipsesource:master Oct 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Debounce changes
3 participants