Skip to content

Commit

Permalink
Make the backend a required argument in createWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Mar 27, 2021
1 parent d0d2089 commit e1667f9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 2 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ Bundle this module and you will obtain a stand-alone bundle that has its worker
### createWorker - select worker backend
`createWorker` allows selecting the worker backend (among web, node, and tiny), and also if you want a blob worker.

The second argument to the `createWorker` is an object that specifies `backend: 'web' | 'node' | 'tiny'` and `blob: boolean`.
You can also pass other `WorkerOptions` in this object.
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`.

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

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

async function run() {
const worker = await createWorker("./worker.js", {backend: "node"})
const worker = await createWorker("./worker.js", "node")
const add = await spawn(worker)
const result = await add(2, 3)
await Thread.terminate(add)
Expand Down
14 changes: 9 additions & 5 deletions src/createWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import {
WorkerImplementation,
} from "./types/master"

/** async function to creat a webworker. This function uses dynamic imports to only import the required implementation */
export async function createWorker(workerPath: string & Blob, options: CreateWorkerOptions) {
/** async function to creat a webworker. This function uses dynamic imports to only import the required implementation
* @param workerPath the path or Blob to the worker code
* @param backend backend for the threads
* @param {CreateWorkerOptions} options an object that can be used to specify `blob: boolean` or other {WorkerOptions}. Defaults to `{}`.
*/
export async function createWorker(workerPath: string & Blob, backend: "web" | "node" | "tiny", options: CreateWorkerOptions = {}) {
let WorkerConstructor: typeof WorkerImplementation | typeof BlobWorker
if (options.backend === "web") {
if (backend === "web") {
const { getWorkerImplementation } = await import("./master/implementation.browser")
WorkerConstructor = options.blob ?
getWorkerImplementation().blob :
getWorkerImplementation().default
} else if (options.backend === "node") {
} else if (backend === "node") {
const { getWorkerImplementation } = await import("./master/implementation-node")
WorkerConstructor = options.blob ?
getWorkerImplementation("node").blob :
getWorkerImplementation("node").default
} else if (options.backend === "tiny") {
} else if (backend === "tiny") {
const { getWorkerImplementation } = await import("./master/implementation-node")
WorkerConstructor = options.blob ?
getWorkerImplementation("tiny").blob :
Expand Down
4 changes: 1 addition & 3 deletions src/types/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ export interface ThreadsWorkerOptions extends WorkerOptions {
}

export interface CreateWorkerOptions extends ThreadsWorkerOptions {
/** backend for the threads */
backend: "web" | "node" | "tiny"
/** flag to return a BlobWorker */
blob: boolean
blob?: boolean
}

/** Worker implementation. Either web worker or a node.js Worker class. */
Expand Down
2 changes: 1 addition & 1 deletion test/rollup/app-createWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createWorker } from "../../createWorker.mjs"
import { spawn, Thread } from "../../"

async function run() {
const add = await spawn(await createWorker("./worker.js", {backend: "node"}))
const add = await spawn(await createWorker("./worker.js", "node"))
const result = await add(2, 3)
await Thread.terminate(add)
return result
Expand Down

0 comments on commit e1667f9

Please sign in to comment.