perf: optimization phases 3-5

This commit is contained in:
2026-02-26 12:50:12 -05:00
parent 7b3141e5e9
commit 2dd3384882
6 changed files with 41 additions and 4 deletions

View File

@ -9,9 +9,13 @@ export const useBusStopStore = defineStore('busStop', () => {
const busStops = ref<BusStop[]>([])
const isLoading = ref(false)
const error = ref<string | null>(null)
const lastFetched = ref<number>(0)
async function loadBusStops(force = false) {
if (!force && busStops.value.length > 0) {
const CACHE_TIME = 1000 * 60 * 30; // 30 minutos
const now = Date.now();
if (!force && busStops.value.length > 0 && (now - lastFetched.value < CACHE_TIME)) {
return
}
@ -19,6 +23,7 @@ export const useBusStopStore = defineStore('busStop', () => {
error.value = null
try {
busStops.value = await busStopsService.getAllBusStops()
lastFetched.value = now;
} catch (e) {
error.value = e instanceof Error ? e.message : 'Failed to load bus stops'
console.error('Error loading bus stops:', e)

View File

@ -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)