});
it('should call refresh', fakeAsync(() => {
+ summaryService.enablePolling();
authStorageService.set('foobar', undefined, undefined);
const calledWith = [];
summaryService.subscribe((data) => {
expect(calledWith).toEqual([summary, summary]);
tick(10000);
expect(calledWith.length).toEqual(4);
- // In order to not trigger setTimeout again,
+ // In order to not trigger setInterval again,
// which would raise 'Error: 1 timer(s) still in the queue.'
- spyOn(summaryService, 'refresh').and.callFake(() => true);
- tick(5000);
+ window.clearInterval(summaryService.polling);
}));
describe('Should test methods after first refresh', () => {
// Observable streams
summaryData$ = this.summaryDataSource.asObservable();
+ polling: number;
+
constructor(private http: HttpClient, private router: Router, private ngZone: NgZone) {
- this.refresh();
+ this.enablePolling();
}
- refresh() {
- if (this.router.url !== '/login') {
- this.http.get('api/summary').subscribe((data) => {
- this.summaryDataSource.next(data);
- });
- }
+ enablePolling() {
+ this.refresh();
this.ngZone.runOutsideAngular(() => {
- setTimeout(() => {
+ this.polling = window.setInterval(() => {
this.ngZone.run(() => {
this.refresh();
});
});
}
+ refresh() {
+ if (this.router.url !== '/login') {
+ this.http.get('api/summary').subscribe((data) => {
+ this.summaryDataSource.next(data);
+ });
+ }
+ }
+
/**
* Returns the current value of summaryData
*/