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

97 lines
2.7 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
import { Observable, Subject, of } from 'rxjs';
import { map } from 'rxjs/operators';
2020-03-11 03:51:50 +01:00
import { Folder } from './folder';
import { Device } from './device';
import { FOLDERS, DEVICES } from './mock-config-data';
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 systemConfig: any;
private folders: Folder[];
private devices: Device[];
private foldersSubject: Subject<Folder[]> = new Subject();
private devicesSubject: Subject<Device[]> = new Subject();
private systemConfigUrl = environment.production ? apiURL + 'rest/system/config' : 'api/config';
2020-03-11 03:51:50 +01:00
2020-03-15 17:33:21 +01:00
private httpOptions;
constructor(private http: HttpClient, private cookieService: CookieService) {
2020-03-15 17:33:21 +01:00
this.httpOptions = { headers: new HttpHeaders(this.cookieService.getCSRFHeader()) };
}
ngOnInit(): void { }
getSystemConfig(): Observable<any> {
return this.http
.get(this.systemConfigUrl, this.httpOptions)
.pipe(map(res => {
this.systemConfig = 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 checkFolders = (): Boolean => {
if (this.folders) {
observer.next(this.folders);
return true;
}
return false;
}
let t = setInterval(() => {
if (checkFolders())
clearInterval(t);
}, 100);
}
});
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 {
// create timer to keep checking for devices
let checkFolders = (): Boolean => {
if (this.devices) {
observer.next(this.devices);
return true;
}
return false;
}
let t = setInterval(() => {
if (checkFolders())
clearInterval(t);
}, 100);
}
});
return deviceObserverable;
// return from(this.devices);
if (this.devices) {
this.devicesSubject.next(this.devices);
}
return this.devicesSubject;
}
}