From e1667f91bb8f38dccc135d65fec3bb0a011abc15 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sat, 27 Mar 2021 10:10:09 -0500 Subject: [PATCH] Make the backend a required argument in createWorker --- docs/usage.md | 5 ++--- src/createWorker.ts | 14 +++++++++----- src/types/master.ts | 4 +--- test/rollup/app-createWorker.js | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 0d0dc16f..61954992 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -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. @@ -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) diff --git a/src/createWorker.ts b/src/createWorker.ts index 0b0a582f..53077bc5 100644 --- a/src/createWorker.ts +++ b/src/createWorker.ts @@ -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 : diff --git a/src/types/master.ts b/src/types/master.ts index cf0a140d..0a21764d 100644 --- a/src/types/master.ts +++ b/src/types/master.ts @@ -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. */ diff --git a/test/rollup/app-createWorker.js b/test/rollup/app-createWorker.js index a6cd9484..c3fd9fce 100644 --- a/test/rollup/app-createWorker.js +++ b/test/rollup/app-createWorker.js @@ -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