I am looking for away to do "lazy rendering" with RxJS in Angular, what I want to achieve is the following:
<div *ngFor="let item of items$ | async">
{{item.text}}
<div>
and in the component I have:
export class ItemsComponent implements OnInit {
public items$: Observable<Item[]>;
constructor(private setStore: SetStore){}
ngOnInit() {
const setId = 1;
this.items$ = this.setStore.sets$.pipe(map(sets => sets.find(set => set.id = 1).items));
}
}
And this works fine but when the set has +50 items, the rendering takes time and it freeze's for a second or more. I was looking for a way to do it lazy by somehow rendering first 30 items and then do load the next 30 after 500ms and so on until the list reach's its end.
Edit: I have tried this approach:
const _items$ = this.setStore.sets$.pipe(
map(sets => sets.find(set => set.id == 1).items)
);
const loadedItems = [];
_items$.subscribe(data => {
this.items$ = from(data).pipe(
concatMap(item => {
loadedItems.push(item);
return of(loadedItems).pipe(delay(1));
})
);
});
})
The above works fine in terms of lazy rendering but has some disadvantages like:
The above codes are not tested, if needed I can provide a sample
Working with RxJS is a little bit like having superpowers: your powers allow you to do extraordinary things, but they’re easy to misuse, and when that happens - it can be quite dangerous!
The RxJS operator combineLatest allows us to combine multiple observables into one observable that contains an array of all our values. We can map this array to an object and then access any of our display values on that object from the async pipe. [2]
HTTP caching simply means the browser stores local copies of web resources for faster retrieval the next time the resource is required, thus reducing the number of server calls. The aim of this article is to show how you can implement caching with only two RxJS operators: publishReplay () and refCount ().
When you search for RxJS caching, you’ll most likely find articles, forums, and discussions that recommend either shareReplay () or publishReplay () with refCount (). In short, shareReplay () is similar to publishReplay () except you need it with refCount () (source A, source B ).
You can use Virtual Scrolling with Different items sizes using ngx-ui-scroll
demo with variable height items it is quite simple to start with
<div class="viewport">
<div *uiScroll="let item of datasource">
<b>{{item.text}}</b>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With