Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18503 from ramonlsouza/native-debounce
refactor: replace debounce function
- Loading branch information
Showing
12 changed files
with
84 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Debounce function, includes leading and trailing options (lodash-like) | ||
* @param {Function} func - function to be debounced | ||
* @param {Number} delay - delay in milliseconds | ||
* @param {Object} options - options object | ||
* @param {Boolean} options.leading - whether to invoke the function on the leading edge | ||
* @param {Boolean} options.trailing - whether to invoke the function on the trailing edge | ||
* @returns {Function} - debounced function | ||
*/ | ||
export function debounce(func, delay, options = {}) { | ||
let timeoutId; | ||
let lastArgs; | ||
let lastThis; | ||
let calledOnce = false; | ||
|
||
const { leading = false, trailing = true } = options; | ||
|
||
function invokeFunc() { | ||
func.apply(lastThis, lastArgs); | ||
lastArgs = null; | ||
lastThis = null; | ||
} | ||
|
||
return function (...args) { | ||
lastArgs = args; | ||
lastThis = this; | ||
|
||
if (!timeoutId) { | ||
if (leading && !calledOnce) { | ||
invokeFunc(); | ||
calledOnce = true; | ||
} | ||
|
||
timeoutId = setTimeout(() => { | ||
if (!trailing) { | ||
clearTimeout(timeoutId); | ||
timeoutId = null; | ||
} else { | ||
invokeFunc(); | ||
timeoutId = null; | ||
} | ||
calledOnce = false; | ||
}, delay); | ||
} else if (trailing) { | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(() => { | ||
invokeFunc(); | ||
timeoutId = null; | ||
}, delay); | ||
} | ||
}; | ||
} |