config: dynamic service confs
This commit is contained in:
parent
f561b77ee3
commit
2d97968445
8 changed files with 82 additions and 29 deletions
20
config.ts
20
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),
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue