« 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/socat.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 /socat.js
parent9a0316cf5214f1eb9f13651c9b3f7cc852ee1bd1 (diff)
more socat stuff : shrug emoji : :)
Diffstat (limited to 'socat.js')
-rw-r--r--socat.js26
1 files changed, 22 insertions, 4 deletions
diff --git a/socat.js b/socat.js
index 8c6b9ed..95ee889 100644
--- a/socat.js
+++ b/socat.js
@@ -33,19 +33,37 @@ module.exports = class Socat extends EventEmitter {
     }
   }
 
-  stop() {
-    if (this.subprocess) {
-      killProcess(this.subprocess)
+  async stop() {
+    const proc = this.subprocess
+    if (proc) {
       this.subprocess = null
+      await killProcess(proc)
     }
   }
 
+  async dispose() {
+    // Don't accept any more messages.
+    this.disposed = true
+    await this.stop()
+  }
+
   async send(message) {
+    if (this.disposed) {
+      return
+    }
     if (!this.subprocess) {
       await this.start()
     }
     if (this.subprocess) {
-      this.subprocess.stdin.write(message + '\r\n')
+      try {
+        this.subprocess.stdin.write(message + '\r\n')
+      } catch (error) {
+        // There's no guarantee we'll actually suceed to write - the process
+        // or pipe we're writing to could have closed unexpectedly. If that
+        // happens, unestablish the socat process; it'll try to reconnect if
+        // we send another message.
+        this.stop();
+      }
     } else {
       try {
         await writeFile(path.resolve(__dirname, this.path), message + '\r\n')