mirror of
https://github.com/nodejs/node.git
synced 2024-11-28 14:33:11 +01:00
d5eb9a378e
PR-URL: https://github.com/nodejs/node/pull/55276 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
71 lines
1.6 KiB
JavaScript
71 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
Promise,
|
|
SymbolDispose,
|
|
} = primordials;
|
|
|
|
const {
|
|
Readline,
|
|
} = require('internal/readline/promises');
|
|
|
|
const {
|
|
Interface: _Interface,
|
|
kQuestion,
|
|
kQuestionCancel,
|
|
} = require('internal/readline/interface');
|
|
|
|
const {
|
|
AbortError,
|
|
} = require('internal/errors');
|
|
const { validateAbortSignal } = require('internal/validators');
|
|
|
|
const {
|
|
kEmptyObject,
|
|
} = require('internal/util');
|
|
let addAbortListener;
|
|
|
|
class Interface extends _Interface {
|
|
// eslint-disable-next-line no-useless-constructor
|
|
constructor(input, output, completer, terminal) {
|
|
super(input, output, completer, terminal);
|
|
}
|
|
question(query, options = kEmptyObject) {
|
|
return new Promise((resolve, reject) => {
|
|
let cb = resolve;
|
|
|
|
if (options?.signal) {
|
|
validateAbortSignal(options.signal, 'options.signal');
|
|
if (options.signal.aborted) {
|
|
return reject(
|
|
new AbortError(undefined, { cause: options.signal.reason }));
|
|
}
|
|
|
|
const onAbort = () => {
|
|
this[kQuestionCancel]();
|
|
reject(new AbortError(undefined, { cause: options.signal.reason }));
|
|
};
|
|
addAbortListener ??= require('internal/events/abort_listener').addAbortListener;
|
|
const disposable = addAbortListener(options.signal, onAbort);
|
|
|
|
cb = (answer) => {
|
|
disposable[SymbolDispose]();
|
|
resolve(answer);
|
|
};
|
|
}
|
|
|
|
this[kQuestion](query, cb);
|
|
});
|
|
}
|
|
}
|
|
|
|
function createInterface(input, output, completer, terminal) {
|
|
return new Interface(input, output, completer, terminal);
|
|
}
|
|
|
|
module.exports = {
|
|
Interface,
|
|
Readline,
|
|
createInterface,
|
|
};
|