Skip to content

Commit

Permalink
fix(cdk/scrolling): unsubscribe from scrolled stream when viewport is…
Browse files Browse the repository at this point in the history
… destroyed (#27800)

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.

(cherry picked from commit 31187ab)
  • Loading branch information
crisbeto committed Sep 18, 2023
1 parent a79fae4 commit 80acf79
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cdk/scrolling/virtual-scroll-viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,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());

Expand Down

0 comments on commit 80acf79

Please sign in to comment.