Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a9df89a

Browse files
committed
fix(template): adjust rendered range to value changes
the scroll strategies could run into an error when the new dataset was smaller than the currently rendered range
1 parent 13282c5 commit a9df89a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

libs/template/experimental/virtual-scrolling/src/lib/scroll-strategies/autosize-virtual-scroll-strategy.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class AutoSizeVirtualScrollStrategy<
209209
}
210210

211211
/** @internal */
212-
private readonly _renderedRange$ = new ReplaySubject<ListRange>(1);
212+
private readonly _renderedRange$ = new Subject<ListRange>();
213213
/** @internal */
214214
readonly renderedRange$ = this._renderedRange$.asObservable();
215215
/** @internal */
@@ -446,6 +446,23 @@ export class AutoSizeVirtualScrollStrategy<
446446
});
447447
}
448448
existingIds.clear();
449+
if (dataLength < this._renderedRange.end) {
450+
const rangeDiff = this._renderedRange.end - this._renderedRange.start;
451+
const anchorDiff = this.anchorItem.index - this._renderedRange.start;
452+
this._renderedRange.end = Math.min(
453+
dataLength,
454+
this._renderedRange.end
455+
);
456+
this._renderedRange.start = Math.max(
457+
0,
458+
this._renderedRange.end - rangeDiff
459+
);
460+
// this.anchorItem.offset = 0;
461+
this.anchorItem.index = Math.max(
462+
0,
463+
this._renderedRange.start + anchorDiff
464+
);
465+
}
449466
this.contentLength = dataLength;
450467
this.contentSize = size;
451468
}),

libs/template/experimental/virtual-scrolling/src/lib/virtual-for.directive.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
RxVirtualScrollStrategy,
5555
RxVirtualViewRepeater,
5656
} from './model';
57+
import { unpatchedMicroTask } from './util';
5758
import {
5859
createVirtualListTemplateManager,
5960
RxVirtualListTemplateManager,
@@ -62,6 +63,7 @@ import {
6263
DEFAULT_TEMPLATE_CACHE_SIZE,
6364
RX_VIRTUAL_SCROLL_DEFAULT_OPTIONS,
6465
} from './virtual-scroll.config';
66+
import { coalesceWith } from '@rx-angular/cdk/coalescing';
6567

6668
/**
6769
* @description Will be provided through Terser global definitions by Angular CLI
@@ -640,6 +642,10 @@ export class RxVirtualFor<T, U extends NgIterable<T> = NgIterable<T>>
640642
this.scrollStrategy.renderedRange$,
641643
this.strategyHandler.strategy$.pipe(distinctUntilChanged()),
642644
]).pipe(
645+
// move calculation to next microtask so that
646+
// scroll-strategies can adjust themselves before calculating
647+
// the new views to display
648+
coalesceWith(unpatchedMicroTask()),
643649
// map iterable to latest diff
644650
switchMap(([items, range, strategy]) => {
645651
const iterable = items.slice(range.start, range.end);

0 commit comments

Comments
 (0)