Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Mar 4, 2024
1 parent e633360 commit d16cf43
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ const worker = workerFactory();
const worker = workerFactory({ name: "editor.worker" });
```

You can import any module as a worker from esm.sh with the `?worker` query. The module will be loaded in a web worker
as variable `$module`, then you can use it in the `inject` code.
You can import any module as a worker from esm.sh with the `?worker` query. The module will be loaded in a web worker as
variable `$module`, then you can use it in the `inject` code.

For example, you can use the `xxhash-wasm` module to hash a string in a web worker:

Expand All @@ -203,9 +203,10 @@ import workerFactory from "https://esm.sh/xxhash-wasm@1.0.2?worker";
const inject = `
// variable '$module' is the xxhash-wasm module
$module.default().then(hasher => {
self.postMessage(hasher.h64ToString(e.data));
})
self.onmessage = async e => {
const hasher = await $module.default()
self.postMessage(hasher.h64ToString(e.data))
}
`;
const worker = workerFactory({ inject });
worker.onmessage = (e) => console.log("hash:", e.data);
Expand Down

0 comments on commit d16cf43

Please sign in to comment.