diff --git a/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/app/AppViewModel.kt b/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/app/AppViewModel.kt index 01189747..edafd880 100644 --- a/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/app/AppViewModel.kt +++ b/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/app/AppViewModel.kt @@ -28,22 +28,7 @@ class AppViewModel( } } - // try to start the server - // if fails -> try again in 3s - // if success, just re-check again in 20s if it's still alive - viewModelScope.launch(dispatcherProvider.viewModel) { - while (isActive) { - messagesServerDelegate.startServer().fold( - doOnSuccess = { - delay(20.seconds) - }, - doOnFailure = { - delay(3.seconds) - } - ) - } - } - + messagesServerDelegate.initialize() viewModelScope.launch { while (isActive) { diff --git a/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/messages/ui/MessagesServerDelegate.kt b/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/messages/ui/MessagesServerDelegate.kt index 61c94d32..8935a69e 100644 --- a/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/messages/ui/MessagesServerDelegate.kt +++ b/FloconDesktop/composeApp/src/commonMain/kotlin/io/github/openflocon/flocondesktop/messages/ui/MessagesServerDelegate.kt @@ -1,22 +1,28 @@ package io.github.openflocon.flocondesktop.messages.ui +import androidx.lifecycle.viewModelScope import io.github.openflocon.flocondesktop.SERVER_PORT import io.github.openflocon.flocondesktop.common.Either import io.github.openflocon.flocondesktop.common.Failure import io.github.openflocon.flocondesktop.common.Success import io.github.openflocon.flocondesktop.common.coroutines.closeable.CloseableDelegate import io.github.openflocon.flocondesktop.common.coroutines.closeable.CloseableScoped +import io.github.openflocon.flocondesktop.common.coroutines.dispatcherprovider.DispatcherProvider import io.github.openflocon.flocondesktop.common.ui.feedback.FeedbackDisplayer import io.github.openflocon.flocondesktop.messages.domain.HandleIncomingMessagesUseCase import io.github.openflocon.flocondesktop.messages.domain.StartServerUseCase +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch +import kotlin.time.Duration.Companion.seconds class MessagesServerDelegate( private val startServerUseCase: StartServerUseCase, private val handleIncomingMessagesUseCase: HandleIncomingMessagesUseCase, private val closeableDelegate: CloseableDelegate, private val feedbackDisplayer: FeedbackDisplayer, + private val dispatcherProvider: DispatcherProvider, ) : CloseableScoped by closeableDelegate { fun initialize() { @@ -24,9 +30,27 @@ class MessagesServerDelegate( handleIncomingMessagesUseCase() .collect() } + + // try to start the server + // if fails -> try again in 3s + // if success, just re-check again in 20s if it's still alive + coroutineScope.launch { + while (isActive) { + startServer().fold( + doOnSuccess = { + delay(20.seconds) + }, + doOnFailure = { + delay(3.seconds) + } + ) + } + } } - fun startServer(): Either { + + + private fun startServer(): Either { return try { startServerUseCase() Success(Unit)