Skip to content

Commit 05245bb

Browse files
committed
Add worker-task-queue package
1 parent 9b6c3b7 commit 05245bb

File tree

8 files changed

+832
-11
lines changed

8 files changed

+832
-11
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/package-lock.json
2-
/node_modules
3-
/dist
1+
package-lock.json
2+
node_modules
3+
dist
44
/polyfill
5+
/packages/worker-task-queue/processor

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"umd:main": "dist/task-worklet.js",
88
"module": "dist/task-worklet.module.js",
99
"scripts": {
10-
"build": "microbundle -f umd,es && microbundle -f iife src/polyfill.mjs -o polyfill/index.js",
10+
"build": "npm run -s build:main && npm run -s build:wtq",
11+
"build:main": "microbundle -f umd,es && microbundle -f iife src/polyfill.js -o polyfill/index.js",
12+
"build:wtq": "cd packages/worker-task-queue && npm run -s build",
1113
"test": "eslint \"{src,test}/**/*.test.{mjs,js}\" && karmatic --no-headless"
1214
},
1315
"files": [
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# `worker-task-queue`
2+
3+
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.
4+
5+
```js
6+
import WorkerTaskQueue from 'worker-task-queue';
7+
8+
// Set up the worker pool
9+
const queue = new WorkerTaskQueue({
10+
// URL/path for our worker script:
11+
workerUrl: '/path/to/worker.js',
12+
// max pool size:
13+
size: 4
14+
});
15+
16+
function demo(image) {
17+
// allocates a thread in the pool doesn't have one free:
18+
const cropped = postTask('crop', image, { box: [10, 20, 30, 40] });
19+
20+
// subsequent tasks run on the same thread to eliminate data transfer:
21+
let large = postTask('resize', cropped, { width: 1000, height: 1000 });
22+
large = postTask('compress', large, quality);
23+
24+
// ... except when they get automatically parallelized by moving the input to a second thread:
25+
let thumb = postTask('resize', cropped, { width: 200, height: 200 });
26+
thumb = postTask('compress', thumb, quality);
27+
28+
// At this point we've only transferred one image to a background thread,
29+
// and transferred another image between two threads.
30+
31+
// Only the final results are transferred back here, and only when we ask for them:
32+
showPreview(await large.result, await thumb.result);
33+
}
34+
35+
demo();
36+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "worker-task-queue",
3+
"version": "0.1.0",
4+
"description": "Streamlined processing of tasks in a shared threadpool.",
5+
"module": "dist/worker-task-queue.module.js",
6+
"main": "dist/worker-task-queue.js",
7+
"umd:main": "dist/worker-task-queue.umd.js",
8+
"scripts": {
9+
"build": "microbundle src/index.ts -f es,cjs,umd && microbundle -f es,cjs,umd src/processor.ts -o processor/index.js"
10+
},
11+
"files": [
12+
"dist",
13+
"processor",
14+
"src"
15+
],
16+
"repository": "developit/task-worklet",
17+
"keywords": [
18+
"tasks",
19+
"task worklet",
20+
"worker task queue",
21+
"task queue",
22+
"parallelization",
23+
"off-main-thread",
24+
"OMT",
25+
"threads",
26+
"multithreading"
27+
],
28+
"author": "Jason Miller <developit@google.com>",
29+
"license": "Apache-2.0",
30+
"homepage": "https://github.com/developit/task-worklet/packages/worker-task-queue"
31+
}

0 commit comments

Comments
 (0)