| 1 |
# Wasm in Web Worker |
| 2 |
|
| 3 |
[View full source code][code] |
| 4 |
|
| 5 |
[code]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/wasm-in-web-worker |
| 6 |
|
| 7 |
A simple example of parallel execution by spawning a web worker with `web_sys`, |
| 8 |
loading Wasm code in the web worker and interacting between the main thread and |
| 9 |
the worker. |
| 10 |
|
| 11 |
## Building & compatibility |
| 12 |
|
| 13 |
At the time of this writing, only Chrome supports modules in web workers, e.g. |
| 14 |
Firefox does not. To have compatibility across browsers, the whole example is |
| 15 |
set up without relying on ES modules as target. Therefore we have to build |
| 16 |
with `--target no-modules`. The full command can be found in `build.sh`. |
| 17 |
|
| 18 |
## `Cargo.toml` |
| 19 |
|
| 20 |
The `Cargo.toml` enables features necessary to work with the DOM, log output to |
| 21 |
the JS console, creating a worker and reacting to message events. |
| 22 |
|
| 23 |
```toml |
| 24 |
{{#include ../../../examples/wasm-in-web-worker/Cargo.toml}} |
| 25 |
``` |
| 26 |
|
| 27 |
## `src/lib.rs` |
| 28 |
|
| 29 |
Creates a struct `NumberEval` with methods to act as stateful object in the |
| 30 |
worker and function `startup` to be launched in the main thread. Also includes |
| 31 |
internal helper functions `setup_input_oninput_callback` to attach a |
| 32 |
`wasm_bindgen::Closure` as callback to the `oninput` event of the input field |
| 33 |
and `get_on_msg_callback` to create a `wasm_bindgen::Closure` which is triggered |
| 34 |
when the worker returns a message. |
| 35 |
|
| 36 |
```rust |
| 37 |
{{#include ../../../examples/wasm-in-web-worker/src/lib.rs}} |
| 38 |
``` |
| 39 |
|
| 40 |
## `index.html` |
| 41 |
|
| 42 |
Includes the input element `#inputNumber` to type a number into and a HTML |
| 43 |
element `#resultField` were the result of the evaluation even/odd is written to. |
| 44 |
Since we require to build with `--target no-modules` to be able to load Wasm |
| 45 |
code in in the worker across browsers, the `index.html` also includes loading |
| 46 |
both `wasm_in_web_worker.js` and `index.js`. |
| 47 |
|
| 48 |
```html |
| 49 |
{{#include ../../../examples/wasm-in-web-worker/index.html}} |
| 50 |
``` |
| 51 |
|
| 52 |
## `index.js` |
| 53 |
|
| 54 |
Loads our Wasm file asynchronously and calls the entry point `startup` of the |
| 55 |
main thread which will create a worker. |
| 56 |
|
| 57 |
```js |
| 58 |
{{#include ../../../examples/wasm-in-web-worker/index.js}} |
| 59 |
``` |
| 60 |
|
| 61 |
## `worker.js` |
| 62 |
|
| 63 |
Loads our Wasm file by first importing `wasm_bindgen` via |
| 64 |
`importScripts('./pkg/wasm_in_web_worker.js')` and then awaiting the Promise |
| 65 |
returned by `wasm_bindgen(...)`. Creates a new object to do the background |
| 66 |
calculation and bind a method of the object to the `onmessage` callback of the |
| 67 |
worker. |
| 68 |
|
| 69 |
```js |
| 70 |
{{#include ../../../examples/wasm-in-web-worker/worker.js}} |
| 71 |
``` |