use super::{FileOptions, ServiceOptions};
use std::cell::RefCell;
use std::fs::File;
use std::io::{self, Read, Write};
/// 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
pub struct ServiceWorker {
output: File,
input: io::Stdin,
options: ServiceOptions,
}
/// Handler for incoming messages via ServiceWorker
pub trait Handler {
fn on_message(&self, msg: &[u8]) -> std::io::Result<()>;
}
thread_local! {
static SERVICE: RefCell