#web-worker #worker

worked

Run functions in Web Workers with ease

2 releases

0.0.2 May 6, 2024
0.0.1 May 3, 2024

#634 in WebAssembly

Download history 8/week @ 2024-09-24 2/week @ 2024-10-01

63 downloads per month

MIT license

16KB
155 lines

🤖 worked 🦀

Crates.io Version docs.rs

Run wasm code in workers, without blocking

use worked::*;

#[wasm_bindgen(start)]
pub async fn start() {
    for i in 0..20 {
        factorial(
            i.clone(), // <-- Function input
            move |o| gloo::console::log!(&format!("{i}! = {o}")) // <-- Callback
        ).await; // <-- Await the spawning of the worker
    }
}

#[worked("/pkg/wasm_workers.js")] // <-- Absolute path to your main wasm_bindgen export
pub fn factorial(n: i64) -> i64 { // <-- Functions can only take one input
    f(n)
}

#[wasm_bindgen]
pub fn f(n: i64) -> i64 {
    match n {
        0 => 1,
        _ => n * f(n - 1),
    }
}

Dependencies

~15–25MB
~347K SLoC