function-thread
Advanced tools
Comparing version 0.2.3 to 0.2.4
'use strict'; | ||
var os = require('os'); | ||
@@ -7,2 +8,3 @@ var _ = require('lodash'); | ||
var genericPool = require('generic-pool'); | ||
/** | ||
@@ -45,2 +47,3 @@ * @param {Function} func | ||
}, options || {}); | ||
if (options.usePool) { | ||
@@ -63,2 +66,3 @@ var pool = genericPool.createPool({ | ||
}, options.pool); | ||
return function (input) { | ||
@@ -83,2 +87,3 @@ return pool.acquire().then(function(thread) { | ||
} | ||
return function (input) { | ||
@@ -85,0 +90,0 @@ var thread = spawn(func); |
{ | ||
"name": "function-thread", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Run the function in a separate thread", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
11
spawn.js
'use strict'; | ||
var childProcess = require('child_process'); | ||
var path = require('path'); | ||
/** | ||
@@ -10,2 +12,3 @@ * @param func | ||
var thread = childProcess.fork(path.join(__dirname, 'worker.js')); | ||
thread.send({ | ||
@@ -15,5 +18,7 @@ type: 'function', | ||
}); | ||
thread.resultHandler = function () {}; | ||
thread.errorHandler = function () {}; | ||
thread.exitHandler = function () {}; | ||
thread.sendData = function (data) { | ||
@@ -26,2 +31,3 @@ this.send({ | ||
}; | ||
thread.onResult = function (handler) { | ||
@@ -31,2 +37,3 @@ this.resultHandler = handler; | ||
}; | ||
thread.onError = function (handler) { | ||
@@ -36,2 +43,3 @@ this.errorHandler = handler; | ||
}; | ||
thread.onExit = function (handler) { | ||
@@ -41,2 +49,3 @@ this.exitHandler = handler; | ||
}; | ||
thread.on('message', function (message) { | ||
@@ -54,4 +63,6 @@ switch (message.type) { | ||
}); | ||
thread.on('exit', thread.exitHandler); | ||
return thread; | ||
}; |
'use strict'; | ||
/** | ||
@@ -12,2 +13,3 @@ * @param {*} err | ||
} | ||
process.send({ | ||
@@ -18,5 +20,8 @@ type: 'error', | ||
} | ||
var ___workerFunction; | ||
process.on('uncaughtException', sendError); | ||
process.on('unhandledRejection', sendError); | ||
process.on('message', function(message) { | ||
@@ -23,0 +28,0 @@ switch (message.type) { |
10927