Napi Async Local Extension
This crate extends napi-rs with the ability to run local futures.
Run local futures with:
use *;
use napi;
use SpawnLocalExt;
This allows for the usage of Channels, Timers and other async utilities in Rust without blocking the main JavaScript thread while retaining the capability of interacting with the underlying JavaScript values.
Installation
Install the crate with:
cargo add napi_async_local
Usage
Use env.spawn_local()
to spawn a non-blocking future on the local thread. Returns a Promise with the value
returned in the async closure.
To ensure the availability of NapiValue
types beyond the life of the parent function scope,
ensure that NapiValue
types that will be used in an async closure are wrapped in a JsRc
which
delegates GC to Rust.
Timers & Callbacks
use Duration;
use *;
use napi;
use JsRc;
use JsRcExt;
use SpawnLocalExt;
use task;
import napi from './napi.node'
napi.
Channels and Threads
You may combine OS threads with async channels to coordinate off-thread workloads.
I recommend using async_std for async utilities as the custom Futures reactor is not compatible with Tokio utilities.
use thread;
use Duration;
use *;
use napi;
use JsRc;
use JsRcExt;
use SpawnLocalExt;
use channel;
Development
To setup the development environment ensure you have installed just
, then run:
npm install
just run example-a