From f53475a204003cc6a7d8ceaa33732b2a5d4ea0ab Mon Sep 17 00:00:00 2001 From: Jesse Lucas Date: Thu, 9 Apr 2020 13:02:34 -0400 Subject: [PATCH] use replay subject in place of timer --- src/app/services/system-config.service.ts | 41 ++++------------------- 1 file changed, 6 insertions(+), 35 deletions(-) diff --git a/src/app/services/system-config.service.ts b/src/app/services/system-config.service.ts index db715e43d..e359ca75a 100644 --- a/src/app/services/system-config.service.ts +++ b/src/app/services/system-config.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@angular/core'; -import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { HttpClient } from '@angular/common/http'; -import { Observable, Subject } from 'rxjs'; +import { Observable, ReplaySubject } from 'rxjs'; import { map } from 'rxjs/operators'; import Folder from '../folder'; @@ -16,13 +16,11 @@ import { ProgressService } from './progress.service'; export class SystemConfigService { private folders: Folder[]; private devices: Device[]; - private foldersSubject: Subject = new Subject(); - private devicesSubject: Subject = new Subject(); + private foldersSubject: ReplaySubject = new ReplaySubject(1); + private devicesSubject: ReplaySubject = new ReplaySubject(1); private systemConfigUrl = environment.production ? apiURL + 'rest/system/config' : 'api/config'; - private checkInterval: number = 100; - constructor( private http: HttpClient, private progressService: ProgressService, @@ -48,37 +46,10 @@ export class SystemConfigService { } getFolders(): Observable { - const folderObservable: Observable = new Observable((observer) => { - if (this.folders) { - observer.next(this.folders); - observer.complete(); - } else { - // create timer to keep checking for folders - let t = setInterval(() => { - if (this.folders) { - clearInterval(t); - observer.next(this.folders) - observer.complete(); - } - }, this.checkInterval); - } - }); - return folderObservable; + return this.foldersSubject.asObservable(); } getDevices(): Observable { - const deviceObserverable: Observable = new Observable((observer) => { - if (this.devices) { - observer.next(this.devices); - } else { - let t = setInterval(() => { - if (this.devices) { - clearInterval(t); - observer.next(this.devices); - } - }, this.checkInterval); - } - }); - return deviceObserverable; + return this.devicesSubject.asObservable(); } } \ No newline at end of file