Skip to content

Commit 2c876b4

Browse files
committed
fix(common): avoid injecting ApplicationRef in FetchBackend (#61649)
fixes a circular dependency caused by injecting applicationRef fixes #61644 PR Close #61649
1 parent a6d5479 commit 2c876b4

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

goldens/public-api/common/http/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { WritableResource } from '@angular/core';
1919

2020
// @public
2121
export class FetchBackend implements HttpBackend {
22+
constructor();
2223
// (undocumented)
2324
handle(request: HttpRequest<any>): Observable<HttpEvent<any>>;
2425
// (undocumented)

packages/common/http/src/fetch.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ApplicationRef, inject, Injectable, InjectionToken, NgZone} from '@angular/core';
9+
import {DestroyRef, inject, Injectable, InjectionToken, NgZone} from '@angular/core';
1010
import {Observable, Observer} from 'rxjs';
1111

1212
import {HttpBackend} from './backend';
@@ -73,7 +73,14 @@ export class FetchBackend implements HttpBackend {
7373
private readonly fetchImpl =
7474
inject(FetchFactory, {optional: true})?.fetch ?? ((...args) => globalThis.fetch(...args));
7575
private readonly ngZone = inject(NgZone);
76-
private readonly appRef = inject(ApplicationRef);
76+
private readonly destroyRef = inject(DestroyRef);
77+
private destroyed = false;
78+
79+
constructor() {
80+
this.destroyRef.onDestroy(() => {
81+
this.destroyed = true;
82+
});
83+
}
7784

7885
handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
7986
return new Observable((observer) => {
@@ -159,7 +166,7 @@ export class FetchBackend implements HttpBackend {
159166
// unnecessary work or triggering side effects after teardown.
160167
// This may happen if the app was explicitly destroyed before
161168
// the response returned entirely.
162-
if (this.appRef.destroyed) {
169+
if (this.destroyed) {
163170
// Streams left in a pending state (due to `break` without cancel) may
164171
// continue consuming or holding onto data behind the scenes.
165172
// Calling `reader.cancel()` allows the browser or the underlying

0 commit comments

Comments
 (0)