« get me outta code hell

Sane-ify all the things, but maybe break things when downloads fail - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/loop-play.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-09-02 12:19:42 -0300
committerFlorrie <towerofnix@gmail.com>2017-09-02 12:19:42 -0300
commit073300097217b468dfef2ad4ca2fde57491a645d (patch)
tree6325c7389c46231f71161f3a3dfed0da144ad36b /src/loop-play.js
parent281de70bbc9328e2b95b6296235b9b0845e54495 (diff)
Sane-ify all the things, but maybe break things when downloads fail
Diffstat (limited to 'src/loop-play.js')
-rw-r--r--src/loop-play.js25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 4274b3c..0cb8b3e 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -312,7 +312,8 @@ class PlayController extends EventEmitter {
     // set it yet.
     this.nextTrack = undefined
 
-    this.startNextDownload()
+    // Download the very first track.
+    this.startNextDownload(this.historyController.getNextTrack())
 
     await this.waitForDownload()
 
@@ -322,7 +323,10 @@ class PlayController extends EventEmitter {
       const next = this.nextFile
       this.nextFile = undefined
 
-      this.startNextDownload()
+      // Pre-download the track that's up next.
+      this.startNextDownload(this.historyController.timeline[
+        this.historyController.timelineIndex + 1
+      ])
 
       if (next) {
         await this.playFile(next)
@@ -336,6 +340,9 @@ class PlayController extends EventEmitter {
         }
       }
 
+      this.historyController.timelineIndex++
+      this.historyController.fillTimeline()
+
       await this.waitForDownload()
     }
   }
@@ -350,12 +357,9 @@ class PlayController extends EventEmitter {
     })
   }
 
-  startNextDownload(moveTimelineIndex = true) {
+  startNextDownload(picked) {
     this.isDownloading = true
 
-    const picked = (moveTimelineIndex
-      ? this.historyController.getNextTrack()
-      : this.historyController.currentTrack)
     this.nextTrack = picked
 
     if (!picked) {
@@ -387,12 +391,11 @@ class PlayController extends EventEmitter {
         this.emit('downloaded')
       })
       .catch(() => {
+        // TODO: Test this!!
         console.warn(
           "\x1b[31mFailed to download (or convert) track \x1b[1m" +
           getItemPathString(this.nextTrack) + "\x1b[0m"
         )
-
-        this.startNextDownload()
       })
 
     return picked
@@ -417,11 +420,11 @@ class PlayController extends EventEmitter {
 
     // The timeline is always one index ahead.
     const tl = this.historyController.timeline
-    tl.splice(this.historyController.timelineIndex - 1, 1)
+    tl.splice(this.historyController.timelineIndex + 1, 1)
     this.historyController.fillTimeline()
 
     this.downloadController.cancel()
-    return this.startNextDownload(false)
+    return this.startNextDownload(tl[this.historyController.timelineIndex + 1])
   }
 
   async stop() {
@@ -454,7 +457,7 @@ class PlayController extends EventEmitter {
 
     const hc = this.historyController
     const tl = hc.timeline
-    const tlI = hc.timelineIndex - 1
+    const tlI = hc.timelineIndex
 
     for (let i = Math.max(0, tlI - 2); i < tlI; i++) {
       console.log(`\x1b[2m(Prev) ${getCleanMessage(tl[i])}\x1b[0m`)