2021-06-14 23:00:05 +02:00
|
|
|
import { Subject } from 'rxjs'
|
|
|
|
|
import { debounceTime } from 'rxjs/operators'
|
|
|
|
|
import { HttpClient } from '@angular/common/http'
|
|
|
|
|
import { Injectable } from '@angular/core'
|
|
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
|
export class AppConnectorService {
|
|
|
|
|
config: any
|
|
|
|
|
private configUpdate = new Subject<string>()
|
|
|
|
|
|
|
|
|
|
constructor (private http: HttpClient) {
|
2021-06-15 23:43:23 +02:00
|
|
|
this.configUpdate.pipe(debounceTime(1000)).subscribe(async content => {
|
|
|
|
|
const result = await this.http.patch(`/api/1/configs/${this.config.id}`, { content }).toPromise()
|
|
|
|
|
Object.assign(this.config, result)
|
2021-06-14 23:00:05 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async loadConfig (): Promise<string> {
|
|
|
|
|
return this.config.content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async saveConfig (content: string): Promise<void> {
|
|
|
|
|
this.configUpdate.next(content)
|
2021-06-15 23:43:23 +02:00
|
|
|
this.config.content = content
|
2021-06-14 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
}
|