mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-11 07:29:14 +01:00
Re-notify replay on every connectivity change, not just availability flips
conflate rather than distinctUntilChanged: switching between two available networks (WiFi LAN server -> cellular -> WiFi) kept the signal true, so the queue never drained until another trigger. Emit on each callback instead, and drop the sink combine's distinctUntilChanged so a repeated ready edge still flushes. Simplify flush to reuse the safe drain. Add a regression test that replays on two true emissions with no false between.
This commit is contained in:
@@ -7,6 +7,7 @@ import android.net.NetworkCapabilities
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.conflate
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
|
||||
enum class NetworkMode(
|
||||
@@ -55,6 +56,8 @@ fun networkMode(context: Context): Flow<NetworkMode> {
|
||||
|
||||
fun networkAvailable(context: Context): Flow<Boolean> {
|
||||
val connectivity = context.applicationContext.getSystemService(ConnectivityManager::class.java)
|
||||
// Conflated, not distinct: switching between two available networks must still re-notify so the
|
||||
// replay retries against the (possibly different) newly active route to the server.
|
||||
return callbackFlow {
|
||||
fun push() {
|
||||
val capabilities =
|
||||
@@ -76,5 +79,5 @@ fun networkAvailable(context: Context): Flow<Boolean> {
|
||||
val registered = runCatching { connectivity?.registerDefaultNetworkCallback(callback) }.isSuccess
|
||||
push()
|
||||
awaitClose { if (registered) runCatching { connectivity?.unregisterNetworkCallback(callback) } }
|
||||
}.distinctUntilChanged()
|
||||
}.conflate()
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
@@ -31,7 +30,6 @@ class PendingServerActionSink(
|
||||
init {
|
||||
scope.launch {
|
||||
combine(offlinePreferences, networkAvailable) { isOffline, online -> !isOffline && online }
|
||||
.distinctUntilChanged()
|
||||
.collect { ready ->
|
||||
// The persisted flag and connectivity are authoritative, unlike the process
|
||||
// flag's default at cold start, so wait for that flag to agree before replaying.
|
||||
@@ -89,14 +87,7 @@ class PendingServerActionSink(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun flush() {
|
||||
try {
|
||||
lock.withLock { drainLocked() }
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
}
|
||||
override suspend fun flush() = lock.withLock { drainSafelyLocked() }
|
||||
|
||||
private suspend fun drainLocked() {
|
||||
if (offline.value) return
|
||||
|
||||
Reference in New Issue
Block a user