fix: re-run search immediately when the mood source changes
CI / build (push) Successful in 3m54s
CI / play (push) Successful in 3m29s

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.
This commit is contained in:
TapTap committed 2026-11-18 19:48:09 +00:00
1 parent 4048262fc5
commit 8d7f6a0ccb
2 files changed
+11 -6

No files matched your search

@@ -369,13 +369,21 @@ class AppStore(
} }
fun search(query: String) { 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() searchJob?.cancel()
if (query.isBlank()) { if (query.isBlank()) {
_state.update { it.copy(searchResults = emptyList(), searching = false) } _state.update { it.copy(searchResults = emptyList(), searching = false) }
return return
} }
searchJob = scope.launch { searchJob = scope.launch {
delay(SEARCH_DEBOUNCE_MS) if (debounce) delay(SEARCH_DEBOUNCE_MS)
_state.update { it.copy(searching = true) } _state.update { it.copy(searching = true) }
try { try {
val target = _state.value.moods.firstOrNull { val target = _state.value.moods.firstOrNull {
@@ -549,7 +557,7 @@ class AppStore(
if (_state.value.searchTargetMoodId == moodId && if (_state.value.searchTargetMoodId == moodId &&
_state.value.searchQuery.isNotBlank() _state.value.searchQuery.isNotBlank()
) { ) {
search(_state.value.searchQuery) runSearch(_state.value.searchQuery, debounce = false)
} }
} catch (t: Throwable) { } catch (t: Throwable) {
message(t) message(t)
@@ -269,10 +269,7 @@ class AppStoreTest {
awaitState("mood source updated") { awaitState("mood source updated") {
store.state.value.moods.first { it.id == "mo-1" }.source == "deezer" store.state.value.moods.first { it.id == "mo-1" }.source == "deezer"
} }
// The source change re-runs the debounced search; move virtual time past // The source change re-runs the search immediately, without debounce.
// the debounce before waiting for the new catalog's results.
advanceTimeBy(350)
runCurrent()
awaitState { store.state.value.searchResults.any { it.source == "deezer" } } awaitState { store.state.value.searchResults.any { it.source == "deezer" } }
assertEquals("deezer", store.state.value.moods.first { it.id == "mo-1" }.source) assertEquals("deezer", store.state.value.moods.first { it.id == "mo-1" }.source)