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 // eslint-disable-next-line
console.warn( console.warn(`Unable to find icon for domain ${domain}`);
"Unable to find icon for domain " + domain + " (" + stateObj + ")"
);
return DEFAULT_DOMAIN_ICON; return DEFAULT_DOMAIN_ICON;
}; };

View File

@ -4,29 +4,25 @@
// be triggered. The function will be called after it stops being called for // be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the // N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing. // leading edge, instead of the trailing.
// eslint-disable-next-line: ban-types
export const debounce = <T extends (...args) => unknown>( export const debounce = <T extends any[]>(
func: T, func: (...args: T) => void,
wait, wait: number,
immediate = false immediate = false
): T => { ) => {
let timeout; let timeout: number | undefined;
// @ts-ignore return (...args: T): void => {
return function (...args) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this;
const later = () => { const later = () => {
timeout = null; timeout = undefined;
if (!immediate) { if (!immediate) {
func.apply(context, args); func(...args);
} }
}; };
const callNow = immediate && !timeout; const callNow = immediate && !timeout;
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(later, wait); timeout = window.setTimeout(later, wait);
if (callNow) { if (callNow) {
func.apply(context, args); func(...args);
} }
}; };
}; };