From cd9bb0c5d6cff78addaf3f5ca3f302e83dc5a84f Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 14 Apr 2022 00:02:06 +0800 Subject: [PATCH] docs: add return Promise callback function Fix https://github.com/napi-rs/napi-rs/issues/1128 --- examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 2889 -> 2932 bytes examples/napi/__test__/values.spec.ts | 17 +++++++++++++++++ examples/napi/index.d.ts | 1 + examples/napi/src/callback.rs | 3 ++- 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 2a93f83a..3a8c38e5 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -40,6 +40,7 @@ Generated by [AVA](https://avajs.dev). /** napi = { version = 2, features = ["serde-json"] } */␊ export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void␊ export function returnJsFunction(): (...args: any[]) => any␊ + export function callbackReturnPromise(functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void): T | Promise␊ export function dateToNumber(input: Date): number␊ export function chronoDateToMillis(input: Date): number␊ export function chronoDateAdd1Minute(input: Date): Date␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 86e0d3e0fd2d31bb1e8f51be1e188a2137a71e1d..68e1450c4a8122191e5d68e64c1677b64a71bf62 100644 GIT binary patch literal 2932 zcmV-)3ybtYRzVr!8An?Do`BX5TrulK$h*Some((xtoR8 z*o?@LG%>|tI78d2*Fg6V`dIX(*nR2m^%Hu|42K+vq$H ze|Uud{pRwe`PM0rYjBQ6R2qo8{v6-rA24l z3o%F|UvUwE{nU3N38dF(k#>^M$t$T69u2oOi?>WkBE^_x=+a^0lIW&mhk}9KE&O{(VYlTZ^1Ya<5g1G>0XQd#7<0*9?WBW2l}r?o zj?z%^-i`2RGPHs?xOM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wkltn)nEirSV@(x|6PAbyT)--M)WrA~po15{b2A!H|b>8p4?H%s+`cCwznHX>W_~Lof zxYz_109xO8St993QbP%V<`epWd>RQ|8fNE%6{6~~20Ke|if%ATzN(z3%WZrg@Db!Y n&t>7RL3bOj?p%R3aMrV65L~a*4+|&pcPsw`itg~%VkZCq95|kD diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index dd66079c..3cda09af 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -2,6 +2,7 @@ import { exec } from 'child_process' import { join } from 'path' import test from 'ava' +import { spy } from 'sinon' import { DEFAULT_COST, @@ -89,6 +90,7 @@ import { bufferPassThrough, JsRepo, asyncReduceBuffer, + callbackReturnPromise, } from '../' test('export const', (t) => { @@ -224,6 +226,21 @@ test('return function', (t) => { }) }) +test('function return Promise', async (t) => { + const cbSpy = spy() + await callbackReturnPromise(() => '1', spy) + t.is(cbSpy.callCount, 0) + await callbackReturnPromise( + () => Promise.resolve('42'), + (err, res) => { + t.is(err, null) + cbSpy(res) + }, + ) + t.is(cbSpy.callCount, 1) + t.deepEqual(cbSpy.args, [['42']]) +}) + test('object', (t) => { t.deepEqual(listObjKeys({ name: 'John Doe', age: 20 }), ['name', 'age']) t.deepEqual(createObj(), { test: 1 }) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 87b6a8f3..c03e1ff1 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -30,6 +30,7 @@ export function optionOnly(callback: (arg0?: string | undefined | null) => void) /** napi = { version = 2, features = ["serde-json"] } */ export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void export function returnJsFunction(): (...args: any[]) => any +export function callbackReturnPromise(functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void): T | Promise export function dateToNumber(input: Date): number export function chronoDateToMillis(input: Date): number export function chronoDateAdd1Minute(input: Date): Date diff --git a/examples/napi/src/callback.rs b/examples/napi/src/callback.rs index 14793f61..f92e01ef 100644 --- a/examples/napi/src/callback.rs +++ b/examples/napi/src/callback.rs @@ -53,7 +53,8 @@ fn return_js_function(env: Env) -> Result { #[napi( ts_generic_types = "T", - ts_args_type = "functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void" + ts_args_type = "functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void", + ts_return_type = "T | Promise" )] fn callback_return_promise Result>( env: Env,