@@ -199,8 +199,16 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType) => {
199199} ) ;
200200
201201const XMLHTTPREQUEST_SOURCE = 'XMLHttpRequest.send' ;
202- const sendNative =
202+ const fetchTaskAborting = zoneSymbol ( 'fetchTaskAborting' ) ;
203+ const fetchTaskScheduling = zoneSymbol ( 'fetchTaskScheduling' ) ;
204+ const sendNative : Function | null =
203205patchMethod ( XMLHttpRequestPrototype , 'send' , ( ) => function ( self : any , args : any [ ] ) {
206+ if ( ( Zone . current as any ) [ fetchTaskScheduling ] === true ) {
207+ // a fetch is scheduling, so we are using xhr to polyfill fetch
208+ // and because we already schedule macroTask for fetch, we should
209+ // not schedule a macroTask for xhr again
210+ return sendNative ! . apply ( self , args ) ;
211+ }
204212if ( self [ XHR_SYNC ] ) {
205213// if the XHR is sync there is no task to schedule, just execute the code.
206214return sendNative ! . apply ( self , args ) ;
@@ -212,22 +220,26 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType) => {
212220}
213221} ) ;
214222
215- const abortNative = patchMethod ( XMLHttpRequestPrototype , 'abort' , ( ) => function ( self : any ) {
216- const task : Task = findPendingTask ( self ) ;
217- if ( task && typeof task . type == 'string' ) {
218- // If the XHR has already completed, do nothing.
219- // If the XHR has already been aborted, do nothing.
220- // Fix #569, call abort multiple times before done will cause
221- // macroTask task count be negative number
222- if ( task . cancelFn == null || ( task . data && ( < XHROptions > task . data ) . aborted ) ) {
223- return ;
224- }
225- task . zone . cancelTask ( task ) ;
226- }
227- // Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
228- // task
229- // to cancel. Do nothing.
230- } ) ;
223+ const abortNative =
224+ patchMethod ( XMLHttpRequestPrototype , 'abort' , ( ) => function ( self : any , args : any [ ] ) {
225+ const task : Task = findPendingTask ( self ) ;
226+ if ( task && typeof task . type == 'string' ) {
227+ // If the XHR has already completed, do nothing.
228+ // If the XHR has already been aborted, do nothing.
229+ // Fix #569, call abort multiple times before done will cause
230+ // macroTask task count be negative number
231+ if ( task . cancelFn == null || ( task . data && ( < XHROptions > task . data ) . aborted ) ) {
232+ return ;
233+ }
234+ task . zone . cancelTask ( task ) ;
235+ } else if ( ( Zone . current as any ) [ fetchTaskAborting ] === true ) {
236+ // the abort is called from fetch polyfill, we need to call native abort of XHR.
237+ return abortNative ! . apply ( self , args ) ;
238+ }
239+ // Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
240+ // task
241+ // to cancel. Do nothing.
242+ } ) ;
231243}
232244} ) ;
233245
0 commit comments