« get me outta code hell

Don't count intentionally .kill()ing a process as it crashing - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-02-23 19:04:57 -0400
committerFlorrie <towerofnix@gmail.com>2018-02-23 19:04:57 -0400
commit68d879fd17821bc5cd71d9aeedf861dd6c0b488a (patch)
treea0c7ca31285404a2c6d347bda9ac7c58e091e736
parentcd661532bcc861d177730273130768a33928ca37 (diff)
Don't count intentionally .kill()ing a process as it crashing
-rw-r--r--src/loop-play.js33
1 files changed, 18 insertions, 15 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index c702bed..9c679c7 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -40,9 +40,11 @@ class Player extends EventEmitter {
   set process(newProcess) {
     this._process = newProcess
     this._process.on('exit', code => {
-      if (code !== 0) {
-        this.emit('crashed', code) // TODO: HANDLE THIS
+      if (code !== 0 && !this._killed) {
+        this.emit('crashed', code)
       }
+
+      this._killed = false
     })
   }
 
@@ -56,7 +58,13 @@ class Player extends EventEmitter {
   volUp(amount) {}
   volDown(amount) {}
   togglePause() {}
-  kill() {}
+
+  async kill() {
+    if (this.process) {
+      this._killed = true
+      await killProcess(this.process)
+    }
+  }
 
   printStatusLine(str) {
     // Quick sanity check - we don't want to print the status line if it's
@@ -124,12 +132,6 @@ class MPVPlayer extends Player {
       this.process.once('close', resolve)
     })
   }
-
-  async kill() {
-    if (this.process) {
-      await killProcess(this.process)
-    }
-  }
 }
 
 class ControllableMPVPlayer extends MPVPlayer {
@@ -245,9 +247,6 @@ class SoXPlayer extends Player {
   }
 
   async kill() {
-    if (this.process) {
-      await killProcess(this.process)
-    }
   }
 }
 
@@ -385,9 +384,13 @@ class PlayController extends EventEmitter {
     this.playFailCount = 0
 
     this.player.on('crashed', () => {
-      console.log('\x1b[31mFailed to play track \x1b[1m' +
-        getItemPathString(this.currentTrack) + '\x1b[0m'
-      )
+      if (this.currentTrack) {
+        console.log('\x1b[31mFailed to play track \x1b[1m' +
+          getItemPathString(this.currentTrack) + '\x1b[0m'
+        )
+      } else {
+        console.log('\x1b[31mFailed to play track.\x1b[0m')
+      }
       this.playFailCount++
 
       if (this.playFailCount >= 5) {