managarten/apps/context/apps/mobile/utils/debounce.ts
Till-JS 86a6ff23c7 fix(context): add expo-font dependency and fix TypeScript issues
- Add expo-font package for font loading
- Fix various TypeScript type issues in components
- Update i18n utilities

Note: Some TypeScript errors remain and will be addressed in follow-up.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 15:30:31 +01:00

37 lines
859 B
TypeScript

export function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number,
options?: { leading?: boolean; trailing?: boolean }
): ((...args: Parameters<T>) => void) & { cancel: () => void } {
let timeout: NodeJS.Timeout | null = null;
let result: any;
const leading = options?.leading ?? false;
const trailing = options?.trailing ?? true;
const debounced = function (this: any, ...args: Parameters<T>) {
const context = this;
const later = () => {
timeout = null;
if (trailing) result = func.apply(context, args);
};
const callNow = leading && !timeout;
if (timeout) clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) result = func.apply(context, args);
return result;
};
debounced.cancel = () => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};
return debounced;
}