Skip to content

Commit 080b368

Browse files
arturovtkirjs
authored andcommitted
fix(core): unregister onDestroy in ResourceImpl when destroy() is called (angular#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 angular#61870
1 parent 6e1df54 commit 080b368

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

packages/core/src/resource/resource.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
167167
private pendingController: AbortController | undefined;
168168
private resolvePendingTask: (() => void) | undefined = undefined;
169169
private destroyed = false;
170+
private unregisterOnDestroy: () => void;
170171

171172
constructor(
172173
request: () => R,
@@ -250,7 +251,7 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
250251
this.pendingTasks = injector.get(PendingTasks);
251252

252253
// Cancel any pending request when the resource itself is destroyed.
253-
injector.get(DestroyRef).onDestroy(() => this.destroy());
254+
this.unregisterOnDestroy = injector.get(DestroyRef).onDestroy(() => this.destroy());
254255
}
255256

256257
override readonly status = computed(() => projectStatusOfState(this.state()));
@@ -302,6 +303,7 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
302303

303304
destroy(): void {
304305
this.destroyed = true;
306+
this.unregisterOnDestroy();
305307
this.effectRef.destroy();
306308
this.abortInProgressLoad();
307309

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy