pub struct ThreadsafeFunction<T: 'static, Return: 'static + FromNapiValue = Unknown<'static>, CallJsBackArgs: 'static + JsValuesTupleIntoVec = T, ErrorStatus: AsRef<str> + From<Status> = Status, const CalleeHandled: bool = true, const Weak: bool = false, const MaxQueueSize: usize = 0> {
pub handle: Arc<ThreadsafeFunctionHandle>,
/* 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:
use std::thread;
use std::sync::Arc;
use napi::{
threadsafe_function::{
ThreadSafeCallContext, ThreadsafeFunctionCallMode, ThreadsafeFunctionReleaseMode,
},
};
use napi_derive::napi;
#[napi]
pub fn call_threadsafe_function(callback: Arc<ThreadsafeFunction<(u32, bool, String), ()>>) {
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((1, false, "NAPI-RS".into())), ThreadsafeFunctionCallMode::Blocking);
tsfn.call(Ok((2, true, "NAPI-RS".into())), ThreadsafeFunctionCallMode::NonBlocking);
});
thread::spawn(move || {
tsfn_cloned.call((3, false, "NAPI-RS".into())), ThreadsafeFunctionCallMode::NonBlocking);
});
}Fields§
§handle: Arc<ThreadsafeFunctionHandle>Implementations§
Source§impl<T: 'static, Return: FromNapiValue, CallJsBackArgs: 'static + JsValuesTupleIntoVec, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
impl<T: 'static, Return: FromNapiValue, CallJsBackArgs: 'static + JsValuesTupleIntoVec, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
Sourcepub fn refer(&mut self, env: &Env) -> Result<()>
👎Deprecated since 2.17.0: Please use ThreadsafeFunction::clone instead of manually increasing the reference count
pub fn refer(&mut self, env: &Env) -> Result<()>
Please use ThreadsafeFunction::clone instead of manually increasing the reference count
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<()>
👎Deprecated since 2.17.0: Please use ThreadsafeFunction::clone instead of manually decreasing the reference count
pub fn unref(&mut self, env: &Env) -> Result<()>
Please use ThreadsafeFunction::clone instead of manually decreasing the reference count
See napi_unref_threadsafe_function for more information.
pub fn aborted(&self) -> bool
pub fn abort(self) -> Result<()>
Drop all references to the ThreadsafeFunction will automatically release it
Sourcepub fn raw(&self) -> napi_threadsafe_function
pub fn raw(&self) -> napi_threadsafe_function
Get the raw ThreadSafeFunction pointer
Source§impl<T: 'static, Return: FromNapiValue, CallJsBackArgs: 'static + JsValuesTupleIntoVec, ErrorStatus: AsRef<str> + From<Status>, const Weak: bool, const MaxQueueSize: usize> ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, true, Weak, MaxQueueSize>
impl<T: 'static, Return: FromNapiValue, CallJsBackArgs: 'static + JsValuesTupleIntoVec, ErrorStatus: AsRef<str> + From<Status>, const Weak: bool, const MaxQueueSize: usize> ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, true, Weak, MaxQueueSize>
Sourcepub fn call(
&self,
value: Result<T, ErrorStatus>,
mode: ThreadsafeFunctionCallMode,
) -> Status
pub fn call( &self, value: Result<T, ErrorStatus>, mode: ThreadsafeFunctionCallMode, ) -> Status
See napi_call_threadsafe_function for more information.
Sourcepub fn call_with_return_value<F: 'static + FnOnce(Result<Return>, Env) -> Result<()>>(
&self,
value: Result<T, ErrorStatus>,
mode: ThreadsafeFunctionCallMode,
cb: F,
) -> Status
pub fn call_with_return_value<F: 'static + FnOnce(Result<Return>, Env) -> Result<()>>( &self, value: Result<T, ErrorStatus>, mode: ThreadsafeFunctionCallMode, cb: F, ) -> Status
Call the ThreadsafeFunction, and handle the return value with a callback
Sourcepub async fn call_async(&self, value: Result<T, ErrorStatus>) -> Result<Return>
pub async fn call_async(&self, value: Result<T, ErrorStatus>) -> Result<Return>
Call the ThreadsafeFunction, and handle the return value with in async way
Source§impl<T: 'static, Return: FromNapiValue, CallJsBackArgs: 'static + JsValuesTupleIntoVec, ErrorStatus: AsRef<str> + From<Status>, const Weak: bool, const MaxQueueSize: usize> ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, false, Weak, MaxQueueSize>
impl<T: 'static, Return: FromNapiValue, CallJsBackArgs: 'static + JsValuesTupleIntoVec, ErrorStatus: AsRef<str> + From<Status>, const Weak: bool, const MaxQueueSize: usize> ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, false, Weak, MaxQueueSize>
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.
Sourcepub fn call_with_return_value<F: 'static + FnOnce(Result<Return>, Env) -> Result<()>>(
&self,
value: T,
mode: ThreadsafeFunctionCallMode,
cb: F,
) -> Status
pub fn call_with_return_value<F: 'static + FnOnce(Result<Return>, Env) -> Result<()>>( &self, value: T, mode: ThreadsafeFunctionCallMode, cb: F, ) -> Status
Call the ThreadsafeFunction, and handle the return value with a callback
Sourcepub async fn call_async(&self, value: T) -> Result<Return>
pub async fn call_async(&self, value: T) -> Result<Return>
Call the ThreadsafeFunction, and handle the return value with in async way