pub struct ServiceWorker { /* private fields */ }
Expand description
Connects Rust Handler with browser service worker via WASI filesystem.
ServiceWorker is a singleton which holds input and output file handles and owns woker 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().
Note: ServiceWorker supposed to operate in single threaded environment like a browser service worker.
TODO: it requires cleaning of filesystem, add drop implementation
Implementations§
Source§impl ServiceWorker
impl ServiceWorker
Sourcepub fn initialize(options: ServiceOptions) -> Result<()>
pub fn initialize(options: ServiceOptions) -> Result<()>
Initialize ServiceWorker instance. ServiceWorker operates as singleton, all struct methods are static. Unless initialized all methods will result in error io::ErrorKind::NotConnected.
Sourcepub fn set_message_handler(new_handler: Box<dyn Handler>)
pub fn set_message_handler(new_handler: Box<dyn Handler>)
Message handler is required to process incoming messages. Please note, there is no queue therefore messages received before handler initialized will be lost.
Sourcepub fn on_message() -> Result<usize>
pub fn on_message() -> Result<usize>
This method is a trigger This is workaround while we don’t have wasi::poll_oneoff, ideally we shall just poll and wait for FD_READ event.
Sourcepub fn post_message(msg: &[u8]) -> Result<()>
pub fn post_message(msg: &[u8]) -> Result<()>
Post message to external consumers
Example usage:
use wasi_worker::ServiceWorker;
ServiceWorker::post_message(b"mymesage");