node-worker-threads-pool
Advanced tools
Comparing version 1.4.3 to 1.5.0
{ | ||
"name": "node-worker-threads-pool", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"description": "Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Async/Await.", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"main": "dist/index.js", | ||
"files": [ | ||
"dist/**/*" | ||
], | ||
"scripts": { | ||
"test": "jest --coverage" | ||
"prepare": "husky install", | ||
"build": "tsc", | ||
"test": "jest --coverage", | ||
"pretest": "yarn build" | ||
}, | ||
@@ -34,9 +39,12 @@ "repository": { | ||
"devDependencies": { | ||
"@types/jest": "^26.0.5", | ||
"@types/node": "^14.0.23", | ||
"husky": "^4.2.5", | ||
"jest": "^24.9.0", | ||
"prettier": "^2.0.5", | ||
"pretty-quick": "^2.0.1" | ||
"@types/jest": "^26.0.23", | ||
"@types/node": "^14.17.2", | ||
"husky": "^6.0.0", | ||
"jest": "^27.0.4", | ||
"prettier": "^2.3.1", | ||
"prettier-plugin-organize-imports": "^2.1.0", | ||
"pretty-quick": "^3.1.0", | ||
"ts-jest": "^27.0.3", | ||
"typescript": "^4.3.2" | ||
} | ||
} |
# node-worker-threads-pool | ||
[![Actions Status](https://github.com/SUCHMOKUO/node-worker-threads-pool/workflows/Workflow/badge.svg)](https://github.com/SUCHMOKUO/node-worker-threads-pool/actions) | ||
[![Actions Status](https://github.com/SUCHMOKUO/node-worker-threads-pool/workflows/CI/badge.svg)](https://github.com/SUCHMOKUO/node-worker-threads-pool/actions) | ||
[![Coverage Status](https://coveralls.io/repos/github/SUCHMOKUO/node-worker-threads-pool/badge.svg?branch=master)](https://coveralls.io/github/SUCHMOKUO/node-worker-threads-pool?branch=master) | ||
@@ -33,11 +33,11 @@ [![](https://img.shields.io/npm/v/node-worker-threads-pool.svg)](https://www.npmjs.com/package/node-worker-threads-pool) | ||
```js | ||
const { StaticPool } = require("node-worker-threads-pool"); | ||
const { StaticPool } = require('node-worker-threads-pool'); | ||
const staticPool = new StaticPool({ | ||
size: 4, | ||
task: (n) => n + 1, | ||
task: (n) => n + 1 | ||
}); | ||
staticPool.exec(1).then((result) => { | ||
console.log("result from thread pool:", result); // result will be 2. | ||
console.log('result from thread pool:', result); // result will be 2. | ||
}); | ||
@@ -51,3 +51,3 @@ ``` | ||
```js | ||
const { DynamicPool } = require("node-worker-threads-pool"); | ||
const { DynamicPool } = require('node-worker-threads-pool'); | ||
@@ -59,3 +59,3 @@ const dynamicPool = new DynamicPool(4); | ||
task: (n) => n + 1, | ||
param: 1, | ||
param: 1 | ||
}) | ||
@@ -69,3 +69,3 @@ .then((result) => { | ||
task: (n) => n + 2, | ||
param: 1, | ||
param: 1 | ||
}) | ||
@@ -100,3 +100,3 @@ .then((result) => { | ||
// Access the workerData by requiring it. | ||
const { parentPort, workerData } = require("worker_threads"); | ||
const { parentPort, workerData } = require('worker_threads'); | ||
@@ -114,5 +114,5 @@ // Something you shouldn"t run in main thread | ||
// through this event listener. | ||
parentPort.on("message", (param) => { | ||
if (typeof param !== "number") { | ||
throw new Error("param must be a number."); | ||
parentPort.on('message', (param) => { | ||
if (typeof param !== 'number') { | ||
throw new Error('param must be a number.'); | ||
} | ||
@@ -122,3 +122,3 @@ const result = fib(param); | ||
// Access the workerData. | ||
console.log("workerData is", workerData); | ||
console.log('workerData is', workerData); | ||
@@ -133,5 +133,5 @@ // return the result to main thread. | ||
```js | ||
const { StaticPool } = require("node-worker-threads-pool"); | ||
const { StaticPool } = require('node-worker-threads-pool'); | ||
const filePath = "absolute/path/to/your/worker/script"; | ||
const filePath = 'absolute/path/to/your/worker/script'; | ||
@@ -141,3 +141,3 @@ const pool = new StaticPool({ | ||
task: filePath, | ||
workerData: "workerData!", | ||
workerData: 'workerData!' | ||
}); | ||
@@ -166,6 +166,6 @@ | ||
size: 4, | ||
workerData: "workerData!", | ||
workerData: 'workerData!', | ||
task() { | ||
console.log(this.workerData); | ||
}, | ||
} | ||
}); | ||
@@ -176,6 +176,5 @@ ``` | ||
### `staticPool.exec(param[, timeout])` | ||
### `staticPool.exec(param)` | ||
- `param` `<any>` The param your worker script or task function need. | ||
- `timeout` `<number>` Timeout in milisecond for limiting the execution time. When timeout, the function will throw a `TimeoutError`, use `isTimeoutError` function to detect it. | ||
- Returns: `<Promise>` | ||
@@ -206,3 +205,3 @@ | ||
// do something with buf. | ||
}, | ||
} | ||
}); | ||
@@ -217,3 +216,3 @@ | ||
.exec(buf) // execute! | ||
.then(() => console.log("done!")); | ||
.then(() => console.log('done!')); | ||
``` | ||
@@ -257,4 +256,2 @@ | ||
- `task` `<function>` Function as a task to do. **⚠️Notice: You can not use closure in task function!** | ||
- ~~`workerData` `<any>` [cloneable data] you want to access in task function.~~ (deprecated since 1.4.0, use `param` instead) | ||
- `param` `<any>` [cloneable data] you want to pass into task function as parameter. | ||
- `timeout` `<number>` Timeout in milisecond for limiting the execution time. When timeout, the function will throw a `TimeoutError`, use `isTimeoutError` function to detect it. | ||
@@ -294,3 +291,3 @@ - Returns: `<Promise>` | ||
.exec(buf) // execute! | ||
.then(() => console.log("done!")); | ||
.then(() => console.log('done!')); | ||
``` | ||
@@ -374,5 +371,5 @@ | ||
task() { | ||
const lib = this.require("lib"); | ||
const lib = this.require('lib'); | ||
// ... | ||
}, | ||
} | ||
}); | ||
@@ -387,5 +384,5 @@ ``` | ||
task() { | ||
const lib = this.require("lib"); | ||
const lib = this.require('lib'); | ||
// ... | ||
}, | ||
} | ||
}) | ||
@@ -392,0 +389,0 @@ .then((result) => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39240
23
693
9
382
1