Revert "feat: reconnect mechanism that doesn't limit retry count"

This reverts commit 456830a4fa.
This commit is contained in:
Infi 2023-11-12 21:41:18 +01:00
parent afa1f5941f
commit 2507206ea3
1 changed files with 13 additions and 25 deletions

View File

@ -1,6 +1,5 @@
package chat.revolt.components.chat package chat.revolt.components.chat
import android.util.Log
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
@ -17,8 +16,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@ -31,10 +28,6 @@ import chat.revolt.R
import chat.revolt.api.realtime.DisconnectionState import chat.revolt.api.realtime.DisconnectionState
import chat.revolt.api.settings.GlobalState import chat.revolt.api.settings.GlobalState
import chat.revolt.ui.theme.Theme import chat.revolt.ui.theme.Theme
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
private val NON_MATERIAL_COLOURS = mapOf( private val NON_MATERIAL_COLOURS = mapOf(
DisconnectionState.Disconnected to (Color(0xff4E0C0C) to Color(0xffff1744)), DisconnectionState.Disconnected to (Color(0xff4E0C0C) to Color(0xffff1744)),
@ -83,28 +76,23 @@ private fun DisconnectedNoticeBase(
@Composable @Composable
fun DisconnectedNotice(state: DisconnectionState, onReconnect: () -> Unit) { fun DisconnectedNotice(state: DisconnectionState, onReconnect: () -> Unit) {
// This is a coroutine-based retry mechanism that will retry every 10 seconds val retries = remember { mutableStateOf(0) }
// until the connection is re-established. If the connection is re-established
// we don't need to keep retrying.
val job = remember { mutableStateOf<Job?>(null) }
val scope = rememberCoroutineScope()
LaunchedEffect(state) { LaunchedEffect(state) {
snapshotFlow { state } when (state) {
.distinctUntilChanged() DisconnectionState.Disconnected -> {
.collect { state -> if (retries.value < 3) {
if (state == DisconnectionState.Disconnected) { onReconnect()
job.value = scope.launch { retries.value++
while (true) {
Log.d("DisconnectedNotice", "Trying to reconnect.")
onReconnect()
delay(10_000)
}
}
} else {
job.value?.cancel()
} }
} }
DisconnectionState.Connected -> {
retries.value = 0
}
else -> Unit
}
} }
val materialColours = mapOf( val materialColours = mapOf(