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> { /* 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);
});
}
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<()>
ThreadsafeFunction::clone
instead of manually increasing the reference countSee 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<()>
ThreadsafeFunction::clone
instead of manually decreasing the reference countSee napi_unref_threadsafe_function for more information.
pub fn aborted(&self) -> bool
pub fn abort(self) -> Result<()>
👎Deprecated since 2.17.0: 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
Trait Implementations§
Source§impl<T: 'static + JsValuesTupleIntoVec, Return: FromNapiValue, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> FromNapiValue for ThreadsafeFunction<T, Return, T, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
impl<T: 'static + JsValuesTupleIntoVec, Return: FromNapiValue, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> FromNapiValue for ThreadsafeFunction<T, Return, T, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
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
fn from_unknown(value: Unknown<'_>) -> Result<Self>
Source§impl<T: 'static + JsValuesTupleIntoVec, Return: FromNapiValue, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> TypeName for ThreadsafeFunction<T, Return, T, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
impl<T: 'static + JsValuesTupleIntoVec, Return: FromNapiValue, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> TypeName for ThreadsafeFunction<T, Return, T, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
Source§impl<T: 'static + JsValuesTupleIntoVec, Return: FromNapiValue, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> ValidateNapiValue for ThreadsafeFunction<T, Return, T, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
impl<T: 'static + JsValuesTupleIntoVec, Return: FromNapiValue, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> ValidateNapiValue for ThreadsafeFunction<T, Return, T, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
Source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Safety Read more
impl<T: 'static, Return: FromNapiValue, CallJsBackArgs: 'static + JsValuesTupleIntoVec, ErrorStatus: AsRef<str> + From<Status>, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> Send for 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> Sync for ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
Auto Trait Implementations§
impl<T, Return, CallJsBackArgs, ErrorStatus, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> Freeze for ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
impl<T, Return, CallJsBackArgs, ErrorStatus, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> RefUnwindSafe for ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>where
T: RefUnwindSafe,
CallJsBackArgs: RefUnwindSafe,
Return: RefUnwindSafe,
ErrorStatus: RefUnwindSafe,
impl<T, Return, CallJsBackArgs, ErrorStatus, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> Unpin for ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
impl<T, Return, CallJsBackArgs, ErrorStatus, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> UnwindSafe for ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more