From 1f34a79676a00d4a4ee138c4a16c866d7ef20cf9 Mon Sep 17 00:00:00 2001 From: davotoula Date: Tue, 24 Mar 2026 19:41:20 +0100 Subject: [PATCH] fix: prevent double-fetch and stop re-polling finished chess games - Mark recentlyLoadedGames timestamp eagerly in refreshGame (before async fetch) to prevent concurrent fetches for the same game - Clear focused game in removeGameId when the removed game matches, so finished games stop being re-polled Co-Authored-By: Claude Opus 4.6 --- .../vitorpamplona/amethyst/commons/chess/ChessEventPolling.kt | 4 ++++ .../vitorpamplona/amethyst/commons/chess/ChessLobbyLogic.kt | 2 ++ 2 files changed, 6 insertions(+) diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/chess/ChessEventPolling.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/chess/ChessEventPolling.kt index 91a46df5c..277ddf542 100644 --- a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/chess/ChessEventPolling.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/chess/ChessEventPolling.kt @@ -161,6 +161,10 @@ class ChessPollingDelegate( */ fun removeGameId(gameId: String) { activeGameIdsFlow.value = activeGameIdsFlow.value - gameId + // If this was the focused game, clear focus to stop re-polling a finished game + if (_focusedGameId.value == gameId) { + _focusedGameId.value = null + } } /** diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/chess/ChessLobbyLogic.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/chess/ChessLobbyLogic.kt index 4ed8ed9d4..49e218047 100644 --- a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/chess/ChessLobbyLogic.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/chess/ChessLobbyLogic.kt @@ -677,6 +677,8 @@ class ChessLobbyLogic( Log.d("chessdebug", "[Lobby] refreshGame: SKIPPED game ${startEventId.take(8)} - loaded ${TimeUtils.now() - lastLoaded}s ago") return } + // Mark immediately to prevent concurrent fetches for the same game + recentlyLoadedGames[startEventId] = TimeUtils.now() Log.d("chessdebug", "[Lobby] refreshGame: fetching game ${startEventId.take(8)} from relays") val events = fetcher.fetchGameEvents(startEventId)