fix(napi-derive): async task void output type (#1795)

This commit is contained in:
LongYinan 2023-11-08 20:05:09 +08:00 committed by GitHub
parent 688ee04247
commit 938f4df83d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 4 deletions

View file

@ -36,9 +36,14 @@ 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;
t.borrow_mut().insert(
self.name.to_string(),
ty_to_ts_type(output_type, false, true, false).0,
if resolved_type == "undefined" {
"void".to_owned()
} else {
resolved_type
},
);
});
}

View file

@ -1033,9 +1033,7 @@ impl ConvertToAST for syn::ItemImpl {
if let Some((_, t, _)) = &self.trait_ {
if let Some(PathSegment { ident, .. }) = t.segments.last() {
if ident == "Task" && m.ident == "JsValue" {
if let Type::Path(_) = &m.ty {
task_output_type = Some(m.ty.clone());
}
} else if ident == "Generator" {
if let Type::Path(_) = &m.ty {
if m.ident == "Yield" {

View file

@ -246,6 +246,8 @@ Generated by [AVA](https://avajs.dev).
export function asyncReduceBuffer(buf: Buffer): Promise<number>
export function asyncTaskVoidReturn(): Promise<void>
export interface B {␊
bar: number␊
}␊

View file

@ -236,6 +236,8 @@ export function asyncPlus100(p: Promise<number>): Promise<number>
export function asyncReduceBuffer(buf: Buffer): Promise<number>
export function asyncTaskVoidReturn(): Promise<void>
export interface B {
bar: number
}

View file

@ -28,3 +28,24 @@ fn without_abort_controller(a: u32, b: u32) -> AsyncTask<DelaySum> {
fn with_abort_controller(a: u32, b: u32, signal: AbortSignal) -> AsyncTask<DelaySum> {
AsyncTask::with_signal(DelaySum(a, b), signal)
}
struct AsyncTaskVoidReturn {}
#[napi]
impl Task for AsyncTaskVoidReturn {
type JsValue = ();
type Output = ();
fn compute(&mut self) -> Result<Self::Output> {
Ok(())
}
fn resolve(&mut self, _env: Env, output: Self::Output) -> Result<Self::JsValue> {
Ok(output)
}
}
#[napi]
fn async_task_void_return() -> AsyncTask<AsyncTaskVoidReturn> {
AsyncTask::new(AsyncTaskVoidReturn {})
}