Files
custum-hyprpanel/customModules/PollVar.ts
Jas Singh 4cdda38604 Implement framework for custom modules and out of the box custom modules as well. (#213)
* Create declarative module scaffolding

* Added ram module (WIP)

* Updates to options, styling and more.

* Added function for styling custom modules.

* Added utility functions and cleaned up code

* Type and fn name updates.

* Update module utils to handle absent values.

* Added icon color in style2 that was missing.

* Linted utils.ts

* Add CPU module and update RAM module to use /proc/meminfo.

* Added disk storage module.

* Consolidate code

* Added netstat module and removed elements from systray default ignore list.

* Added keyboard layout module.

* Fix hook types and move module to customModules directory

* Added updates modules.

* Spacing updates

* Added weather module.

* Added power menu and power module in bar. Increased update default interval to 6 ours.

* Updated styling of bar buttons, made power menu label toggleable, etc.

* Consolidate code and add dynamic tooltips based on data being used.

* Make default custom mogules matugen compatible

* Update base theme

* Fix custom module background coloring

* Remove testing opacity.

* Update themes to account for new modules

* Update nix stuff for libgtop (Need someone to test this)

* Update nix

* Update fractions to multiplications

* Move styling in style directory

* Implement a polling framework for variables that can dynamically adjust polling intervals.

* Netstat module updates when interface name is changed.

* Readme update
2024-09-02 21:10:59 -07:00

77 lines
2.9 KiB
TypeScript

import GLib from "gi://GLib?version=2.0";
import { Binding } from "types/service";
import { Variable as VariableType } from "types/variable";
type GenericFunction = (...args: any[]) => any;
/**
* @param {VariableType<T>} targetVariable - The Variable to update with the function's result.
* @param {Array<VariableType<any>>} trackers - Array of trackers to watch.
* @param {Binding<any, any, unknown>} pollingInterval - The polling interval in milliseconds.
* @param {GenericFunction} someFunc - The function to execute at each interval, which updates the Variable.
* @param {...any} params - Parameters to pass to someFunc.
*/
export const pollVariable = <T>(
targetVariable: VariableType<T>,
trackers: Array<Binding<any, any, unknown>>,
pollingInterval: Binding<any, any, unknown>,
someFunc: GenericFunction,
...params: any[]
): void => {
let intervalInstance: number | null = null;
const intervalFn = (pollIntrvl: number) => {
if (intervalInstance !== null) {
GLib.source_remove(intervalInstance);
}
intervalInstance = Utils.interval(pollIntrvl, () => {
targetVariable.value = someFunc(...params);
});
};
Utils.merge([pollingInterval, ...trackers], (pollIntrvl: number) => {
intervalFn(pollIntrvl);
});
};
/**
* @param {VariableType<T>} targetVariable - The Variable to update with the result of the command.
* @param {Binding<any, any, unknown>} pollingInterval - The polling interval in milliseconds.
* @param {string} someCommand - The bash command to execute.
* @param {GenericFunction} someFunc - The function to execute after processing the command result.
* @param {...any} params - Parameters to pass to someFunc.
*/
export const pollVariableBash = <T>(
targetVariable: VariableType<T>,
trackers: Array<Binding<any, any, unknown>>,
pollingInterval: Binding<any, any, unknown>,
someCommand: string,
someFunc: (res: any, ...params: any[]) => T,
...params: any[]
): void => {
let intervalInstance: number | null = null;
const intervalFn = (pollIntrvl: number) => {
if (intervalInstance !== null) {
GLib.source_remove(intervalInstance);
}
intervalInstance = Utils.interval(pollIntrvl, () => {
Utils.execAsync(`bash -c "${someCommand}"`).then((res: any) => {
try {
targetVariable.value = someFunc(res, ...params);
} catch (error) {
console.warn(`An error occurred when running interval bash function: ${error}`);
}
})
.catch((err) => console.error(`Error running command "${someCommand}": ${err}`));
});
};
// Set up the interval initially with the provided polling interval
Utils.merge([pollingInterval, ...trackers], (pollIntrvl: number) => {
intervalFn(pollIntrvl);
});
};