« get me outta code hell

Skip back/forwards - 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 13:04:17 -0300
committerFlorrie <towerofnix@gmail.com>2017-09-02 13:04:17 -0300
commit8d9cd7b216dafa8a18e481c31b6f2ec9af6faaab (patch)
tree354e29d8b51d26cbe1fe2eccea428957ee981a90 /src/loop-play.js
parent3c3084aab308685f03a0546f9e46d143106283a6 (diff)
Skip back/forwards
Diffstat (limited to 'src/loop-play.js')
-rw-r--r--src/loop-play.js31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 4cccb35..aaaaafe 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -305,6 +305,7 @@ class PlayController extends EventEmitter {
     this.nextTrack = null
     this.nextFile = undefined // TODO: Why isn't this null?
     this.stopped = false
+    this.shouldMoveNext = true
   }
 
   async loopPlay() {
@@ -340,8 +341,12 @@ class PlayController extends EventEmitter {
         }
       }
 
-      this.historyController.timelineIndex++
-      this.historyController.fillTimeline()
+      if (!this.shouldMoveNext) {
+        this.shouldMoveNext = true
+      } else {
+        this.historyController.timelineIndex++
+        this.historyController.fillTimeline()
+      }
 
       await this.waitForDownload()
     }
@@ -420,6 +425,27 @@ class PlayController extends EventEmitter {
     // TODO: It would be nice if this returned the next track, but that
     // probably isn't possible with the current play/loop-setup.
 
+    if (this.nextTrack !== this.historyController.getNextTrack(false)) {
+      this.downloadController.cancel()
+      this.startNextDownload(this.historyController.getNextTrack())
+      this.shouldMoveNext = false
+    }
+
+    await this.player.kill()
+    this.currentTrack = null
+  }
+
+  async skipBack() {
+    // Usually the downloader moves forwards in time (so, the NEXT track will
+    // be pre-downloaded). Here, we want to move back, so we need to override
+    // the downloader ourselves.
+
+    if (this.nextTrack !== this.historyController.getBackTrack(false)) {
+      this.downloadController.cancel()
+      this.startNextDownload(this.historyController.getBackTrack())
+    }
+
+    this.shouldMoveNext = false
     await this.player.kill()
     this.currentTrack = null
   }
@@ -429,7 +455,6 @@ class PlayController extends EventEmitter {
       await safeUnlink(this.nextFile, this.playlist)
     }
 
-    // The timeline is always one index ahead.
     const tl = this.historyController.timeline
     tl.splice(this.historyController.timelineIndex + 1, 1)
     this.historyController.fillTimeline()