Struct nodex_api::context::NapiAsyncContext
source · [−]pub struct NapiAsyncContext { /* private fields */ }
Implementations
sourceimpl NapiAsyncContext
impl NapiAsyncContext
pub fn env(&self) -> NapiEnv
pub fn raw(&self) -> napi_async_context
sourcepub fn new(env: NapiEnv, name: impl AsRef<str>) -> NapiResult<NapiAsyncContext>
pub fn new(env: NapiEnv, name: impl AsRef<str>) -> NapiResult<NapiAsyncContext>
The async_resource object needs to be kept alive until napi_async_destroy to keep async_hooks related API acts correctly. In order to retain ABI compatibility with previous versions, napi_async_contexts are not maintaining the strong reference to the async_resource objects to avoid introducing causing memory leaks. However, if the async_resource is garbage collected by JavaScript engine before the napi_async_context was destroyed by napi_async_destroy, calling napi_async_context related APIs like napi_open_callback_scope and napi_make_callback can cause problems like loss of async context when using the AsyncLocalStorage API. In order to retain ABI compatibility with previous versions, passing NULL for async_resource does not result in an error. However, this is not recommended as this will result poor results with async_hooks init hooks and async_hooks.executionAsyncResource() as the resource is now required by the underlying async_hooks implementation in order to provide the linkage between async callbacks.
sourcepub fn destroy(&mut self) -> NapiResult<()>
pub fn destroy(&mut self) -> NapiResult<()>
This API can be called even if there is a pending JavaScript exception.
sourcepub fn make_callback<R, T>(
&self,
this: JsObject,
func: Function<R>,
args: T
) -> NapiResult<R> where
R: NapiValueT,
T: ToJsArgs,
pub fn make_callback<R, T>(
&self,
this: JsObject,
func: Function<R>,
args: T
) -> NapiResult<R> where
R: NapiValueT,
T: ToJsArgs,
This method allows a JavaScript function object to be called from a native add-on. This API is similar to napi_call_function. However, it is used to call from native code back into JavaScript after returning from an async operation (when there is no other script on the stack). It is a fairly simple wrapper around node::MakeCallback.
Note it is not necessary to use napi_make_callback from within a napi_async_complete_callback; in that situation the callback’s async context has already been set up, so a direct call to napi_call_function is sufficient and appropriate. Use of the napi_make_callback function may be required when implementing custom async behavior that does not use napi_create_async_work.
Any process.nextTicks or Promises scheduled on the microtask queue by JavaScript during he callback are ran before returning back to C/C++.
sourcepub fn scope(&self) -> NapiResult<NapiCallbackScope>
pub fn scope(&self) -> NapiResult<NapiCallbackScope>
There are cases (for example, resolving promises) where it is necessary to have the equivalent of the scope associated with a callback in place when making certain Node-API calls. If there is no other script on the stack the napi_open_callback_scope and napi_close_callback_scope functions can be used to open/close the required scope.
Trait Implementations
sourceimpl Clone for NapiAsyncContext
impl Clone for NapiAsyncContext
sourcefn clone(&self) -> NapiAsyncContext
fn clone(&self) -> NapiAsyncContext
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for NapiAsyncContext
impl Debug for NapiAsyncContext
Auto Trait Implementations
impl RefUnwindSafe for NapiAsyncContext
impl !Send for NapiAsyncContext
impl !Sync for NapiAsyncContext
impl Unpin for NapiAsyncContext
impl UnwindSafe for NapiAsyncContext
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more