ref(toolbar): replace custom debounce with lodash throttle
The current custom debounce function sets a timeout and ensures additional calls are not executed while the timeout is pending. Regulating the call of a function while also ensuring it gets called at a certain time is a throttle.
This commit is contained in:
@@ -16,36 +16,6 @@ export function createDeferred() {
|
||||
return deferred;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a debounced function that delays invoking func until after wait
|
||||
* milliseconds have elapsed since the last time the debounced function was
|
||||
* invoked.
|
||||
*
|
||||
* @param fn
|
||||
* @param wait
|
||||
* @param options
|
||||
* @returns {function(...[*])}
|
||||
*/
|
||||
export function debounce(fn, wait = 0, options = {}) {
|
||||
const leading = options.leading || false;
|
||||
const trailing
|
||||
= (typeof options.trailing === 'undefined') || options.trailing;
|
||||
let called = false;
|
||||
|
||||
return (...args) => {
|
||||
if (!called) {
|
||||
leading && fn(...args);
|
||||
|
||||
setTimeout(() => {
|
||||
called = false;
|
||||
trailing && fn(...args);
|
||||
}, wait);
|
||||
|
||||
called = true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the namespace for all global variables, functions, etc that we need.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user