Skip to content

Commit

Permalink
feat(js): batch DOM updates (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored Nov 16, 2020
1 parent 9b98240 commit d06873e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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

0 comments on commit d06873e

Please sign in to comment.