Struct napi::threadsafe_function::ThreadsafeFunction
source · pub struct ThreadsafeFunction<T: 'static, ES: T = CalleeHandled> { /* private fields */ }
Expand description
Communicate with the addon’s main thread by invoking a JavaScript function from other threads.
Example
An example of using ThreadsafeFunction
:
#[macro_use]
extern crate napi_derive;
use std::thread;
use napi::{
threadsafe_function::{
ThreadSafeCallContext, ThreadsafeFunctionCallMode, ThreadsafeFunctionReleaseMode,
},
CallContext, Error, JsFunction, JsNumber, JsUndefined, Result, Status,
};
#[js_function(1)]
pub fn test_threadsafe_function(ctx: CallContext) -> Result<JsUndefined> {
let func = ctx.get::<JsFunction>(0)?;
let tsfn =
ctx
.env
.create_threadsafe_function(&func, 0, |ctx: ThreadSafeCallContext<Vec<u32>>| {
ctx.value
.iter()
.map(|v| ctx.env.create_uint32(*v))
.collect::<Result<Vec<JsNumber>>>()
})?;
let tsfn_cloned = tsfn.clone();
thread::spawn(move || {
let output: Vec<u32> = vec![0, 1, 2, 3];
// It's okay to call a threadsafe function multiple times.
tsfn.call(Ok(output.clone()), ThreadsafeFunctionCallMode::Blocking);
});
thread::spawn(move || {
let output: Vec<u32> = vec![3, 2, 1, 0];
// It's okay to call a threadsafe function multiple times.
tsfn_cloned.call(Ok(output.clone()), ThreadsafeFunctionCallMode::NonBlocking);
});
ctx.env.get_undefined()
}
Implementations§
source§impl<T: 'static, ES: T> ThreadsafeFunction<T, ES>
impl<T: 'static, ES: T> ThreadsafeFunction<T, ES>
sourcepub fn refer(&mut self, env: &Env) -> Result<()>
pub fn refer(&mut self, env: &Env) -> Result<()>
See napi_ref_threadsafe_function for more information.
“ref” is a keyword so that we use “refer” here.
sourcepub fn unref(&mut self, env: &Env) -> Result<()>
pub fn unref(&mut self, env: &Env) -> Result<()>
See napi_unref_threadsafe_function for more information.
pub fn aborted(&self) -> bool
pub fn abort(self) -> Result<()>
sourcepub fn raw(&self) -> napi_threadsafe_function
pub fn raw(&self) -> napi_threadsafe_function
Get the raw ThreadSafeFunction
pointer
source§impl<T: 'static> ThreadsafeFunction<T, CalleeHandled>
impl<T: 'static> ThreadsafeFunction<T, CalleeHandled>
sourcepub fn call(&self, value: Result<T>, mode: ThreadsafeFunctionCallMode) -> Status
pub fn call(&self, value: Result<T>, mode: ThreadsafeFunctionCallMode) -> Status
See napi_call_threadsafe_function for more information.
pub fn call_with_return_value<D: FromNapiValue, F: 'static + FnOnce(D) -> Result<()>>(
&self,
value: Result<T>,
mode: ThreadsafeFunctionCallMode,
cb: F
) -> Status
pub async fn call_async<D: 'static + FromNapiValue>(
&self,
value: Result<T>
) -> Result<D>
source§impl<T: 'static> ThreadsafeFunction<T, Fatal>
impl<T: 'static> ThreadsafeFunction<T, Fatal>
sourcepub fn call(&self, value: T, mode: ThreadsafeFunctionCallMode) -> Status
pub fn call(&self, value: T, mode: ThreadsafeFunctionCallMode) -> Status
See napi_call_threadsafe_function for more information.
pub fn call_with_return_value<D: FromNapiValue, F: 'static + FnOnce(D) -> Result<()>>(
&self,
value: T,
mode: ThreadsafeFunctionCallMode,
cb: F
) -> Status
pub async fn call_async<D: 'static + FromNapiValue>(&self, value: T) -> Result<D>
Trait Implementations§
source§impl<T: 'static, ES: T> Clone for ThreadsafeFunction<T, ES>
impl<T: 'static, ES: T> Clone for ThreadsafeFunction<T, ES>
source§impl<T: ToNapiValue + 'static, ES: T> FromNapiValue for ThreadsafeFunction<T, ES>
impl<T: ToNapiValue + 'static, ES: T> FromNapiValue for ThreadsafeFunction<T, ES>
source§unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> Result<Self>
unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> Result<Self>
Safety Read more