Skip to content

Commit

Permalink
docs: perf debounce the search query (#16586)
Browse files Browse the repository at this point in the history
* perf: debounce search results

* fix nits

Co-authored-by: Amaresh  S M  <amareshsm13@gmail.com>

* fix typo

Co-authored-by: Amaresh  S M  <amareshsm13@gmail.com>

* add apply

* chore: update the debounce fn parameter

* chore: update js doc commments

* chore: add punctuation

* perf: debounce search results

* fix nits

Co-authored-by: Amaresh  S M  <amareshsm13@gmail.com>

* fix typo

Co-authored-by: Amaresh  S M  <amareshsm13@gmail.com>

* add apply

* chore: update the debounce fn parameter

* chore: update js doc commments

* chore: add punctuation

Co-authored-by: Amaresh  S M  <amareshsm13@gmail.com>
  • Loading branch information
shanpriyan and amareshsm committed Nov 27, 2022
1 parent a91332b commit ade621d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions docs/src/assets/js/search.js
Expand Up @@ -111,6 +111,25 @@ function maintainScrollVisibility(activeElement, scrollParent) {

}

/**
* Debounces the provided callback with a given delay.
* @param {Function} callback The callback that needs to be debounced.
* @param {Number} delay Time in ms that the timer should wait before the callback is executed.
* @returns {Function} Returns the new debounced function.
*/
function debounce(callback, delay) {
let timer;
return (...args) => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => callback.apply(this, args), delay);
}
}

const debouncedFetchSearchResults = debounce((query) => {
fetchSearchResults(query)
.then(displaySearchResults)
.catch(clearSearchResults);
}, 300);

//-----------------------------------------------------------------------------
// Event Handlers
Expand All @@ -127,9 +146,8 @@ if(searchInput)
else searchClearBtn.setAttribute('hidden', '');

if (query.length > 2) {
fetchSearchResults(query)
.then(displaySearchResults)
.catch(clearSearchResults);

debouncedFetchSearchResults(query);

document.addEventListener('click', function(e) {
if(e.target !== resultsElement) clearSearchResults();
Expand Down

0 comments on commit ade621d

Please sign in to comment.