Loading in chunks, vsEnd keeps triggering with endIndex === buffer.length-1
See original GitHub issueWhat am I doing wrong? Without scrolling at all, vsEnd triggers everytime pics$ is updated, and with an endIndex value equaling the length of items-1. Should I not use AsyncPipe? Does virtual-scroller
need to have a fixed pixel height instead of 100% (.h-100)?
@Component({
selector: 'app-pic-list',
templateUrl: './pic-list.component.html',
styleUrls: ['./pic-list.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class PicListComponent implements OnInit, OnDestroy {
private take = 100;
destroy$ = new Subject();
pics$: Observable<IPic[]>;
private buffer: IPic[] = [];
private loading = false;
private gotAll$ = new Subject();
@ViewChild('scroll', { static: true }) scroll: VirtualScrollerComponent;
constructor(private api: ApiService) {}
ngOnInit() {
this.pics$ = this.scroll.vsEnd.pipe(
takeUntil(this.destroy$),
takeUntil(this.gotAll$),
startWith(null),
filter(
event =>
!this.loading && (!event || event.endIndex >= this.buffer.length - 1)
),
tap(e => console.log(e, this.buffer.length)),
tap(() => (this.loading = true)),
switchMap(() =>
this.api
.get<IPic[]>('pics', {
take: this.take,
skip: this.buffer.length
})
.pipe(
tap(pics => pics.length < this.take && this.gotAll$.next()),
tap(pics => (this.buffer = this.buffer.concat(pics))),
map(() => this.buffer),
tap(() => (this.loading = false))
)
)
);
}
ngOnDestroy() {
this.destroy$.next();
}
}
<virtual-scroller #scroll [items]="pics$ | async" class="h-100">
<app-pic [pic]="pic" *ngFor="let pic of scroll.viewPortItems"></app-pic>
</virtual-scroller>
// app-pic.scss
:host { display:inline-block; height:200px; width:200px; }
// Real scroll position should be close to 0
// tap(e => console.log(e, this.buffer.length))
$event: {
endIndex: 699
endIndexWithBuffer: 699
maxScrollPosition: 0
scrollEndPosition: 28000
scrollStartPosition: 0
startIndex: 0
startIndexWithBuffer: 0
}
this.buffer.length: 700
Issue Analytics
- State:
- Created 4 years ago
- Comments:13
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, it fires infinitely for me when you reach the end. It seems to have to do with an improper or failure to round fractional values. Forgetting the exact issue, but I’m pretty sure that someplace it is comparing the current scroll position (rounded) with the scroll position not rounded. So if you end up having a difference there then it will loop infinitely.
Not infinite but a hell of a lot more than it should. It just fires a whole load of duplicate events. Last time I checked it fired around 7 or 8 times I think.
On Tue, Oct 5, 2021 at 12:56 AM Ray Suelzer @.***> wrote: