From 7fc4443206026b03f70b618182aeab924f9282b0 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 21 Dec 2024 17:00:36 +0200 Subject: [PATCH] chore(client/ts): port services/debounce --- .../app/services/{debounce.js => debounce.ts} | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) rename src/public/app/services/{debounce.js => debounce.ts} (70%) diff --git a/src/public/app/services/debounce.js b/src/public/app/services/debounce.ts similarity index 70% rename from src/public/app/services/debounce.js rename to src/public/app/services/debounce.ts index 4e5429a32..4f9972a7b 100644 --- a/src/public/app/services/debounce.js +++ b/src/public/app/services/debounce.ts @@ -7,13 +7,17 @@ * * @source underscore.js * @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/ - * @param {Function} func to wrap - * @param {Number} waitMs in ms (`100`) - * @param {Boolean} [immediate=false] whether to execute at the beginning (`false`) + * @param func to wrap + * @param waitMs in ms (`100`) + * @param whether to execute at the beginning (`false`) * @api public */ -function debounce(func, waitMs, immediate = false) { - let timeout, args, context, timestamp, result; +function debounce(func: (...args: unknown[]) => T, waitMs: number, immediate: boolean = false) { + let timeout: any; // TODO: fix once we split client and server. + let args: unknown[] | null; + let context: unknown; + let timestamp: number; + let result: T; if (null == waitMs) waitMs = 100; function later() { @@ -24,20 +28,20 @@ function debounce(func, waitMs, immediate = false) { } else { timeout = null; if (!immediate) { - result = func.apply(context, args); + result = func.apply(context, args || []); context = args = null; } } } - const debounced = function () { + const debounced = function (this: any) { context = this; - args = arguments; + args = arguments as unknown as unknown[]; timestamp = Date.now(); const callNow = immediate && !timeout; if (!timeout) timeout = setTimeout(later, waitMs); if (callNow) { - result = func.apply(context, args); + result = func.apply(context, args || []); context = args = null; } @@ -53,7 +57,7 @@ function debounce(func, waitMs, immediate = false) { debounced.flush = function() { if (timeout) { - result = func.apply(context, args); + result = func.apply(context, args || []); context = args = null; clearTimeout(timeout);