diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/src/app/shared/services/timer.service.ts')
-rw-r--r-- | src/pybind/mgr/dashboard/frontend/src/app/shared/services/timer.service.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/timer.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/timer.service.ts new file mode 100644 index 000000000..716b71096 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/timer.service.ts @@ -0,0 +1,29 @@ +import { Injectable } from '@angular/core'; + +import { Observable, timer } from 'rxjs'; +import { observeOn, shareReplay, switchMap } from 'rxjs/operators'; + +import { whenPageVisible } from '../rxjs/operators/page-visibilty.operator'; +import { NgZoneSchedulerService } from './ngzone-scheduler.service'; + +@Injectable({ + providedIn: 'root' +}) +export class TimerService { + readonly DEFAULT_REFRESH_INTERVAL = 5000; + readonly DEFAULT_DUE_TIME = 0; + constructor(private ngZone: NgZoneSchedulerService) {} + + get( + next: () => Observable<any>, + refreshInterval: number = this.DEFAULT_REFRESH_INTERVAL, + dueTime: number = this.DEFAULT_DUE_TIME + ): Observable<any> { + return timer(dueTime, refreshInterval, this.ngZone.leave).pipe( + observeOn(this.ngZone.enter), + switchMap(next), + shareReplay({ refCount: true, bufferSize: 1 }), + whenPageVisible() + ); + } +} |