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
use crate::{api, prelude::*};
use std::{marker::PhantomData, mem::MaybeUninit, os::raw::c_char};
#[derive(Debug, Copy, Clone)]
pub struct JsPromise<L: NapiValueT, R: NapiValueT>(
    pub(crate) JsValue,
    pub(crate) napi_deferred,
    PhantomData<L>,
    PhantomData<R>,
);
impl<L: NapiValueT + Copy, R: NapiValueT + Copy> JsPromise<L, R> {
    pub(crate) fn from_raw(value: JsValue, deferred: napi_deferred) -> JsPromise<L, R> {
        JsPromise(value, deferred, PhantomData, PhantomData)
    }
    pub fn env(&self) -> NapiEnv {
        self.0.env()
    }
    pub fn raw(&self) -> napi_value {
        self.0.raw()
    }
    pub fn value(&self) -> JsValue {
        self.0
    }
    
    pub fn new(env: NapiEnv) -> NapiResult<JsPromise<L, R>> {
        let mut deferred = MaybeUninit::uninit();
        let promise = napi_call!(
            =napi_create_promise,
            env,
            deferred.as_mut_ptr(),
        );
        let deferred = unsafe { deferred.assume_init() };
        Ok(Self::from_raw(JsValue(env, promise), deferred))
    }
    
    
    
    
    
    
    
    pub fn resolve(&self, resolution: L) -> NapiResult<()> {
        napi_call!(napi_resolve_deferred, self.env(), self.1, resolution.raw())
    }
    
    
    
    
    
    
    
    pub fn reject(&self, rejection: R) -> NapiResult<()> {
        napi_call!(napi_reject_deferred, self.env(), self.1, rejection.raw())
    }
}
impl<L: NapiValueT + Copy + 'static, R: NapiValueT + Copy + 'static> JsPromise<L, R> {
    
    pub fn spawn<T>(
        env: NapiEnv,
        mut work: impl FnMut(&mut T) + Send + 'static,
        mut complete: impl FnMut(Self, NapiStatus, T) -> NapiResult<()> + 'static,
    ) -> NapiResult<JsPromise<L, R>>
    where
        T: Default,
    {
        let promise: JsPromise<L, R> = JsPromise::new(env)?;
        env.async_work(
            "napi-promise-task",
            T::default(),
            move |state| work(state),
            
            move |_, status, state| complete(promise, status, state),
        )?
        .queue()?;
        Ok(promise)
    }
}
impl<L: NapiValueT + Copy, R: NapiValueT + Copy> NapiValueCheck for JsPromise<L, R> {
    fn check(&self) -> NapiResult<bool> {
        Ok(napi_call!(=napi_is_promise, self.env(), self.raw()))
    }
}