From e81ea0c3dd67fc86c7203de65896c3119813fe4c Mon Sep 17 00:00:00 2001 From: arturovt Date: Thu, 22 May 2025 15:08:47 +0300 Subject: [PATCH] fix(core): unregister `onDestroy` in `outputToObservable` (#61882) We should remove the `onDestroy` listener once subscription is unsubscribed because components might not be destroyed yet, but they still would capture subscribers. PR Close #61882 --- packages/core/rxjs-interop/src/output_to_observable.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/core/rxjs-interop/src/output_to_observable.ts b/packages/core/rxjs-interop/src/output_to_observable.ts index 9512020ca69..0a20df21776 100644 --- a/packages/core/rxjs-interop/src/output_to_observable.ts +++ b/packages/core/rxjs-interop/src/output_to_observable.ts @@ -24,9 +24,12 @@ export function outputToObservable(ref: OutputRef): Observable { // Complete the observable upon directive/component destroy. // Note: May be `undefined` if an `EventEmitter` is declared outside // of an injection context. - destroyRef?.onDestroy(() => observer.complete()); + const unregisterOnDestroy = destroyRef?.onDestroy(() => observer.complete()); const subscription = ref.subscribe((v) => observer.next(v)); - return () => subscription.unsubscribe(); + return () => { + subscription.unsubscribe(); + unregisterOnDestroy?.(); + }; }); }