« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/backend.js
diff options
context:
space:
mode:
Diffstat (limited to 'backend.js')
-rw-r--r--backend.js20
1 files changed, 19 insertions, 1 deletions
diff --git a/backend.js b/backend.js
index 69aa815..81726c6 100644
--- a/backend.js
+++ b/backend.js
@@ -66,6 +66,7 @@ class QueuePlayer extends EventEmitter {
     this.playingTrack = null
     this.queueGrouplike = {name: 'Queue', isTheQueue: true, items: []}
     this.pauseNextTrack = false
+    this.loopQueueAtEnd = false
     this.playedTrackToEnd = false
     this.timeData = null
 
@@ -433,7 +434,11 @@ class QueuePlayer extends EventEmitter {
     if (playingThisTrack) {
       this.playedTrackToEnd = true
       if (!this.playNext(item)) {
-        this.clearPlayingTrack()
+        if (this.loopQueueAtEnd) {
+          this.playFirst()
+        } else {
+          this.clearPlayingTrack()
+        }
       }
     }
   }
@@ -496,6 +501,15 @@ class QueuePlayer extends EventEmitter {
     return true
   }
 
+  playFirst() {
+    const queue = this.queueGrouplike
+    if (queue.items.length) {
+      this.play(queue.items[0])
+      return true
+    }
+    return false
+  }
+
   clearPlayingTrack() {
     if (this.playingTrack !== null) {
       const oldTrack = this.playingTrack
@@ -557,6 +571,10 @@ class QueuePlayer extends EventEmitter {
     this.pauseNextTrack = !!value
   }
 
+  setLoopQueueAtEnd(value) {
+    this.loopQueueAtEnd = !!value
+  }
+
   get remainingTracks() {
     const index = this.queueGrouplike.items.indexOf(this.playingTrack)
     const length = this.queueGrouplike.items.length