feat: reconnect mechanism that doesn't limit retry count
Signed-off-by: Infi <infi@infi.sh>
This commit is contained in:
parent
182566dcc0
commit
456830a4fa
|
|
@ -1,5 +1,6 @@
|
||||||
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
|
||||||
|
|
@ -16,6 +17,8 @@ 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
|
||||||
|
|
@ -28,6 +31,10 @@ 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)),
|
||||||
|
|
@ -76,23 +83,28 @@ private fun DisconnectedNoticeBase(
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun DisconnectedNotice(state: DisconnectionState, onReconnect: () -> Unit) {
|
fun DisconnectedNotice(state: DisconnectionState, onReconnect: () -> Unit) {
|
||||||
val retries = remember { mutableStateOf(0) }
|
// This is a coroutine-based retry mechanism that will retry every 10 seconds
|
||||||
|
// 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) {
|
||||||
when (state) {
|
snapshotFlow { state }
|
||||||
DisconnectionState.Disconnected -> {
|
.distinctUntilChanged()
|
||||||
if (retries.value < 3) {
|
.collect { state ->
|
||||||
onReconnect()
|
if (state == DisconnectionState.Disconnected) {
|
||||||
retries.value++
|
job.value = scope.launch {
|
||||||
|
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(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue