#[cfg(not(feature = "safe_lang"))]
#[cfg(all(feature = "unsafe_ffi", feature = "alloc", not(windows)))]
use crate::String;
#[allow(unused_imports)]
use crate::{TaskPoll, Web, js_uint32};
#[doc = crate::_tags!(web uid)]
#[doc = crate::_doc_location!("sys/os/browser/web")]
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WebWorker {
pub(crate) id: js_uint32,
}
#[rustfmt::skip]
impl WebWorker {
pub const fn invalid() -> Self { WebWorker { id: 0 } }
pub const fn id(self) -> js_uint32 { self.id }
}
#[rustfmt::skip]
#[cfg(not(feature = "safe_lang"))]
#[cfg(all(feature = "unsafe_ffi", not(windows)))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_ffi")))]
#[cfg_attr(nightly_doc, doc(cfg(target_arch = "wasm32")))]
impl WebWorker {
pub fn spawn(script: &str) -> Result<Self, WebWorkerError> { Web::worker_spawn(script) }
pub fn stop(self) { Web::worker_stop(self); }
pub fn is_active(self) -> bool { Web::worker_is_active(self) }
pub fn send_message(self, message: &str) { Web::worker_send_message(self, message); }
pub fn eval(self, js_code: &str) -> Result<WebWorkerJob, WebWorkerError> {
if !self.is_active() { return Err(WebWorkerError::WorkerNotFound); }
let job = Web::worker_eval(self, js_code);
if job.id == 0 { return Err(WebWorkerError::WorkerNotFound); }
Ok(job)
}
}
#[doc = crate::_tags!(web error)]
#[doc = crate::_doc_location!("sys/os/browser/web")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WebWorkerError {
InvalidScript,
WorkerNotFound,
JobNotFound,
}
#[doc = crate::_tags!(web uid)]
#[doc = crate::_doc_location!("sys/os/browser/web")]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct WebWorkerJob {
pub(crate) worker: WebWorker,
pub(crate) id: js_uint32,
}
#[rustfmt::skip]
impl WebWorkerJob {
pub const fn worker(self) -> WebWorker { self.worker }
pub const fn id(self) -> js_uint32 { self.id }
}
#[rustfmt::skip]
#[cfg(not(feature = "safe_lang"))]
#[cfg(all(feature = "unsafe_ffi", not(windows)))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_ffi")))]
#[cfg_attr(nightly_doc, doc(cfg(target_arch = "wasm32")))]
impl WebWorkerJob {
pub fn poll(self, buffer: &mut [u8]) -> TaskPoll<Result<usize, WebWorkerError>> {
Web::worker_poll(self, buffer)
}
#[cfg(feature = "alloc")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "alloc")))]
pub fn poll_alloc(self) -> TaskPoll<Result<String, WebWorkerError>> {
Web::worker_poll_alloc(self)
}
pub fn cancel(self) { Web::worker_eval_cancel(self); }
}