« get me outta code hell

more socat stuff : shrug emoji : :) - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/players.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-02-06 17:40:33 -0400
committerFlorrie <towerofnix@gmail.com>2020-02-06 17:40:33 -0400
commitfd09d0196f8db2102f9364a56f3075bf2cd93c88 (patch)
treeae517e71ad9d1d96e0cb37a7e17c867cb1a3fefb /players.js
parent9a0316cf5214f1eb9f13651c9b3f7cc852ee1bd1 (diff)
more socat stuff : shrug emoji : :)
Diffstat (limited to 'players.js')
-rw-r--r--players.js33
1 files changed, 25 insertions, 8 deletions
diff --git a/players.js b/players.js
index f2cced2..5d332b3 100644
--- a/players.js
+++ b/players.js
@@ -4,6 +4,10 @@ const { spawn } = require('child_process')
 const { commandExists, killProcess, getTimeStrings } = require('./general-util')
 const EventEmitter = require('events')
 const Socat = require('./socat')
+const fs = require('fs')
+const util = require('util')
+
+const unlink = util.promisify(fs.unlink)
 
 class Player extends EventEmitter {
   constructor() {
@@ -120,16 +124,19 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer {
   }
 
   playFile(file) {
-    let path
+    this.removeSocket(this.socketPath)
+
     do {
-      // path = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000)
-      path = './mtui-socket-' + Math.floor(Math.random() * 10000)
-    } while (this.existsSync(path))
+      // this.socketPathpath = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000)
+      this.socketPath = __dirname + '/mtui-socket-' + Math.floor(Math.random() * 10000)
+    } while (this.existsSync(this.socketPath))
 
-    this.socat = new Socat(path)
+    this.socat = new Socat(this.socketPath)
 
     const mpv = super.playFile(file)
 
+    mpv.then(() => this.removeSocket(this.socketPath))
+
     return mpv
   }
 
@@ -191,11 +198,21 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer {
     this.sendCommand('set', 'loop', this.isLooping)
   }
 
-  kill() {
+  async kill() {
+    const path = this.socketPath
+    delete this.socketPath
     if (this.socat) {
-      this.socat.stop()
+      await this.socat.dispose()
+      await this.socat.stop()
+    }
+    await super.kill()
+    await this.removeSocket(path)
+  }
+
+  async removeSocket(path) {
+    if (path) {
+      await unlink(path).catch(() => {})
     }
-    return super.kill()
   }
 }