Skip to content

Commit 42361c0

Browse files
authored
fix(cdk/collections): recycle repeater strategy retaining references 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.
1 parent ccf2b60 commit 42361c0

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

src/cdk/collections/recycle-view-repeater-strategy.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte
9292
for (const view of this._viewCache) {
9393
view.destroy();
9494
}
95+
this._viewCache = [];
9596
}
9697

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

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

121122
/** Moves view at the previous index to the current index. */
122123
private _moveView(adjustedPreviousIndex: number, currentIndex: number,
123124
viewContainerRef: ViewContainerRef, value: T): EmbeddedViewRef<C> {
124-
const view = viewContainerRef.get(adjustedPreviousIndex!) as
125-
EmbeddedViewRef<C>;
125+
const view = viewContainerRef.get(adjustedPreviousIndex!) as EmbeddedViewRef<C>;
126126
viewContainerRef.move(view, currentIndex);
127127
view.context.$implicit = value;
128128
return view;
@@ -159,9 +159,4 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte
159159
}
160160
return cachedView || null;
161161
}
162-
163-
/** Detaches the embedded view at the given index. */
164-
private _detachView(index: number, viewContainerRef: ViewContainerRef): EmbeddedViewRef<C> {
165-
return viewContainerRef.detach(index) as EmbeddedViewRef<C>;
166-
}
167162
}

0 commit comments

Comments
 (0)