mirror of
https://github.com/nodejs/node.git
synced 2025-05-01 17:03:34 +00:00
readline: remove question
method from InterfaceConstructor
That method is overwritten in both `require('node:readline').Interface.prototype` and `require('node:readline/promises').Interface.prototype`, and is very much not useful outside of interacting with TTY, removing it from the parent class could enable the use of `InterfaceConstructor` in other contexts (such as interacting with files). PR-URL: https://github.com/nodejs/node/pull/44606 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
5ec2c99007
commit
481a959adb
@ -303,61 +303,6 @@ paused.
|
||||
If the `InterfaceConstructor` was created with `output` set to `null` or
|
||||
`undefined` the prompt is not written.
|
||||
|
||||
### `rl.question(query[, options], callback)`
|
||||
|
||||
<!-- YAML
|
||||
added: v0.3.3
|
||||
-->
|
||||
|
||||
* `query` {string} A statement or query to write to `output`, prepended to the
|
||||
prompt.
|
||||
* `options` {Object}
|
||||
* `signal` {AbortSignal} Optionally allows the `question()` to be canceled
|
||||
using an `AbortController`.
|
||||
* `callback` {Function} A callback function that is invoked with the user's
|
||||
input in response to the `query`.
|
||||
|
||||
The `rl.question()` method displays the `query` by writing it to the `output`,
|
||||
waits for user input to be provided on `input`, then invokes the `callback`
|
||||
function passing the provided input as the first argument.
|
||||
|
||||
When called, `rl.question()` will resume the `input` stream if it has been
|
||||
paused.
|
||||
|
||||
If the `InterfaceConstructor` was created with `output` set to `null` or
|
||||
`undefined` the `query` is not written.
|
||||
|
||||
The `callback` function passed to `rl.question()` does not follow the typical
|
||||
pattern of accepting an `Error` object or `null` as the first argument.
|
||||
The `callback` is called with the provided answer as the only argument.
|
||||
|
||||
An error will be thrown if calling `rl.question()` after `rl.close()`.
|
||||
|
||||
Example usage:
|
||||
|
||||
```js
|
||||
rl.question('What is your favorite food? ', (answer) => {
|
||||
console.log(`Oh, so your favorite food is ${answer}`);
|
||||
});
|
||||
```
|
||||
|
||||
Using an `AbortController` to cancel a question.
|
||||
|
||||
```js
|
||||
const ac = new AbortController();
|
||||
const signal = ac.signal;
|
||||
|
||||
rl.question('What is your favorite food? ', { signal }, (answer) => {
|
||||
console.log(`Oh, so your favorite food is ${answer}`);
|
||||
});
|
||||
|
||||
signal.addEventListener('abort', () => {
|
||||
console.log('The food question timed out');
|
||||
}, { once: true });
|
||||
|
||||
setTimeout(() => ac.abort(), 10000);
|
||||
```
|
||||
|
||||
### `rl.resume()`
|
||||
|
||||
<!-- YAML
|
||||
|
@ -81,6 +81,7 @@ const lineEnding = /\r?\n|\r(?!\n)/;
|
||||
|
||||
const kLineObjectStream = Symbol('line object stream');
|
||||
const kQuestionCancel = Symbol('kQuestionCancel');
|
||||
const kQuestion = Symbol('kQuestion');
|
||||
|
||||
// GNU readline library - keyseq-timeout is 500ms (default)
|
||||
const ESCAPE_CODE_TIMEOUT = 500;
|
||||
@ -401,7 +402,7 @@ class Interface extends InterfaceConstructor {
|
||||
}
|
||||
}
|
||||
|
||||
question(query, cb) {
|
||||
[kQuestion](query, cb) {
|
||||
if (this.closed) {
|
||||
throw new ERR_USE_AFTER_CLOSE('readline');
|
||||
}
|
||||
@ -1405,6 +1406,7 @@ module.exports = {
|
||||
kOnLine,
|
||||
kPreviousKey,
|
||||
kPrompt,
|
||||
kQuestion,
|
||||
kQuestionCallback,
|
||||
kQuestionCancel,
|
||||
kRefreshLine,
|
||||
|
@ -81,6 +81,7 @@ const {
|
||||
kOnLine,
|
||||
kPreviousKey,
|
||||
kPrompt,
|
||||
kQuestion,
|
||||
kQuestionCallback,
|
||||
kQuestionCancel,
|
||||
kRefreshLine,
|
||||
@ -120,8 +121,6 @@ function Interface(input, output, completer, terminal) {
|
||||
ObjectSetPrototypeOf(Interface.prototype, _Interface.prototype);
|
||||
ObjectSetPrototypeOf(Interface, _Interface);
|
||||
|
||||
const superQuestion = _Interface.prototype.question;
|
||||
|
||||
/**
|
||||
* Displays `query` by writing it to the `output`.
|
||||
* @param {string} query
|
||||
@ -129,7 +128,7 @@ const superQuestion = _Interface.prototype.question;
|
||||
* @param {Function} cb
|
||||
* @returns {void}
|
||||
*/
|
||||
Interface.prototype.question = function(query, options, cb) {
|
||||
Interface.prototype.question = function question(query, options, cb) {
|
||||
cb = typeof options === 'function' ? options : cb;
|
||||
if (options === null || typeof options !== 'object') {
|
||||
options = kEmptyObject;
|
||||
@ -156,7 +155,7 @@ Interface.prototype.question = function(query, options, cb) {
|
||||
}
|
||||
|
||||
if (typeof cb === 'function') {
|
||||
FunctionPrototypeCall(superQuestion, this, query, cb);
|
||||
this[kQuestion](query, cb);
|
||||
}
|
||||
};
|
||||
Interface.prototype.question[promisify.custom] = function question(query, options) {
|
||||
|
@ -10,6 +10,7 @@ const {
|
||||
|
||||
const {
|
||||
Interface: _Interface,
|
||||
kQuestion,
|
||||
kQuestionCancel,
|
||||
} = require('internal/readline/interface');
|
||||
|
||||
@ -49,7 +50,7 @@ class Interface extends _Interface {
|
||||
};
|
||||
}
|
||||
|
||||
super.question(query, cb);
|
||||
this[kQuestion](query, cb);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user