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>

source

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.

source

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

See napi_unref_threadsafe_function for more information.

source

pub fn aborted(&self) -> bool

source

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

source

pub fn raw(&self) -> napi_threadsafe_function

Get the raw ThreadSafeFunction pointer

source§

impl<T: 'static> ThreadsafeFunction<T, CalleeHandled>

source

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

See napi_call_threadsafe_function for more information.

source

pub fn call_with_return_value<D: FromNapiValue, F: 'static + FnOnce(D) -> Result<()>>( &self, value: Result<T>, mode: ThreadsafeFunctionCallMode, cb: F ) -> Status

source

pub fn call_with_return_value_raw<D: FromNapiValue, F: 'static + FnOnce(Result<D>) -> Result<()>>( &self, value: Result<T>, mode: ThreadsafeFunctionCallMode, cb: F ) -> Status

source

pub async fn call_async<D: 'static + FromNapiValue>( &self, value: Result<T> ) -> Result<D>

source§

impl<T: 'static> ThreadsafeFunction<T, Fatal>

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<D: FromNapiValue, F: 'static + FnOnce(D) -> Result<()>>( &self, value: T, mode: ThreadsafeFunctionCallMode, cb: F ) -> Status

source

pub fn call_with_return_value_raw<D: FromNapiValue, F: 'static + FnOnce(Result<D>) -> Result<()>>( &self, value: T, mode: ThreadsafeFunctionCallMode, cb: F ) -> Status

source

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>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: JsValuesTupleIntoVec + 'static, ES: T> FromNapiValue for ThreadsafeFunction<T, ES>

source§

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

Safety Read more
source§

fn from_unknown(value: JsUnknown) -> Result<Self>

source§

impl<T: 'static, ES: T> Send for ThreadsafeFunction<T, ES>

source§

impl<T: 'static, ES: T> Sync for ThreadsafeFunction<T, ES>

Auto Trait Implementations§

§

impl<T, ES> Freeze for ThreadsafeFunction<T, ES>

§

impl<T, ES> RefUnwindSafe for ThreadsafeFunction<T, ES>

§

impl<T, ES> Unpin for ThreadsafeFunction<T, ES>
where ES: Unpin, T: Unpin,

§

impl<T, ES> UnwindSafe for ThreadsafeFunction<T, ES>
where ES: UnwindSafe, T: 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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

§

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.