From 3eab11752072f5c6cccba4740ed6f6b52ffd3ff1 Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Fri, 11 Jan 2019 11:08:01 -0100 Subject: [PATCH] mgr/dashboard: Fix summary refresh call stack Currently each time we created a task it would call the summary refresh method. Since that method was a recursive method, overtime we would get N calls to the refresh each 5 seconds. Signed-off-by: Tiago Melo --- .../shared/services/summary.service.spec.ts | 6 ++--- .../app/shared/services/summary.service.ts | 22 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts index 86ea4e108976d..d4439416d9873 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts @@ -47,6 +47,7 @@ describe('SummaryService', () => { }); it('should call refresh', fakeAsync(() => { + summaryService.enablePolling(); authStorageService.set('foobar', undefined, undefined); const calledWith = []; summaryService.subscribe((data) => { @@ -57,10 +58,9 @@ describe('SummaryService', () => { 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', () => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts index a4397f85cba18..0f81659c7c7b4 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts @@ -18,19 +18,17 @@ export class SummaryService { // 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(); }); @@ -38,6 +36,14 @@ export class SummaryService { }); } + refresh() { + if (this.router.url !== '/login') { + this.http.get('api/summary').subscribe((data) => { + this.summaryDataSource.next(data); + }); + } + } + /** * Returns the current value of summaryData */ -- 2.39.5