Skip to content

Native Android client for Spotify, built with Jetpack Compose

Merge branch 'refs/heads/worktree-agent-adee56a78e12c09a1' into claude/discord-messages-review-72e44d

kota-zo113d7494fc65
diff --cc base/api-spotify/src/main/kotlin/app/myspoti/spotify/bridge/Bridge.kt
index 94207dd2,9c19f457..c0ca85cb
--- 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
@@@ -175,19 -180,35 +180,35 @@@ class Bridge(private val host: BridgeHo
val waiter = CompletableDeferred<JsonElement>()
pending[id] = waiter
try {
+ // The page went away between the check above and the waiter's registration: pageGone
+ // did not see this call, and nothing of that page will answer it.
+ if (gone || pagesGone != page) throw BridgeException("page reloaded")
val started = System.currentTimeMillis()
- if (!quiet) logger?.invoke("call #$id $method${label?.let { " ($it)" }.orEmpty()} ${args.toString().take(200)}")
+ if (!quiet) logger?.invoke("call #$id $method${label?.let { " ($it)" }.orEmpty()} ${LogMask.shown(args.toString())}")
- host.evaluate("window.Myspoti && window.Myspoti.call($id, ${json.encodeToString(JsonElement.serializer(), kotlinx.serialization.json.JsonPrimitive(method))}, $args).catch(() => {})")
+ // Run only by the page it was made for (the generation set by inject), which says so.
+ val script = "(window.Myspoti && window.__mspPage === $page) ? (window.Myspoti.call($id, ${json.encodeToString(JsonElement.serializer(), kotlinx.serialization.json.JsonPrimitive(method))}, $args).catch(() => {}), true) : false"
return try {
// withTimeoutOrNull, not withTimeout: a timeout must surface as a
// BridgeException, not as a CancellationException — callers in plain
// launch {} blocks would otherwise end silently, indistinguishable
// from the caller itself being cancelled (which still throws).
- val result = withTimeoutOrNull(timeoutMs) { waiter.await() }
- ?: throw BridgeException("timeout after $timeoutMs ms: $method")
+ val result = withTimeoutOrNull(timeoutMs) {
+ coroutineScope {
+ // A script the host could not run, or one that found no bridge of this
+ // page, fails the call now instead of after its timeout.
+ val sent = launch {
+ val value = try { host.evaluateValue(script) } catch (e: CancellationException) { throw e } catch (e: Exception) {
+ waiter.completeExceptionally(e as? BridgeException ?: BridgeException("not sent: ${e.message}"))
+ return@launch
+ }
+ if (value == "false") waiter.completeExceptionally(BridgeException("the bridge is not in this page"))
+ }
+ try { waiter.await() } finally { sent.cancel() }
+ }
+ } ?: throw BridgeException("timeout after $timeoutMs ms: $method")
result.also {
if (quiet) return@also
- val shown = if (method == "token.get") "(token hidden)" else it.toString().take(200)
+ val shown = if (method == "token.get") "(token hidden)" else LogMask.shown(it.toString())
logger?.invoke("call #$id $method ok in ${System.currentTimeMillis() - started} ms: $shown")
}
} catch (e: Exception) {