Skip to content

Commit

Permalink
readline: use removeListener event
Browse files Browse the repository at this point in the history
use removeListener event rather than checking listeners().length every `data` event
  • Loading branch information
adammw committed Oct 14, 2012
1 parent 4b238b4 commit 6b03921
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -804,24 +804,29 @@ function emitKeypressEvents(stream) {
stream._emitKeypress = true;

function onData(b) {
if (stream.listeners('keypress').length > 0) {
emitKey(stream, b);
} else {
// Nobody's watching anyway
stream.removeListener('data', onData);
stream.on('newListener', onNewListener);
}
emitKey(stream, b);
}

function onNewListener(event) {
if (event == 'keypress') {
stream.on('data', onData);
stream.on('removeListener', onRemovedListener);
stream.removeListener('newListener', onNewListener);
}
}

function onRemovedListener(event) {
if (event == 'keypress' && stream.listeners('keypress').length == 0) {
// Nobody's watching anyway
stream.removeListener('data', onData);
stream.removeListener('removeListener', onRemovedListener);
stream.on('newListener', onNewListener);
}
}

if (stream.listeners('keypress').length > 0) {
stream.on('data', onData);
stream.on('removeListener', onRemovedListener);
} else {
stream.on('newListener', onNewListener);
}
Expand Down

0 comments on commit 6b03921

Please sign in to comment.