1
0
mirror of https://github.com/pocket-id/pocket-id.git synced 2026-03-22 20:50:07 +00:00
Files
pocket-id/frontend/src/lib/utils/debounce-util.ts
Kyle Mendell c111b79147 feat: oidc client data preview (#624)
Co-authored-by: Elias Schneider <login@eliasschneider.com>
2025-06-09 15:46:03 +00:00

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);
};
}