« get me outta code hell

Only create one converter per instance of DownloadController - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-08-08 19:56:00 -0300
committerFlorrie <towerofnix@gmail.com>2017-08-08 19:56:00 -0300
commit3b84dc71da84264a78c4594db592e99f509a6499 (patch)
tree6f3cdbd7cb41113c110bed445c276d7cc3d232af
parent5e90c82c494567a091ff13a632ec34dd33fa9e1b (diff)
Only create one converter per instance of DownloadController
This also makes the message "using (ffmpeg|avconv) converter" appear once
(upon startup).
-rw-r--r--src/loop-play.js11
-rwxr-xr-xsrc/play.js4
2 files changed, 9 insertions, 6 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 7fec995..66de65a 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -164,6 +164,10 @@ class DownloadController extends EventEmitter {
     this.playlist = playlist
   }
 
+  async init() {
+    this.converter = await makeConverter('wav')
+  }
+
   waitForDownload() {
     // Returns a promise that resolves when a download is
     // completed.  Note that this isn't necessarily the download
@@ -226,10 +230,8 @@ class DownloadController extends EventEmitter {
 
     let convertFile
 
-    const converter = await makeConverter('wav')
-
     try {
-      convertFile = await converter(downloadFile)
+      convertFile = await this.converter(downloadFile)
     } catch(err) {
       this.emit('errored', err)
       return
@@ -417,7 +419,7 @@ class PlayController extends EventEmitter {
   }
 }
 
-module.exports = function loopPlay(
+module.exports = async function startLoopPlay(
   playlist, picker, playerCommand = 'mpv', playOpts = []
 ) {
   // Looping play function. Takes one argument, the "picker" function,
@@ -447,6 +449,7 @@ module.exports = function loopPlay(
   }
 
   const downloadController = new DownloadController(playlist)
+  await downloadController.init()
 
   const playController = new PlayController(
     picker, player, playlist, downloadController
diff --git a/src/play.js b/src/play.js
index 6b9f26c..680e326 100755
--- a/src/play.js
+++ b/src/play.js
@@ -8,7 +8,7 @@ const fs = require('fs')
 const fetch = require('node-fetch')
 const commandExists = require('./command-exists')
 const pickers = require('./pickers')
-const loopPlay = require('./loop-play')
+const startLoopPlay = require('./loop-play')
 const processArgv = require('./process-argv')
 const processSmartPlaylist = require('./smart-playlist')
 
@@ -326,7 +326,7 @@ async function main(args) {
       playController,
       downloadController,
       player
-    } = loopPlay(activePlaylist, picker, playerCommand, playOpts)
+    } = await startLoopPlay(activePlaylist, picker, playerCommand, playOpts)
 
     // We're looking to gather standard input one keystroke at a time.
     // But that isn't *always* possible, e.g. when piping into the http-music