Cancel ongoing weather forecast job before launching a new one to prevent duplicate requests

This commit is contained in:
Nebojsa Vuksic 2025-08-07 11:02:13 +02:00
parent de0692bba6
commit 54ca91fea4
2 changed files with 11 additions and 4 deletions

View File

@ -1,9 +1,8 @@
package org.jetbrains.plugins.template.weatherApp.services package org.jetbrains.plugins.template.weatherApp.services
import com.intellij.openapi.Disposable import com.intellij.openapi.Disposable
import com.intellij.openapi.application.EDT
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -59,6 +58,8 @@ class WeatherAppViewModel(
private val weatherService: WeatherForecastServiceApi, private val weatherService: WeatherForecastServiceApi,
) : MyLocationsViewModelApi, WeatherViewModelApi { ) : MyLocationsViewModelApi, WeatherViewModelApi {
private var currentWeatherJob: Job? = null
private val myLocations = MutableStateFlow(myInitialLocations) private val myLocations = MutableStateFlow(myInitialLocations)
private val selectedLocationIndex = MutableStateFlow(myLocations.value.lastIndex) private val selectedLocationIndex = MutableStateFlow(myLocations.value.lastIndex)
@ -124,7 +125,9 @@ class WeatherAppViewModel(
} }
override fun onLoadWeatherForecast(location: Location) { override fun onLoadWeatherForecast(location: Location) {
viewModelScope.launch { currentWeatherJob?.cancel()
currentWeatherJob = viewModelScope.launch {
val weatherForecastData = weatherService.loadWeatherForecastFor(location).getOrNull() ?: return@launch val weatherForecastData = weatherService.loadWeatherForecastFor(location).getOrNull() ?: return@launch
_weatherForecast.value = weatherForecastData _weatherForecast.value = weatherForecastData

View File

@ -3,6 +3,7 @@ package org.jetbrains.plugins.template.weatherApp.services
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import org.jetbrains.plugins.template.weatherApp.model.* import org.jetbrains.plugins.template.weatherApp.model.*
import java.time.LocalDate import java.time.LocalDate
import java.time.LocalDateTime import java.time.LocalDateTime
@ -45,10 +46,13 @@ class WeatherForecastService(
private suspend fun getWeatherData(location: Location): WeatherForecastData { private suspend fun getWeatherData(location: Location): WeatherForecastData {
val currentTime = LocalDateTime.of(LocalDate.now(), getRandomTime()) val currentTime = LocalDateTime.of(LocalDate.now(), getRandomTime())
yield() // Check cancellation
// Generate 7-day forecast data // Generate 7-day forecast data
val dailyForecasts = generateDailyForecasts(currentTime) val dailyForecasts = generateDailyForecasts(currentTime)
// Simulates a network request // Simulates a network request and stops the execution in case the coroutine
// that launched the getWeatherData task is canceled
delay(100) delay(100)
return WeatherForecastData( return WeatherForecastData(