mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(core): unregister onDestroy in ResourceImpl when destroy() is called (#61870)
This commit unregisters the `onDestroy` listener when `destroy()` is called on the `ResourceImpl`. This prevents memory leaks and ensures that the resource reference is not captured in the destroy callback after it has already been destroyed. PR Close #61870
This commit is contained in:
@@ -167,6 +167,7 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
|
||||
private pendingController: AbortController | undefined;
|
||||
private resolvePendingTask: (() => void) | undefined = undefined;
|
||||
private destroyed = false;
|
||||
private unregisterOnDestroy: () => void;
|
||||
|
||||
constructor(
|
||||
request: () => R,
|
||||
@@ -250,7 +251,7 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
|
||||
this.pendingTasks = injector.get(PendingTasks);
|
||||
|
||||
// Cancel any pending request when the resource itself is destroyed.
|
||||
injector.get(DestroyRef).onDestroy(() => this.destroy());
|
||||
this.unregisterOnDestroy = injector.get(DestroyRef).onDestroy(() => this.destroy());
|
||||
}
|
||||
|
||||
override readonly status = computed(() => projectStatusOfState(this.state()));
|
||||
@@ -302,6 +303,7 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
|
||||
|
||||
destroy(): void {
|
||||
this.destroyed = true;
|
||||
this.unregisterOnDestroy();
|
||||
this.effectRef.destroy();
|
||||
this.abortInProgressLoad();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user