0

here's the code:

loadNextBatch() {
    console.log('scrolldown');

    this.pageIndex = this.pageIndex + 1;
    this.global.getData(`/conditions/latest?start=${this.pageIndex}&length=${this.pageSize}`)
      .pipe(take(1)).subscribe(res => {

        const newBatch = res['data'];
        if (newBatch.length === 0) {
          return false;
        }

        this.tempThermometer.next(this.tempThermometer.getValue().concat(newBatch));
      console.log(this.tempThermometer);
      });
  }

what I'm trying to do here is when there's no data from the /conditions/latest?start=9&length=4 it still running to start=11 ...etc how to make it stop. when there's no data it should be stop in start=8 because on start=9 there's no data anymore.

How to stop it from the start=8 but when there's a data on start=9 it can be load. but if there's no data on start=9 it shouldn't be loaded.

also the problem when I scroll down it continued to start=11``start=12it should be stop onstart=8cause there's no data onstart=9```

Panda
  • 365
  • 9
  • 23
  • Does this answer your question? [RxJs: Incrementally push stream of data to BehaviorSubject<\[\]>](https://stackoverflow.com/questions/44272402/rxjs-incrementally-push-stream-of-data-to-behaviorsubject) – developer033 Mar 26 '20 at 02:47
  • @developer033 it doesn't work. it will just add an item undefined – Panda Mar 26 '20 at 02:53
  • Put the next statement in else clause of newbatch=0 – JWP Mar 26 '20 at 03:47

1 Answers1

0

Defined a global variable and wrap the API call in an if condition seems logical right?

canLoadNextbatch = true;

loadNextBatch() {
  console.log('scrolldown');

  this.pageIndex = this.pageIndex + 1;
  if (this.canLoadNextbatch) {
    this.global.getData(`/conditions/latest?start=${this.pageIndex}&length=${this.pageSize}`)
      .pipe(take(1)).subscribe(res => {

        const newBatch = res['data'];
        if (newBatch.length === 0) {
          this.canLoadNextbatch = false;
        } else {
          this.tempThermometer.next(this.tempThermometer.getValue().concat(newBatch));
          console.log(this.tempThermometer);
        }
      });
  }
}
Naren Murali
  • 19,250
  • 3
  • 27
  • 54