« get me outta code hell

Automatically play next track - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-05-30 18:10:29 -0300
committerFlorrie <towerofnix@gmail.com>2018-05-30 18:10:29 -0300
commit8766d0f6c9fa89c1db9d2df063ffe14603ab5742 (patch)
tree564cd3ea68aaae50f42e11ce05c0a985bab5974c
parent12ea120dba62d22e27badd4286f2310f0bc5990c (diff)
Automatically play next track
-rw-r--r--.gitignore3
-rw-r--r--index.js8
-rw-r--r--record-store.js4
-rw-r--r--ui.js26
4 files changed, 37 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index 3c3629e..542b57c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
-node_modules
+/node_modules
+/flat.json
diff --git a/index.js b/index.js
index 635426e..a3f3b2f 100644
--- a/index.js
+++ b/index.js
@@ -3,6 +3,7 @@
 const { getPlayer } = require('./players')
 const { getDownloaderFor } = require('./downloaders')
 const { AppElement } = require('./ui')
+const { updatePlaylistFormat } = require('./playlist-utils')
 const ansi = require('./tui-lib/util/ansi')
 const CommandLineInterfacer = require('./tui-lib/util/CommandLineInterfacer')
 const EventEmitter = require('events')
@@ -94,8 +95,9 @@ async function main() {
     process.exit(0)
   })
 
-  const grouplike = {
+  let grouplike = {
     items: [
+      {name: 'bears', downloaderArg: 'http://www.billwurtz.com/bears.mp3'},
       {name: 'alphabet shuffle', downloaderArg: 'http://www.billwurtz.com/alphabet-shuffle.mp3'},
       {name: 'in california', downloaderArg: 'http://www.billwurtz.com/in-california.mp3'},
       {name: 'i love you', downloaderArg: 'http://www.billwurtz.com/i-love-you.mp3'},
@@ -107,6 +109,10 @@ async function main() {
     ]
   }
 
+  // let grouplike = require('./flat.json')
+
+  grouplike = updatePlaylistFormat(grouplike)
+
   appElement.grouplikeListingElement.loadGrouplike(grouplike)
 
   root.select(appElement.grouplikeListingElement)
diff --git a/record-store.js b/record-store.js
index d0c7623..80c8d3a 100644
--- a/record-store.js
+++ b/record-store.js
@@ -1,4 +1,4 @@
-const recordSymbolKey = Symbol()
+const recordSymbolKey = Symbol('Record symbol')
 
 module.exports = class RecordStore {
   constructor() {
@@ -13,7 +13,7 @@ module.exports = class RecordStore {
     }
 
     if (!obj[recordSymbolKey]) {
-      obj[recordSymbolKey] = Symbol()
+      obj[recordSymbolKey] = Symbol('Reference to a record')
     }
 
     if (!this.data[obj[recordSymbolKey]]) {
diff --git a/ui.js b/ui.js
index 8d43db8..bb22074 100644
--- a/ui.js
+++ b/ui.js
@@ -1,5 +1,6 @@
 const { getDownloaderFor } = require('./downloaders')
 const { getPlayer } = require('./players')
+const { parentSymbol } = require('./playlist-utils')
 const ansi = require('./tui-lib/util/ansi')
 const Button = require('./tui-lib/ui/form/Button')
 const FocusElement = require('./tui-lib/ui/form/FocusElement')
@@ -70,6 +71,12 @@ class AppElement extends FocusElement {
       throw new Error('Attempted to play before a player was loaded')
     }
 
+    let playingThisTrack = true
+    this.emit('playing new track')
+    this.once('playing new track', () => {
+      playingThisTrack = false
+    })
+
     // TODO: Check if it's an item or a group
 
     const downloadFile = await this.downloadGrouplikeItem(item)
@@ -80,6 +87,25 @@ class AppElement extends FocusElement {
     } finally {
       this.recordStore.getRecord(item).playing = false
     }
+
+    // playingThisTrack now means whether the track played through to the end
+    // (true), or was stopped by a different track being started (false).
+
+    if (playingThisTrack) {
+      this.playNextTrack(item)
+    }
+  }
+
+  playNextTrack(track) {
+    const parent = track[parentSymbol]
+    if (!parent) {
+      return
+    }
+    const index = parent.items.indexOf(track)
+    const nextItem = parent.items[index + 1]
+    if (nextItem) {
+      this.playGrouplikeItem(nextItem)
+    }
   }
 }