fix(napi): allow ErrorStrategy to be specified while creating TSFN
This commit is contained in:
parent
d7da82e269
commit
d1a5f84aa0
6 changed files with 30 additions and 9 deletions
|
@ -120,15 +120,16 @@ impl JsFunction {
|
|||
}
|
||||
|
||||
#[cfg(feature = "napi4")]
|
||||
pub fn create_threadsafe_function<T, V, F>(
|
||||
pub fn create_threadsafe_function<T, V, F, ES>(
|
||||
&self,
|
||||
max_queue_size: usize,
|
||||
callback: F,
|
||||
) -> Result<ThreadsafeFunction<T>>
|
||||
) -> Result<ThreadsafeFunction<T, ES>>
|
||||
where
|
||||
T: 'static,
|
||||
V: NapiRaw,
|
||||
F: 'static + Send + FnMut(ThreadSafeCallContext<T>) -> Result<Vec<V>>,
|
||||
ES: crate::threadsafe_function::ErrorStrategy::T,
|
||||
{
|
||||
ThreadsafeFunction::create(self.0.env, self.0.value, max_queue_size, callback)
|
||||
}
|
||||
|
|
|
@ -57,6 +57,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function withAbortController(a: number, b: number, signal: AbortSignal): Promise<number>␊
|
||||
export function callThreadsafeFunction(callback: (...args: any[]) => any): void␊
|
||||
export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void␊
|
||||
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊
|
||||
export function getBuffer(): Buffer␊
|
||||
export class Animal {␊
|
||||
readonly kind: Kind␊
|
||||
|
|
Binary file not shown.
|
@ -47,6 +47,7 @@ import {
|
|||
getNull,
|
||||
setSymbolInObj,
|
||||
createSymbol,
|
||||
threadsafeFunctionFatalMode,
|
||||
} from '../'
|
||||
|
||||
test('export const', (t) => {
|
||||
|
@ -304,6 +305,13 @@ Napi4Test('throw error from thread safe function', async (t) => {
|
|||
t.is(err.message, 'ThrowFromNative')
|
||||
})
|
||||
|
||||
Napi4Test('throw error from thread safe function fatal mode', async (t) => {
|
||||
const tsfnFatalMode = new Promise<boolean>((resolve) => {
|
||||
threadsafeFunctionFatalMode(resolve)
|
||||
})
|
||||
t.true(await tsfnFatalMode)
|
||||
})
|
||||
|
||||
Napi4Test('await Promise in rust', async (t) => {
|
||||
const fx = 20
|
||||
const result = await asyncPlus100(
|
||||
|
|
1
examples/napi/index.d.ts
vendored
1
examples/napi/index.d.ts
vendored
|
@ -47,6 +47,7 @@ export function withoutAbortController(a: number, b: number): Promise<number>
|
|||
export function withAbortController(a: number, b: number, signal: AbortSignal): Promise<number>
|
||||
export function callThreadsafeFunction(callback: (...args: any[]) => any): void
|
||||
export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void
|
||||
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void
|
||||
export function getBuffer(): Buffer
|
||||
export class Animal {
|
||||
readonly kind: Kind
|
||||
|
|
|
@ -2,14 +2,15 @@ use std::thread;
|
|||
|
||||
use napi::{
|
||||
bindgen_prelude::*,
|
||||
threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunctionCallMode},
|
||||
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
|
||||
};
|
||||
|
||||
#[napi]
|
||||
pub fn call_threadsafe_function(callback: JsFunction) -> Result<()> {
|
||||
let tsfn = callback.create_threadsafe_function(0, |ctx: ThreadSafeCallContext<u32>| {
|
||||
ctx.env.create_uint32(ctx.value + 1).map(|v| vec![v])
|
||||
})?;
|
||||
let tsfn: ThreadsafeFunction<u32, ErrorStrategy::CalleeHandled> = callback
|
||||
.create_threadsafe_function(0, |ctx| {
|
||||
ctx.env.create_uint32(ctx.value + 1).map(|v| vec![v])
|
||||
})?;
|
||||
for n in 0..100 {
|
||||
let tsfn = tsfn.clone();
|
||||
thread::spawn(move || {
|
||||
|
@ -21,9 +22,8 @@ pub fn call_threadsafe_function(callback: JsFunction) -> Result<()> {
|
|||
|
||||
#[napi]
|
||||
pub fn threadsafe_function_throw_error(cb: JsFunction) -> Result<()> {
|
||||
let tsfn = cb.create_threadsafe_function(0, |ctx: ThreadSafeCallContext<bool>| {
|
||||
ctx.env.get_boolean(ctx.value).map(|v| vec![v])
|
||||
})?;
|
||||
let tsfn: ThreadsafeFunction<bool, ErrorStrategy::CalleeHandled> =
|
||||
cb.create_threadsafe_function(0, |ctx| ctx.env.get_boolean(ctx.value).map(|v| vec![v]))?;
|
||||
thread::spawn(move || {
|
||||
tsfn.call(
|
||||
Err(Error::new(
|
||||
|
@ -35,3 +35,13 @@ pub fn threadsafe_function_throw_error(cb: JsFunction) -> Result<()> {
|
|||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn threadsafe_function_fatal_mode(cb: JsFunction) -> Result<()> {
|
||||
let tsfn: ThreadsafeFunction<bool, ErrorStrategy::Fatal> =
|
||||
cb.create_threadsafe_function(0, |ctx| ctx.env.get_boolean(ctx.value).map(|v| vec![v]))?;
|
||||
thread::spawn(move || {
|
||||
tsfn.call(true, ThreadsafeFunctionCallMode::Blocking);
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue