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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//!
//! Abortable trigger, can be used to cancel (abort) an asynchronous task.
//!

use wasm_bindgen::prelude::*;

use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};

/// Error emitted by [`Abortable`].
/// @category General
#[wasm_bindgen]
pub struct Aborted;

impl std::error::Error for Aborted {}

impl std::fmt::Debug for Aborted {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "task aborted")
    }
}

impl std::fmt::Display for Aborted {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "task aborted")
    }
}

///
/// Abortable trigger wraps an `Arc<AtomicBool>`, which can be cloned
/// to signal task terminating using an atomic bool.
///
/// ```text
/// let abortable = Abortable::default();
/// let result = my_task(abortable).await?;
/// // ... elsewhere
/// abortable.abort();
/// ```
///
/// @category General
#[derive(Default, Clone)]
#[wasm_bindgen]
pub struct Abortable(Arc<AtomicBool>);

#[wasm_bindgen]
impl Abortable {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self(Arc::new(AtomicBool::new(false)))
    }

    #[inline]
    #[wasm_bindgen(js_name=isAborted)]
    pub fn is_aborted(&self) -> bool {
        self.0.load(Ordering::SeqCst)
    }

    #[inline]
    pub fn abort(&self) {
        self.0.store(true, Ordering::SeqCst);
    }

    #[inline]
    pub fn check(&self) -> Result<(), Aborted> {
        if self.is_aborted() {
            Err(Aborted)
        } else {
            Ok(())
        }
    }

    #[inline]
    pub fn reset(&self) {
        self.0.store(false, Ordering::SeqCst);
    }
}

impl TryFrom<&JsValue> for Abortable {
    type Error = JsValue;
    fn try_from(value: &JsValue) -> Result<Self, Self::Error> {
        use wasm_bindgen::convert::*;

        let idx = IntoWasmAbi::into_abi(value);
        #[link(wasm_import_module = "__wbindgen_placeholder__")]
        #[cfg(all(
            target_arch = "wasm32",
            not(any(target_os = "emscripten", target_os = "wasi"))
        ))]
        extern "C" {
            fn __wbg_abortable_unwrap(ptr: u32) -> u32;
        }
        #[cfg(not(all(
            target_arch = "wasm32",
            not(any(target_os = "emscripten", target_os = "wasi"))
        )))]
        unsafe fn __wbg_abortable_unwrap(_: u32) -> u32 {
            panic!("cannot convert from JsValue outside of the wasm target")
        }
        let ptr = unsafe { __wbg_abortable_unwrap(idx) };
        if ptr == 0 {
            wasm_bindgen::__rt::std::result::Result::Err(value.clone())
        } else {
            unsafe {
                wasm_bindgen::__rt::std::result::Result::Ok(
                    <Self as FromWasmAbi>::from_abi(ptr).clone(),
                )
            }
        }
    }
}