From a9b6e5a0568803a38ba023a5d535d18591695f8f Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Thu, 31 Mar 2022 18:13:49 +0400 Subject: [PATCH] Compute lastPlayedCache length once a call. --- internal/radio/icecast.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/internal/radio/icecast.go b/internal/radio/icecast.go index 905e5b9..018674f 100644 --- a/internal/radio/icecast.go +++ b/internal/radio/icecast.go @@ -65,19 +65,20 @@ func IcecastGetStatus(icecastURL string) (*IcecastStatus, error) { } func IcecastLastPlayedSongs(lastNSongs int, playlistPath string) ([]Song, error) { - if len(lastPlayedCache) > 0 { - if lastNSongs > len(lastPlayedCache) { - lastNSongs = len(lastPlayedCache) + + if lpcLen := len(lastPlayedCache); lpcLen > 0 { + if lastNSongs > lpcLen { + lastNSongs = lpcLen } - return lastPlayedCache[len(lastPlayedCache)-lastNSongs:], nil + return lastPlayedCache[lpcLen-lastNSongs:], nil } return lastPlayedSongs(lastNSongs, playlistPath) } func IcecastLastSong(playlistPath string) (Song, error) { - if len(lastPlayedCache) > 0 { - return lastPlayedCache[len(lastPlayedCache)-1], nil + if lpcLen := len(lastPlayedCache); lpcLen > 0 { + return lastPlayedCache[lpcLen-1], nil } songs, err := lastPlayedSongs(1, playlistPath)