h-worker v0.0.7
H-Worker
Introduce
Help developers quickly and conveniently use workers to handle method
Advantages:
- No need to create worker files, various methods in development can be executed at any time through workers
- Use the "exec" method to execute any method in the worker
- Use the "execDynamic" method to automatically create new workers to execute functions, and automatically destroy workers that exceed idle time
- "Idle time" refers to a period of time without a return value, with a default of 10 seconds
- execDynamic will create multiple workers to execute methods, be careful to prevent memory leaks
Note that not all interfaces and functions are available to web workers. See Functions and classes available to Web Workers for details.
Usage
Install
npm install h-worker
Init
import HWorker from "h-worker";
let aliveTime = 10000; // Maximum idle time, default 10 seconds
let Helper = new HWorker(aliveTime);
Example
/** 1. Define variables */
let num1 = 1;
let num2 = 2;
/**
* 2. Define the function to be executed
* - props: Custom Parameters
* - callback: Final return value
*/
function sum(props, callback) {
callback(num1 + num2 + props.number);
}
/** 3. Initialize context */
Helper.setCtx({ num1, num2, sum });
/**
* 4. Execute
* - result: worker return result
* - terminate: terminate worker
*/
const getResult = (result, terminate) => {
console.log("result = ", result); // result = 6
// terminate worker, Next time need to setCtx again
terminate();
};
Helper.exec(sum, { number: 3 }, getResult);
Method
Before introducing the method, let's first define two variables to facilitate the introduction during use
let a = 1;
this.b = 2;
Define execution function
This is important, I need to ensure that I can receive the return value of this function and throw it out
For executing functions, two fixed parameters are received, as follows:
(
props: any, // External input parameters
callback: Function, // Throwing a callback for data
) => void
This is an example where in each worker, we will execute some methods and return the processing results through that method
function getSum(props, callback) {
let num = props.num || 0;
let result = this.a + this.b + num;
callback(result); // return 1 + 2 + 0
}
setCtx
To initialize the context to be executed, all involved scopes need to be passed in
In the following example, a function will be defined and initialized:
// worker context
let ctx = {
a,
b: this.b,
getSum,
};
Helper.setCtx(ctx);
exec
As long as this worker is not terminated, the same worker will still be used for execution the next time this method is used
This method accepts three parameters:
(
fnc: Function, // The function to be executed
props: any, // The variables required for the function
(
result: any, // Return result after worker execution
terminate: Function, // Termination current worker
) => void,
) => void
You can execute the getSum method as follows and pass in a parameter {num: 3}
Helper.exec(getSum, { num: 3 }, (res, terminate) => {
console.log("result = ", res); // result = 1 + 2 + 3
terminate(); // termination current worker
});
execDynamic - Dynamically create workers and execute functions
Executing a function in a worker will create a new worker with each call, but it is important to note when using it
- After use, please remember call the "terminate" callback to terminate it。
- It is easy to experience memory leaks when multiple workers are used simultaneously。
- For idle workers, they will be automatically cleared after 10 seconds.
This method accepts three parameters:
(
fnc: Function, // The function to be executed
props: any, // The variables required for the function
(
result: any, // Return result after worker execution
terminate: Function, // Termination current worker
) => void,
) => void
You can execute the getSum method as follows and pass in a parameter {num: 3}
Helper.exec(getSum, { num: 3 }, (res, terminate) => {
console.log("result = ", res); // result = 1 + 2 + 3
terminate(); // termination current worker
});
terminateAll
Delete all currently created workers (including executing and idle workers)
Helper.terminateAll();