« get me outta code hell

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:
Diffstat (limited to 'backend.js')
-rw-r--r--backend.js35
1 files changed, 26 insertions, 9 deletions
diff --git a/backend.js b/backend.js
index d68d448..f2a9d99 100644
--- a/backend.js
+++ b/backend.js
@@ -35,6 +35,11 @@ async function download(item, record) {
     return
   }
 
+  // You can't download things that aren't tracks!
+  if (!isTrack(item)) {
+    return
+  }
+
   // Don't start downloading an item if we're already downloading it!
   if (record.downloading) {
     return
@@ -110,6 +115,11 @@ class QueuePlayer extends EventEmitter {
         return
       }
 
+      // If the item isn't a track, it can't be queued.
+      if (!isTrack(item)) {
+        return
+      }
+
       // You can't put the same track in the queue twice - we automatically
       // remove the old entry. (You can't for a variety of technical reasons,
       // but basically you either have the display all bork'd, or new tracks
@@ -161,15 +171,15 @@ class QueuePlayer extends EventEmitter {
     }
 
     const { items } = this.queueGrouplike
-    const newItems = flattenGrouplike(grouplike).items
+    const newTracks = flattenGrouplike(grouplike).items.filter(isTrack)
 
     // Expressly do an initial pass and unqueue the items we want to queue -
     // otherwise they would mess with the math we do afterwords.
-    for (const item of newItems) {
+    for (const item of newTracks) {
       if (items.includes(item)) {
         /*
         if (!movePlayingTrack && item === this.playingTrack) {
-          // NB: if uncommenting this code, splice item from newItems and do
+          // NB: if uncommenting this code, splice item from newTracks and do
           // continue instead of return!
           return
         }
@@ -207,17 +217,17 @@ class QueuePlayer extends EventEmitter {
 
     if (how === 'evenly') {
       let offset = 0
-      for (const item of newItems) {
+      for (const item of newTracks) {
         const insertIndex = distributeStart + Math.floor(offset)
         items.splice(insertIndex, 0, item)
         offset++
-        offset += distributeSize / newItems.length
+        offset += distributeSize / newTracks.length
       }
     } else if (how === 'randomly') {
-      const indexes = newItems.map(() => Math.floor(Math.random() * distributeSize))
+      const indexes = newTracks.map(() => Math.floor(Math.random() * distributeSize))
       indexes.sort()
-      for (let i = 0; i < newItems.length; i++) {
-        const item = newItems[i]
+      for (let i = 0; i < newTracks.length; i++) {
+        const item = newTracks[i]
         const insertIndex = distributeStart + indexes[i] + i
         items.splice(insertIndex, 0, item)
       }
@@ -375,6 +385,11 @@ class QueuePlayer extends EventEmitter {
       return
     }
 
+    // If it's not a track, you can't play it.
+    if (!isTrack(item)) {
+      return
+    }
+
     playTrack: {
       // No downloader argument? That's no good - stop here.
       // TODO: An error icon on this item, or something???
@@ -660,7 +675,9 @@ class Backend extends EventEmitter {
       items = [item]
     }
 
-    const seconds = items.reduce(durationFn, 0)
+    const tracks = items.filter(isTrack)
+
+    const seconds = tracks.reduce(durationFn, 0)
 
     let { duration: string } = getTimeStringsFromSec(0, seconds)
     const approxSymbol = noticedMissingMetadata ? '+' : ''