-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* moved the queue and added a couple of util functions + tests * added the queue command * gofmt
- Loading branch information
1 parent
e510ea1
commit 1bf16f8
Showing
10 changed files
with
321 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package music | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
|
||
"errors" | ||
"github.com/vansante/go-event-emitter" | ||
"time" | ||
) | ||
|
||
const ( | ||
songAdded eventemitter.EventType = "song-added" | ||
) | ||
|
||
// Queue holds an array of songs | ||
type Queue struct { | ||
*eventemitter.Emitter | ||
songs []*Song | ||
lock sync.Mutex | ||
} | ||
|
||
func (queue *Queue) Append(songs ...*Song) { | ||
queue.lock.Lock() | ||
defer queue.lock.Unlock() | ||
|
||
queue.songs = append(queue.songs, songs...) | ||
log.Println("Song added to the queue") | ||
queue.EmitEvent(songAdded) | ||
} | ||
|
||
// GetNext returns the next item in the queue if it exists | ||
func (queue *Queue) GetNext() *Song { | ||
queue.lock.Lock() | ||
defer queue.lock.Unlock() | ||
|
||
return queue.getNext() | ||
} | ||
|
||
func (queue *Queue) getNext() *Song { | ||
if len(queue.songs) == 0 { | ||
return nil | ||
} | ||
song, remaining := queue.songs[0], queue.songs[1:] | ||
|
||
queue.songs = remaining | ||
|
||
return song | ||
} | ||
|
||
func (queue *Queue) GetLength() int { | ||
queue.lock.Lock() | ||
defer queue.lock.Unlock() | ||
|
||
return len(queue.songs) | ||
} | ||
|
||
func (queue *Queue) GetTotalDuration() time.Duration { | ||
queue.lock.Lock() | ||
defer queue.lock.Unlock() | ||
|
||
var duration time.Duration | ||
|
||
for _, song := range queue.songs { | ||
duration = duration + song.Duration | ||
} | ||
|
||
return duration.Round(time.Second) | ||
} | ||
|
||
func (queue *Queue) GetNextN(limit int) ([]Song, error) { | ||
if limit <= 0 { | ||
return nil, errors.New("limit must be greater than 0") | ||
} | ||
|
||
queue.lock.Lock() | ||
defer queue.lock.Unlock() | ||
|
||
if len(queue.songs) < limit { | ||
limit = len(queue.songs) | ||
} | ||
|
||
result := make([]Song, limit) | ||
for i := 0; i < limit; i++ { | ||
result[i] = *queue.songs[i] | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// WaitForNext is a blocking call that returns the next song in the queue and wait for one to be added | ||
// if there is no song available. | ||
func (queue *Queue) WaitForNext() *Song { | ||
next := queue.GetNext() | ||
|
||
if next != nil { | ||
return next | ||
} | ||
|
||
done := make(chan struct{}) | ||
queue.ListenOnce(songAdded, func(args ...interface{}) { | ||
done <- struct{}{} | ||
}) | ||
|
||
<-done | ||
return queue.GetNext() | ||
} | ||
|
||
// NewQueue creates a new instance of Queue | ||
func NewQueue() *Queue { | ||
return &Queue{ | ||
songs: make([]*Song, 0), | ||
Emitter: eventemitter.NewEmitter(true), | ||
} | ||
} |
Oops, something went wrong.