fix-initialize (#39)

Co-authored-by: Florent Champigny <florent@bere.al>
This commit is contained in:
Florent CHAMPIGNY 2025-08-04 14:38:34 +02:00 committed by GitHub
parent c4318c1f84
commit 6f9c6c7228
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 17 deletions

View file

@ -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) {

View file

@ -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<Throwable, Unit> {
private fun startServer(): Either<Throwable, Unit> {
return try {
startServerUseCase()
Success(Unit)