« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-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