fix(ui): cancel stale Spotify playlist lookups

This commit is contained in:
Bjarne Øverli
2026-08-20 19:48:27 +02:00
parent 9edbcc2107
commit c11504d1ba
3 changed files with 46 additions and 1 deletions
+2
View File
@@ -133,6 +133,8 @@ func (m *Model) handleSpotSearchResultsKey(msg tea.KeyPressMsg) tea.Cmd {
}
case "esc", "backspace":
m.invalidateSpotAlbumRequest()
nextRequest(&m.requests.spotLists)
m.spotSearch.loading = false
m.spotSearch.screen = spotSearchInput
m.spotSearch.err = ""
case "ctrl+u":
+3 -1
View File
@@ -108,7 +108,9 @@ func (m Model) isCurrentSpotRequest(gen uint64, providerName string) bool {
}
func (m Model) isCurrentSpotListRequest(gen uint64, providerName string) bool {
return m.isCurrentSpotProvider(providerName) && gen == m.requests.spotLists
return m.spotSearch.screen == spotSearchResults &&
m.isCurrentSpotProvider(providerName) &&
gen == m.requests.spotLists
}
func (m Model) isCurrentSpotMutation(gen uint64, providerName string) bool {
+41
View File
@@ -91,3 +91,44 @@ func TestFetchSpotAlbumTracksCmdUsesContextLoader(t *testing.T) {
t.Fatalf("album load error = %v, want context.Canceled", msg.err)
}
}
func TestLeavingSpotResultsCancelsPlaylistLookup(t *testing.T) {
canceled := make(chan struct{})
m := Model{
spotSearch: spotSearchState{
visible: true,
screen: spotSearchResults,
loading: true,
prov: commandsTestProvider{name: "Spotify"},
cancel: func() { close(canceled) },
},
}
const gen = 7
m.requests.spotLists = gen
m.handleSpotSearchResultsKey(tea.KeyPressMsg{Code: tea.KeyEscape})
select {
case <-canceled:
default:
t.Fatal("playlist lookup context was not canceled")
}
if m.requests.spotLists == gen {
t.Fatal("playlist request generation was not invalidated")
}
if m.spotSearch.loading {
t.Fatal("playlist lookup remained loading")
}
updated, cmd := m.Update(spotPlaylistsMsg{
gen: gen,
providerName: "Spotify",
playlists: []playlist.PlaylistInfo{{ID: "late", Name: "Late"}},
})
m = updated.(Model)
if cmd != nil {
t.Fatal("stale playlist response returned a command")
}
if m.spotSearch.screen != spotSearchInput || m.spotSearch.playlists != nil {
t.Fatalf("stale playlist response changed search state: %+v", m.spotSearch)
}
}