How to restart rxjs interval in Ionic with Angular

Md Riyazuddin Verified
In this code, we will start or stop intervals with the help of takeUntil() operator and repeatWhen() operator.
import { Observable, timer, Subject, fromEvent } from 'rxjs';
import { map, takeUntil, repeatWhen } from 'rxjs/operators';

export class RepeatingServiceCall<T> {

  public  readonly observable$: Observable<T>;
  private readonly stop = new Subject<void>();
  private readonly start = new Subject<void>();

  constructor(delay: number) {
    this.observable$ = timer(0, delay)
      .pipe(
        map(() => <T>{}),
        takeUntil(this.stop),
        repeatWhen(() => this.start)
      );
  }

  startInterval(): void {
    this.start.next();
  }

  stopInterval(): void {
    this.stop.next();
  }

}


Tags

NA

Comments

Leave a Comment