@@ -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,19 @@ 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- }
4457
45- if ( options . abortSignal ) {
46- exitConditions . push ( abortTimeout ( options . abortSignal ) ) ;
58+ const signal = options . abortController ?. signal ?? options . abortSignal ;
59+
60+ let _clearSignalListener = ( ) => { } ;
61+
62+ if ( signal ) {
63+ const { aborted, clearListener } = abortTimeout ( signal ) ;
64+ _clearSignalListener = clearListener ;
65+ exitConditions . push ( aborted ) ;
4766 }
4867
49- return Promise . race ( exitConditions ) ;
68+ return Promise . race < WaiterResult > ( exitConditions ) . then ( ( result ) => {
69+ _clearSignalListener ( ) ;
70+ return result ;
71+ } ) ;
5072} ;
0 commit comments