@@ -4,9 +4,16 @@ import { runPolling } from "./poller";
44import { validateWaiterOptions } from "./utils" ;
55import { WaiterOptions , WaiterResult , waiterServiceDefaults , WaiterState } from "./waiter" ;
66
7- const abortTimeout = async ( abortSignal : AbortSignal | DeprecatedAbortSignal ) : Promise < WaiterResult > => {
8- return new Promise ( ( resolve ) => {
9- const onAbort = ( ) => resolve ( { state : WaiterState . ABORTED } ) ;
7+ const abortTimeout = (
8+ abortSignal : AbortSignal | DeprecatedAbortSignal
9+ ) : {
10+ clearListener : ( ) => void ;
11+ aborted : Promise < WaiterResult > ;
12+ } => {
13+ let onAbort : ( ) => void ;
14+
15+ const promise = new Promise < WaiterResult > ( ( resolve ) => {
16+ onAbort = ( ) => resolve ( { state : WaiterState . ABORTED } ) ;
1017 if ( typeof ( abortSignal as AbortSignal ) . addEventListener === "function" ) {
1118 // preferred.
1219 ( abortSignal as AbortSignal ) . addEventListener ( "abort" , onAbort ) ;
@@ -15,6 +22,15 @@ const abortTimeout = async (abortSignal: AbortSignal | DeprecatedAbortSignal): P
1522 abortSignal . onabort = onAbort ;
1623 }
1724 } ) ;
25+
26+ return {
27+ clearListener ( ) {
28+ if ( typeof ( abortSignal as AbortSignal ) . removeEventListener === "function" ) {
29+ ( abortSignal as AbortSignal ) . removeEventListener ( "abort" , onAbort ) ;
30+ }
31+ } ,
32+ aborted : promise ,
33+ } ;
1834} ;
1935
2036/**
@@ -38,13 +54,24 @@ export const createWaiter = async <Client, Input>(
3854 validateWaiterOptions ( params ) ;
3955
4056 const exitConditions = [ runPolling < Client , Input > ( params , input , acceptorChecks ) ] ;
41- if ( options . abortController ) {
42- exitConditions . push ( abortTimeout ( options . abortController . signal ) ) ;
43- }
57+
58+ const finalize = [ ] as Array < ( ) => void > ;
4459
4560 if ( options . abortSignal ) {
46- exitConditions . push ( abortTimeout ( options . abortSignal ) ) ;
61+ const { aborted, clearListener } = abortTimeout ( options . abortSignal ) ;
62+ finalize . push ( clearListener ) ;
63+ exitConditions . push ( aborted ) ;
64+ }
65+ if ( options . abortController ?. signal ) {
66+ const { aborted, clearListener } = abortTimeout ( options . abortController . signal ) ;
67+ finalize . push ( clearListener ) ;
68+ exitConditions . push ( aborted ) ;
4769 }
4870
49- return Promise . race ( exitConditions ) ;
71+ return Promise . race < WaiterResult > ( exitConditions ) . then ( ( result ) => {
72+ for ( const fn of finalize ) {
73+ fn ( ) ;
74+ }
75+ return result ;
76+ } ) ;
5077} ;
0 commit comments