"Hello, world!"
12 | 13 | 14 | 15 | } 16 | } 17 | 18 | fn main() { 19 | mount_to_body(|| view! {Test | 28 |Outcome | 29 |Expected | 30 |
---|---|---|
copy-dir |
34 | ![]() |
35 | Should see PNG image | 36 |
copy-file |
40 | Should see SVG image | 42 |
See the console for the thread output
21 | 22 | 23 | -------------------------------------------------------------------------------- /examples/wasm_threads/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | # Before upgrading check that everything is available on all tier1 targets here: 2 | # https://rust-lang.github.io/rustup-components-history 3 | [toolchain] 4 | channel = "nightly-2024-08-02" 5 | targets = ["wasm32-unknown-unknown"] 6 | components = ["rust-src", "rustfmt", "clippy"] 7 | -------------------------------------------------------------------------------- /examples/wasm_threads/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::time::Duration; 2 | 3 | use wasm_thread as thread; 4 | 5 | fn main() { 6 | #[cfg(target_arch = "wasm32")] 7 | { 8 | console_log::init().unwrap(); 9 | console_error_panic_hook::set_once(); 10 | } 11 | 12 | #[cfg(not(target_arch = "wasm32"))] 13 | env_logger::init_from_env(env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info")); 14 | 15 | log::info!("Available parallelism: {:?}", thread::available_parallelism()); 16 | 17 | let mut threads = vec![]; 18 | 19 | for _ in 0..2 { 20 | threads.push(thread::spawn(|| { 21 | for i in 1..3 { 22 | log::info!("hi number {} from the spawned thread {:?}!", i, thread::current().id()); 23 | thread::sleep(Duration::from_millis(1)); 24 | } 25 | })); 26 | } 27 | 28 | for i in 1..3 { 29 | log::info!("hi number {} from the main thread {:?}!", i, thread::current().id()); 30 | } 31 | 32 | // It's not possible to do a scope on the main thread, because blocking waits are not supported, but we can use 33 | // scope inside web workers. 34 | threads.push(thread::spawn(|| { 35 | log::info!("Start scope test on thread {:?}", thread::current().id()); 36 | 37 | let mut a = vec![1, 2, 3]; 38 | let mut x = 0; 39 | 40 | thread::scope(|s| { 41 | let handle = s.spawn(|| { 42 | log::info!("hello from the first scoped thread {:?}", thread::current().id()); 43 | // We can borrow `a` here. 44 | log::info!("a = {:?}", &a); 45 | // Return a subslice of borrowed `a` 46 | &a[0..2] 47 | }); 48 | 49 | // Wait for the returned value from first thread 50 | log::info!("a[0..2] = {:?}", handle.join().unwrap()); 51 | 52 | s.spawn(|| { 53 | log::info!("hello from the second scoped thread {:?}", thread::current().id()); 54 | // We can even mutably borrow `x` here, 55 | // because no other threads are using it. 56 | x += a[0] + a[2]; 57 | }); 58 | 59 | log::info!( 60 | "Hello from scope \"main\" thread {:?} inside scope.", 61 | thread::current().id() 62 | ); 63 | }); 64 | 65 | // After the scope, we can modify and access our variables again: 66 | a.push(4); 67 | assert_eq!(x, a.len()); 68 | log::info!("Scope done x = {}, a.len() = {}", x, a.len()); 69 | })); 70 | 71 | // Wait for all threads, otherwise program exits before threads finish execution. 72 | // We can't do blocking join on wasm main thread though, but the browser window will continue running. 73 | #[cfg(not(target_arch = "wasm32"))] 74 | for handle in threads { 75 | handle.join().unwrap(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /examples/webworker-gloo/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "webworker-gloo" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [[bin]] 7 | name = "app" 8 | path = "src/bin/app.rs" 9 | 10 | [[bin]] 11 | name = "worker" 12 | path = "src/bin/worker.rs" 13 | 14 | [dependencies] 15 | console_error_panic_hook = "0.1" 16 | gloo-console = "0.3" 17 | gloo-worker = "0.5" 18 | wasm-bindgen = "0.2" 19 | web-sys = { version = "0.3", features = ["console"] } 20 | -------------------------------------------------------------------------------- /examples/webworker-gloo/Trunk.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "index.html" 3 | dist = "dist" 4 | -------------------------------------------------------------------------------- /examples/webworker-gloo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |{"Trunk is a WASM web application bundler for Rust."}
46 | })} 47 | {view_card("Yew", Some("yew.svg"), html! { 48 |{"Yew is a modern Rust framework for creating multi-threaded front-end web apps with WebAssembly."}
49 | })} 50 | {view_card("Tailwind CSS", None, html! { 51 |{"Tailwind CSS is a library for styling markup using a comprehensive set of utility classes, no CSS required."}
52 | })} 53 |{"The current location is: "} {location}
34 |{{ page.description }}
31 | {% endif %} 32 | 33 | {{ page.content | safe }} 34 | {% endblock content %} 35 | -------------------------------------------------------------------------------- /site/templates/blog.html: -------------------------------------------------------------------------------- 1 | {% import "juice/templates/_macros.html" as macros %} 2 | {% extends "index.html" %} 3 | 4 | {% block title %}{{ section.title }} | {{ super() }} {% endblock title %} 5 | 6 | {% block head %} 7 | {% block rss %} 8 | 9 | {% endblock %} 10 | {% endblock head %} 11 | 12 | {% block header %} 13 |{{ post.description }}
38 | {% endfor %} 39 | 40 | {% endblock content %} 41 | -------------------------------------------------------------------------------- /site/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "juice/templates/index.html" %} 2 | 3 | {% block head %} 4 | 5 | 6 | {% endblock head %} 7 | 8 | {% block hero %} 9 | 10 |