ALwrity Version 0.5.0 (Fastapi + React )
This commit is contained in:
54
frontend/src/hooks/usePerformanceOptimization.ts
Normal file
54
frontend/src/hooks/usePerformanceOptimization.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { useEffect, useCallback, useRef } from 'react';
|
||||
|
||||
export const usePerformanceOptimization = () => {
|
||||
const animationFrameRef = useRef<number>();
|
||||
const timeoutRef = useRef<NodeJS.Timeout>();
|
||||
|
||||
// Debounce function for expensive operations
|
||||
const debounce = useCallback((func: Function, delay: number) => {
|
||||
return (...args: any[]) => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
timeoutRef.current = setTimeout(() => func(...args), delay);
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Throttle function for scroll/resize events
|
||||
const throttle = useCallback((func: Function, delay: number) => {
|
||||
let lastCall = 0;
|
||||
return (...args: any[]) => {
|
||||
const now = Date.now();
|
||||
if (now - lastCall >= delay) {
|
||||
lastCall = now;
|
||||
func(...args);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Optimize animations with requestAnimationFrame
|
||||
const smoothAnimation = useCallback((callback: () => void) => {
|
||||
if (animationFrameRef.current) {
|
||||
cancelAnimationFrame(animationFrameRef.current);
|
||||
}
|
||||
animationFrameRef.current = requestAnimationFrame(callback);
|
||||
}, []);
|
||||
|
||||
// Cleanup on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
if (animationFrameRef.current) {
|
||||
cancelAnimationFrame(animationFrameRef.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
debounce,
|
||||
throttle,
|
||||
smoothAnimation,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user