« get me outta code hell

old "wait when done playing" code - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2020-09-15 15:48:37 -0300
committer(quasar) nebula <towerofnix@gmail.com>2020-09-15 15:48:37 -0300
commit3d6c5b9bb1f9a42cebc61cbe3b0b5fb69bc146e8 (patch)
treea6b65a7c7079193cfe57820b5b737c4f83e02d1d
parent12ff28d327d72541234572a592c57f449467863f (diff)
old "wait when done playing" code
I haven't tested this since writing it in July so I have no idea what it
does! :D
-rw-r--r--backend.js14
-rw-r--r--socket.js27
2 files changed, 39 insertions, 2 deletions
diff --git a/backend.js b/backend.js
index f2c4d59..f2610d0 100644
--- a/backend.js
+++ b/backend.js
@@ -70,6 +70,7 @@ class QueuePlayer extends EventEmitter {
     this.queueGrouplike = {name: 'Queue', isTheQueue: true, items: []}
     this.pauseNextTrack = false
     this.alwaysStartPaused = false
+    this.waitWhenDonePlaying = false
 
     this.playedTrackToEnd = false
     this.timeData = null
@@ -458,7 +459,8 @@ class QueuePlayer extends EventEmitter {
 
     if (playingThisTrack) {
       this.playedTrackToEnd = true
-      if (!this.playNext(item)) {
+      this.emit('done playing', this.playingTrack)
+      if (!this.waitWhenDonePlaying && !this.playNext(item)) {
         this.clearPlayingTrack()
       }
     }
@@ -646,6 +648,7 @@ class Backend extends EventEmitter {
 
     this.queuePlayers = []
     this.alwaysStartPaused = false
+    this.waitWhenDonePlaying = false
 
     this.recordStore = new RecordStore()
     this.throttleMetadata = throttlePromise(10)
@@ -678,12 +681,14 @@ class Backend extends EventEmitter {
     }
 
     queuePlayer.alwaysStartPaused = this.alwaysStartPaused
+    queuePlayer.waitWhenDonePlaying = this.waitWhenDonePlaying
 
     this.queuePlayers.push(queuePlayer)
     this.emit('added queue player', queuePlayer)
 
     for (const event of [
       'playing',
+      'done playing',
       'queue',
       'distribute-queue',
       'unqueue',
@@ -811,6 +816,13 @@ class Backend extends EventEmitter {
     }
   }
 
+  setWaitWhenDonePlaying(value) {
+    this.waitWhenDonePlaying = !!value
+    for (const queuePlayer of this.queuePlayers) {
+      queuePlayer.waitWhenDonePlaying = !!value
+    }
+  }
+
   async stopPlayingAll() {
     for (const queuePlayer of this.queuePlayers) {
       await queuePlayer.stopPlaying()
diff --git a/socket.js b/socket.js
index a092e4a..fec66d7 100644
--- a/socket.js
+++ b/socket.js
@@ -170,6 +170,7 @@ function validateCommand(command) {
           )
         case 'status':
           return (
+            command.status === 'done-playing' ||
             (
               command.status === 'ready-to-resume' &&
               typeof command.queuePlayer === 'string'
@@ -227,8 +228,9 @@ function makeSocketServer() {
 
   server.canonicalBackend = null
 
-  // readyToResume -> queue player id -> array: socket
+  // <variable> -> queue player id -> array: socket
   const readyToResume = {}
+  const donePlaying = {}
 
   server.on('connection', socket => {
     sockets.push(socket)
@@ -263,6 +265,19 @@ function makeSocketServer() {
 
       if (command.code === 'status') {
         switch (command.status) {
+          case 'done-playing': {
+            const doneSockets = donePlaying[command.queuePlayer]
+            if (doneSockets && !doneSockets.includes(socket)) {
+              doneSockets.push(socket)
+              if (doneSockets.length === sockets.length) {
+                // determine next track
+                for (const socket of sockets) {
+                  // play next track
+                }
+                delete donePlaying[command.queuePlayer]
+              }
+            }
+          }
           case 'ready-to-resume': {
             const readySockets = readyToResume[command.queuePlayer]
             if (readySockets && !readySockets.includes(socket)) {
@@ -276,6 +291,7 @@ function makeSocketServer() {
                     startingTrack: true,
                     paused: false
                   }) + '\n')
+                  donePlaying[command.queuePlayer] = []
                 }
                 delete readyToResume[command.queuePlayer]
               }
@@ -396,6 +412,7 @@ function attachBackendToSocketClient(backend, client, {
   // other through commands lives here.
 
   backend.setAlwaysStartPaused(true)
+  backend.setWaitWhenDonePlaying(true)
 
   function logCommand(command) {
     const nickToMessage = nickname => `\x1b[32;1m${nickname}\x1b[0m`
@@ -609,6 +626,14 @@ function attachBackendToSocketClient(backend, client, {
     })
   })
 
+  backend.on('done playing', queuePlayer => {
+    client.sendCommand({
+      code: 'status',
+      status: 'done-playing',
+      queuePlayer: queuePlayer.id
+    })
+  })
+
   backend.on('playing', (queuePlayer, track) => {
     if (track) {
       client.sendCommand({