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
use crate::{api, prelude::*};
use std::{marker::PhantomData, mem::MaybeUninit, os::raw::c_char};

#[derive(Debug)]
pub struct JsPromise<L, R>(
    pub(crate) JsValue,
    pub(crate) napi_deferred,
    PhantomData<L>,
    PhantomData<R>,
);

impl<L, R> Clone for JsPromise<L, R> {
    fn clone(&self) -> Self {
        JsPromise(self.0, self.1, PhantomData, PhantomData)
    }
}

impl<L: NapiValueT, R: NapiValueT> 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
    }

    /// This API creates a deferred object and a JavaScript promise.
    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))
    }

    /// Spawn a busy task in the libuv pool.
    pub fn spawn<T>(
        env: NapiEnv,
        mut work: impl FnMut(&mut T) + Send,
        mut complete: impl FnMut(Self, NapiStatus, T) -> NapiResult<()>,
    ) -> 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),
            // NB: execute in the main js thread.
            |_, status, state| complete(promise.clone(), status, state),
        )?
        .queue()?;

        Ok(promise)
    }

    /// This API resolves a JavaScript promise by way of the deferred object with which it is
    /// associated. Thus, it can only be used to resolve JavaScript promises for which the
    /// corresponding deferred object is available. This effectively means that the promise must
    /// have been created using napi_create_promise() and the deferred object returned from that
    /// call must have been retained in order to be passed to this API.
    ///
    /// The deferred object is freed upon successful completion.
    pub fn resolve(&self, resolution: L) -> NapiResult<()> {
        napi_call!(napi_resolve_deferred, self.env(), self.1, resolution.raw())
    }

    /// This API rejects a JavaScript promise by way of the deferred object with which it is
    /// associated. Thus, it can only be used to reject JavaScript promises for which the
    /// corresponding deferred object is available. This effectively means that the promise
    /// must have been created using napi_create_promise() and the deferred object returned
    /// from that call must have been retained in order to be passed to this API.
    ///
    /// The deferred object is freed upon successful completion.
    pub fn reject(&self, rejection: R) -> NapiResult<()> {
        napi_call!(napi_reject_deferred, self.env(), self.1, rejection.raw())
    }
}

impl<L: NapiValueT, R: NapiValueT> NapiValueCheck for JsPromise<L, R> {
    fn check(&self) -> NapiResult<bool> {
        Ok(napi_call!(=napi_is_promise, self.env(), self.raw()))
    }
}