node-worker-threads-pool
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "node-worker-threads-pool", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "simple worker pool using node's worker_threads api.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -116,3 +116,3 @@ # node-worker-threads-pool | ||
task: function(n) { | ||
const num = workerData.num; | ||
const num = this.workerData.num; | ||
for (let i = 0; i < num; i++) { | ||
@@ -119,0 +119,0 @@ n += i; |
@@ -7,2 +7,3 @@ const Pool = require('./pool'); | ||
const { parentPort } = require('worker_threads'); | ||
parentPort.on('message', ({ code, workerData }) => { | ||
@@ -36,6 +37,23 @@ this.workerData = workerData; | ||
} | ||
const code = `(${ task.toString() })()`; | ||
const code = createCode(task); | ||
const param = { code, workerData }; | ||
return this.runTask(param); | ||
} | ||
} | ||
const fnReg = /^task[^]*([^]*)[^]*{[^]*}$/; | ||
/** | ||
* @param { Function } fn | ||
*/ | ||
function createCode(fn) { | ||
const strFn = fn.toString(); | ||
let expression = ""; | ||
if (fnReg.test(strFn)) { | ||
// es6 style in-object function. | ||
expression = "function " + strFn; | ||
} else { | ||
// es5 function or arrow function. | ||
expression = strFn; | ||
} | ||
return `({ workerData, task: (${ expression })}).task();`; | ||
} |
@@ -12,3 +12,3 @@ const { Worker } = require('worker_threads'); | ||
super(...args); | ||
this.pool = pool; | ||
@@ -20,3 +20,3 @@ // working status. | ||
this.prependListener('message', () => this.done()); | ||
this.once('exit', code => { | ||
this.once('exit', (code) => { | ||
if (this.pool.isDeprecated || code === 0) { | ||
@@ -29,2 +29,4 @@ // exit normally, do nothing. | ||
}); | ||
this.setMaxListeners(0); | ||
} | ||
@@ -31,0 +33,0 @@ |
@@ -26,3 +26,3 @@ const WaitingQueue = require('./waiting-queue'); | ||
// worker generator function. | ||
this.newWorker = null; | ||
this.createWorker = null; | ||
// waiting queue | ||
@@ -37,3 +37,3 @@ this.queue = new WaitingQueue(); | ||
fill(fn) { | ||
this.newWorker = fn; | ||
this.createWorker = fn; | ||
this.workers = this.workers.map(fn); | ||
@@ -67,3 +67,3 @@ } | ||
worker.terminate(); | ||
this.workers[i] = this.newWorker(); | ||
this.workers[i] = this.createWorker(); | ||
} | ||
@@ -70,0 +70,0 @@ } |
const Pool = require('./pool'); | ||
const PoolWorker = require('./pool-worker'); | ||
const fnReg = /^task[^]*([^]*)[^]*{[^]*}$/; | ||
/** | ||
* @param { Function } fn | ||
* @param { Function } fn | ||
*/ | ||
function createScript(fn) { | ||
const strFn = fn.toString(); | ||
let expression = ""; | ||
if (fnReg.test(strFn)) { | ||
// es6 style in-object function. | ||
expression = "function " + strFn; | ||
} else { | ||
// es5 function or arrow function. | ||
expression = strFn; | ||
} | ||
return ` | ||
const { parentPort, workerData } = require('worker_threads'); | ||
const fn = ${ fn.toString() }; | ||
const container = { | ||
workerData, | ||
task: (${ expression }) | ||
}; | ||
parentPort.on('message', param => { | ||
parentPort.postMessage(fn(param)); | ||
parentPort.on('message', (param) => { | ||
parentPort.postMessage(container.task(param)); | ||
}); | ||
@@ -16,0 +29,0 @@ `; |
@@ -6,6 +6,7 @@ const Events = require('events'); | ||
super(); | ||
this._queue = []; | ||
// when a worker turns idle. | ||
this.on('worker-idle', worker => { | ||
this.on('worker-idle', (worker) => { | ||
const callback = this._queue.shift(); | ||
@@ -22,3 +23,3 @@ callback && callback(worker); | ||
return new Promise((resolve, reject) => { | ||
this._queue.push(worker => { | ||
this._queue.push((worker) => { | ||
worker.work(param) | ||
@@ -25,0 +26,0 @@ .then(resolve) |
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
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
14568
261
0