use std::ops::Deref;
use wasm_bindgen::JsValue;
use crate::worker_sys::{AbortController as AbortControllerSys, AbortSignal as AbortSignalSys};
#[derive(Debug)]
pub struct AbortController {
inner: AbortControllerSys,
}
impl AbortController {
pub fn signal(&self) -> AbortSignal {
AbortSignal::from(self.inner.signal())
}
pub fn abort(self) {
self.inner.abort(JsValue::undefined())
}
pub fn abort_with_reason(self, reason: impl Into<JsValue>) {
self.inner.abort(reason.into())
}
}
impl Default for AbortController {
fn default() -> Self {
Self {
inner: AbortControllerSys::new(),
}
}
}
#[derive(Debug)]
pub struct AbortSignal {
inner: AbortSignalSys,
}
impl AbortSignal {
pub fn aborted(&self) -> bool {
self.inner.aborted()
}
pub fn reason(&self) -> Option<JsValue> {
self.aborted().then(|| self.inner.reason())
}
pub fn abort() -> Self {
Self::from(AbortSignalSys::abort(JsValue::undefined()))
}
pub fn abort_with_reason(reason: impl Into<JsValue>) -> Self {
let reason = reason.into();
Self::from(AbortSignalSys::abort(reason))
}
}
impl From<AbortSignalSys> for AbortSignal {
fn from(inner: AbortSignalSys) -> Self {
Self { inner }
}
}
impl Deref for AbortSignal {
type Target = AbortSignalSys;
fn deref(&self) -> &Self::Target {
&self.inner
}
}