1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::ptr;

use log::debug;

use crate::sys::napi_threadsafe_function;
use crate::NjError;
use crate::val::JsEnv;
use crate::sys::napi_env;

/// Wrapper for thread safe function that are safe to send and sync across thread
pub struct ThreadSafeFunction {
    env: JsEnv,
    tf: napi_threadsafe_function
}

unsafe impl Sync for ThreadSafeFunction{}
unsafe impl Send for ThreadSafeFunction{}


impl ThreadSafeFunction {

    pub fn new<E>(env: E,tf: napi_threadsafe_function) -> Self 
        where E: Into<JsEnv>
    {
        Self {
            env: env.into(),
            tf
        }
    }

    pub fn inner(self) -> napi_threadsafe_function {
        self.tf
    }

    pub fn env(&self) -> napi_env {
        self.env.inner()
    }

    pub fn call(&self, data: Option<*mut ::std::os::raw::c_void>) -> Result<(),NjError> {
        
        let data_ptr = match data {
            Some(ptr) => ptr,
            None => ptr::null_mut()
        };
        debug!("calling thread safe");
        crate::napi_call_result!(
            crate::sys::napi_call_threadsafe_function(
                self.tf,
                data_ptr,
                crate::sys::napi_threadsafe_function_call_mode_napi_tsfn_blocking
            )
        )
        
    }
}

impl Drop for ThreadSafeFunction {

    fn drop(&mut self) {
        
        crate::napi_call_assert!(
            crate::sys::napi_release_threadsafe_function(
                self.tf,
                crate::sys::napi_threadsafe_function_release_mode_napi_tsfn_release
            )
        );        
    }
}