« get me outta code hell

fix ancient waitForData util listener memory leak - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-03-25 17:39:41 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-03-25 17:39:41 -0300
commit3614ce99f2eaa910a2b11b0f2f4a8a291ee3a4b0 (patch)
treeac3a4bcfc988958ae7505b2f2d7dc24e9a8ca78e
parenta90b90044fa6000bf0fa3490c20b6bf5c6502b1d (diff)
fix ancient waitForData util listener memory leak
Particularly, this added a new event listener (i.e. function) every time
the screen was resized, which would never be garbage collected.

Oops.
-rw-r--r--util/waitForData.js6
1 files changed, 4 insertions, 2 deletions
diff --git a/util/waitForData.js b/util/waitForData.js
index bf40c52..ed88402 100644
--- a/util/waitForData.js
+++ b/util/waitForData.js
@@ -1,9 +1,11 @@
 module.exports = function waitForData(stream, cond = null) {
   return new Promise(resolve => {
-    stream.on('data', data => {
+    const listener = data => {
       if (cond ? cond(data) : true) {
         resolve(data)
+        stream.removeListener('data', listener)
       }
-    })
+    }
+    stream.on('data', listener)
   })
 }