« get me outta code hell

Do a proper clone when creating active playlist - 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:
authorliam4 <towerofnix@gmail.com>2017-07-18 17:18:54 -0300
committerliam4 <towerofnix@gmail.com>2017-07-18 17:18:54 -0300
commit3f7969bcd75a2cdc4c6b2273fa300eabfc6bd9ad (patch)
tree081605cdd1607dd38c389f24e3e8e50b1caa2a78 /src
parentebada93f0d2b2ec3a158dae2ba43e98d81f3182a (diff)
Do a proper clone when creating active playlist
Diffstat (limited to 'src')
-rwxr-xr-xsrc/http-music.js32
1 files changed, 18 insertions, 14 deletions
diff --git a/src/http-music.js b/src/http-music.js
index 12c93e7..a28b0f2 100755
--- a/src/http-music.js
+++ b/src/http-music.js
@@ -3,11 +3,12 @@
 'use strict'
 
 const { promisify } = require('util')
+const clone = require('clone')
 const fs = require('fs')
+const fetch = require('node-fetch')
 const pickers = require('./pickers')
 const loopPlay = require('./loop-play')
 const processArgv = require('./process-argv')
-const fetch = require('node-fetch')
 
 const {
   filterPlaylistByPathString, removeGroupByPathString, getPlaylistTreeString,
@@ -40,7 +41,7 @@ process.on('warning', e => console.warn(e.stack))
 Promise.resolve()
   .then(async () => {
     let sourcePlaylist = null
-    let activePlaylistGroup = null
+    let activePlaylist = null
 
     let pickerType = 'shuffle'
     let playOpts = []
@@ -71,14 +72,17 @@ Promise.resolve()
 
       const openedPlaylist = updatePlaylistFormat(JSON.parse(playlistText))
 
+      // The active playlist is a clone of the source playlist; after all it's
+      // quite possible we'll be messing with the value of the active playlist,
+      // and we don't want to reflect those changes in the source playlist.
       sourcePlaylist = openedPlaylist
-      activePlaylistGroup = {items: openedPlaylist.items}
+      activePlaylist = clone(openedPlaylist)
 
       processArgv(openedPlaylist.options, optionFunctions)
     }
 
     function requiresOpenPlaylist() {
-      if (activePlaylistGroup === null) {
+      if (activePlaylist === null) {
         throw new Error(
           "This action requires an open playlist - try --open (file)"
         )
@@ -117,7 +121,7 @@ Promise.resolve()
 
         requiresOpenPlaylist()
 
-        activePlaylistGroup = {items: []}
+        activePlaylist.items = []
       },
 
       'c': util => util.alias('-clear'),
@@ -133,7 +137,7 @@ Promise.resolve()
 
         const pathString = util.nextArg()
         const group = filterPlaylistByPathString(sourcePlaylist, pathString)
-        activePlaylistGroup.items.push(group)
+        activePlaylist.items.push(group)
       },
 
       'k': util => util.alias('-keep'),
@@ -146,7 +150,7 @@ Promise.resolve()
 
         const pathString = util.nextArg()
         console.log("Ignoring path: " + pathString)
-        removeGroupByPathString(activePlaylistGroup, pathString)
+        removeGroupByPathString(activePlaylist, pathString)
       },
 
       'r': util => util.alias('-remove'),
@@ -158,7 +162,7 @@ Promise.resolve()
 
         requiresOpenPlaylist()
 
-        console.log(getPlaylistTreeString(activePlaylistGroup))
+        console.log(getPlaylistTreeString(activePlaylist))
 
         // If this is the last item in the argument list, the user probably
         // only wants to get the list, so we'll mark the 'should run' flag
@@ -177,7 +181,7 @@ Promise.resolve()
 
         requiresOpenPlaylist()
 
-        console.log(getPlaylistTreeString(activePlaylistGroup, true))
+        console.log(getPlaylistTreeString(activePlaylist, true))
 
         // As with -l, if this is the last item in the argument list, we
         // won't actually be playing the playlist.
@@ -230,7 +234,7 @@ Promise.resolve()
 
         requiresOpenPlaylist()
 
-        console.log(JSON.stringify(activePlaylistGroup, null, 2))
+        console.log(JSON.stringify(activePlaylist, null, 2))
       }
     }
 
@@ -238,7 +242,7 @@ Promise.resolve()
 
     await processArgv(process.argv, optionFunctions)
 
-    if (activePlaylistGroup === null) {
+    if (activePlaylist === null) {
       throw new Error(
         "Cannot play - no open playlist. Try --open <playlist file>?"
       )
@@ -248,10 +252,10 @@ Promise.resolve()
       let picker
       if (pickerType === 'shuffle') {
         console.log("Using shuffle picker.")
-        picker = pickers.makeShufflePlaylistPicker(activePlaylistGroup)
+        picker = pickers.makeShufflePlaylistPicker(activePlaylist)
       } else if (pickerType === 'ordered') {
         console.log("Using ordered picker.")
-        picker = pickers.makeOrderedPlaylistPicker(activePlaylistGroup)
+        picker = pickers.makeOrderedPlaylistPicker(activePlaylist)
       } else {
         console.error("Invalid picker type: " + pickerType)
         return
@@ -348,7 +352,7 @@ Promise.resolve()
 
       return playPromise
     } else {
-      return activePlaylistGroup
+      return activePlaylist
     }
   })
   .catch(err => console.error(err))