]>
Commit | Line | Data |
---|---|---|
1911f103 TL |
1 | import { Injectable } from '@angular/core'; |
2 | ||
3 | import { Observable, timer } from 'rxjs'; | |
4 | import { observeOn, shareReplay, switchMap } from 'rxjs/operators'; | |
5 | ||
6 | import { NgZoneSchedulerService } from './ngzone-scheduler.service'; | |
7 | ||
8 | @Injectable({ | |
9 | providedIn: 'root' | |
10 | }) | |
11 | export class TimerService { | |
12 | readonly DEFAULT_REFRESH_INTERVAL = 5000; | |
13 | readonly DEFAULT_DUE_TIME = 0; | |
14 | constructor(private ngZone: NgZoneSchedulerService) {} | |
15 | ||
16 | get( | |
17 | next: () => Observable<any>, | |
18 | refreshInterval: number = this.DEFAULT_REFRESH_INTERVAL, | |
19 | dueTime: number = this.DEFAULT_DUE_TIME | |
20 | ): Observable<any> { | |
21 | return timer(dueTime, refreshInterval, this.ngZone.leave).pipe( | |
22 | observeOn(this.ngZone.enter), | |
23 | switchMap(next), | |
24 | shareReplay({ refCount: true, bufferSize: 1 }) | |
25 | ); | |
26 | } | |
27 | } |