@@ -84,28 +84,30 @@ export class PollingWithTimeoutAndAbort {
8484
8585 /**
8686 * Abort the pending request.
87- * This will clean up the request entry if it exists, and abort the pending request if it exists.
88- * Note: this needs to be called before making a new request (with await) to avoid race conditions with the new request.
87+ * To make sure that the request is actually aborted, we will listen to the abort event and resolve the promise when the abort event is triggered.
8988 *
9089 * @param requestId - The ID of the request to abort.
91- * @returns A promise that resolves when the request is aborted.
90+ * @returns A promise that resolves when the request is aborted and cleaned up .
9291 */
93- async abortPendingRequest ( requestId : string ) {
94- return new Promise ( ( resolve ) => {
95- // firstly clean up the request entry if it exists
96- // note: this does not abort the request, it only cleans up the request entry from the map.
97- const removedEntry = this . #cleanUp( requestId ) ;
98- // if there's no existing entry, then we can just resolve the promise
99- if ( ! removedEntry ) {
100- resolve ( undefined ) ;
92+ abortPendingRequest ( requestId : string ) : Promise < void > {
93+ return new Promise < void > ( ( resolve ) => {
94+ const entry = this . #requestEntries. get ( requestId ) ;
95+ if ( ! entry ) {
96+ resolve ( ) ;
10197 return ;
10298 }
103- // otherwise, we will abort the existing/pending request
104- removedEntry . abortController . abort ( this . ABORT_REASON_CANCELLED ) ;
105- // we want to make sure that the request is actually aborted, before inserting the new request entry
106- if ( removedEntry . abortController . signal . aborted ) {
107- resolve ( undefined ) ;
108- }
99+
100+ // Listen for the abort event to ensure cleanup completes before resolving
101+ const cleanupCompleteHandler = ( ) => {
102+ resolve ( ) ;
103+ } ;
104+ entry . abortController . signal . addEventListener (
105+ 'abort' ,
106+ cleanupCompleteHandler ,
107+ { once : true } ,
108+ ) ;
109+
110+ entry . abortController . abort ( this . ABORT_REASON_CANCELLED ) ;
109111 } ) ;
110112 }
111113
0 commit comments