« get me outta code hell

Ah, progress - 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-08-04 23:40:30 -0300
committerFlorrie <towerofnix@gmail.com>2017-08-04 23:40:30 -0300
commit21848502be0a0e923470f2ce5679fe6759b7b8a1 (patch)
tree091492df1402b86619b6b1aa1f0c22f9f45c3b48 /src/loop-play.js
parenta0c2b302f8a7d2e0aef062fae3ba8badf79c3b9e (diff)
Ah, progress
Diffstat (limited to 'src/loop-play.js')
-rw-r--r--src/loop-play.js51
1 files changed, 26 insertions, 25 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 5690f9b..4ea73c7 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -91,43 +91,44 @@ class PlayController {
     this.playerCommand = null
     this.currentTrack = null
     this.process = null
+    this.downloadProcess = null
   }
 
   async loopPlay() {
-    let nextFile
-
     // Null would imply there's NO up-next track, but really we just haven't
     // set it yet.
     this.nextTrack = undefined
 
-    const downloadNext = async () => {
-      this.nextTrack = this.startNextDownload()
-      if (this.nextTrack !== null) {
-        try {
-          nextFile = await this.downloadController.waitForDownload()
-        } catch(err) {
-          console.warn(
-            "\x1b[31mFailed to download (or convert) track \x1b[1m" +
-            getItemPathString(this.nextTrack) + "\x1b[0m"
-          )
-          await downloadNext()
-        }
-      } else {
-        nextFile = null
+    await (this.downloadProcess = this.downlold())
+
+    while (this.nextTrack) {
+      this.currentTrack = this.nextTrack
+
+      this.downlold()
+
+      // Maybe Promise.all isn't good here? Rather, wait for play-file then
+      // downlold? Or - we should have some "downlolder" promise, set by
+      // downlold.
+      if (this.nextFile) {
+        await this.playFile(this.nextFile)
       }
+
+      await this.downloadProcess
     }
+  }
 
-    await downloadNext()
+  async downlold() {
+    this.nextTrack = this.startNextDownload()
 
-    while (this.nextTrack) {
-      this.currentTrack = this.nextTrack
+    try {
+      this.nextFile = await this.downloadController.waitForDownload()
+    } catch(err) {
+      console.warn(
+        "\x1b[31mFailed to download (or convert) track \x1b[1m" +
+        getItemPathString(this.nextTrack) + "\x1b[0m"
+      )
 
-      await Promise.all([
-        // If the downloader returns false, the file failed to download; that
-        // means we'll just skip this track and wait for the next.
-        nextFile !== false ? this.playFile(nextFile) : Promise.resolve(),
-        downloadNext()
-      ])
+      await (this.downloadProcess = this.downlold())
     }
   }