1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
}
}