perf: complete performance audit optimizations
This commit is contained in:
@ -12,7 +12,8 @@ export const useRouteStore = defineStore('route', () => {
|
||||
const isLoadingRoutes = ref(false)
|
||||
const isLoadingStops = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
const lastFetched = ref<number>(0) // ⚡ CACHÉ ESTÁTICO
|
||||
const lastFetched = ref<number>(0) // ⚡ CACHÉ ESTÁTICO RUTAS
|
||||
const stopsCache = ref<Map<string, { fetchedAt: number, stops: BusStop[] }>>(new Map()) // ⚡ CACHÉ ESTÁTICO PARADAS
|
||||
|
||||
const hasSelectedRoute = computed(() => selectedRouteId.value !== null && selectedRouteName.value !== null)
|
||||
|
||||
@ -38,11 +39,24 @@ export const useRouteStore = defineStore('route', () => {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadRouteStops(routeId: string) {
|
||||
async function loadRouteStops(routeId: string, force = false) {
|
||||
const CACHE_TIME = 1000 * 60 * 15; // 15 minutos
|
||||
const now = Date.now();
|
||||
|
||||
if (!force && stopsCache.value.has(routeId)) {
|
||||
const cacheEntry = stopsCache.value.get(routeId)!;
|
||||
if (now - cacheEntry.fetchedAt < CACHE_TIME) {
|
||||
selectedRouteStops.value = cacheEntry.stops;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
isLoadingStops.value = true
|
||||
error.value = null
|
||||
try {
|
||||
selectedRouteStops.value = await routesService.getRouteStops(routeId)
|
||||
const stops = await routesService.getRouteStops(routeId)
|
||||
selectedRouteStops.value = stops
|
||||
stopsCache.value.set(routeId, { fetchedAt: now, stops })
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to load route stops'
|
||||
console.error('Error loading route stops:', e)
|
||||
|
||||
Reference in New Issue
Block a user