1
0

In song.go: added comments; embed sync.Mutex in SongList instead of having it as a field; changed a json field name duration_milliseconds to duration_msec; listMaxLen field was renamed to maxLen; Len() was renamed to MaxLen().

This commit is contained in:
Alexander Andreev 2023-10-04 18:10:17 +04:00
parent 20b8b62b73
commit 1c6d288cd7
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -6,8 +6,8 @@ import (
"time"
)
// Song stores artist and title of a song, a timestamp of when it started, and
// a maximum number of listeners.
// Song stores artist and title of a song, a timestamp of when it started, its
// duration, and a maximum number of listeners.
type Song struct {
Artist string
Title string
@ -16,10 +16,13 @@ type Song struct {
StartAt time.Time
}
// ArtistTitle returns a concatination of an artist and a title with a hyphen
// between then.
func (s *Song) ArtistTitle() string {
return s.Artist + " - " + s.Title
}
// DurationString returns song's duration as a string formatted as [H:]M:SS.
func (s *Song) DurationString() string {
if s.Duration.Hours() >= 1 {
return time.UnixMilli(s.Duration.Milliseconds()).Format("3:4:05")
@ -31,7 +34,7 @@ func (s *Song) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Artist string `json:"artist"`
Title string `json:"title"`
DurationMill int64 `json:"duration_milliseconds"`
DurationMill int64 `json:"duration_msec"`
Duration string `json:"duration"`
MaxListeners int `json:"listeners"`
StartAt string `json:"start_at"`
@ -45,27 +48,28 @@ func (s *Song) MarshalJSON() ([]byte, error) {
}
type SongList struct {
mut sync.Mutex
current Song
lastSongs []Song
listMaxLen int
sync.Mutex
current Song
lastSongs []Song
maxLen int
}
func NewSongList(length int) *SongList {
return &SongList{listMaxLen: length, lastSongs: make([]Song, 0, length)}
// NewSongList returns a new SongList with a maximal length set with maxLen.
func NewSongList(maxLen int) *SongList {
return &SongList{maxLen: maxLen, lastSongs: make([]Song, 0, maxLen)}
}
// Add a new song that is currently playing and update a list.
func (sl *SongList) Add(newSong Song) {
sl.mut.Lock()
defer sl.mut.Unlock()
sl.Lock()
defer sl.Unlock()
if sl.current.StartAt.Year() == 1 {
sl.current = newSong
return
}
if len(sl.lastSongs) == sl.listMaxLen {
if len(sl.lastSongs) == sl.maxLen {
sl.lastSongs = append(sl.lastSongs[1:], sl.current)
} else {
sl.lastSongs = append(sl.lastSongs, sl.current)
@ -73,10 +77,10 @@ func (sl *SongList) Add(newSong Song) {
sl.current = newSong
}
// Current returns a current playing song.
// Current returns a currently playing song.
func (sl *SongList) Current() *Song {
sl.mut.Lock()
defer sl.mut.Unlock()
sl.Lock()
defer sl.Unlock()
if sl.current.StartAt.Year() == 1 {
return nil
}
@ -85,11 +89,12 @@ func (sl *SongList) Current() *Song {
// List returns a list of lastly played songs.
func (sl *SongList) List() []Song {
sl.mut.Lock()
defer sl.mut.Unlock()
sl.Lock()
defer sl.Unlock()
return sl.lastSongs
}
func (sl *SongList) Len() int {
return sl.listMaxLen
// MaxLen returns a maximal length of a list
func (sl *SongList) MaxLen() int {
return sl.maxLen
}