diff options
author | Timothy Gu <timothygu99@gmail.com> | 2018-10-26 17:53:04 -0700 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2018-11-20 15:41:16 -0800 |
commit | 2a7432dadec08bbe7063d84f1aa4a6396807305c (patch) | |
tree | 817cca2a785ef5e6b595cf89e90667e803efcbb1 /lib | |
parent | 2742f3869a540b01ed51ca91075af9215c13bd54 (diff) | |
download | android-node-v8-2a7432dadec08bbe7063d84f1aa4a6396807305c.tar.gz android-node-v8-2a7432dadec08bbe7063d84f1aa4a6396807305c.tar.bz2 android-node-v8-2a7432dadec08bbe7063d84f1aa4a6396807305c.zip |
readline: add support for async iteration
Co-authored-by: Ivan Filenko <ivan.filenko@protonmail.com>
Fixes: https://github.com/nodejs/node/issues/18603
Refs: https://github.com/nodejs/node/pull/18904
PR-URL: https://github.com/nodejs/node/pull/23916
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/readline.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/readline.js b/lib/readline.js index b293292d83..049f5aaecc 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -33,6 +33,7 @@ const { ERR_INVALID_OPT_VALUE } = require('internal/errors').codes; const { debug, inherits } = require('util'); +const { emitExperimentalWarning } = require('internal/util'); const { Buffer } = require('buffer'); const EventEmitter = require('events'); const { @@ -54,11 +55,16 @@ const { // Lazy load StringDecoder for startup performance. let StringDecoder; +// Lazy load Readable for startup performance. +let Readable; + const kHistorySize = 30; const kMincrlfDelay = 100; // \r\n, \n, or \r followed by something other than \n const lineEnding = /\r?\n|\r(?!\n)/; +const kLineObjectStream = Symbol('line object stream'); + const KEYPRESS_DECODER = Symbol('keypress-decoder'); const ESCAPE_DECODER = Symbol('escape-decoder'); @@ -190,6 +196,8 @@ function Interface(input, output, completer, terminal) { self._refreshLine(); } + this[kLineObjectStream] = undefined; + if (!this.terminal) { function onSelfCloseWithoutTerminal() { input.removeListener('data', ondata); @@ -1019,6 +1027,41 @@ Interface.prototype._ttyWrite = function(s, key) { } }; +Interface.prototype[Symbol.asyncIterator] = function() { + emitExperimentalWarning('readline Interface [Symbol.asyncIterator]'); + + if (this[kLineObjectStream] === undefined) { + if (Readable === undefined) { + Readable = require('stream').Readable; + } + const readable = new Readable({ + objectMode: true, + read: () => { + this.resume(); + }, + destroy: (err, cb) => { + this.off('line', lineListener); + this.off('close', closeListener); + this.close(); + cb(err); + } + }); + const lineListener = (input) => { + if (!readable.push(input)) { + this.pause(); + } + }; + const closeListener = () => { + readable.push(null); + }; + this.on('line', lineListener); + this.on('close', closeListener); + this[kLineObjectStream] = readable; + } + + return this[kLineObjectStream][Symbol.asyncIterator](); +}; + /** * accepts a readable Stream instance and makes it emit "keypress" events */ |