mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-03-22 20:50:07 +00:00
24 lines
484 B
TypeScript
24 lines
484 B
TypeScript
export function debounced<T extends (...args: any[]) => any>(
|
|
func: T,
|
|
delay: number,
|
|
onLoadingChange?: (loading: boolean) => void
|
|
) {
|
|
let debounceTimeout: ReturnType<typeof setTimeout>;
|
|
|
|
return (...args: Parameters<T>) => {
|
|
if (debounceTimeout !== undefined) {
|
|
clearTimeout(debounceTimeout);
|
|
}
|
|
|
|
onLoadingChange?.(true);
|
|
|
|
debounceTimeout = setTimeout(async () => {
|
|
try {
|
|
await func(...args);
|
|
} finally {
|
|
onLoadingChange?.(false);
|
|
}
|
|
}, delay);
|
|
};
|
|
}
|