#worker #web-worker

工作

轻松在Web Workers中运行函数

2个版本

0.0.2 2024年5月6日
0.0.1 2024年5月3日

#387 in WebAssembly

MIT许可证

16KB
155

🤖工作🦀

Crates.io Version docs.rs

在不阻塞的情况下在workers中运行wasm代码

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),
    }
}

依赖

~18MB
~349K SLoC