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 = new Subject(); constructor(events: EventsService) { this.syncSubject.pipe(debounceTime(100)).subscribe(() => this.underlyingSync()); events.listen("config").subscribe(cfg => Object.assign(this.cfg, cfg.payload)); } get config() { return this.proxied; } init() { return invoke('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(target: Config, property: T, value: Config[T]) { target[property] = value; }