From 68d879fd17821bc5cd71d9aeedf861dd6c0b488a Mon Sep 17 00:00:00 2001
From: Florrie <towerofnix@gmail.com>
Date: Fri, 23 Feb 2018 19:04:57 -0400
Subject: Don't count intentionally .kill()ing a process as it crashing

---
 src/loop-play.js | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

(limited to 'src')

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) {
-- 
cgit 1.3.0-6-gf8a5