Skip to content

Commit f371641

Browse files
Remove rxjs from http in devextreme-angular (#32798)
1 parent 43e2218 commit f371641

1 file changed

Lines changed: 55 additions & 17 deletions

File tree

  • packages/devextreme-angular/src/http

packages/devextreme-angular/src/http/ajax.ts

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import {
2-
HttpClient, HttpEventType, HttpParams, HttpEvent, HttpErrorResponse, HttpResponse,
2+
HttpClient,
3+
HttpEventType,
4+
HttpParams,
5+
HttpEvent,
6+
HttpErrorResponse,
7+
HttpResponse,
8+
HttpHeaders,
39
} from '@angular/common/http';
4-
import { throwError, Subject } from 'rxjs';
5-
import { takeUntil, timeoutWith } from 'rxjs/operators';
610
import { Deferred, DeferredObj } from 'devextreme/core/utils/deferred';
711
import { isDefined } from 'devextreme/core/utils/type';
812
import { getWindow } from 'devextreme/core/utils/window';
@@ -33,6 +37,10 @@ interface XHRSurrogate {
3337
statusText?: string;
3438
}
3539

40+
interface SubscriptionLike {
41+
unsubscribe: () => void;
42+
}
43+
3644
const PARSER_ERROR = 'parsererror';
3745
const SUCCESS = 'success';
3846
const ERROR = 'error';
@@ -206,7 +214,9 @@ function getUploadCallbacks(options: Options, deferred: DeferredResult, xhrSurro
206214
}
207215

208216
export const sendRequestFactory = (httpClient: HttpClient) => (options: Options) => {
209-
const abort$ = new Subject<void>();
217+
let subscription: SubscriptionLike | null = null;
218+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
219+
210220
const deferred: DeferredResult = Deferred();
211221
const result = deferred.promise() as Result;
212222
const isGet = isGetMethod(options);
@@ -216,13 +226,23 @@ export const sendRequestFactory = (httpClient: HttpClient) => (options: Options)
216226
options.crossDomain = isCrossDomain(options.url);
217227
options.cache = isCacheNeed(options);
218228

229+
const clearTimeoutIfSet = () => {
230+
if (timeoutId !== null) {
231+
clearTimeout(timeoutId);
232+
timeoutId = null;
233+
}
234+
};
235+
219236
const headers = getRequestHeaders(options);
220237
const xhrSurrogate: XHRSurrogate = {
221238
type: 'XMLHttpRequestSurrogate',
222239
aborted: false,
223240
abort() {
224241
this.aborted = true;
225-
abort$.next();
242+
clearTimeoutIfSet();
243+
subscription?.unsubscribe();
244+
subscription = null;
245+
rejectIfAborted(deferred, this, () => options.upload?.onabort?.(this));
226246
},
227247
};
228248

@@ -276,18 +296,36 @@ export const sendRequestFactory = (httpClient: HttpClient) => (options: Options)
276296
},
277297
);
278298

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-
);
299+
const callbacks = upload
300+
? getUploadCallbacks(options, deferred, xhrSurrogate)
301+
: getRequestCallbacks(options, deferred, xhrSurrogate);
302+
303+
if (options.timeout) {
304+
timeoutId = setTimeout(() => {
305+
timeoutId = null;
306+
subscription?.unsubscribe();
307+
subscription = null;
308+
const timeoutError = {
309+
statusText: TIMEOUT,
310+
status: 0,
311+
ok: false,
312+
headers: new HttpHeaders(),
313+
} as HttpErrorResponse;
314+
callbacks.error(timeoutError);
315+
}, options.timeout);
316+
}
317+
318+
subscription = request.subscribe({
319+
next(value) {
320+
clearTimeoutIfSet();
321+
callbacks.next(value);
322+
},
323+
error(err) {
324+
clearTimeoutIfSet();
325+
callbacks.error(err);
326+
},
327+
complete: callbacks.complete,
328+
});
291329

292330
return result;
293331
};

0 commit comments

Comments
 (0)