Allow cancel both by debounced and original functions

4de51087bce43aa4c2435d378176258186ed624a

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +7 -3Showing whitespace changes
public/scripts/utils.js+7 -3
@@ -285,16 +285,20 @@ const debounceMap = new WeakMap();
285 */285 */
286export function debounce(func, timeout = debounce_timeout.standard) {286export function debounce(func, timeout = debounce_timeout.standard) {
287 let timer;287 let timer;
288 return (...args) => {288 let fn = (...args) => {
289 clearTimeout(timer);289 clearTimeout(timer);
290 timer = setTimeout(() => { func.apply(this, args); }, timeout);290 timer = setTimeout(() => { func.apply(this, args); }, timeout);
291 debounceMap.set(func, timer);291 debounceMap.set(func, timer);
292 debounceMap.set(fn, timer);
292 };293 };
294
295 return fn;
293}296}
294297
295/**298/**
296 * Cancels a scheduled debounced function. Does nothing if the function is not debounced or not scheduled.299 * Cancels a scheduled debounced function.
297 * @param {function} func The function to cancel.300 * Does nothing if the function is not debounced or not scheduled.
301 * @param {function} func The function to cancel. Either the original or the debounced function.
298 */302 */
299export function cancelDebounce(func) {303export function cancelDebounce(func) {
300 if (debounceMap.has(func)) {304 if (debounceMap.has(func)) {