Skip to content

Commit ddd22be

Browse files
arturovtkirjs
authored andcommitted
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
1 parent 41b1039 commit ddd22be

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)