Repository navigation
feat(napi): support build_threadsafe_function variant with error status - #2694
Conversation
There was a problem hiding this comment.
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_statusto theFunctionAPI for custom error status support - Provide an example, TypeScript declarations, JS exports, and tests for the new builder variant
- Keep the original
build_threadsafe_functionunchanged 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_statusis quite long and inconsistent withthreadsafe_function_throw_error_with_status. Consider renaming it to something likethreadsafe_function_with_error_status_via_builderfor 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_statusis ambiguous—consider renaming it tobuild_threadsafe_function_with_error_statusto 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
ErrorStatusgeneric 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_statusAPI surface currently lacks crate-level unit tests. Consider adding tests inbindgen_runtimeto validate the builder’s behavior and ensure it integrates correctly.
}
|
I think maybe we can also follow the convention of 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 |
Add new
Function::build_threadsafe_function_with_status. This supports returning a newThreadsafeFunctionBuilderwith 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_functionas is to avoid any breaking changes.