« 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.js30
1 files changed, 25 insertions, 5 deletions
diff --git a/backend.js b/backend.js
index f2610d0..d222d10 100644
--- a/backend.js
+++ b/backend.js
@@ -69,13 +69,14 @@ class QueuePlayer extends EventEmitter {
     this.playingTrack = null
     this.queueGrouplike = {name: 'Queue', isTheQueue: true, items: []}
     this.pauseNextTrack = false
-    this.alwaysStartPaused = false
-    this.waitWhenDonePlaying = false
-
+    this.loopQueueAtEnd = false
     this.playedTrackToEnd = false
     this.timeData = null
     this.time = null
 
+    this.alwaysStartPaused = false
+    this.waitWhenDonePlaying = false
+
     this.getPlayer = getPlayer
     this.getRecordFor = getRecordFor
   }
@@ -460,8 +461,14 @@ class QueuePlayer extends EventEmitter {
     if (playingThisTrack) {
       this.playedTrackToEnd = true
       this.emit('done playing', this.playingTrack)
-      if (!this.waitWhenDonePlaying && !this.playNext(item)) {
-        this.clearPlayingTrack()
+      if (!this.waitWhenDonePlaying) {
+        if (!this.playNext(item)) {
+          if (this.loopQueueAtEnd) {
+            this.playFirst()
+          } else {
+            this.clearPlayingTrack()
+          }
+        }
       }
     }
   }
@@ -524,6 +531,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
@@ -609,6 +625,10 @@ class QueuePlayer extends EventEmitter {
     this.emit('set-pause-next-track', !!value)
   }
 
+  setLoopQueueAtEnd(value) {
+    this.loopQueueAtEnd = !!value
+  }
+
   get remainingTracks() {
     const index = this.queueGrouplike.items.indexOf(this.playingTrack)
     const length = this.queueGrouplike.items.length