Skip to content

Commit

Permalink
Merge branch 'release/v3.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
andreashuber69 committed May 9, 2024
2 parents 1941a98 + 840e5e0 commit 50b910a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -298,16 +301,13 @@ export const createCalculatorWorker = implementObjectWorkerExternal(
{ type: "module" },
),
// Provide required information about the served object
new ObjectInfo<typeof Calculator>("multiply", "divide"),
new ObjectInfo<typeof Calculator>(),
);
```

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

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 50b910a

Please sign in to comment.