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

84 lines
2.3 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
2020-03-25 14:41:22 +01:00
import Folder from '../folder';
import Device from '../device';
2020-03-25 14:41:22 +01:00
import { environment } from '../../environments/environment'
import { apiURL } from '../api-utils'
import { ProgressService } from './progress.service';
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';
private checkInterval: number = 100;
2020-03-15 22:00:56 +01:00
constructor(
private http: HttpClient,
private progressService: ProgressService,
) { }
getSystemConfig(): Observable<any> {
return this.http
.get(this.systemConfigUrl)
2020-03-18 03:36:42 +01:00
.pipe(
map(res => {
this.folders = res['folders'];
this.devices = res['devices'];
// Set the total for the progress service
this.progressService.total = this.folders.length + this.devices.length;
2020-03-18 03:36:42 +01:00
this.foldersSubject.next(this.folders);
this.devicesSubject.next(this.devices);
2020-03-18 03:36:42 +01:00
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);
2020-03-24 18:17:32 +01:00
observer.complete();
} else {
// create timer to keep checking for folders
let t = setInterval(() => {
if (this.folders) {
clearInterval(t);
2020-03-24 18:17:32 +01:00
observer.next(this.folders)
observer.complete();
}
}, this.checkInterval);
}
});
return folderObservable;
2020-03-11 03:51:50 +01:00
}
getDevices(): Observable<Device[]> {
const deviceObserverable: Observable<Device[]> = new Observable((observer) => {
if (this.devices) {
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;
}
}