node-worker-threads-pool
Advanced tools
Comparing version 1.4.2 to 1.4.3
@@ -7,8 +7,13 @@ /// <reference types="node" /> | ||
type TaskFunc<ParamType, ResultType> = | ||
| (() => Promise<ResultType>) | ||
| (() => ResultType) | ||
| ((param: ParamType) => Promise<ResultType>) | ||
| ((param: ParamType) => ResultType); | ||
type TaskFuncThis<WorkerData = any> = { | ||
workerData: WorkerData; | ||
require: NodeRequire; | ||
}; | ||
type TaskFunc<ParamType, ResultType, WorkerData = any> = | ||
| ((this: TaskFuncThis<WorkerData>) => Promise<ResultType>) | ||
| ((this: TaskFuncThis<WorkerData>) => ResultType) | ||
| ((this: TaskFuncThis<WorkerData>, param: ParamType) => Promise<ResultType>) | ||
| ((this: TaskFuncThis<WorkerData>, param: ParamType) => ResultType); | ||
type CommonWorkerSettings = { | ||
@@ -15,0 +20,0 @@ /** |
{ | ||
"name": "node-worker-threads-pool", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"description": "Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Async/Await.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -351,2 +351,31 @@ # node-worker-threads-pool | ||
## Integration with Webpack | ||
If you are using webpack in your project and want to import third-party libraries in task function, please use `this.require`: | ||
```js | ||
const staticPool = new StaticPool({ | ||
size: 4, | ||
task() { | ||
const lib = this.require("lib"); | ||
// ... | ||
}, | ||
}); | ||
``` | ||
```js | ||
const dynamicPool = new DynamicPool(4); | ||
dynamicPool | ||
.exec({ | ||
task() { | ||
const lib = this.require("lib"); | ||
// ... | ||
}, | ||
}) | ||
.then((result) => { | ||
// ... | ||
}); | ||
``` | ||
[cloneable data]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm | ||
@@ -353,0 +382,0 @@ [resourcelimits]: https://nodejs.org/api/worker_threads.html#worker_threads_worker_resourcelimits |
@@ -27,3 +27,3 @@ /** | ||
const task = vm.runInThisContext(code); | ||
const container = { task, workerData }; | ||
const container = { task, workerData, require }; | ||
const result = await container.task(param); | ||
@@ -30,0 +30,0 @@ parentPort.postMessage(result); |
@@ -16,2 +16,3 @@ const { Pool } = require("./pool"); | ||
workerData, | ||
require, | ||
task: ${createCode(fn)} | ||
@@ -18,0 +19,0 @@ }; |
30570
629
385