Struct ServiceWorker

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn post_message(msg: &[u8]) -> Result<()>

Post message to external consumers

Example usage:

use wasi_worker::ServiceWorker;
ServiceWorker::post_message(b"mymesage");
Source

pub fn kill()

Trait Implementations§

Source§

impl Drop for ServiceWorker

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.