diff options
author | isaacs <i@izs.me> | 2012-07-24 07:44:12 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-07-25 02:36:53 +0400 |
commit | e4c9c9f412ca64435fbe93a2700d569b84ba7b36 (patch) | |
tree | 8415b26ff1f011e2ada29f2a5342afd73e3a58b1 /lib | |
parent | 2c487669f74aabff30d85813461ae9877fe913a3 (diff) |
readline: Remove event listeners on close
Fix #3756
Diffstat (limited to 'lib')
-rw-r--r-- | lib/readline.js | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/lib/readline.js b/lib/readline.js index 7348201fa9b..d9a6fcdb4a5 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -86,12 +86,28 @@ function Interface(input, output, completer, terminal) { this.terminal = !!terminal; + function ondata(data) { + self._normalWrite(data); + } + + function onend() { + self.close(); + } + + function onkeypress(s, key) { + self._ttyWrite(s, key); + } + + function onresize() { + self._refreshLine(); + } + if (!this.terminal) { - input.on('data', function(data) { - self._normalWrite(data); - }); - input.on('end', function() { - self.close(); + input.on('data', ondata); + input.on('end', onend); + self.once('close', function() { + input.removeListener('data', ondata); + input.removeListener('end', onend); }); var StringDecoder = require('string_decoder').StringDecoder; // lazy load this._decoder = new StringDecoder('utf8'); @@ -101,9 +117,7 @@ function Interface(input, output, completer, terminal) { exports.emitKeypressEvents(input); // input usually refers to stdin - input.on('keypress', function(s, key) { - self._ttyWrite(s, key); - }); + input.on('keypress', onkeypress); // Current line this.line = ''; @@ -117,8 +131,10 @@ function Interface(input, output, completer, terminal) { this.history = []; this.historyIndex = -1; - output.on('resize', function() { - self._refreshLine(); + output.on('resize', onresize); + self.once('close', function() { + input.removeListener('keypress', onkeypress); + output.removeListener('resize', onresize); }); } } |