pub struct NapiAsyncWork<T>(/* private fields */);
Implementations§
Source§impl<T> NapiAsyncWork<T>
impl<T> NapiAsyncWork<T>
pub fn env(&self) -> NapiEnv
pub fn raw(&self) -> napi_async_work
Sourcepub fn new(
env: NapiEnv,
name: impl AsRef<str>,
state: T,
execute: impl FnMut(&mut T) + Send + 'static,
complete: impl FnMut(NapiEnv, NapiStatus, T) -> NapiResult<()> + 'static,
) -> NapiResult<NapiAsyncWork<T>>
pub fn new( env: NapiEnv, name: impl AsRef<str>, state: T, execute: impl FnMut(&mut T) + Send + 'static, complete: impl FnMut(NapiEnv, NapiStatus, T) -> NapiResult<()> + 'static, ) -> NapiResult<NapiAsyncWork<T>>
This API allocates a work object that is used to execute logic asynchronously. It should be freed using napi_delete_async_work once the work is no longer required. async_resource_name should be a null-terminated, UTF-8-encoded string.
The async_resource_name identifier is provided by the user and should be representative of the type of async work being performed. It is also recommended to apply namespacing to the identifier, e.g. by including the module name. See the async_hooks documentation for more information.
§Arguments
env
- napi_envname
- napi async work identifierstate
- The state shared betweenexecute
&complete
execute
- The native function which should be called to execute the logic asynchronously. The given function is called from a worker pool thread and can execute in parallel with the main event loop thread.complete
- The native function which will be called when the asynchronous logic is completed or is cancelled. The given function is called from the main event loop thread.
Sourcepub fn queue(&mut self) -> NapiResult<Option<()>>
pub fn queue(&mut self) -> NapiResult<Option<()>>
This API requests that the previously allocated work be scheduled for execution. Once it returns successfully, this API must not be called again with the same napi_async_work item or the result will be undefined.
NB: The NapiAsyncWork
can not be queued more than once.
Sourcepub fn cancel(&self) -> NapiResult<()>
pub fn cancel(&self) -> NapiResult<()>
This API cancels queued work if it has not yet been started. If it has already started executing, it cannot be cancelled and napi_generic_failure will be returned. If successful, the complete callback will be invoked with a status value of napi_cancelled. The work should not be deleted before the complete callback invocation, even if it has been successfully cancelled.
This API can be called even if there is a pending JavaScript exception.
Sourcepub fn delete(self) -> NapiResult<()>
pub fn delete(self) -> NapiResult<()>
This API frees a previously allocated work object. This API can be called even if there is a pending JavaScript exception.
NB: should not delete a queued task.