Skip to content

Commit bce94d6

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

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

docs/usage.md

Lines changed: 2 additions & 3 deletions
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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ 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+
/** async function to creat a webworker. This function uses dynamic imports to only import the required implementation
8+
* @param workerPath the path or Blob to the worker code
9+
* @param backend backend for the threads
10+
* @param {CreateWorkerOptions} options an object that can be used to specify `blob: boolean` or other {WorkerOptions}. Defaults to `{}`.
11+
*/
12+
export async function createWorker(workerPath: string & Blob, backend: "web" | "node" | "tiny", options: CreateWorkerOptions = {}) {
913
let WorkerConstructor: typeof WorkerImplementation | typeof BlobWorker
10-
if (options.backend === "web") {
14+
if (backend === "web") {
1115
const { getWorkerImplementation } = await import("./master/implementation.browser")
1216
WorkerConstructor = options.blob ?
1317
getWorkerImplementation().blob :
1418
getWorkerImplementation().default
15-
} else if (options.backend === "node") {
19+
} else if (backend === "node") {
1620
const { getWorkerImplementation } = await import("./master/implementation-node")
1721
WorkerConstructor = options.blob ?
1822
getWorkerImplementation("node").blob :
1923
getWorkerImplementation("node").default
20-
} else if (options.backend === "tiny") {
24+
} else if (backend === "tiny") {
2125
const { getWorkerImplementation } = await import("./master/implementation-node")
2226
WorkerConstructor = options.blob ?
2327
getWorkerImplementation("tiny").blob :

src/types/master.ts

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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)