From 74cd9c49c2d015a0f2c0a514aee78be30c1c006e Mon Sep 17 00:00:00 2001 From: Michael Orenstein Date: Sun, 26 Jul 2020 17:46:45 +0930 Subject: [PATCH 1/2] Fixed documentation of threadsafe functions --- napi/src/threadsafe_function.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/napi/src/threadsafe_function.rs b/napi/src/threadsafe_function.rs index 2083eee2..78d43671 100644 --- a/napi/src/threadsafe_function.rs +++ b/napi/src/threadsafe_function.rs @@ -45,12 +45,10 @@ pub trait ToJs: Copy + Clone { /// /// impl ToJs for HandleNumber { /// type Output = u8; -/// type JsValue = JsNumber; /// -/// fn resolve(&self, env: &mut Env, output: Self::Output) -> Result<(u64, Self::JsValue)> { -/// let argv: u64 = 1; -/// let value = env.create_uint32(output as u32)?; -/// Ok((argv, value)) +/// fn resolve(&self, env: &mut Env, output: Self::Output) -> Result> { +/// let value = env.create_uint32(output as u32)?.into_unknown()?; +/// Ok(vec![value]) /// } /// } /// From 6b8bd01bccf742b85646c90c8d2ae00ea988bcfa Mon Sep 17 00:00:00 2001 From: Michael Orenstein Date: Sun, 26 Jul 2020 18:26:42 +0930 Subject: [PATCH 2/2] Updated threadsafe documentation to mention the first argument error convention --- napi/src/threadsafe_function.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/napi/src/threadsafe_function.rs b/napi/src/threadsafe_function.rs index 78d43671..68f2e561 100644 --- a/napi/src/threadsafe_function.rs +++ b/napi/src/threadsafe_function.rs @@ -48,6 +48,10 @@ pub trait ToJs: Copy + Clone { /// /// fn resolve(&self, env: &mut Env, output: Self::Output) -> Result> { /// let value = env.create_uint32(output as u32)?.into_unknown()?; +/// // The first argument in the NodeJS callback will be either a null or an error +/// // depending on the result returned by this function. +/// // If this Result is Ok, the first argument will be null. +/// // If this Result is Err, the first argument will be the error. /// Ok(vec![value]) /// } /// } @@ -200,7 +204,9 @@ unsafe extern "C" fn call_js_cb( let status; - // Follow the convention of Node.js async callback. + // Follow async callback conventions: https://nodejs.org/en/knowledge/errors/what-are-the-error-conventions/ + // Check if the Result is okay, if so, pass a null as the first (error) argument automatically. + // If the Result is an error, pass that as the first argument. if ret.is_ok() { let values = ret.unwrap(); let js_null = env.get_null().unwrap();