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

feat(js): batch DOM updates #372

Merged
merged 2 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }],
'import/extensions': 0,
'@typescript-eslint/camelcase': ['error', { allow: ['__autocomplete_id'] }],
'@typescript-eslint/no-use-before-define': 0,
// Useful to call functions like `nodeItem?.scrollIntoView()`.
'no-unused-expressions': 0,
complexity: 0,
Expand Down
11 changes: 9 additions & 2 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ export function autocomplete<TItem>({
classNames,
});

function onStateChange(state: AutocompleteState<TItem>) {
// This batches state changes to limit DOM mutations.
// Every time we call a setter in `autocomplete-core` (e.g., in `onInput`),
// the core `onStateChange` function is called.
// We don't need to be notified of all these state changes to render.
// As an example:
// - without debouncing: "iphone case" query → 85 renders
// - with debouncing: "iphone case" query → 12 renders
const onStateChange = debounce((state: AutocompleteState<TItem>) => {
render(renderer, {
state,
...autocomplete,
Expand All @@ -61,7 +68,7 @@ export function autocomplete<TItem>({
panel,
resetButton,
});
}
}, 0);

function setPanelPosition() {
setProperties(panel, {
Expand Down