Skip to content

feat(napi): support build_threadsafe_function variant with error status - #2694

Merged
Brooooooklyn merged 1 commit into
napi-rs:mainfrom
h-a-n-a:build-tsfn
Jun 5, 2025
Merged

Brooooooklyn merged 1 commit into
napi-rs:mainfrom
h-a-n-a:build-tsfn

Conversation

@h-a-n-a

@h-a-n-a h-a-n-a commented Jun 5, 2025 •

Copy link
Copy Markdown
Contributor

Add new Function::build_threadsafe_function_with_status. This supports returning a new ThreadsafeFunctionBuilder with custom status. I don't know if this name is ideal though. Please feel free to give me any advice on the naming.

I decided to keep the original build_threadsafe_function as is to avoid any breaking changes.

@Brooooooklyn
Brooooooklyn requested a review from Copilot June 5, 2025 08:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new variant of the threadsafe function builder that allows specifying a custom error status type at build time.

  • Add build_threadsafe_function_with_status to the Function API for custom error status support
  • Provide an example, TypeScript declarations, JS exports, and tests for the new builder variant
  • Keep the original build_threadsafe_function unchanged to avoid breaking changes

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
examples/napi/src/threadsafe_function.rs Add example function threadsafe_function_build_throw_error_with_status demonstrating builder usage
examples/napi/index.d.cts Add TypeScript declaration for threadsafeFunctionBuildThrowErrorWithStatus
examples/napi/index.cjs Export the new builder variant in CommonJS module
examples/napi/example.wasi.cjs Export the new builder variant in WASI CJS module
examples/napi/example.wasi-browser.js Export the new builder variant in the browser build
examples/napi/tests/values.spec.ts Add an AVA test for the builder-based error status example
examples/napi/tests/snapshots/values.spec.ts.md Update snapshot to include the new declaration
crates/napi/src/bindgen_runtime/js_values/function.rs Implement build_threadsafe_function_with_status on the Function type
Comments suppressed due to low confidence (4)

examples/napi/src/threadsafe_function.rs:83

  • [nitpick] The example function name threadsafe_function_build_throw_error_with_status is quite long and inconsistent with threadsafe_function_throw_error_with_status. Consider renaming it to something like threadsafe_function_with_error_status_via_builder for clarity and consistency.
pub fn threadsafe_function_build_throw_error_with_status(cb: Function<'static>) -> Result<()> {

crates/napi/src/bindgen_runtime/js_values/function.rs:223

  • [nitpick] The method name build_threadsafe_function_with_status is ambiguous—consider renaming it to build_threadsafe_function_with_error_status to clearly convey that this builder variant supports a custom error status type.
pub fn build_threadsafe_function_with_status<

crates/napi/src/bindgen_runtime/js_values/function.rs:222

  • The doc comment could be expanded to mention that this builder variant allows specifying a custom error status type (the ErrorStatus generic parameter).
/// Create a threadsafe function from the JavaScript function.

crates/napi/src/bindgen_runtime/js_values/function.rs:235

  • The new build_threadsafe_function_with_status API surface currently lacks crate-level unit tests. Consider adding tests in bindgen_runtime to validate the builder’s behavior and ensure it integrates correctly.
}

@Brooooooklyn
Brooooooklyn merged commit 0a90c8a into napi-rs:main Jun 5, 2025
@h-a-n-a

h-a-n-a commented Jun 5, 2025 •

Copy link
Copy Markdown
Contributor Author

I think maybe we can also follow the convention of ThreadsafeFunctionBuilder. In that way, we can remove build_threadsafe_function_with_status and change it to a more flexible implementation:

impl<...> ThreadsafeFunctionBuilder<...> {
    pub fn error_status<NewErrorStatus: AsRef<str> + From<Status>>(
    self,
  ) -> ThreadsafeFunctionBuilder<
    'env,
    T,
    Args,
    Return,
    NewErrorStatus,
    CalleeHandled,
    Weak,
    MaxQueueSize,
  > {
    ThreadsafeFunctionBuilder {
      env: self.env,
      value: self.value,
      _args: std::marker::PhantomData,
      _return: std::marker::PhantomData,
    }
  }
}

Which one do you prefer? I can make another PR if you want me to change to this implementation. cc @Brooooooklyn

@Brooooooklyn

Copy link
Copy Markdown
Member

I think maybe we can also follow the convention of ThreadsafeFunctionBuilder. In that way, we can remove build_threadsafe_function_with_status to a more flexible implementation:

impl<...> ThreadsafeFunctionBuilder<...> {

    pub fn error_status<NewErrorStatus: AsRef<str> + From<Status>>(

    self,

  ) -> ThreadsafeFunctionBuilder<

    'env,

    T,

    Args,

    Return,

    NewErrorStatus,

    CalleeHandled,

    Weak,

    MaxQueueSize,

  > {

    ThreadsafeFunctionBuilder {

      env: self.env,

      value: self.value,

      _args: std::marker::PhantomData,

      _return: std::marker::PhantomData,

    }

  }

}

Which one do you prefer? I can make another PR if you want me to change to this implementation. cc @Brooooooklyn

Let's ship this one

@h-a-n-a

h-a-n-a commented Jun 5, 2025

Copy link
Copy Markdown
Contributor Author

I think maybe we can also follow the convention of ThreadsafeFunctionBuilder. In that way, we can remove build_threadsafe_function_with_status to a more flexible implementation:

impl<...> ThreadsafeFunctionBuilder<...> {

    pub fn error_status<NewErrorStatus: AsRef<str> + From<Status>>(

    self,

  ) -> ThreadsafeFunctionBuilder<

    'env,

    T,

    Args,

    Return,

    NewErrorStatus,

    CalleeHandled,

    Weak,

    MaxQueueSize,

  > {

    ThreadsafeFunctionBuilder {

      env: self.env,

      value: self.value,

      _args: std::marker::PhantomData,

      _return: std::marker::PhantomData,

    }

  }

}

Which one do you prefer? I can make another PR if you want me to change to this implementation. cc @Brooooooklyn

Let's ship this one

@Brooooooklyn Done ;-) #2695

@h-a-n-a
h-a-n-a deleted the build-tsfn branch June 5, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants