Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class MainViewModel @Inject constructor(

private var sseJob: Job? = null
private var pollJob: Job? = null
private var lastHealthCheckTime = 0L
private var connectionTestJob: Job? = null

init {
loadSettings()
Expand Down Expand Up @@ -368,10 +368,8 @@ class MainViewModel @Inject constructor(
}

fun testConnection() {
val now = System.currentTimeMillis()
if (now - lastHealthCheckTime < 30_000) return
lastHealthCheckTime = now
launchConnectionTest(viewModelScope, repository, _state) {
connectionTestJob?.cancel()
connectionTestJob = launchConnectionTest(viewModelScope, repository, _state) {
loadInitialData()
startSSE()
startBusyPolling()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.yage.opencode_client.data.audio.AIBuildersAudioClient
import com.yage.opencode_client.data.repository.OpenCodeRepository
import com.yage.opencode_client.util.SettingsManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -49,8 +50,8 @@ internal fun launchConnectionTest(
repository: OpenCodeRepository,
state: MutableStateFlow<AppState>,
onHealthyConnection: () -> Unit
) {
scope.launch {
): Job {
return scope.launch {
state.update { it.copy(isConnecting = true, error = null) }
repository.checkHealth()
.onSuccess { health ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ class MainViewModelTest {
}

@Test
fun `testConnection skips second health check within cooldown`() = runTest {
fun `testConnection cancels previous test and starts a new one`() = runTest {
coEvery { repository.checkHealth() } returns Result.success(HealthResponse(healthy = false, version = "1.0"))

val viewModel = createViewModel()
Expand Down