UNPKG

@chauffleet/expo-custom-map

Version:

Open source custom map library for Expo/React Native. Use your own tiles without Google Maps, Mapbox, or API keys. Created by ChaufFleet.

156 lines 5.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // src/hooks/useMapPerformance.ts const react_1 = require("react"); const useMapPerformance = () => { const [stats, setStats] = (0, react_1.useState)({ fps: 0, frameTime: 0, tileLoadTime: 0, cacheHitRate: 0, memoryUsage: 0, }); const [cache, setCache] = (0, react_1.useState)({ size: 0, currentSizeMB: 0, maxSizeMB: 100, hitRate: 0, }); const [isMonitoring, setIsMonitoring] = (0, react_1.useState)(false); const intervalRef = (0, react_1.useRef)(null); const frameCountRef = (0, react_1.useRef)(0); const lastFrameTimeRef = (0, react_1.useRef)(0); const frameTimesRef = (0, react_1.useRef)([]); const tileLoadTimesRef = (0, react_1.useRef)([]); const cacheHitsRef = (0, react_1.useRef)(0); const cacheMissesRef = (0, react_1.useRef)(0); // Mesurer les FPS const measureFPS = (0, react_1.useCallback)(() => { const now = performance.now(); if (lastFrameTimeRef.current > 0) { const frameTime = now - lastFrameTimeRef.current; frameTimesRef.current.push(frameTime); // Garder seulement les 60 dernières mesures if (frameTimesRef.current.length > 60) { frameTimesRef.current.shift(); } // Calculer FPS moyen const avgFrameTime = frameTimesRef.current.reduce((a, b) => a + b, 0) / frameTimesRef.current.length; const fps = Math.round(1000 / avgFrameTime); setStats(prev => ({ ...prev, fps: Math.min(fps, 60), // Cap à 60 FPS frameTime: Math.round(avgFrameTime * 100) / 100, })); } lastFrameTimeRef.current = now; frameCountRef.current++; }, []); // Enregistrer le temps de chargement d'une tuile const recordTileLoadTime = (0, react_1.useCallback)((loadTime) => { tileLoadTimesRef.current.push(loadTime); // Garder seulement les 100 dernières mesures if (tileLoadTimesRef.current.length > 100) { tileLoadTimesRef.current.shift(); } const avgTileLoadTime = tileLoadTimesRef.current.reduce((a, b) => a + b, 0) / tileLoadTimesRef.current.length; setStats(prev => ({ ...prev, tileLoadTime: Math.round(avgTileLoadTime), })); }, []); // Enregistrer un hit de cache const recordCacheHit = (0, react_1.useCallback)(() => { cacheHitsRef.current++; updateCacheStats(); }, []); // Enregistrer un miss de cache const recordCacheMiss = (0, react_1.useCallback)(() => { cacheMissesRef.current++; updateCacheStats(); }, []); // Mettre à jour les statistiques de cache const updateCacheStats = (0, react_1.useCallback)(() => { const totalRequests = cacheHitsRef.current + cacheMissesRef.current; const hitRate = totalRequests > 0 ? cacheHitsRef.current / totalRequests : 0; setStats(prev => ({ ...prev, cacheHitRate: Math.round(hitRate * 100) / 100, })); setCache(prev => ({ ...prev, hitRate: Math.round(hitRate * 100) / 100, })); }, []); // Estimer l'utilisation mémoire const estimateMemoryUsage = (0, react_1.useCallback)(() => { if ('memory' in performance) { const memInfo = performance.memory; const usedJSHeapSize = memInfo.usedJSHeapSize; const memoryUsageMB = Math.round(usedJSHeapSize / 1024 / 1024 * 100) / 100; setStats(prev => ({ ...prev, memoryUsage: memoryUsageMB, })); } }, []); // Démarrer la surveillance const startMonitoring = (0, react_1.useCallback)(() => { if (isMonitoring) return; setIsMonitoring(true); // Surveillance des FPS et mémoire intervalRef.current = setInterval(() => { measureFPS(); estimateMemoryUsage(); }, 1000 / 60); // 60 FPS }, [isMonitoring, measureFPS, estimateMemoryUsage]); // Arrêter la surveillance const stopMonitoring = (0, react_1.useCallback)(() => { if (!isMonitoring) return; setIsMonitoring(false); if (intervalRef.current) { clearInterval(intervalRef.current); intervalRef.current = null; } }, [isMonitoring]); // Réinitialiser les statistiques const resetStats = (0, react_1.useCallback)(() => { frameCountRef.current = 0; lastFrameTimeRef.current = 0; frameTimesRef.current = []; tileLoadTimesRef.current = []; cacheHitsRef.current = 0; cacheMissesRef.current = 0; setStats({ fps: 0, frameTime: 0, tileLoadTime: 0, cacheHitRate: 0, memoryUsage: 0, }); setCache(prev => ({ ...prev, hitRate: 0, })); }, []); // Nettoyer à la désinscription (0, react_1.useEffect)(() => { return () => { if (intervalRef.current) { clearInterval(intervalRef.current); } }; }, []); return { stats, cache, startMonitoring, stopMonitoring, resetStats, isMonitoring, }; }; exports.default = useMapPerformance; //# sourceMappingURL=useMapPerformance.js.map