mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-16 23:59:39 +02:00
- 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>
37 lines
859 B
TypeScript
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;
|
|
}
|