Skip to content

Commit 56108ac

Browse files
committed
Fix showing loading indicator after changing connection configuration
1 parent 13476e1 commit 56108ac

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

rpc/src/main/kotlin/org/equeim/tremotesf/rpc/RpcRequestState.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ sealed interface RpcRequestState<out T> {
4747
fun <T> RpcClient.performRecoveringRequest(
4848
performRequest: suspend RpcClient.() -> T,
4949
): Flow<RpcRequestState<T>> =
50-
requestRestartEvents()
50+
startRequestFromScratchEvents()
5151
.transformLatest {
5252
if (it.nonRecoverableError != null) {
5353
emit(it.nonRecoverableError)
@@ -70,14 +70,15 @@ fun <T> RpcClient.performPeriodicRequest(
7070
return channelFlow {
7171
var lastEmittedState: RpcRequestState<T>? = null
7272
merge(
73-
requestRestartEvents(),
74-
manualRefreshRequests.map { RequestRestartEvent() }
73+
startRequestFromScratchEvents(),
74+
manualRefreshRequests.map { ManualRefresh() }
7575
).transformLatest {
76-
if (it.nonRecoverableError != null) {
77-
emit(it.nonRecoverableError)
76+
(it as? StartRequestFromScratch)?.nonRecoverableError?.let { error ->
77+
emit(error)
7878
return@transformLatest
7979
}
80-
if (lastEmittedState !is RpcRequestState.Loaded) {
80+
// If user triggered manual refresh and we are already loaded then we don't want to emit Loading state
81+
if (!(it is ManualRefresh && lastEmittedState is RpcRequestState.Loaded)) {
8182
emit(RpcRequestState.Loading)
8283
}
8384
while (currentCoroutineContext().isActive) {
@@ -96,6 +97,8 @@ fun <T> RpcClient.performPeriodicRequest(
9697
}
9798
}
9899

100+
private class ManualRefresh
101+
99102
fun <T> RpcClient.performPeriodicRequestIntoStateFlow(
100103
scope: CoroutineScope,
101104
manualRefreshRequests: Flow<*> = emptyFlow<Unit>(),
@@ -161,13 +164,13 @@ private suspend fun <T> RpcClient.actuallyPerformRecoveringRequest(
161164
}
162165
}
163166

164-
private class RequestRestartEvent(val nonRecoverableError: RpcRequestState.Error? = null)
167+
private class StartRequestFromScratch(val nonRecoverableError: RpcRequestState.Error? = null)
165168

166-
private fun RpcClient.requestRestartEvents(): Flow<RequestRestartEvent> = combine(
169+
private fun RpcClient.startRequestFromScratchEvents(): Flow<StartRequestFromScratch> = combine(
167170
getConnectionConfiguration(),
168171
shouldConnectToServer
169172
) { configuration, shouldConnectToServer ->
170-
RequestRestartEvent(getInitialNonRecoverableError(configuration, shouldConnectToServer))
173+
StartRequestFromScratch(getInitialNonRecoverableError(configuration, shouldConnectToServer))
171174
}
172175

173176
private fun getInitialNonRecoverableError(

0 commit comments

Comments
 (0)