perf: optimization phases 3-5
This commit is contained in:
@ -12,11 +12,16 @@ 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 hasSelectedRoute = computed(() => selectedRouteId.value !== null && selectedRouteName.value !== null)
|
||||
|
||||
async function loadRoutes(filters?: { originCity?: string, destinationCity?: string }, force = false) {
|
||||
if (!force && !filters && allRoutes.value.length > 0) {
|
||||
const CACHE_TIME = 1000 * 60 * 15; // 15 minutos
|
||||
const now = Date.now();
|
||||
|
||||
// Si no forzamos, no hay filtros raros, ya tenemos rutas y aún no expira el caché, omitir llamada
|
||||
if (!force && !filters && allRoutes.value.length > 0 && (now - lastFetched.value < CACHE_TIME)) {
|
||||
return
|
||||
}
|
||||
|
||||
@ -24,6 +29,7 @@ export const useRouteStore = defineStore('route', () => {
|
||||
error.value = null
|
||||
try {
|
||||
allRoutes.value = await routesService.getAllRoutes(filters)
|
||||
if (!filters) lastFetched.value = now; // Solo actualizar timer si es un request general limio
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to load routes'
|
||||
console.error('Error loading routes:', e)
|
||||
|
||||
Reference in New Issue
Block a user