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
This commit is contained in:
arturovt 2025-05-22 15:08:47 +03:00 committed by Pawel Kozlowski
parent eb20c1f710
commit e81ea0c3dd

View file

@ -24,9 +24,12 @@ export function outputToObservable<T>(ref: OutputRef<T>): Observable<T> {
// 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?.();
};
});
}