refactor(map): fix race conditions and async issues with atomic GPS/Data fetch
This commit is contained in:
@ -43,29 +43,31 @@ export const useRouteStore = defineStore('route', () => {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadRouteStops(routeId: string, force = false) {
|
||||
async function loadRouteStops(routeId: string, force = false): Promise<BusStop[]> {
|
||||
const CACHE_TIME = 1000 * 60 * 15; // 15 minutos
|
||||
const now = Date.now();
|
||||
|
||||
if (isLoadingStops.value) return;
|
||||
if (!force && stopsCache.value.has(routeId)) {
|
||||
if (stopsCache.value.has(routeId) && !force) {
|
||||
const cacheEntry = stopsCache.value.get(routeId)!;
|
||||
if (now - cacheEntry.fetchedAt < CACHE_TIME) {
|
||||
selectedRouteStops.value = cacheEntry.stops;
|
||||
return;
|
||||
return cacheEntry.stops;
|
||||
}
|
||||
}
|
||||
|
||||
if (isLoadingStops.value) return [];
|
||||
isLoadingStops.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const stops = await routesService.getRouteStops(routeId)
|
||||
selectedRouteStops.value = stops
|
||||
stopsCache.value.set(routeId, { fetchedAt: now, stops })
|
||||
return stops
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to load route stops'
|
||||
console.error('Error loading route stops:', e)
|
||||
selectedRouteStops.value = []
|
||||
return []
|
||||
} finally {
|
||||
isLoadingStops.value = false
|
||||
}
|
||||
@ -75,8 +77,7 @@ export const useRouteStore = defineStore('route', () => {
|
||||
if (selectedRouteId.value === routeId) return
|
||||
selectedRouteId.value = routeId
|
||||
selectedRouteName.value = routeName
|
||||
selectedRouteStops.value = [] // Clear old stops immediately
|
||||
await loadRouteStops(routeId)
|
||||
selectedRouteStops.value = [] // Limpia para forzar recarga atómica en la vista
|
||||
}
|
||||
|
||||
function clearSelection() {
|
||||
|
||||
Reference in New Issue
Block a user