Change the type of debounce, use arrow functions (#9328)

This commit is contained in:
Bram Kragten 2021-06-01 20:53:45 +02:00 committed by GitHub
parent 8a1eab7ceb
commit 038199c447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 18 deletions

View File

@ -89,8 +89,6 @@ export const domainIcon = (
}
// eslint-disable-next-line
console.warn(
"Unable to find icon for domain " + domain + " (" + stateObj + ")"
);
console.warn(`Unable to find icon for domain ${domain}`);
return DEFAULT_DOMAIN_ICON;
};

View File

@ -4,29 +4,25 @@
// 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.
// eslint-disable-next-line: ban-types
export const debounce = <T extends (...args) => unknown>(
func: T,
wait,
export const debounce = <T extends any[]>(
func: (...args: T) => void,
wait: number,
immediate = false
): T => {
let timeout;
// @ts-ignore
return function (...args) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this;
) => {
let timeout: number | undefined;
return (...args: T): void => {
const later = () => {
timeout = null;
timeout = undefined;
if (!immediate) {
func.apply(context, args);
func(...args);
}
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
timeout = window.setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
func(...args);
}
};
};