Skip to content

Native Android client for Spotify, built with Jetpack Compose

Bridge buffers: drop oldest, keep the newest state

liliapkc395c83cf10c
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/bridge/Bridge.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/bridge/Bridge.kt
index 3f742e2e..4ba8d2b1 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/bridge/Bridge.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/bridge/Bridge.kt
@@ -1,5 +1,6 @@
package app.myspoti.spotify.bridge
+import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
@@ -32,8 +33,9 @@ class Bridge(private val host: BridgeHost, scope: CoroutineScope) {
private val _ready = MutableStateFlow(false)
val ready: StateFlow<Boolean> = _ready
- /** `state` and `queue` messages, raw payload of each. */
- private val _events = MutableSharedFlow<Pair<String, JsonElement>>(extraBufferCapacity = 64)
+ /** `state` and `queue` messages, raw payload of each. DROP_OLDEST: under a stall the
+ * newest state is the only one that matters; SUSPEND would drop exactly that one. */
+ private val _events = MutableSharedFlow<Pair<String, JsonElement>>(extraBufferCapacity = 64, onBufferOverflow = BufferOverflow.DROP_OLDEST)
val events: SharedFlow<Pair<String, JsonElement>> = _events
init {
diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt
index 5e6003d0..9d27c824 100644
--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt
+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt
@@ -18,6 +18,7 @@ import android.webkit.WebViewClient
import app.myspoti.spotify.bridge.AdBlock
import app.myspoti.spotify.bridge.BridgeHost
import kotlinx.coroutines.CompletableDeferred
+import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
@@ -36,7 +37,9 @@ import java.io.ByteArrayInputStream
class WebPlayerHost(context: Context) : BridgeHost {
private val main = Handler(Looper.getMainLooper())
- private val _messages = MutableSharedFlow<String>(extraBufferCapacity = 256)
+ // DROP_OLDEST: tryEmit under a main-thread stall must not drop the newest
+ // state/result (the default SUSPEND overflow drops exactly the new value).
+ private val _messages = MutableSharedFlow<String>(extraBufferCapacity = 256, onBufferOverflow = BufferOverflow.DROP_OLDEST)
override val messages: SharedFlow<String> = _messages
private val _url = MutableStateFlow("")