11import {
22 HttpClient , HttpEventType , HttpParams , HttpEvent , HttpErrorResponse , HttpResponse ,
33} from '@angular/common/http' ;
4- import { throwError , Subject } from 'rxjs' ;
5- import { takeUntil , timeoutWith } from 'rxjs/operators' ;
64import { Deferred , DeferredObj } from 'devextreme/core/utils/deferred' ;
75import { isDefined } from 'devextreme/core/utils/type' ;
86import { getWindow } from 'devextreme/core/utils/window' ;
@@ -33,6 +31,10 @@ interface XHRSurrogate {
3331 statusText ?: string ;
3432}
3533
34+ interface SubscriptionLike {
35+ unsubscribe ( ) : void ;
36+ }
37+
3638const PARSER_ERROR = 'parsererror' ;
3739const SUCCESS = 'success' ;
3840const ERROR = 'error' ;
@@ -206,7 +208,9 @@ function getUploadCallbacks(options: Options, deferred: DeferredResult, xhrSurro
206208}
207209
208210export const sendRequestFactory = ( httpClient : HttpClient ) => ( options : Options ) => {
209- const abort$ = new Subject < void > ( ) ;
211+ let subscription : SubscriptionLike | null = null ;
212+ let timeoutId : ReturnType < typeof setTimeout > | null = null ;
213+
210214 const deferred : DeferredResult = Deferred ( ) ;
211215 const result = deferred . promise ( ) as Result ;
212216 const isGet = isGetMethod ( options ) ;
@@ -222,7 +226,12 @@ export const sendRequestFactory = (httpClient: HttpClient) => (options: Options)
222226 aborted : false ,
223227 abort ( ) {
224228 this . aborted = true ;
225- abort$ . next ( ) ;
229+ if ( timeoutId !== null ) {
230+ clearTimeout ( timeoutId ) ;
231+ timeoutId = null ;
232+ }
233+ subscription ?. unsubscribe ( ) ;
234+ subscription = null ;
226235 } ,
227236 } ;
228237
@@ -276,18 +285,37 @@ export const sendRequestFactory = (httpClient: HttpClient) => (options: Options)
276285 } ,
277286 ) ;
278287
279- const subscriptionCallbacks = upload
280- ? getUploadCallbacks
281- : getRequestCallbacks ;
282-
283- request . pipe . apply ( request , [
284- takeUntil ( abort$ ) as any ,
285- ...options . timeout
286- ? [ timeoutWith ( options . timeout , throwError ( { statusText : TIMEOUT , status : 0 , ok : false } ) ) as any ]
287- : [ ] ,
288- ] ) . subscribe (
289- subscriptionCallbacks ( options , deferred , xhrSurrogate ) ,
290- ) ;
288+ const clearTimeoutIfSet = ( ) => {
289+ if ( timeoutId !== null ) {
290+ clearTimeout ( timeoutId ) ;
291+ timeoutId = null ;
292+ }
293+ } ;
294+
295+ if ( options . timeout ) {
296+ timeoutId = setTimeout ( ( ) => {
297+ timeoutId = null ;
298+ deferred . reject ( { statusText : TIMEOUT , status : 0 , ok : false } ) ;
299+ subscription ?. unsubscribe ( ) ;
300+ subscription = null ;
301+ } , options . timeout ) ;
302+ }
303+
304+ const callbacks = upload
305+ ? getUploadCallbacks ( options , deferred , xhrSurrogate )
306+ : getRequestCallbacks ( options , deferred , xhrSurrogate ) ;
307+
308+ subscription = request . subscribe ( {
309+ next ( value ) {
310+ clearTimeoutIfSet ( ) ;
311+ callbacks . next ( value ) ;
312+ } ,
313+ error ( err ) {
314+ clearTimeoutIfSet ( ) ;
315+ callbacks . error ( err ) ;
316+ } ,
317+ complete : callbacks . complete ,
318+ } ) ;
291319
292320 return result ;
293321} ;
0 commit comments