Struct napi::threadsafe_function::ThreadsafeFunction [−][src]
pub struct ThreadsafeFunction<T: 'static, ES: T = CalleeHandled> { /* fields omitted */ }
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
impl<T: 'static, ES: T> ThreadsafeFunction<T, ES>
[src]
impl<T: 'static, ES: T> ThreadsafeFunction<T, ES>
[src]pub fn create<V: NapiRaw, R: 'static + Send + FnMut(ThreadSafeCallContext<T>) -> Result<Vec<V>>>(
env: napi_env,
func: &JsFunction,
max_queue_size: usize,
callback: R
) -> Result<Self>
[src]
pub fn create<V: NapiRaw, R: 'static + Send + FnMut(ThreadSafeCallContext<T>) -> Result<Vec<V>>>(
env: napi_env,
func: &JsFunction,
max_queue_size: usize,
callback: R
) -> Result<Self>
[src]See napi_create_threadsafe_function for more information.
pub fn refer(&mut self, env: &Env) -> Result<()>
[src]
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]
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 raw(&self) -> napi_threadsafe_function
[src]
pub fn raw(&self) -> napi_threadsafe_function
[src]Get the raw ThreadSafeFunction
pointer
impl<T: 'static> ThreadsafeFunction<T, CalleeHandled>
[src]
impl<T: 'static> ThreadsafeFunction<T, CalleeHandled>
[src]pub fn call(&self, value: Result<T>, mode: ThreadsafeFunctionCallMode) -> Status
[src]
pub fn call(&self, value: Result<T>, mode: ThreadsafeFunctionCallMode) -> Status
[src]See napi_call_threadsafe_function for more information.
impl<T: 'static> ThreadsafeFunction<T, Fatal>
[src]
impl<T: 'static> ThreadsafeFunction<T, Fatal>
[src]pub fn call(&self, value: T, mode: ThreadsafeFunctionCallMode) -> Status
[src]
pub fn call(&self, value: T, mode: ThreadsafeFunctionCallMode) -> Status
[src]See napi_call_threadsafe_function for more information.
Trait Implementations
impl<T: 'static, ES: T> Clone for ThreadsafeFunction<T, ES>
[src]
impl<T: 'static, ES: T> Clone for ThreadsafeFunction<T, ES>
[src]impl<T: 'static, ES: T> Drop for ThreadsafeFunction<T, ES>
[src]
impl<T: 'static, ES: T> Drop for ThreadsafeFunction<T, ES>
[src]impl<T, ES: T> Send for ThreadsafeFunction<T, ES>
[src]
impl<T, ES: T> Sync for ThreadsafeFunction<T, ES>
[src]
Auto Trait Implementations
impl<T, ES> RefUnwindSafe for ThreadsafeFunction<T, ES> where
ES: RefUnwindSafe,
T: RefUnwindSafe,
ES: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, ES> Unpin for ThreadsafeFunction<T, ES> where
ES: Unpin,
T: Unpin,
ES: Unpin,
T: Unpin,
impl<T, ES> UnwindSafe for ThreadsafeFunction<T, ES> where
ES: UnwindSafe,
T: UnwindSafe,
ES: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more