Skip to content

Commit c323c34

Browse files
committed
Make the backend a required argument in createWorker
1 parent 3be9a05 commit c323c34

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

docs/usage.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ Bundle this module and you will obtain a stand-alone bundle that has its worker
169169
### createWorker - select worker backend
170170
`createWorker` allows selecting the worker backend (among web, node, and tiny), and also if you want a blob worker.
171171

172-
The second argument to the `createWorker` is an object that specifies `backend: 'web' | 'node' | 'tiny'` and `blob: boolean`.
173-
You can also pass other `WorkerOptions` in this object.
172+
The second required argument to the `createWorker` is a string that specifies `backend: 'web' | 'node' | 'tiny'`. The third optional argument is an object that can be used to specify `blob: boolean` or other `WorkerOptions`.
174173

175174
`createWorker` uses dynamic imports to only import the needed implementation, so you can import the needed functions directly to reduce the bundle size.
176175

@@ -179,7 +178,7 @@ import { createWorker } from "threads/createWorker"
179178
import { spawn, Thread } from "threads"
180179

181180
async function run() {
182-
const worker = await createWorker("./worker.js", {backend: "node"})
181+
const worker = await createWorker("./worker.js", "node")
183182
const add = await spawn(worker)
184183
const result = await add(2, 3)
185184
await Thread.terminate(add)

src/createWorker.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@ import {
44
WorkerImplementation,
55
} from "./types/master"
66

7-
/** async function to creat a webworker. This function uses dynamic imports to only import the required implementation */
8-
export async function createWorker(workerPath: string & Blob, options: CreateWorkerOptions) {
7+
/**
8+
* async function to creat a webworker. This function uses dynamic imports to only import the required implementation
9+
* @param workerPath the path or Blob to the worker code
10+
* @param backend backend for the threads
11+
* @param {CreateWorkerOptions} options an object that can be used to specify `blob: boolean` or other {WorkerOptions}. Defaults to `{}`.
12+
*/
13+
export async function createWorker(workerPath: string & Blob, backend: "web" | "node" | "tiny", options: CreateWorkerOptions = {}) {
914
let WorkerConstructor: typeof WorkerImplementation | typeof BlobWorker
10-
if (options.backend === "web") {
15+
if (backend === "web") {
1116
const { getWorkerImplementation } = await import("./master/implementation.browser")
1217
WorkerConstructor = options.blob ?
1318
getWorkerImplementation().blob :
1419
getWorkerImplementation().default
15-
} else if (options.backend === "node") {
20+
} else if (backend === "node") {
1621
const { getWorkerImplementation } = await import("./master/implementation-node")
1722
WorkerConstructor = options.blob ?
1823
getWorkerImplementation("node").blob :
1924
getWorkerImplementation("node").default
20-
} else if (options.backend === "tiny") {
25+
} else if (backend === "tiny") {
2126
const { getWorkerImplementation } = await import("./master/implementation-node")
2227
WorkerConstructor = options.blob ?
2328
getWorkerImplementation("tiny").blob :

src/types/master.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ export interface ThreadsWorkerOptions extends WorkerOptions {
8989
}
9090

9191
export interface CreateWorkerOptions extends ThreadsWorkerOptions {
92-
/** backend for the threads */
93-
backend: "web" | "node" | "tiny"
9492
/** flag to return a BlobWorker */
95-
blob: boolean
93+
blob?: boolean
9694
}
9795

9896
/** Worker implementation. Either web worker or a node.js Worker class. */

test/rollup/app-createWorker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createWorker } from "../../createWorker.mjs"
22
import { spawn, Thread } from "../../"
33

44
async function run() {
5-
const add = await spawn(await createWorker("./worker.js", {backend: "node"}))
5+
const add = await spawn(await createWorker("./worker.js", "node"))
66
const result = await add(2, 3)
77
await Thread.terminate(add)
88
return result

0 commit comments

Comments
 (0)