Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular async lazy rendering with RxJS

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:

  • initially you don't have see any item in the page
  • items are loaded one by one every 1ms, not in batch

The above codes are not tested, if needed I can provide a sample

like image 290
Sabri Aziri Avatar asked Jul 03 '20 19:07

Sabri Aziri


People also ask

What is rxjs and why is it dangerous?

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!

What is the use of combinelatest in RxJS?

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]

What is HTTP caching in RxJS?

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 ().

What is sharereplay () in RxJS caching?

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 ).


1 Answers

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>
like image 183
hanan Avatar answered Sep 28 '22 04:09

hanan