Add debounce() wrapper for buefy autocomplete

per docs, although was not very clear "which" debounce i needed, this
one at least works without errors..

hoping this fixes some page performance issues when tailbone
autocomplete component is present
This commit is contained in:
Lance Edgar 2021-10-12 14:17:10 -04:00
parent 20492410ad
commit aeace0c7cf
3 changed files with 44 additions and 5 deletions

View file

@ -0,0 +1,36 @@
// this code was politely stolen from
// https://vanillajstoolkit.com/helpers/debounce/
// its purpose is to help with Buefy autocomplete performance
// https://buefy.org/documentation/autocomplete/
/**
* Debounce functions for better performance
* (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Function} fn The function to debounce
*/
function debounce (fn) {
// Setup a timer
let timeout;
// Return a function to run debounced
return function () {
// Setup the arguments
let context = this;
let args = arguments;
// If there's a timer, cancel it
if (timeout) {
window.cancelAnimationFrame(timeout);
}
// Setup the new requestAnimationFrame()
timeout = window.requestAnimationFrame(function () {
fn.apply(context, args);
});
};
}

View file

@ -98,7 +98,7 @@ const TailboneAutocomplete = {
// TODO: buefy example uses `debounce()` here and perhaps we should too? // TODO: buefy example uses `debounce()` here and perhaps we should too?
// https://buefy.org/documentation/autocomplete // https://buefy.org/documentation/autocomplete
getAsyncData: function (entry) { getAsyncData: debounce(function (entry) {
if (entry.length < 3) { if (entry.length < 3) {
this.data = [] this.data = []
return return
@ -115,7 +115,7 @@ const TailboneAutocomplete = {
.finally(() => { .finally(() => {
this.isFetching = false this.isFetching = false
}) })
}, }),
}, },
} }

View file

@ -77,6 +77,9 @@
## some commonly-useful logic for detecting (non-)numeric input ## some commonly-useful logic for detecting (non-)numeric input
${h.javascript_link(request.static_url('tailbone:static/js/numeric.js') + '?ver={}'.format(tailbone.__version__))} ${h.javascript_link(request.static_url('tailbone:static/js/numeric.js') + '?ver={}'.format(tailbone.__version__))}
## debounce, for better autocomplete performance
${h.javascript_link(request.static_url('tailbone:static/js/debounce.js') + '?ver={}'.format(tailbone.__version__))}
## Tailbone / Buefy stuff ## Tailbone / Buefy stuff
${h.javascript_link(request.static_url('tailbone:static/js/tailbone.buefy.datepicker.js') + '?ver={}'.format(tailbone.__version__))} ${h.javascript_link(request.static_url('tailbone:static/js/tailbone.buefy.datepicker.js') + '?ver={}'.format(tailbone.__version__))}
${h.javascript_link(request.static_url('tailbone:static/js/tailbone.buefy.numericinput.js') + '?ver={}'.format(tailbone.__version__))} ${h.javascript_link(request.static_url('tailbone:static/js/tailbone.buefy.numericinput.js') + '?ver={}'.format(tailbone.__version__))}