Skip to content

Add worker-task-queue package #14

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

Open
wants to merge 5 commits 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
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/package-lock.json
/node_modules
/dist
package-lock.json
node_modules
dist
/polyfill
/packages/worker-task-queue/processor
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"umd:main": "dist/task-worklet.js",
"module": "dist/task-worklet.module.js",
"scripts": {
"build": "microbundle -f umd,es && microbundle -f iife src/polyfill.js -o polyfill/index.js",
"build": "npm run -s build:main && npm run -s build:wtq",
"build:main": "microbundle -f umd,es && microbundle -f iife src/polyfill.js -o polyfill/index.js",
"build:wtq": "cd packages/worker-task-queue && npm run -s build",
"test": "eslint \"{src,test}/**/*.test.{mjs,js}\" && karmatic --no-coverage"
},
"files": [
Expand Down Expand Up @@ -50,10 +52,15 @@
"license": "Apache-2.0",
"homepage": "https://github.com/developit/task-worklet",
"devDependencies": {
"@babel/preset-env": "^7.15.0",
"@babel/preset-typescript": "^7.15.0",
"@babel/register": "^7.15.3",
"@types/jest": "^27.0.1",
"eslint": "^7.9.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^6.11.0",
"file-loader": "^6.1.0",
"jest": "^27.0.6",
"karmatic": "^2.1.0",
"microbundle": "^0.12.3",
"prettier": "^2.1.2",
Expand Down
65 changes: 65 additions & 0 deletions packages/worker-task-queue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# `worker-task-queue`

This is a standalone implementation of the cooperative multithreading model from [Task Worklet](https://github.com/developit/task-worklet) as a zero-dependency library for Web and Node.

**main thread code:**

```js
import WorkerTaskQueue from 'worker-task-queue';

// Set up the worker pool
const queue = new WorkerTaskQueue({
// URL/path for our worker script:
workerUrl: '/path/to/worker.js',
// max pool size:
size: 4
});

function demo(image) {
// allocates a thread in the pool doesn't have one free:
const cropped = postTask('crop', image, { box: [10, 20, 30, 40] });

// subsequent tasks run on the same thread to eliminate data transfer:
let large = postTask('resize', cropped, { width: 1000, height: 1000 });
large = postTask('compress', large, quality);

// ... except when they get automatically parallelized by moving the input to a second thread:
let thumb = postTask('resize', cropped, { width: 200, height: 200 });
thumb = postTask('compress', thumb, quality);

// At this point we've only transferred one image to a background thread,
// and transferred another image between two threads.

// Only the final results are transferred back here, and only when we ask for them:
showPreview(await large.result, await thumb.result);
}

demo();
```

**worker code:**

```js
import { registerTask } from 'worker-task-queue/processor';

registerTask('crop', class {
process(image, { box }) {
// complicated stuff in here
return image;
}
});

registerTask('resize', class {
process(image, { width, height }) {
// complicated stuff in here
return image;
}
});

registerTask('compress', class {
process(image, quality) {
// complicated stuff in here
return image;
}
});
```
66 changes: 66 additions & 0 deletions packages/worker-task-queue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "worker-task-queue",
"version": "0.2.1",
"description": "Streamlined processing of tasks in a shared threadpool.",
"exports": {
"./processor": {
"module": "./processor/index.mjs",
"import": "./processor/index.mjs",
"default": "./processor/index.js"
},
".": {
"module": "./dist/worker-task-queue.mjs",
"import": "./dist/worker-task-queue.mjs",
"default": "./dist/worker-task-queue.js"
}
},
"module": "./dist/worker-task-queue.mjs",
"main": "./dist/worker-task-queue.js",
"umd:main": "./dist/worker-task-queue.umd.js",
"scripts": {
"build": "microbundle src/index.ts -f es,cjs,umd && microbundle -f es,cjs,umd src/processor.ts -o processor/index.js",
"test": "../../node_modules/.bin/jest"
},
"files": [
"dist",
"processor",
"src"
],
"babel": {
"env": {
"test": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
},
"modules": "commonjs"
}
],
"@babel/preset-typescript"
]
}
}
},
"repository": "developit/task-worklet",
"keywords": [
"tasks",
"task worklet",
"worker task queue",
"task queue",
"parallelization",
"off-main-thread",
"OMT",
"threads",
"multithreading"
],
"author": "Jason Miller <[email protected]>",
"license": "Apache-2.0",
"homepage": "https://github.com/developit/task-worklet/packages/worker-task-queue",
"devDependencies": {
"microbundle": "^0.13.3",
"web-worker": "^1.0.0"
}
}
Loading