mirror of
https://github.com/openflocon/Flocon.git
synced 2026-05-06 05:18:41 +00:00
fix-initialize (#39)
Co-authored-by: Florent Champigny <florent@bere.al>
This commit is contained in:
parent
c4318c1f84
commit
6f9c6c7228
2 changed files with 26 additions and 17 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue