From 073300097217b468dfef2ad4ca2fde57491a645d Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 2 Sep 2017 12:19:42 -0300 Subject: Sane-ify all the things, but maybe break things when downloads fail --- src/loop-play.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'src/loop-play.js') 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`) -- cgit 1.3.0-6-gf8a5