import { HttpClient } from '@angular/common/http';
-import { Injectable, NgZone } from '@angular/core';
+import { Injectable } from '@angular/core';
-import { BehaviorSubject, Subscription } from 'rxjs';
+import { BehaviorSubject, Observable, Subscription } from 'rxjs';
import { cdEncode, cdEncodeNot } from '../decorators/cd-encode';
+import { TimerService } from '../services/timer.service';
import { ApiModule } from './api.module';
@cdEncode
providedIn: ApiModule
})
export class RbdMirroringService {
+ readonly REFRESH_INTERVAL = 30000;
// Observable sources
private summaryDataSource = new BehaviorSubject(null);
-
// Observable streams
summaryData$ = this.summaryDataSource.asObservable();
- constructor(private http: HttpClient, private ngZone: NgZone) {
- this.refreshAndSchedule();
+ constructor(private http: HttpClient, private timerService: TimerService) {}
+
+ startPolling(): Subscription {
+ return this.timerService
+ .get(() => this.retrieveSummaryObservable(), this.REFRESH_INTERVAL)
+ .subscribe(this.retrieveSummaryObserver());
}
- refresh() {
- this.http.get('api/block/mirroring/summary').subscribe((data) => {
- this.summaryDataSource.next(data);
- });
+ refresh(): Subscription {
+ return this.retrieveSummaryObservable().subscribe(this.retrieveSummaryObserver());
}
- refreshAndSchedule() {
- this.refresh();
+ private retrieveSummaryObservable(): Observable<Object> {
+ return this.http.get('api/block/mirroring/summary');
+ }
- this.ngZone.runOutsideAngular(() => {
- setTimeout(() => {
- this.ngZone.run(() => {
- this.refreshAndSchedule();
- });
- }, 30000);
- });
+ private retrieveSummaryObserver(): (data: any) => void {
+ return (data: any) => {
+ this.summaryDataSource.next(data);
+ };
}
/**
/**
* Subscribes to the summaryData,
- * which is updated once every 30 seconds or when a new task is created.
+ * which is updated periodically or when a new task is created.
*/
subscribeSummary(next: (summary: any) => void, error?: (error: any) => void): Subscription {
return this.summaryData$.subscribe(next, error);