From 5a6ee715b8e055b4ad98ecdc39ec3640dbbe0b93 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 18 Sep 2023 09:01:41 +0200 Subject: [PATCH] fix(cdk/scrolling): unsubscribe from scrolled stream when viewport is destroyed Initially the virtual scroll viewport was written under the assumption that it is the scrollable and that it doesn't need to unsubscribe from its `elementScrolled` stream, because it'll be completed on destroy. This might not be the case if a `CdkVirtualScrollableElement` is provided and the viewport might be destroyed without the scrollable being destroyed. These changes add a `takeUntil` to avoid any potential leaks. Fixes #27799. --- src/cdk/scrolling/virtual-scroll-viewport.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cdk/scrolling/virtual-scroll-viewport.ts b/src/cdk/scrolling/virtual-scroll-viewport.ts index 9040272c125c..6547edd5b468 100644 --- a/src/cdk/scrolling/virtual-scroll-viewport.ts +++ b/src/cdk/scrolling/virtual-scroll-viewport.ts @@ -229,6 +229,10 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On // there are multiple scroll events in the same frame we only need to recheck // our layout once. auditTime(0, SCROLL_SCHEDULER), + // Usually `elementScrolled` is completed when the scrollable is destroyed, but + // that may not be the case if a `CdkVirtualScrollableElement` is used so we have + // to unsubscribe here just in case. + takeUntil(this._destroyed), ) .subscribe(() => this._scrollStrategy.onContentScrolled());