From 8d7f6a0ccb2d9a22fabb4d022f1c8d2a71ffec4e Mon Sep 17 00:00:00 2001 From: TapTap Date: Wed, 18 Nov 2026 19:48:09 +0000 Subject: [PATCH] fix: re-run search immediately when the mood source changes The explicit source switch cancelled the debounced search and then waited another 300 ms for no reason. It now searches right away, which also makes the test deterministic. --- .../win/taptap/musicmanager/client/store/AppStore.kt | 12 ++++++++++-- .../win/taptap/musicmanager/client/AppStoreTest.kt | 5 +---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt index 5f18010..0efab92 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt @@ -369,13 +369,21 @@ class AppStore( } fun search(query: String) { + runSearch(query, debounce = true) + } + + /** + * A source change is explicit, so it re-runs the query immediately instead + * of waiting for the keystroke debounce. + */ + private fun runSearch(query: String, debounce: Boolean) { searchJob?.cancel() if (query.isBlank()) { _state.update { it.copy(searchResults = emptyList(), searching = false) } return } searchJob = scope.launch { - delay(SEARCH_DEBOUNCE_MS) + if (debounce) delay(SEARCH_DEBOUNCE_MS) _state.update { it.copy(searching = true) } try { val target = _state.value.moods.firstOrNull { @@ -549,7 +557,7 @@ class AppStore( if (_state.value.searchTargetMoodId == moodId && _state.value.searchQuery.isNotBlank() ) { - search(_state.value.searchQuery) + runSearch(_state.value.searchQuery, debounce = false) } } catch (t: Throwable) { message(t) diff --git a/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt b/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt index 7762512..1edc76e 100644 --- a/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt +++ b/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt @@ -269,10 +269,7 @@ class AppStoreTest { awaitState("mood source updated") { store.state.value.moods.first { it.id == "mo-1" }.source == "deezer" } - // The source change re-runs the debounced search; move virtual time past - // the debounce before waiting for the new catalog's results. - advanceTimeBy(350) - runCurrent() + // The source change re-runs the search immediately, without debounce. awaitState { store.state.value.searchResults.any { it.source == "deezer" } } assertEquals("deezer", store.state.value.moods.first { it.id == "mo-1" }.source)