[−][src]Struct napi::threadsafe_function::ThreadsafeFunction
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.try_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
impl<T: 'static> ThreadsafeFunction<T>
[src]
pub fn create<V: NapiValue, R: 'static + Send + FnMut(ThreadSafeCallContext<T>) -> Result<Vec<V>>>(
env: napi_env,
func: &JsFunction,
max_queue_size: usize,
callback: R
) -> Result<Self>
[src]
env: napi_env,
func: &JsFunction,
max_queue_size: usize,
callback: R
) -> Result<Self>
See napi_create_threadsafe_function for more information.
pub fn call(&self, value: Result<T>, mode: ThreadsafeFunctionCallMode) -> Status
[src]
See napi_call_threadsafe_function for more information.
pub fn refer(&mut self, env: &Env) -> Result<()>
[src]
See napi_ref_threadsafe_function for more information.
"ref" is a keyword so that we use "refer" here.
pub fn unref(&mut self, env: &Env) -> Result<()>
[src]
See napi_unref_threadsafe_function for more information.
pub fn aborted(&self) -> bool
[src]
pub fn abort(self) -> Result<()>
[src]
pub fn try_clone(&self) -> Result<Self>
[src]
pub fn raw(&self) -> napi_threadsafe_function
[src]
Get the raw ThreadSafeFunction
pointer
Trait Implementations
impl<T: 'static> Drop for ThreadsafeFunction<T>
[src]
impl<T> Send for ThreadsafeFunction<T>
[src]
impl<T> Sync for ThreadsafeFunction<T>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for ThreadsafeFunction<T> where
T: RefUnwindSafe,
[src]
T: RefUnwindSafe,
impl<T> Unpin for ThreadsafeFunction<T> where
T: Unpin,
[src]
T: Unpin,
impl<T> UnwindSafe for ThreadsafeFunction<T> where
T: UnwindSafe,
[src]
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,