@@ -30,6 +30,7 @@ const {
3030 Promise,
3131 PromiseReject,
3232 StringPrototypeSlice,
33+ SymbolDispose,
3334} = primordials ;
3435
3536const {
@@ -94,6 +95,7 @@ const {
9495 kWordRight,
9596 kWriteToOutput,
9697} = require ( 'internal/readline/interface' ) ;
98+ let addAbortListener ;
9799
98100function Interface ( input , output , completer , terminal ) {
99101if ( ! ( this instanceof Interface ) ) {
@@ -144,15 +146,13 @@ Interface.prototype.question = function(query, options, cb) {
144146const onAbort = ( ) => {
145147this [ kQuestionCancel ] ( ) ;
146148} ;
147- options . signal . addEventListener ( 'abort' , onAbort , { once : true } ) ;
148- const cleanup = ( ) => {
149- options . signal . removeEventListener ( 'abort' , onAbort ) ;
150- } ;
149+ addAbortListener ??= require ( 'events' ) . addAbortListener ;
150+ const disposable = addAbortListener ( options . signal , onAbort ) ;
151151const originalCb = cb ;
152152cb = typeof cb === 'function' ? ( answer ) => {
153- cleanup ( ) ;
153+ disposable [ SymbolDispose ] ( ) ;
154154return originalCb ( answer ) ;
155- } : cleanup ;
155+ } : disposable [ SymbolDispose ] ;
156156}
157157
158158if ( typeof cb === 'function' ) {
@@ -176,9 +176,10 @@ Interface.prototype.question[promisify.custom] = function question(query, option
176176const onAbort = ( ) => {
177177reject ( new AbortError ( undefined , { cause : options . signal . reason } ) ) ;
178178} ;
179- options . signal . addEventListener ( 'abort' , onAbort , { once : true } ) ;
179+ addAbortListener ??= require ( 'events' ) . addAbortListener ;
180+ const disposable = addAbortListener ( options . signal , onAbort ) ;
180181cb = ( answer ) => {
181- options . signal . removeEventListener ( 'abort' , onAbort ) ;
182+ disposable [ SymbolDispose ] ( ) ;
182183resolve ( answer ) ;
183184} ;
184185}
0 commit comments