Added helper queue methods
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2022-03-02 16:08:31 +01:00
parent f29e715be1
commit 2cc8719e06
2 changed files with 39 additions and 6 deletions

View file

@ -694,7 +694,7 @@ func TestQueue(t *testing.T) {
t.Logf("Finished playback of %d %s: output %d samples\n", entry.Identifier, fullPath, entry.ReadSamples)
}, func(q *audio.Queue, entry *audio.QueueEntry) {
fp.Close()
if len(q.GetQueue()) == 0 {
if q.GetQueueSize() == 0 {
t.Log("Finished playback, closing\n")
q.Close()
}

View file

@ -217,7 +217,34 @@ func (q *Queue) Remove(identifier QueueIdentifier) bool {
}
func (q *Queue) GetQueueEntry(identifier QueueIdentifier) (int, *QueueEntry) {
func (q *Queue) GetQueueHead() *QueueEntry {
q.lock.RLock()
defer q.lock.RUnlock()
if len(q.queue) > 0 {
return q.queue[0]
}
return nil
}
func (q *Queue) GetQueueTail() *QueueEntry {
q.lock.RLock()
defer q.lock.RUnlock()
if len(q.queue) > 0 {
return q.queue[len(q.queue)-1]
}
return nil
}
func (q *Queue) GetQueueIndex(index int) *QueueEntry {
q.lock.RLock()
defer q.lock.RUnlock()
if len(q.queue) > index {
return q.queue[index]
}
return nil
}
func (q *Queue) GetQueueEntry(identifier QueueIdentifier) (index int, entry *QueueEntry) {
q.lock.RLock()
defer q.lock.RUnlock()
for i, e := range q.queue {
@ -228,13 +255,19 @@ func (q *Queue) GetQueueEntry(identifier QueueIdentifier) (int, *QueueEntry) {
return -1, nil
}
func (q *Queue) GetQueue() (ids []QueueIdentifier) {
func (q *Queue) GetQueueSize() int {
q.lock.RLock()
defer q.lock.RUnlock()
for _, e := range q.queue {
ids = append(ids, e.Identifier)
}
return len(q.queue)
}
func (q *Queue) GetQueue() (entries []*QueueEntry) {
q.lock.RLock()
defer q.lock.RUnlock()
entries = make([]*QueueEntry, len(q.queue))
copy(entries, q.queue)
return
}