« get me outta code hell

Re-add download function to fix process metadata - 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:
authorFlorrie <towerofnix@gmail.com>2019-09-23 04:10:01 -0300
committerFlorrie <towerofnix@gmail.com>2019-09-23 04:11:37 -0300
commit5a8f8eafa4e30d7a0febcf804253b6853caf36e7 (patch)
treeb8fa6adf3fa7ae69c0bf33038104e8bbd450cbfc /backend.js
parent88dd9466e513bb562e242d152765ec204e2e2b2d (diff)
Re-add download function to fix process metadata
The download code doesn't actually really depend on state, besides
having access to the record for the track, which we can pass in from
anywhere.
Diffstat (limited to 'backend.js')
-rw-r--r--backend.js44
1 files changed, 26 insertions, 18 deletions
diff --git a/backend.js b/backend.js
index d3e0db7..d68d448 100644
--- a/backend.js
+++ b/backend.js
@@ -28,6 +28,27 @@ const fs = require('fs')
 const writeFile = promisify(fs.writeFile)
 const readFile = promisify(fs.readFile)
 
+async function download(item, record) {
+  if (isGroup(item)) {
+    // TODO: Download all children (recursively), show a confirmation prompt
+    // if there are a lot of items (remember to flatten).
+    return
+  }
+
+  // Don't start downloading an item if we're already downloading it!
+  if (record.downloading) {
+    return
+  }
+
+  const arg = item.downloaderArg
+  record.downloading = true
+  try {
+    return await getDownloaderFor(arg)(arg)
+  } finally {
+    record.downloading = false
+  }
+}
+
 class QueuePlayer extends EventEmitter {
   constructor({
     getRecordFor
@@ -460,24 +481,7 @@ class QueuePlayer extends EventEmitter {
   }
 
   async download(item) {
-    if (isGroup(item)) {
-      // TODO: Download all children (recursively), show a confirmation prompt
-      // if there are a lot of items (remember to flatten).
-      return
-    }
-
-    // Don't start downloading an item if we're already downloading it!
-    if (this.getRecordFor(item).downloading) {
-      return
-    }
-
-    const arg = item.downloaderArg
-    this.getRecordFor(item).downloading = true
-    try {
-      return await getDownloaderFor(arg)(arg)
-    } finally {
-      this.getRecordFor(item).downloading = false
-    }
+    return download(item, this.getRecordFor(item))
   }
 
   seekAhead(seconds) {
@@ -670,6 +674,10 @@ class Backend extends EventEmitter {
       await queuePlayer.stopPlaying()
     }
   }
+
+  async download(item) {
+    return download(item, this.getRecordFor(item))
+  }
 }
 
 module.exports = Backend