@ev-core/workers 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class for executing a function in a Web Worker and returning the result as a Promise.
|
|
3
|
+
* This is useful for offloading computationally expensive tasks to a background thread.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const worker = new WorkHandler();
|
|
7
|
+
* const enhancedData = await worker.run(() => {
|
|
8
|
+
* const data = bigCalculation();
|
|
9
|
+
* return data;
|
|
10
|
+
* });
|
|
11
|
+
*/
|
|
12
|
+
export declare class BackgroundTask {
|
|
13
|
+
private worker?;
|
|
14
|
+
/**
|
|
15
|
+
* Executes the provided function in a Web Worker and returns the result as a Promise.
|
|
16
|
+
* @template T The type of the result returned by the function.
|
|
17
|
+
* @param task A function containing the logic to execute in the worker.
|
|
18
|
+
* @returns A Promise that resolves with the result of the function.
|
|
19
|
+
*/
|
|
20
|
+
run<T>(task: () => T): Promise<T>;
|
|
21
|
+
/**
|
|
22
|
+
* Terminates the worker if it exists.
|
|
23
|
+
*/
|
|
24
|
+
terminateWorker(): void;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=background-task.d.ts.map
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class for executing a function in a Web Worker and returning the result as a Promise.
|
|
3
|
+
* This is useful for offloading computationally expensive tasks to a background thread.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const worker = new WorkHandler();
|
|
7
|
+
* const enhancedData = await worker.run(() => {
|
|
8
|
+
* const data = bigCalculation();
|
|
9
|
+
* return data;
|
|
10
|
+
* });
|
|
11
|
+
*/
|
|
12
|
+
export class BackgroundTask {
|
|
13
|
+
/**
|
|
14
|
+
* Executes the provided function in a Web Worker and returns the result as a Promise.
|
|
15
|
+
* @template T The type of the result returned by the function.
|
|
16
|
+
* @param task A function containing the logic to execute in the worker.
|
|
17
|
+
* @returns A Promise that resolves with the result of the function.
|
|
18
|
+
*/
|
|
19
|
+
run(task) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const taskString = task.toString();
|
|
22
|
+
// Verify the task is serializable to a string
|
|
23
|
+
if (typeof task !== 'function' || !taskString.startsWith('function') && !taskString.startsWith('(')) {
|
|
24
|
+
reject(new Error('The provided task must be a pure function.'));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const blob = new Blob([`
|
|
28
|
+
self.onmessage = function() {
|
|
29
|
+
try {
|
|
30
|
+
const result = (${taskString})();
|
|
31
|
+
postMessage({ result });
|
|
32
|
+
} catch (error) {
|
|
33
|
+
postMessage({ error: error.message });
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
`], { type: 'application/javascript' });
|
|
37
|
+
this.worker = new Worker(URL.createObjectURL(blob));
|
|
38
|
+
this.worker.onmessage = (event) => {
|
|
39
|
+
const { result, error } = event.data;
|
|
40
|
+
if (error) {
|
|
41
|
+
reject(new Error(error));
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
resolve(result);
|
|
45
|
+
}
|
|
46
|
+
this.terminateWorker();
|
|
47
|
+
};
|
|
48
|
+
this.worker.onerror = (error) => {
|
|
49
|
+
reject(error);
|
|
50
|
+
this.terminateWorker();
|
|
51
|
+
};
|
|
52
|
+
this.worker.postMessage(null); // Start the worker task.
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Terminates the worker if it exists.
|
|
57
|
+
*/
|
|
58
|
+
terminateWorker() {
|
|
59
|
+
if (this.worker) {
|
|
60
|
+
this.worker.terminate();
|
|
61
|
+
this.worker = undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=background-task.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ev-core/workers",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Worker library for ev-core",
|
|
5
5
|
"types": "dist/esm/index.d.ts",
|
|
6
6
|
"module": "dist/esm/index.mjs",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "pnpm run build:cjs && pnpm run build:esm",
|
|
23
23
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
24
|
-
"build:esm": "tsc -p tsconfig.esm.json && ts2mjs dist/esm
|
|
24
|
+
"build:esm": "tsc -p tsconfig.esm.json && ts2mjs dist/esm",
|
|
25
25
|
"clean": "rimraf dist"
|
|
26
26
|
}
|
|
27
27
|
}
|