* custom module updates to class based. * Finish poller logic. * Use composition for pollers * Rename poller * Handle recorder polling. * Fix quotes in bash command * Remove logs
87 lines
2.5 KiB
TypeScript
87 lines
2.5 KiB
TypeScript
import { Variable as VariableType } from 'types/variable';
|
|
import { Bind } from 'lib/types/variable';
|
|
import { GenericFunction } from 'lib/types/customModules/generic';
|
|
import { BarModule } from 'lib/types/options';
|
|
import { Poller } from './Poller';
|
|
|
|
/**
|
|
* A class that manages polling of a variable by executing a generic function at specified intervals.
|
|
*/
|
|
export class FunctionPoller<Value, Parameters extends unknown[] = []> {
|
|
private poller: Poller;
|
|
|
|
private params: Parameters;
|
|
|
|
/**
|
|
* Creates an instance of FunctionPoller.
|
|
*
|
|
* @param targetVariable - The target variable to poll.
|
|
* @param trackers - An array of trackers to monitor.
|
|
* @param pollingInterval - The interval at which polling occurs.
|
|
* @param pollingFunction - The function to execute during each poll.
|
|
* @param params - Additional parameters for the polling function.
|
|
*
|
|
* @example
|
|
*
|
|
* ```ts
|
|
* //##################### EXAMPLE ##########################
|
|
* const cpuPoller = new FunctionPoller<number, []>(
|
|
* cpuUsage,
|
|
* [round.bind('value')],
|
|
* pollingInterval.bind('value'),
|
|
* computeCPU,
|
|
* );
|
|
* //#######################################################
|
|
*
|
|
* ```
|
|
*/
|
|
constructor(
|
|
private targetVariable: VariableType<Value>,
|
|
private trackers: Bind[],
|
|
private pollingInterval: Bind,
|
|
private pollingFunction: GenericFunction<Value, Parameters>,
|
|
...params: Parameters
|
|
) {
|
|
this.params = params;
|
|
|
|
this.poller = new Poller(this.pollingInterval, this.trackers, this.execute);
|
|
}
|
|
|
|
/**
|
|
* Executes the polling function with the provided parameters.
|
|
*
|
|
* The result of the function is assigned to the target variable.
|
|
*/
|
|
private execute = async (): Promise<void> => {
|
|
try {
|
|
const result = await this.pollingFunction(...this.params);
|
|
this.targetVariable.value = result;
|
|
} catch (error) {
|
|
console.error('Error executing polling function:', error);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Starts the polling process.
|
|
*/
|
|
public start(): void {
|
|
this.poller.start();
|
|
}
|
|
|
|
/**
|
|
* Stops the polling process.
|
|
*/
|
|
public stop(): void {
|
|
this.poller.stop();
|
|
}
|
|
|
|
/**
|
|
* Initializes the poller with the specified module.
|
|
*
|
|
* @param moduleName - The name of the module to initialize.
|
|
*/
|
|
public initialize(moduleName?: BarModule): void {
|
|
this.poller.initialize(moduleName);
|
|
}
|
|
}
|