fix(segmentation): Allow for debounced pub/sub events to be reliably cancelled when unsubscribed to. (#5092)

This commit is contained in:
Joe Boccanfuso authored and GitHub committed 2025-06-02 12:12:31 -04:00
1 parent a1fb7217bd
commit 38ed7ba744
5 files changed
+36 -9

No files matched your search

+10 -1
View File
@@ -2,9 +2,11 @@
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
// The callback function returned is assigned a clearDebounceTimeout function
// that provides for clearing the timeout/debounced function so that it is not called.
function debounce(func, wait, immediate) {
var timeout;
return function () {
const callback = function () {
var context = this,
args = arguments;
var later = function () {
@@ -20,6 +22,13 @@ function debounce(func, wait, immediate) {
func.apply(context, args);
}
};
callback.clearDebounceTimeout = () => {
clearTimeout(timeout);
timeout = null;
};
return callback;
}
export default debounce;