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

Skip to content

Commit e6727bf

Browse files
committed
fix(template): wait for scroll until container is init. fixes #1779
1 parent 6526a6a commit e6727bf

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
merge,
1515
MonoTypeOperatorFunction,
1616
Observable,
17+
of,
1718
pairwise,
1819
ReplaySubject,
1920
Subject,
@@ -28,6 +29,7 @@ import {
2829
mergeMap,
2930
startWith,
3031
switchMap,
32+
take,
3133
takeUntil,
3234
takeWhile,
3335
tap,
@@ -225,6 +227,14 @@ export class AutoSizeVirtualScrollStrategy<
225227
private readonly _scrolledIndex$ = new ReplaySubject<number>(1);
226228
/** @internal */
227229
readonly scrolledIndex$ = this._scrolledIndex$.pipe(distinctUntilChanged());
230+
/**
231+
* @internal
232+
* The action used to kick off the scroll process
233+
*/
234+
private scrollToTrigger$ = new Subject<{
235+
scrollTop: number;
236+
behavior?: ScrollBehavior;
237+
}>();
228238
/** @internal */
229239
private _scrolledIndex = 0;
230240
/** @internal */
@@ -332,6 +342,7 @@ export class AutoSizeVirtualScrollStrategy<
332342
this.maintainVirtualItems();
333343
this.calcRenderedRange();
334344
this.positionElements();
345+
this.listenToScrollTrigger();
335346
}
336347

337348
/** @internal */
@@ -352,7 +363,7 @@ export class AutoSizeVirtualScrollStrategy<
352363
if (_index !== this.scrolledIndex) {
353364
const scrollTop = this.calcInitialPosition(_index);
354365
this._scrollToIndex = _index;
355-
this.scrollTo(scrollTop, behavior);
366+
this.scrollToTrigger$.next({ scrollTop, behavior });
356367
}
357368
}
358369

@@ -796,6 +807,25 @@ export class AutoSizeVirtualScrollStrategy<
796807
.subscribe();
797808
}
798809

810+
/** listen to API initiated scroll triggers (e.g. initialScrollIndex) */
811+
private listenToScrollTrigger(): void {
812+
this.scrollToTrigger$
813+
.pipe(
814+
switchMap((scrollTo) =>
815+
// wait until containerRect at least emitted once
816+
this.containerSize === 0
817+
? this.viewport!.containerRect$.pipe(
818+
map(() => scrollTo),
819+
take(1),
820+
)
821+
: of(scrollTo),
822+
),
823+
this.until$(),
824+
)
825+
.subscribe(({ scrollTop, behavior }) => {
826+
this.scrollTo(scrollTop, behavior);
827+
});
828+
}
799829
/** @internal */
800830
private adjustContentSize(position: number) {
801831
let newContentSize = position;

0 commit comments

Comments
 (0)