MCPcopy Create free account
hub / github.com/angular/angular / handle

Method handle

packages/common/http/src/fetch.ts:84–154  ·  view source on GitHub ↗
(request: HttpRequest<any>)

Source from the content-addressed store, hash-verified

82 private readonly maxResponseSize = inject(HTTP_FETCH_MAX_RESPONSE_SIZE);
83
84 handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
85 return new Observable((observer) => {
86 if (this.destroyRef.destroyed) {
87 observer.unsubscribe();
88 return undefined;
89 }
90
91 const aborter = new AbortController();
92 let done = false;
93 const wrappedObserver: Observer<HttpEvent<any>> = {
94 next: (val) => {
95 if (this.destroyRef.destroyed) {
96 return;
97 }
98 if (val.type === HttpEventType.Response) {
99 done = true;
100 }
101 observer.next(val);
102 },
103 error: (err) => {
104 if (this.destroyRef.destroyed) {
105 return;
106 }
107 done = true;
108 observer.error(err);
109 },
110 complete: () => {
111 if (this.destroyRef.destroyed) {
112 return;
113 }
114 done = true;
115 observer.complete();
116 },
117 };
118
119 const unregisterOnDestroy = this.destroyRef.onDestroy(() => {
120 // Call `unsubscribe()` instead of `complete()` or `error()` when the
121 // injector is destroyed. This aborts the in-flight `fetch` and releases
122 // observers without triggering `EmptyError` in `firstValueFrom`/`lastValueFrom`
123 // or unhandled rejections in unawaited async lifecycle hooks.
124 observer.unsubscribe();
125 });
126
127 this.doRequest(request, aborter.signal, wrappedObserver).then(noop, (error) =>
128 wrappedObserver.error(new HttpErrorResponse({error})),
129 );
130
131 let timeoutId: ReturnType<typeof setTimeout> | undefined;
132 if (request.timeout) {
133 // TODO: Replace with AbortSignal.any([aborter.signal, AbortSignal.timeout(request.timeout)])
134 // when AbortSignal.any support is Baseline widely available (NET nov. 2026)
135 timeoutId = this.ngZone.runOutsideAngular(() =>
136 setTimeout(() => {
137 if (!aborter.signal.aborted) {
138 aborter.abort(new DOMException('signal timed out', 'TimeoutError'));
139 }
140 }, request.timeout),
141 );

Callers

nothing calls this directly

Calls 11

doRequestMethod · 0.95
setTimeoutFunction · 0.85
clearTimeoutFunction · 0.85
unsubscribeMethod · 0.65
errorMethod · 0.65
onDestroyMethod · 0.65
thenMethod · 0.65
abortMethod · 0.65
nextMethod · 0.45
completeMethod · 0.45
runOutsideAngularMethod · 0.45

Tested by

no test coverage detected