wasi-worker-yew

Crates.iowasi-worker-yew
lib.rswasi-worker-yew
version0.5.2
sourcesrc
created_at2019-11-18 16:13:00.091808
updated_at2020-02-07 15:20:16.917283
descriptionWASM/WASI browser service worker for yew (Agent)
homepage
repositoryhttps://github.com/dunnock/wasi-worker/tree/master/crates/wasi-worker-yew
max_upload_size
id182240
size8,955
Maxim Vorobjov (dunnock)

documentation

README

Yew worker compiled to wasm32-wasi

This library allows to compile and deploy yew worker with wasm32-wasi target. It allows to compile and run POSIX-like applications, having access to timer, random numbers and to emulated file system (memfs).

On some operations wasi workers run faster than wasm-bindgen or stdweb.

Example usage:

In your Cargo.toml

wasi-worker-yew = "0.4"

In src/bin/worker.rs

use wasi_worker_yew::{ThreadedWASI, WASIAgent};
use yew::agent::*;
use wasi_worker::{FileOptions, ServiceOptions, ServiceWorker};

pub struct MyAgent;
impl Agent for MyAgent {
    type Reach = Public;
    type Message = String;
    type Input = String;
    type Output = String;
    fn create(_link: AgentLink<Self>) -> Self { MyAgent { } }
    fn update(&mut self, _msg: Self::Message) { /* ... */ }
    fn handle(&mut self, _msg: Self::Input, _who: HandlerId) { /* */ }
    fn name_of_resource() -> &'static str {
        "worker.js"
    }
};

fn main() {
  let opt = ServiceOptions::default().with_cleanup();
  ServiceWorker::initialize(opt)
    .expect("ServiceWorker::initialize");
  ServiceWorker::set_message_handler(Box::new(WASIAgent::<MyAgent>::new()));
}

Deploy

To simplify build and deploy you may use wasi-worker-cli:

cargo install wasi-worker-cli --force

Deployment script will place compiled and optimized worker target and JS glue code under ./dist:

wasiworker deploy
Commit count: 88

cargo fmt