syncthing/src/app/system-config.service.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-03-11 03:51:50 +01:00
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
2020-03-11 03:51:50 +01:00
2020-03-17 14:46:32 +01:00
import { Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
2020-03-11 03:51:50 +01:00
import { Folder } from './folder';
import { Device } from './device';
import { CookieService } from './cookie.service';
2020-03-15 17:33:21 +01:00
import { environment } from '../environments/environment'
import { apiURL } from './api-utils'
2020-03-11 03:51:50 +01:00
@Injectable({
providedIn: 'root'
})
export class SystemConfigService {
private folders: Folder[];
private devices: Device[];
private foldersSubject: Subject<Folder[]> = new Subject();
private devicesSubject: Subject<Device[]> = new Subject();
2020-03-11 03:51:50 +01:00
2020-03-15 22:00:56 +01:00
private systemConfigUrl = environment.production ? apiURL + 'rest/system/config' : 'api/config';
2020-03-17 14:46:32 +01:00
private httpOptions: any;
private checkInterval: number = 100;
2020-03-15 22:00:56 +01:00
constructor(private http: HttpClient, private cookieService: CookieService) {
this.httpOptions = { headers: new HttpHeaders(this.cookieService.getCSRFHeader()) };
2020-03-15 17:33:21 +01:00
}
getSystemConfig(): Observable<any> {
return this.http
.get(this.systemConfigUrl, this.httpOptions)
.pipe(map(res => {
this.folders = res['folders'];
this.devices = res['devices'];
this.foldersSubject.next(this.folders);
this.devicesSubject.next(this.devices);
return res;
})
);
}
2020-03-11 03:51:50 +01:00
getFolders(): Observable<Folder[]> {
const folderObservable: Observable<Folder[]> = new Observable((observer) => {
if (this.folders) {
observer.next(this.folders);
} else {
// create timer to keep checking for folders
let t = setInterval(() => {
if (this.folders) {
clearInterval(t);
observer.next(this.folders);
}
}, this.checkInterval);
}
});
return folderObservable;
2020-03-11 03:51:50 +01:00
}
getDevices(): Observable<Device[]> {
const deviceObserverable: Observable<Device[]> = new Observable((observer) => {
if (this.folders) {
observer.next(this.devices);
} else {
let t = setInterval(() => {
if (this.devices) {
clearInterval(t);
2020-03-16 03:07:11 +01:00
observer.next(this.devices);
}
}, this.checkInterval);
}
});
return deviceObserverable;
}
}