Skip to content

Commit

Permalink
fix(cdk/collections): recycle repeater strategy retaining references …
Browse files Browse the repository at this point in the history
…to destroyed views (#21744)

When the `_RecycleViewRepeaterStrategy` is destroyed, it destroys all views in its cache, but it
still retains references to them. These changes clear the references and do some minor internal
cleanup.
  • Loading branch information
crisbeto authored Feb 1, 2021
1 parent ccf2b60 commit 42361c0
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/cdk/collections/recycle-view-repeater-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte
for (const view of this._viewCache) {
view.destroy();
}
this._viewCache = [];
}

/**
Expand All @@ -101,7 +102,7 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte
private _insertView(viewArgsFactory: () => _ViewRepeaterItemInsertArgs<C>, currentIndex: number,
viewContainerRef: ViewContainerRef,
value: T): EmbeddedViewRef<C> | undefined {
let cachedView = this._insertViewFromCache(currentIndex!, viewContainerRef);
const cachedView = this._insertViewFromCache(currentIndex!, viewContainerRef);
if (cachedView) {
cachedView.context.$implicit = value;
return undefined;
Expand All @@ -114,15 +115,14 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte

/** Detaches the view at the given index and inserts into the view cache. */
private _detachAndCacheView(index: number, viewContainerRef: ViewContainerRef) {
const detachedView = this._detachView(index, viewContainerRef);
const detachedView = viewContainerRef.detach(index) as EmbeddedViewRef<C>;
this._maybeCacheView(detachedView, viewContainerRef);
}

/** Moves view at the previous index to the current index. */
private _moveView(adjustedPreviousIndex: number, currentIndex: number,
viewContainerRef: ViewContainerRef, value: T): EmbeddedViewRef<C> {
const view = viewContainerRef.get(adjustedPreviousIndex!) as
EmbeddedViewRef<C>;
const view = viewContainerRef.get(adjustedPreviousIndex!) as EmbeddedViewRef<C>;
viewContainerRef.move(view, currentIndex);
view.context.$implicit = value;
return view;
Expand Down Expand Up @@ -159,9 +159,4 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte
}
return cachedView || null;
}

/** Detaches the embedded view at the given index. */
private _detachView(index: number, viewContainerRef: ViewContainerRef): EmbeddedViewRef<C> {
return viewContainerRef.detach(index) as EmbeddedViewRef<C>;
}
}

0 comments on commit 42361c0

Please sign in to comment.