[−][src]Crate wasi_worker
This crate provides rust library to easily compose WASM/WASI browser service worker.
General overview
ServiceWorker is a singleton which holds input and output file handles and owns worker via Handler trait. Worker is supposedly reactive, usually operating on incoming events (on_message) and posting messages to main browser application via ServiceWorker::post_message().
Example usage:
use wasi_worker::*; struct MyWorker {} impl Handler for MyWorker { fn on_message(&self, msg: &[u8]) -> std::io::Result<()> { // Process incoming message println!("My Worker got message: {:?}", msg); Ok(()) } } fn main() { // In WASI setup with JS glue all output will be posted to memfs::/output.bin // In native OS to be able to run test from shell output goes to ./output.bin let opt = ServiceOptions::default().with_cleanup(); let output_file = match &opt.output { FileOptions::File(path) => path.clone() }; ServiceWorker::initialize(opt) .expect("ServiceWorker::initialize"); // Attach Agent to ServiceWorker as message handler singleton ServiceWorker::set_message_handler(Box::new(MyWorker {})); // Send binary message to main browser application ServiceWorker::post_message(b"message") .expect("ServiceWorker.post_message"); } // this function will be called from worker.js when it receives message // In the future it will be substituted by poll_oneoff or thread::yield, // though currently poll_oneoff does not return control to browser pub extern "C" fn message_ready() -> usize { ServiceWorker::on_message() .expect("ServiceWorker.on_message") }
Structs
ServiceOptions | Options for ServiceWorker |
ServiceWorker | Connects Rust Handler with browser service worker via WASI filesystem. |
Enums
FileOptions | Instructs on file descriptor configuration for ServiceWorker |
Traits
Handler | Handler for incoming messages via ServiceWorker |