diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d583bba..eb762f0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,5 +9,5 @@ jobs: ci: uses: andreashuber69/actions/.github/workflows/ubuntu-latest-node-lts-ci.yml@master secrets: inherit - with: - tag: beta + # with: + # tag: beta diff --git a/README.md b/README.md index 16660df..b9062d1 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ That's it, we've defined our worker with a single statement! Let's see how we ca ```ts // ./src/main.ts -import { createFibonacciWorker } from "./createFibonacciWorker.ts"; +import { createFibonacciWorker } from "./createFibonacciWorker.js"; // Start a new worker thread waiting for work. const worker = createFibonacciWorker(); @@ -124,6 +124,10 @@ Here are a few facts that might not be immediately obvious: the worker thread. This is possible because `implementFunctionWorker()` detects on which thread it is run. However, this detection would **not** work correctly, if code in a worker thread attempted to start another worker thread. This can easily be fixed, see [Worker Code Isolation](#worker-code-isolation). +- In order for build tools to be able to put worker code into a separate chunk, it is vital that the expression + `() => new Worker(new URL("createFibonacciWorker.js", import.meta.url), { type: "module" })` is kept as is. Please see + associated instructions for [vite](https://vitejs.dev/guide/assets.html#new-url-url-import-meta-url) and + [webpack](https://webpack.js.org/guides/web-workers/). Other build tools will likely have similar constraints. ### Example 2: Object @@ -160,7 +164,7 @@ export const createCalculatorWorker = implementObjectWorker( ```ts // ./src/main.ts -import { createCalculatorWorker } from "./createCalculatorWorker.ts"; +import { createCalculatorWorker } from "./createCalculatorWorker.js"; // Start a new worker thread waiting for work. const worker = await createCalculatorWorker(); @@ -206,9 +210,7 @@ counterparts has the following advantages: - A factory function returned by `implementFunctionWorkerExternal()` or `implementObjectWorkerExternal()` can be executed on **any** thread (not just the main thread). - The code of the served function or object is only ever loaded on the worker thread. This can become important when the - amount of code running on the worker thread is significant, such that you'd rather not load it anywhere else. Build - tools like [vite](vitejs.dev) support this use case by detecting `new Worker(...)` calls and putting the worker script - as well as all directly and indirectly called code into a separate chunk. + amount of code running on the worker thread is significant, such that you'd rather not load it anywhere else. Lets see how [Example 1](#example-1-single-function) can be implemented such that worker code is fully isolated. @@ -251,8 +253,9 @@ export const createFibonacciWorker = implementFunctionWorkerExternal( ``` The usage from *./src/main.ts* is the same as in [Example 1](#example-1-single-function). What was done in a single file -before is now split into two. Note that *./src/fibonacci.ts* only exports a **type**. Type-only exports and imports are -removed during compilation to ECMAScript. +before is now split into two. Note that *./src/fibonacci.ts* only exports a **type**, so we can no longer pass +the function itself. Instead, we pass a `FunctionInfo` instance to convey the required information. Type-only exports +and imports are removed during compilation to ECMAScript. Finally, let's see how [Example 2](#example-2-object) can be implemented such that worker code is fully isolated. @@ -298,16 +301,13 @@ export const createCalculatorWorker = implementObjectWorkerExternal( { type: "module" }, ), // Provide required information about the served object - new ObjectInfo("multiply", "divide"), + new ObjectInfo(), ); ``` -Again, the usage from *./src/main.ts* is the same as in [Example 2](#example-2-object). Note that -`implementObjectWorkerExternal` can only work as advertised if it knows the method names of the object being served on -the worker thread. Due to TypeScript design constraints, method names cannot be extracted from a type at runtime and -therefore have to be supplied by the user. The `ObjectInfo` class supports this process by ensuring that the supplied -method names are always in sync with the method names declared by the type. If they are not, the TS compiler will show -an error. +The usage from *./src/main.ts* is the same as in [Example 2](#example-2-object). Again, note that *./src/Calculator.ts* +only exports a **type**, so we can no longer pass the constructor function itself. Instead, we pass an `ObjectInfo` +instance to convey the required information. ## Limitations diff --git a/package-lock.json b/package-lock.json index b4b944b..5820061 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "kiss-worker", - "version": "3.0.0-beta.0", + "version": "3.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "kiss-worker", - "version": "3.0.0-beta.0", + "version": "3.0.0", "license": "MIT", "devDependencies": { "@andreashuber69/eslint-config": "^1.2.14", diff --git a/package.json b/package.json index dcc6394..2734ab5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kiss-worker", - "version": "3.0.0-beta.0", + "version": "3.0.0", "description": "Provides one of the easiest ways to run a function on a worker thread in the browser.", "keywords": [ "kiss",