Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prebuilt web worker #428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "workerpool",
"license": "Apache-2.0",
"version": "9.1.0",
"version": "9.1.1",
"description": "Offload tasks to a pool of workers on node.js and in the browser",
"homepage": "https://github.com/josdejong/workerpool",
"author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)",
Expand Down
3 changes: 3 additions & 0 deletions src/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function Pool(script, options) {
/** @readonly */
this.workerOpts = Object.freeze(options.workerOpts || {});
/** @readonly */
this.webWorker = options.webWorker || null;
/** @readonly */
this.workerThreadOpts = Object.freeze(options.workerThreadOpts || {})
/** @private */
this.debugPortStart = (options.debugPortStart || 43210);
Expand Down Expand Up @@ -430,6 +432,7 @@ Pool.prototype._createWorkerHandler = function () {
workerType: this.workerType,
workerTerminateTimeout: this.workerTerminateTimeout,
emitStdStreams: this.emitStdStreams,
webWorker: this.webWorker
});
}

Expand Down
9 changes: 4 additions & 5 deletions src/WorkerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function getDefaultWorker() {
function setupWorker(script, options) {
if (options.workerType === 'web') { // browser only
ensureWebWorker();
return setupBrowserWorker(script, options.workerOpts, Worker);
return setupBrowserWorker(script, options.workerOpts, Worker, options.webWorker);
} else if (options.workerType === 'thread') { // node.js only
WorkerThreads = ensureWorkerThreads();
return setupWorkerThreadWorker(script, WorkerThreads, options);
Expand All @@ -73,7 +73,7 @@ function setupWorker(script, options) {
} else { // options.workerType === 'auto' or undefined
if (environment.platform === 'browser') {
ensureWebWorker();
return setupBrowserWorker(script, options.workerOpts, Worker);
return setupBrowserWorker(script, options.workerOpts, Worker, options.webWorker);
}
else { // environment.platform === 'node'
var WorkerThreads = tryRequireWorkerThreads();
Expand All @@ -86,12 +86,11 @@ function setupWorker(script, options) {
}
}

function setupBrowserWorker(script, workerOpts, Worker) {
function setupBrowserWorker(script, workerOpts, Worker, existingWorker) {
// validate the options right before creating the worker (not when creating the pool)
validateOptions(workerOpts, workerOptsNames, 'workerOpts')

// create the web worker
var worker = new Worker(script, workerOpts);
var worker = existingWorker || new Worker(script, workerOpts);

worker.isBrowserWorker = true;
// add node.js API to the web worker
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @property {string[]} [forkArgs] For `process` worker type. An array passed as `args` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options)
* @property {import('child_process').ForkOptions} [forkOpts] For `process` worker type. An object passed as `options` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options).
* @property {WorkerOptions} [workerOpts] For `web` worker type. An object passed to the [constructor of the web worker](https://html.spec.whatwg.org/multipage/workers.html#dom-worker). See [WorkerOptions specification](https://html.spec.whatwg.org/multipage/workers.html#workeroptions) for available options.
* @property {Worker} [webWorker] For `web` worker type. An allocated `Worker` object.
* @property {import('worker_threads').WorkerOptions} [workerThreadOpts] Object`. For `worker` worker type. An object passed to [worker_threads.options](https://nodejs.org/api/worker_threads.html#new-workerfilename-options).
* @property {boolean} [emitStdStreams] Capture stdout and stderr from the worker and emit them via the `stdout` and `stderr` events. Not supported by the `web` worker type.
* @property { (arg: WorkerArg) => WorkerArg | undefined } [onCreateWorker] A callback that is called whenever a worker is being created. It can be used to allocate resources for each worker for example. Optionally, this callback can return an object containing one or more of the `WorkerArg` properties. The provided properties will be used to override the Pool properties for the worker being created.
Expand Down