Skip to content

Commit 6a12c27

Browse files
committed
worker
1 parent c35cc93 commit 6a12c27

File tree

7 files changed

+22
-31
lines changed

7 files changed

+22
-31
lines changed

examples/wasm-in-web-worker/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ features = [
2121
'MessageEvent',
2222
'Window',
2323
'Worker',
24+
'WorkerOptions',
25+
'WorkerType',
2426
]
2527
path = "../../crates/web-sys"
2628

examples/wasm-in-web-worker/build.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
set -ex
44

5-
# This example requires to *not* create ES modules, therefore we pass the flag
6-
# `--target no-modules`
7-
wasm-pack build --target no-modules
5+
wasm-pack build --target web

examples/wasm-in-web-worker/index.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ <h1>Main Thread/Wasm Web Worker Interaction</h1>
1414
<div id="resultField"></div>
1515
</div>
1616

17-
<!-- Make `wasm_bindgen` available for `index.js` -->
18-
<script src='./pkg/wasm_in_web_worker.js'></script>
19-
<!-- Note that there is no `type="module"` in the script tag -->
20-
<script src="./index.js"></script>
17+
<script type="module" src="./index.js"></script>
18+
</body>
2119

20+
</html>
2221
</body>
2322

2423
</html>

examples/wasm-in-web-worker/index.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
// We only need `startup` here which is the main entry point
2-
// In theory, we could also use all other functions/struct types from Rust which we have bound with
3-
// `#[wasm_bindgen]`
4-
const {startup} = wasm_bindgen;
1+
// Use ES module import syntax to import functionality from the module
2+
// that we have compiled.
3+
import init, { startup } from './pkg/wasm_in_web_worker.js';
54

65
async function run_wasm() {
7-
// Load the Wasm file by awaiting the Promise returned by `wasm_bindgen`
8-
// `wasm_bindgen` was imported in `index.html`
9-
await wasm_bindgen();
6+
await init();
107

118
console.log('index.js loaded');
129

examples/wasm-in-web-worker/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cell::RefCell;
22
use std::rc::Rc;
33
use wasm_bindgen::prelude::*;
4-
use web_sys::{console, HtmlElement, HtmlInputElement, MessageEvent, Worker};
4+
use web_sys::{console, HtmlElement, HtmlInputElement, MessageEvent, Worker, WorkerOptions, WorkerType};
55

66
/// A number evaluation struct
77
///
@@ -45,7 +45,9 @@ pub fn startup() {
4545
// able to interact with the code in the worker. Therefore, we wrap it in
4646
// `Rc<RefCell>` following the interior mutability pattern. Here, it would
4747
// not be needed but we include the wrapping anyway as example.
48-
let worker_handle = Rc::new(RefCell::new(Worker::new("./worker.js").unwrap()));
48+
let worker_options = WorkerOptions::new();
49+
worker_options.set_type(WorkerType::Module);
50+
let worker_handle = Rc::new(RefCell::new(Worker::new_with_options("./worker.js", &worker_options).unwrap()));
4951
console::log_1(&"Created a new worker from within Wasm".into());
5052

5153
// Pass the worker to the function which sets up the `oninput` callback.

examples/wasm-in-web-worker/worker.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
// Use ES module import syntax to import functionality from the module
2+
// that we have compiled.
3+
//
14
// The worker has its own scope and no direct access to functions/objects of the
2-
// global scope. We import the generated JS file to make `wasm_bindgen`
3-
// available which we need to initialize our Wasm code.
4-
importScripts('./pkg/wasm_in_web_worker.js');
5+
// global scope.
6+
import init, { NumberEval } from './pkg/wasm_in_web_worker.js';
7+
58

69
console.log('Initializing worker')
710

8-
// In the worker, we have a different struct that we want to use as in
9-
// `index.js`.
10-
const {NumberEval} = wasm_bindgen;
1111

1212
async function init_wasm_in_worker() {
13-
// Load the Wasm file by awaiting the Promise returned by `wasm_bindgen`.
14-
await wasm_bindgen('./pkg/wasm_in_web_worker_bg.wasm');
13+
// Load the Wasm file.
14+
await init();
1515

1616
// Create a new object of the `NumberEval` struct.
1717
var num_eval = NumberEval.new();

guide/src/examples/wasm-in-web-worker.md

-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ A simple example of parallel execution by spawning a web worker with `web_sys`,
88
loading Wasm code in the web worker and interacting between the main thread and
99
the worker.
1010

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-
1811
## `Cargo.toml`
1912

2013
The `Cargo.toml` enables features necessary to work with the DOM, log output to

0 commit comments

Comments
 (0)