1
0

Store current song in a separate variable. A base for most listened song functionality.

This commit is contained in:
Alexander Andreev 2023-03-12 22:25:58 +04:00
parent 63fcb36e60
commit 53d24c5781
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -22,6 +22,7 @@ const (
) )
var ( var (
currentlyPlaying Song
lastPlayedCache []Song lastPlayedCache []Song
lastPlayedCacheMutex sync.Mutex lastPlayedCacheMutex sync.Mutex
) )
@ -129,12 +130,10 @@ func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
lines := bytes.Split(buf, []byte("\n")) lines := bytes.Split(buf, []byte("\n"))
if len(lines) < 2 { if len(lines) < 1 {
return nil, nil return nil, nil
} }
lines = lines[:len(lines)]
if len(lines) > n { if len(lines) > n {
lines = lines[len(lines)-n:] lines = lines[len(lines)-n:]
} }
@ -190,10 +189,15 @@ func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) {
if mask&syscall.IN_MODIFY > 0 { if mask&syscall.IN_MODIFY > 0 {
lastPlayedCacheMutex.Lock() lastPlayedCacheMutex.Lock()
if songs, err := icecastLastPlayedSongs(playlistPath, 1); err == nil && len(songs) > 0 { if songs, err := icecastLastPlayedSongs(playlistPath, 1); err == nil && len(songs) > 0 {
if len(lastPlayedCache) == n { if currentlyPlaying.Time == "" {
lastPlayedCache = append(lastPlayedCache[1:], songs...) currentlyPlaying = songs[0]
} else { } else {
lastPlayedCache = append(lastPlayedCache, songs...) if len(lastPlayedCache) == n {
lastPlayedCache = append(lastPlayedCache[1:], currentlyPlaying)
} else {
lastPlayedCache = append(lastPlayedCache, currentlyPlaying)
}
currentlyPlaying = songs[0]
} }
} }
lastPlayedCacheMutex.Unlock() lastPlayedCacheMutex.Unlock()