]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.ts
import 15.2.2 octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / rbd-mirroring.service.ts
index d32b09487e867db48dcb383bf0c751b08789a446..c1f069323be2480efcd39cd5380acb08679b7e95 100644 (file)
@@ -1,9 +1,10 @@
 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
@@ -11,32 +12,32 @@ import { ApiModule } from './api.module';
   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);
+    };
   }
 
   /**
@@ -48,7 +49,7 @@ export class RbdMirroringService {
 
   /**
    * 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);