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:
arturovt
2025-05-22 00:54:56 +03:00
committed by kirjs
parent 6e1df54799
commit 080b3687d3
+3 -1
View File
@@ -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();