Skip to content

Commit

Permalink
readline: use addAbortListener
Browse files Browse the repository at this point in the history
PR-URL: nodejs#48550
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
atlowChemi authored and Ceres6 committed Aug 14, 2023
1 parent a6d68c4 commit 961d209
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 @@ -28,6 +28,7 @@ const {
StringPrototypeStartsWith,
StringPrototypeTrim,
Symbol,
SymbolDispose,
SymbolAsyncIterator,
SafeStringIterator,
} = primordials;
Expand Down Expand Up @@ -325,8 +326,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 @@ -95,6 +96,7 @@ const {
kWordRight,
kWriteToOutput,
} = require('internal/readline/interface');
let addAbortListener;

function Interface(input, output, completer, terminal) {
if (!(this instanceof Interface)) {
Expand Down Expand Up @@ -143,15 +145,13 @@ Interface.prototype.question = function question(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 @@ -175,9 +175,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 @@ -22,6 +23,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 @@ -43,9 +45,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 961d209

Please sign in to comment.