From 4de51087bce43aa4c2435d378176258186ed624a Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Mon, 22 Jul 2024 22:33:48 +0300 Subject: [PATCH] Allow cancel both by debounced and original functions --- public/scripts/utils.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/public/scripts/utils.js b/public/scripts/utils.js index 7a55ec3a7..ea7306c9c 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -285,16 +285,20 @@ const debounceMap = new WeakMap(); */ export function debounce(func, timeout = debounce_timeout.standard) { let timer; - return (...args) => { + let fn = (...args) => { clearTimeout(timer); timer = setTimeout(() => { func.apply(this, args); }, timeout); debounceMap.set(func, timer); + debounceMap.set(fn, timer); }; + + return fn; } /** - * Cancels a scheduled debounced function. Does nothing if the function is not debounced or not scheduled. - * @param {function} func The function to cancel. + * Cancels a scheduled debounced function. + * Does nothing if the function is not debounced or not scheduled. + * @param {function} func The function to cancel. Either the original or the debounced function. */ export function cancelDebounce(func) { if (debounceMap.has(func)) {