|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {ApplicationRef, inject, Injectable, InjectionToken, NgZone} from '@angular/core'; |
| 9 | +import {DestroyRef, inject, Injectable, InjectionToken, NgZone} from '@angular/core'; |
10 | 10 | import {Observable, Observer} from 'rxjs'; |
11 | 11 |
|
12 | 12 | import {HttpBackend} from './backend'; |
@@ -73,7 +73,14 @@ export class FetchBackend implements HttpBackend { |
73 | 73 | private readonly fetchImpl = |
74 | 74 | inject(FetchFactory, {optional: true})?.fetch ?? ((...args) => globalThis.fetch(...args)); |
75 | 75 | 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 | + } |
77 | 84 |
|
78 | 85 | handle(request: HttpRequest<any>): Observable<HttpEvent<any>> { |
79 | 86 | return new Observable((observer) => { |
@@ -159,7 +166,7 @@ export class FetchBackend implements HttpBackend { |
159 | 166 | // unnecessary work or triggering side effects after teardown. |
160 | 167 | // This may happen if the app was explicitly destroyed before |
161 | 168 | // the response returned entirely. |
162 | | - if (this.appRef.destroyed) { |
| 169 | + if (this.destroyed) { |
163 | 170 | // Streams left in a pending state (due to `break` without cancel) may |
164 | 171 | // continue consuming or holding onto data behind the scenes. |
165 | 172 | // Calling `reader.cancel()` allows the browser or the underlying |
|
0 commit comments