From 2d979684453659318daf794febafb0887a12fb50 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Sat, 22 Feb 2025 20:35:20 +1100 Subject: config: dynamic service confs --- config.ts | 20 +++++++++---------- src/services/cpu.ts | 6 +++++- src/services/gpu.ts | 8 +++++++- src/services/math.ts | 7 +++++-- src/services/memory.ts | 6 +++++- src/services/storage.ts | 6 +++++- src/services/updates.ts | 7 ++++++- src/services/weather.ts | 51 +++++++++++++++++++++++++++++++++++++------------ 8 files changed, 82 insertions(+), 29 deletions(-) diff --git a/config.ts b/config.ts index 8d9c2a8..8127052 100644 --- a/config.ts +++ b/config.ts @@ -138,28 +138,28 @@ const config = { }, // Services math: { - maxHistory: 100, + maxHistory: s(100), }, updates: { - interval: 900000, + interval: s(900000), }, weather: { - interval: 600000, - key: "assets/weather-api-key.txt", // Path to file containing api key relative to the base directory. To get a key, visit https://weatherapi.com/ - location: "", // Location as a string or empty to autodetect - imperial: false, + interval: s(600000), + key: s("assets/weather-api-key.txt"), // Path to file containing api key relative to the base directory. To get a key, visit https://weatherapi.com/ + location: s(""), // Location as a string or empty to autodetect + imperial: s(false), }, cpu: { - interval: 2000, + interval: s(2000), }, gpu: { - interval: 2000, + interval: s(2000), }, memory: { - interval: 5000, + interval: s(5000), }, storage: { - interval: 5000, + interval: s(5000), }, }; diff --git a/src/services/cpu.ts b/src/services/cpu.ts index f1699f7..5f80d11 100644 --- a/src/services/cpu.ts +++ b/src/services/cpu.ts @@ -40,6 +40,10 @@ export default class Cpu extends GObject.Object { constructor() { super(); - interval(config.interval, () => this.update()); + let source = interval(config.interval.get(), () => this.update()); + config.interval.subscribe(i => { + source.cancel(); + source = interval(i, () => this.update()); + }); } } diff --git a/src/services/gpu.ts b/src/services/gpu.ts index 179f14e..5ac2d8d 100644 --- a/src/services/gpu.ts +++ b/src/services/gpu.ts @@ -52,6 +52,12 @@ export default class Gpu extends GObject.Object { } } - if (this.available) interval(config.interval, () => this.update()); + if (this.available) { + let source = interval(config.interval.get(), () => this.update()); + config.interval.subscribe(i => { + source.cancel(); + source = interval(i, () => this.update()); + }); + } } } diff --git a/src/services/math.ts b/src/services/math.ts index 5c7dfce..f5807ef 100644 --- a/src/services/math.ts +++ b/src/services/math.ts @@ -17,7 +17,6 @@ export default class Math extends GObject.Object { return this.instance; } - readonly #maxHistory = config.maxHistory; readonly #path = `${STATE}/math-history.json`; readonly #history: HistoryItem[] = []; @@ -42,7 +41,7 @@ export default class Math extends GObject.Object { // Try select first to prevent duplicates, if it fails, add it if (!this.select(this.#lastExpression)) { this.#history.unshift(this.#lastExpression); - if (this.#history.length > this.#maxHistory) this.#history.pop(); + while (this.#history.length > config.maxHistory.get()) this.#history.pop(); this.notify("history"); this.#save(); } @@ -148,5 +147,9 @@ export default class Math extends GObject.Object { console.error("Math - Unable to load history", e); } } + + config.maxHistory.subscribe(n => { + while (this.#history.length > n) this.#history.pop(); + }); } } diff --git a/src/services/memory.ts b/src/services/memory.ts index 74fa228..b1231b9 100644 --- a/src/services/memory.ts +++ b/src/services/memory.ts @@ -55,6 +55,10 @@ export default class Memory extends GObject.Object { constructor() { super(); - interval(config.interval, () => this.update().catch(console.error)); + let source = interval(config.interval.get(), () => this.update().catch(console.error)); + config.interval.subscribe(i => { + source.cancel(); + source = interval(i, () => this.update().catch(console.error)); + }); } } diff --git a/src/services/storage.ts b/src/services/storage.ts index e1e1c55..3f8992d 100644 --- a/src/services/storage.ts +++ b/src/services/storage.ts @@ -56,6 +56,10 @@ export default class Storage extends GObject.Object { constructor() { super(); - interval(config.interval, () => this.update()); + let source = interval(config.interval.get(), () => this.update()); + config.interval.subscribe(i => { + source.cancel(); + source = interval(i, () => this.update()); + }); } } diff --git a/src/services/updates.ts b/src/services/updates.ts index 9c4abe1..06e86b1 100644 --- a/src/services/updates.ts +++ b/src/services/updates.ts @@ -151,7 +151,7 @@ export default class Updates extends GObject.Object { this.notify("loading"); this.#timeout?.destroy(); - this.#timeout = setTimeout(() => this.getUpdates(), config.interval); + this.#timeout = setTimeout(() => this.getUpdates(), config.interval.get()); }) .catch(console.error); } @@ -164,5 +164,10 @@ export default class Updates extends GObject.Object { writeFileAsync(this.#cachePath, JSON.stringify(this.#data)).catch(console.error) ); this.getUpdates(); + + config.interval.subscribe(i => { + this.#timeout?.destroy(); + this.#timeout = setTimeout(() => this.getUpdates(), i); + }); } } diff --git a/src/services/weather.ts b/src/services/weather.ts index c3d5b3c..1d9a2dd 100644 --- a/src/services/weather.ts +++ b/src/services/weather.ts @@ -1,6 +1,16 @@ import { weatherIcons } from "@/utils/icons"; import { notify } from "@/utils/system"; -import { execAsync, GLib, GObject, interval, property, readFileAsync, register, writeFileAsync } from "astal"; +import { + execAsync, + GLib, + GObject, + interval, + property, + readFileAsync, + register, + writeFileAsync, + type Time, +} from "astal"; import { weather as config } from "config"; export interface WeatherCondition { @@ -211,6 +221,8 @@ export default class Weather extends GObject.Object { #key: string = ""; #data: WeatherData = DEFAULT; + #interval: Time | null = null; + @property(Object) get raw() { return this.#data; @@ -243,8 +255,8 @@ export default class Weather extends GObject.Object { @property(String) get wind() { - return `${Math.round(this.#data.current[`wind_${config.imperial ? "m" : "k"}ph`])} ${ - config.imperial ? "m" : "k" + return `${Math.round(this.#data.current[`wind_${config.imperial.get() ? "m" : "k"}ph`])} ${ + config.imperial.get() ? "m" : "k" }ph`; } @@ -275,7 +287,7 @@ export default class Weather extends GObject.Object { } getTemp(data: _WeatherState) { - return `${Math.round(data[`temp_${config.imperial ? "f" : "c"}`])}°${config.imperial ? "F" : "C"}`; + return `${Math.round(data[`temp_${config.imperial.get() ? "f" : "c"}`])}°${config.imperial.get() ? "F" : "C"}`; } getTempIcon(temp: number) { @@ -322,7 +334,7 @@ export default class Weather extends GObject.Object { if (GLib.file_test(this.#cache, GLib.FileTest.EXISTS)) { const cache = await readFileAsync(this.#cache); const cache_data: WeatherData = JSON.parse(cache); - if (cache_data.location.localtime_epoch * 1000 + config.interval > Date.now()) { + if (cache_data.location.localtime_epoch * 1000 + config.interval.get() > Date.now()) { if (JSON.stringify(this.#data) !== cache) { this.#data = cache_data; this.#notify(); @@ -342,18 +354,16 @@ export default class Weather extends GObject.Object { this.#notify(); } - constructor() { - super(); - - if (GLib.file_test(config.key, GLib.FileTest.EXISTS)) - readFileAsync(config.key) + #init(first: boolean) { + if (GLib.file_test(config.key.get(), GLib.FileTest.EXISTS)) + readFileAsync(config.key.get()) .then(k => { this.#key = k.trim(); this.updateWeather().catch(console.error); - interval(config.interval, () => this.updateWeather().catch(console.error)); + this.#interval = interval(config.interval.get(), () => this.updateWeather().catch(console.error)); }) .catch(console.error); - else + else if (first) notify({ summary: "Weather API key required", body: `A weather API key is required to get weather data. Get one from https://www.weatherapi.com and put it in ${config.key}.`, @@ -364,4 +374,21 @@ export default class Weather extends GObject.Object { }, }); } + + constructor() { + super(); + + this.#init(true); + config.key.subscribe(() => this.#init(false)); + + config.interval.subscribe(i => { + this.#interval?.cancel(); + this.#interval = interval(i, () => this.updateWeather().catch(console.error)); + }); + + config.imperial.subscribe(() => { + this.notify("temperature"); + this.notify("wind"); + }); + } } -- cgit v1.2.3-freya