« get me outta code hell

End flushable when socket disconnects - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-07-06 11:40:55 -0300
committerFlorrie <towerofnix@gmail.com>2019-07-06 11:40:55 -0300
commitdac84d4998947e49dae1ab9233d88d0edc10413d (patch)
treeb95577d257295ac440c1fca35e127255898f9ac2
parent8512c44b6403c126bf961c0fd0c2798d6bfdfcea (diff)
End flushable when socket disconnects
This prevents occasional errors from showing up when a flushable tries
to flush to the socket after it's been closed.
-rw-r--r--client.js2
-rw-r--r--telnet-server.js8
2 files changed, 6 insertions, 4 deletions
diff --git a/client.js b/client.js
index 7f21d1a..a0c8352 100644
--- a/client.js
+++ b/client.js
@@ -82,7 +82,7 @@ const setupClient = async ({backend, writable, interfacer, appConfig}) => {
     flushable.flush()
   }, 100)
 
-  return {appElement, cleanTerminal, renderInterval}
+  return {appElement, cleanTerminal, flushable, renderInterval}
 }
 
 module.exports = setupClient
diff --git a/telnet-server.js b/telnet-server.js
index 6cee554..479bd2b 100644
--- a/telnet-server.js
+++ b/telnet-server.js
@@ -22,7 +22,7 @@ class TelnetServer {
 
   async handleConnection(socket) {
     const interfacer = new TelnetInterfacer(socket)
-    const { appElement, renderInterval, cleanTerminal } = await setupClient({
+    const { appElement, cleanTerminal, flushable, renderInterval } = await setupClient({
       backend: this.backend,
       writable: socket,
       interfacer,
@@ -39,8 +39,9 @@ class TelnetServer {
       interfacer.cleanTelnetOptions()
       socket.write('\r' + msg + '\r\n')
       socket.end()
-      this.sockets.splice(this.sockets.indexOf(socket, 1))
+      flushable.end()
       closed = true
+      this.sockets.splice(this.sockets.indexOf(socket, 1))
     }
 
     appElement.on('quitRequested', quit)
@@ -48,8 +49,9 @@ class TelnetServer {
     socket.on('close', () => {
       if (!closed) {
         clearInterval(renderInterval)
-        this.sockets.splice(this.sockets.indexOf(socket, 1))
+        flushable.end()
         closed = true
+        this.sockets.splice(this.sockets.indexOf(socket, 1))
       }
     })