Skip to content

Commit

Permalink
fix(napi-derive): async task optional output type
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Nov 8, 2023
1 parent 3804bed commit 011db7a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/backend/src/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ fn is_ts_function_type_notation(ty: &Type) -> bool {
}
}

// return (type, is_optional, is_variadic)
pub fn ty_to_ts_type(
ty: &Type,
is_return_ty: bool,
Expand Down
8 changes: 6 additions & 2 deletions crates/backend/src/typegen/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ impl ToTypeDef for NapiImpl {
fn to_type_def(&self) -> Option<TypeDef> {
if let Some(output_type) = &self.task_output_type {
TASK_STRUCTS.with(|t| {
let resolved_type = ty_to_ts_type(output_type, false, true, false).0;
let (resolved_type, is_optional, _) = ty_to_ts_type(output_type, false, true, false);
t.borrow_mut().insert(
self.name.to_string(),
if resolved_type == "undefined" {
"void".to_owned()
} else {
resolved_type
if is_optional {
format!("{} | null", resolved_type)
} else {
resolved_type
}
},
);
});
Expand Down
2 changes: 2 additions & 0 deletions examples/napi/__tests__/__snapshots__/typegen.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ Generated by [AVA](https://avajs.dev).
export function asyncReduceBuffer(buf: Buffer): Promise<number>␊
export function asyncTaskOptionalReturn(): Promise<number | undefined>␊
export function asyncTaskVoidReturn(): Promise<void>␊
export interface B {␊
Expand Down
Binary file modified examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ export function asyncPlus100(p: Promise<number>): Promise<number>

export function asyncReduceBuffer(buf: Buffer): Promise<number>

export function asyncTaskOptionalReturn(): Promise<number | undefined>

export function asyncTaskVoidReturn(): Promise<void>

export interface B {
Expand Down
21 changes: 21 additions & 0 deletions examples/napi/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,24 @@ impl Task for AsyncTaskVoidReturn {
fn async_task_void_return() -> AsyncTask<AsyncTaskVoidReturn> {
AsyncTask::new(AsyncTaskVoidReturn {})
}

struct AsyncTaskOptionalReturn {}

#[napi]
impl Task for AsyncTaskOptionalReturn {
type JsValue = Option<u32>;
type Output = ();

fn compute(&mut self) -> Result<Self::Output> {
Ok(())
}

fn resolve(&mut self, _env: Env, _: Self::Output) -> Result<Self::JsValue> {
Ok(None)
}
}

#[napi]
fn async_task_optional_return() -> AsyncTask<AsyncTaskOptionalReturn> {
AsyncTask::new(AsyncTaskOptionalReturn {})
}

1 comment on commit 011db7a

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 011db7a Previous: 688ee04 Ratio
noop#napi-rs 48969242 ops/sec (±0.89%) 54757912 ops/sec (±1.01%) 1.12
noop#JavaScript 683846652 ops/sec (±0.94%) 663656359 ops/sec (±0.94%) 0.97
Plus number#napi-rs 14344759 ops/sec (±0.89%) 17309798 ops/sec (±1.21%) 1.21
Plus number#JavaScript 690973553 ops/sec (±0.89%) 672162126 ops/sec (±1.2%) 0.97
Create buffer#napi-rs 295286 ops/sec (±11.38%) 389726 ops/sec (±9.55%) 1.32
Create buffer#JavaScript 1496198 ops/sec (±7.71%) 1648526 ops/sec (±8.64%) 1.10
createArray#createArrayJson 32139 ops/sec (±0.66%) 36896 ops/sec (±1.14%) 1.15
createArray#create array for loop 5521 ops/sec (±0.97%) 6820 ops/sec (±1.01%) 1.24
createArray#create array with serde trait 5478 ops/sec (±0.92%) 6810 ops/sec (±1.31%) 1.24
getArrayFromJs#get array from json string 13318 ops/sec (±1.09%) 16288 ops/sec (±1.01%) 1.22
getArrayFromJs#get array from serde 7667 ops/sec (±0.8%) 9221 ops/sec (±1.13%) 1.20
getArrayFromJs#get array with for loop 9595 ops/sec (±1.14%) 11431 ops/sec (±1.13%) 1.19
Get Set property#Get Set from native#u32 376190 ops/sec (±10.06%) 416068 ops/sec (±8.8%) 1.11
Get Set property#Get Set from JavaScript#u32 312229 ops/sec (±10.59%) 367107 ops/sec (±9.29%) 1.18
Get Set property#Get Set from native#string 360040 ops/sec (±3.76%) 378515 ops/sec (±15.91%) 1.05
Get Set property#Get Set from JavaScript#string 282383 ops/sec (±18.13%) 362662 ops/sec (±3.35%) 1.28
Async task#spawn task 26261 ops/sec (±2.55%) 32985 ops/sec (±1.11%) 1.26
Async task#ThreadSafeFunction 4522 ops/sec (±4.37%) 6843 ops/sec (±18%) 1.51
Async task#Tokio future to Promise 24160 ops/sec (±2.19%) 29159 ops/sec (±1.02%) 1.21
Query#query * 100 1738 ops/sec (±2.21%) 1988 ops/sec (±1.8%) 1.14
Query#query * 1 20607 ops/sec (±1.83%) 27914 ops/sec (±1.79%) 1.35

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.