fix: robust mobile suspend and auth recovery
This commit is contained in:
@ -18,19 +18,46 @@ export const useRouteStore = defineStore('route', () => {
|
||||
|
||||
const hasSelectedRoute = computed(() => selectedRouteId.value !== null && selectedRouteName.value !== null)
|
||||
|
||||
async function loadRoutes(filters?: { originCity?: string, destinationCity?: string }, force = false) {
|
||||
// Safety: nunca más de 12s cargando (protección contra thread congelado por OS al apagar pantalla)
|
||||
let _routesSafetyTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let _stopsSafetyTimer: ReturnType<typeof setTimeout> | null = null
|
||||
function _startRoutesSafety() {
|
||||
if (_routesSafetyTimer) clearTimeout(_routesSafetyTimer)
|
||||
_routesSafetyTimer = setTimeout(() => {
|
||||
if (isLoadingRoutes.value) {
|
||||
console.warn('SIBU | routeStore: routes safety timeout — reseteando isLoadingRoutes')
|
||||
isLoadingRoutes.value = false
|
||||
}
|
||||
}, 12000)
|
||||
}
|
||||
function _startStopsSafety() {
|
||||
if (_stopsSafetyTimer) clearTimeout(_stopsSafetyTimer)
|
||||
_stopsSafetyTimer = setTimeout(() => {
|
||||
if (isLoadingStops.value) {
|
||||
console.warn('SIBU | routeStore: stops safety timeout — reseteando isLoadingStops')
|
||||
isLoadingStops.value = false
|
||||
}
|
||||
}, 12000)
|
||||
}
|
||||
|
||||
async function loadRoutes(filters?: { originCity?: string, destinationCity?: string }, force = false, isBackground = false) {
|
||||
const CACHE_TIME = 1000 * 60 * 15; // 15 minutos
|
||||
const now = Date.now();
|
||||
|
||||
// Guard: Si ya se están cargando rutas, no iniciar otra petición
|
||||
if (isLoadingRoutes.value) return;
|
||||
// Guard: Si ya se están cargando rutas y NO estamos en background, omitir
|
||||
// Si estamos en background, podemos sobreescribir la carga sin mostrar spinner
|
||||
if (isLoadingRoutes.value && !isBackground) return;
|
||||
|
||||
// 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)) {
|
||||
// Excepción: isBackground de refocus obliga a actualizar para asegurar frescura después de mucho rato apagado.
|
||||
if (!force && !isBackground && !filters && allRoutes.value.length > 0 && (now - lastFetched.value < CACHE_TIME)) {
|
||||
return
|
||||
}
|
||||
|
||||
isLoadingRoutes.value = true
|
||||
if (!isBackground) {
|
||||
isLoadingRoutes.value = true
|
||||
_startRoutesSafety()
|
||||
}
|
||||
error.value = null
|
||||
try {
|
||||
allRoutes.value = await routesService.getAllRoutes(filters)
|
||||
@ -39,7 +66,10 @@ export const useRouteStore = defineStore('route', () => {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to load routes'
|
||||
console.error('Error loading routes:', e)
|
||||
} finally {
|
||||
isLoadingRoutes.value = false
|
||||
if (!isBackground) {
|
||||
if (_routesSafetyTimer) { clearTimeout(_routesSafetyTimer); _routesSafetyTimer = null }
|
||||
isLoadingRoutes.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,6 +88,7 @@ export const useRouteStore = defineStore('route', () => {
|
||||
if (isLoadingStops.value) return [];
|
||||
isLoadingStops.value = true
|
||||
error.value = null
|
||||
_startStopsSafety()
|
||||
try {
|
||||
const stops = await routesService.getRouteStops(routeId)
|
||||
selectedRouteStops.value = stops
|
||||
@ -69,6 +100,7 @@ export const useRouteStore = defineStore('route', () => {
|
||||
selectedRouteStops.value = []
|
||||
return []
|
||||
} finally {
|
||||
if (_stopsSafetyTimer) { clearTimeout(_stopsSafetyTimer); _stopsSafetyTimer = null }
|
||||
isLoadingStops.value = false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user