« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/loop-play.js26
-rw-r--r--src/pickers2.js1
-rwxr-xr-xsrc/play.js9
3 files changed, 20 insertions, 16 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 212ee1c..c95c1c4 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -13,9 +13,8 @@ const FIFO = require('fifo-js')
 const EventEmitter = require('events')
 const promisifyProcess = require('./promisify-process')
 const killProcess = require('./kill-process')
-const { getItemPathString } = require('./playlist-utils')
-
-const { safeUnlink } = require('./playlist-utils')
+const { getItemPathString, safeUnlink } = require('./playlist-utils')
+const { HistoryController, generalPicker } = require('./pickers2')
 
 const {
   getDownloaderFor, byName: downloadersByName, makeConverter
@@ -294,12 +293,12 @@ class DownloadController extends EventEmitter {
 }
 
 class PlayController extends EventEmitter {
-  constructor(picker, player, playlist, downloadController) {
+  constructor(player, playlist, historyController, downloadController) {
     super()
 
-    this.picker = picker
     this.player = player
     this.playlist = playlist
+    this.historyController = historyController
     this.downloadController = downloadController
 
     this.currentTrack = null
@@ -354,7 +353,7 @@ class PlayController extends EventEmitter {
   startNextDownload() {
     this.isDownloading = true
 
-    const picked = this.picker()
+    const picked = this.historyController.getNextTrack()
     this.nextTrack = picked
 
     if (!picked) {
@@ -452,14 +451,13 @@ class PlayController extends EventEmitter {
 
 module.exports = async function startLoopPlay(
   playlist, {
-    picker, playerCommand = 'mpv',
+    pickerOptions, playerCommand = 'mpv',
     disablePlaybackStatus = false
   }
 ) {
-  // Looping play function. Takes one argument, the "picker" function,
-  // which returns a track to play. Stops when the result of the picker
-  // function is null (or similar). Optionally takes a second argument
-  // used as arguments to the `play` process (before the file name).
+  // Looping play function. Takes a playlist and an object containing general
+  // options (picker options, player command, and disable-playback-status).
+  // Stops when the history controller returns null.
 
   let player
   if (playerCommand === 'sox' || playerCommand === 'play') {
@@ -487,8 +485,12 @@ module.exports = async function startLoopPlay(
   const downloadController = new DownloadController(playlist)
   await downloadController.init()
 
+  const historyController = new HistoryController(
+    playlist, generalPicker, pickerOptions
+  )
+
   const playController = new PlayController(
-    picker, player, playlist, downloadController
+    player, playlist, historyController, downloadController
   )
 
   Object.assign(playController, {playerCommand})
diff --git a/src/pickers2.js b/src/pickers2.js
index c7ff1a6..e36d257 100644
--- a/src/pickers2.js
+++ b/src/pickers2.js
@@ -204,6 +204,7 @@ function generalPicker(playlist, lastTrack, options) {
   }
 }
 
+module.exports = {HistoryController, generalPicker}
 
 // ----------------------------------------------------------------------------
 
diff --git a/src/play.js b/src/play.js
index efc60df..c955ce8 100755
--- a/src/play.js
+++ b/src/play.js
@@ -7,7 +7,6 @@ const clone = require('clone')
 const fs = require('fs')
 const fetch = require('node-fetch')
 const commandExists = require('./command-exists')
-const makePicker = require('./pickers')
 const startLoopPlay = require('./loop-play')
 const processArgv = require('./process-argv')
 const processSmartPlaylist = require('./smart-playlist')
@@ -337,8 +336,6 @@ async function main(args) {
   if (willPlay || (willPlay === null && shouldPlay)) {
     console.log(`Using sort: ${pickerSortMode} and loop: ${pickerLoopMode}.`)
 
-    const picker = makePicker(activePlaylist, pickerSortMode, pickerLoopMode)
-
     console.log(`Using ${playerCommand} player.`)
 
     const {
@@ -347,7 +344,11 @@ async function main(args) {
       downloadController,
       player
     } = await startLoopPlay(activePlaylist, {
-      picker, playerCommand,
+      pickerOptions: {
+        loop: pickerLoopMode,
+        sort: pickerSortMode
+      },
+      playerCommand,
       disablePlaybackStatus
     })