Skip to content

Commit

Permalink
readline: use addAbortListener
Browse files Browse the repository at this point in the history
PR-URL: #48550
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
atlowChemi authored and ruyadorno committed Sep 11, 2023
1 parent aaa8857 commit d28b1ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
5 changes: 3 additions & 2 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const {
StringPrototypeStartsWith,
StringPrototypeTrim,
Symbol,
SymbolDispose,
SymbolAsyncIterator,
SafeStringIterator,
} = primordials;
Expand Down Expand Up @@ -332,8 +333,8 @@ function InterfaceConstructor(input, output, completer, terminal) {
if (signal.aborted) {
process.nextTick(onAborted);
} else {
signal.addEventListener('abort', onAborted, { once: true });
self.once('close', () => signal.removeEventListener('abort', onAborted));
const disposable = EventEmitter.addAbortListener(signal, onAborted);
self.once('close', disposable[SymbolDispose]);
}
}

Expand Down
17 changes: 9 additions & 8 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
Promise,
PromiseReject,
StringPrototypeSlice,
SymbolDispose,
} = primordials;

const {
Expand Down Expand Up @@ -94,6 +95,7 @@ const {
kWordRight,
kWriteToOutput,
} = require('internal/readline/interface');
let addAbortListener;

function Interface(input, output, completer, terminal) {
if (!(this instanceof Interface)) {
Expand Down Expand Up @@ -144,15 +146,13 @@ Interface.prototype.question = function(query, options, cb) {
const onAbort = () => {
this[kQuestionCancel]();
};
options.signal.addEventListener('abort', onAbort, { once: true });
const cleanup = () => {
options.signal.removeEventListener('abort', onAbort);
};
addAbortListener ??= require('events').addAbortListener;
const disposable = addAbortListener(options.signal, onAbort);
const originalCb = cb;
cb = typeof cb === 'function' ? (answer) => {
cleanup();
disposable[SymbolDispose]();
return originalCb(answer);
} : cleanup;
} : disposable[SymbolDispose];
}

if (typeof cb === 'function') {
Expand All @@ -176,9 +176,10 @@ Interface.prototype.question[promisify.custom] = function question(query, option
const onAbort = () => {
reject(new AbortError(undefined, { cause: options.signal.reason }));
};
options.signal.addEventListener('abort', onAbort, { once: true });
addAbortListener ??= require('events').addAbortListener;
const disposable = addAbortListener(options.signal, onAbort);
cb = (answer) => {
options.signal.removeEventListener('abort', onAbort);
disposable[SymbolDispose]();
resolve(answer);
};
}
Expand Down
8 changes: 6 additions & 2 deletions lib/readline/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
Promise,
SymbolDispose,
} = primordials;

const {
Expand All @@ -21,6 +22,7 @@ const { validateAbortSignal } = require('internal/validators');
const {
kEmptyObject,
} = require('internal/util');
let addAbortListener;

class Interface extends _Interface {
// eslint-disable-next-line no-useless-constructor
Expand All @@ -42,9 +44,11 @@ class Interface extends _Interface {
this[kQuestionCancel]();
reject(new AbortError(undefined, { cause: options.signal.reason }));
};
options.signal.addEventListener('abort', onAbort, { once: true });
addAbortListener ??= require('events').addAbortListener;
const disposable = addAbortListener(options.signal, onAbort);

cb = (answer) => {
options.signal.removeEventListener('abort', onAbort);
disposable[SymbolDispose]();
resolve(answer);
};
}
Expand Down

0 comments on commit d28b1ee

Please sign in to comment.