Skip to content

Commit

Permalink
Add web worker support (#285)
Browse files Browse the repository at this point in the history
* Add data-type attribute to rust links

This allows web workers to be included in `index.html`. Since this makes the
rust-worker attribute unnecessary it has been removed.

Also adds an example showing basic usage and updates the documentation.

* Adjust worker file naming and update samples

Co-authored-by: Dominik Nakamura <[email protected]>
  • Loading branch information
kristoff3r and dnaka91 authored Feb 26, 2022
1 parent d46ba85 commit b989bc9
Show file tree
Hide file tree
Showing 17 changed files with 825 additions and 83 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe
### added
- Added the `--address` option for `trunk serve`.
- Open autoreload websocket using wss when assets are served over a secure connection.
- Added the `data-type` attribute to Rust assets. Can be set to either `main` (previous behaviour and default) or `worker`, which builds the asset and includes it as a web worker.

### changed
- Bump notify to 5.0.0-pre.13, which fixes [notify-rs/notify#356](https://github.com/notify-rs/notify/issues/356)
Expand Down
317 changes: 317 additions & 0 deletions examples/webworker-gloo/Cargo.lock

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

11 changes: 11 additions & 0 deletions examples/webworker-gloo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "webworker-gloo"
version = "0.1.0"
edition = "2021"

[dependencies]
console_error_panic_hook = "0.1.7"
gloo-worker = "0.1.0"
wasm-bindgen = "0.2.79"
web-sys = { version = "0.3.56", features = ["console"] }
wee_alloc = "0.4.5"
14 changes: 14 additions & 0 deletions examples/webworker-gloo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Trunk | Web worker | Gloo</title>

<base data-trunk-public-url />
</head>
<body>
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="app" data-type="main" />
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="worker" data-type="worker" />
</body>
</html>
20 changes: 20 additions & 0 deletions examples/webworker-gloo/src/bin/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::rc::Rc;

use gloo_worker::Bridged;
use webworker_gloo::Multiplier;

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

fn main() {
console_error_panic_hook::set_once();

let bridge = Multiplier::bridge(Rc::new(Box::new(|((a, b), result)| {
web_sys::console::log_1(&format!("{a} x {b} = {result}").into());
})));
let bridge = Box::leak(bridge);

bridge.send((2, 5));
bridge.send((3, 3));
bridge.send((50, 5));
}
11 changes: 11 additions & 0 deletions examples/webworker-gloo/src/bin/worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use gloo_worker::PublicWorker;
use webworker_gloo::Multiplier;

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

fn main() {
console_error_panic_hook::set_once();

Multiplier::register();
}
Loading

0 comments on commit b989bc9

Please sign in to comment.