You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Nothing/src/app/_services/config.service.ts

75 lines
2.1 KiB

import { Injectable } from '@angular/core';
import { Config } from '../_models/generated/Config';
import { app, webviewWindow as appWindow} from '@tauri-apps/api';
import { Subject, debounceTime, from } from 'rxjs';
import { EventsService } from './events.service';
// import { appWindow, WebviewWindow } from 'webviewWindow'
import { invoke } from '@tauri-apps/api/core';
@Injectable({
providedIn: 'root'
})
export class ConfigService {
private cfg!: Config;
private proxied!: Config;
private syncSubject: Subject<void> = new Subject<void>();
constructor(events: EventsService) {
this.syncSubject.pipe(debounceTime(100)).subscribe(() => this.underlyingSync());
events.listen<Config>("config").subscribe(cfg => Object.assign(this.cfg, cfg.payload));
}
get config() {
return this.proxied;
}
init() {
return invoke<Config>('load_config').then(cfg => {
if (!cfg) {
console.error("Could not load config, should generally not happen :')");
return;
}
const _this = this;
this.cfg = cfg;
// Mostly to wrap setters so we can run the (debounced) sync function on any config changes!
this.proxied = new Proxy(this.cfg, {
get(target: any, property) {
return target[property as keyof Config];
},
set(target: Config, property: any, value) {
setConfig(target, property, value);
_this.sync();
return true;
}
});
});
}
sync() {
let target = "Normal"
if (appWindow.getCurrent().label == "Normal") {
target = "Overlay";
}
const targetWindow = appWindow.WebviewWindow.getByLabel(target);
if (targetWindow) {
from(targetWindow.isVisible()).subscribe(visible => {
if (visible) {
targetWindow?.emit("config", this.cfg);
}
});
}
this.syncSubject.next();
}
private underlyingSync() {
invoke('update_config', { config: this.config });
}
}
function setConfig<T extends keyof Config>(target: Config, property: T, value: Config[T]) {
target[property] = value;
}