ThreadsafeFunction

Struct ThreadsafeFunction 

Source
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>

Source

pub fn refer(&mut self, env: &Env) -> Result<()>

👎Deprecated since 2.17.0: 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.

Source

pub fn unref(&mut self, env: &Env) -> Result<()>

👎Deprecated since 2.17.0: Please use ThreadsafeFunction::clone instead of manually decreasing the reference count

See napi_unref_threadsafe_function for more information.

Source

pub fn aborted(&self) -> bool

Source

pub fn abort(self) -> Result<()>

👎Deprecated since 2.17.0: Drop all references to the ThreadsafeFunction will automatically release it
Source

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>

Source

pub fn call( &self, value: Result<T, ErrorStatus>, mode: ThreadsafeFunctionCallMode, ) -> Status

See napi_call_threadsafe_function for more information.

Source

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

Source

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>

Source

pub fn call(&self, value: T, mode: ThreadsafeFunctionCallMode) -> Status

See napi_call_threadsafe_function for more information.

Source

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

Source

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>

Source§

unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> Result<Self>

Safety Read more
Source§

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>

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>

Source§

unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>

Safety Read more
Source§

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>

Source§

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>
where T: Unpin, CallJsBackArgs: Unpin, Return: Unpin, ErrorStatus: Unpin,

§

impl<T, Return, CallJsBackArgs, ErrorStatus, const CalleeHandled: bool, const Weak: bool, const MaxQueueSize: usize> UnwindSafe for ThreadsafeFunction<T, Return, CallJsBackArgs, ErrorStatus, CalleeHandled, Weak, MaxQueueSize>
where T: UnwindSafe, CallJsBackArgs: UnwindSafe, Return: UnwindSafe, ErrorStatus: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.