add napi callback snippets

Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
chen-zhongwei050 2024-11-07 19:11:45 +08:00
parent 13f7e4f0c3
commit 4274da8b03
2 changed files with 36 additions and 0 deletions

View File

@ -119,6 +119,10 @@
{
"language": "cpp",
"path": "./snippets/napi_promise_snippets.json"
},
{
"language": "cpp",
"path": "./snippets/napi_callback_snippets.json"
}
]
},

View File

@ -0,0 +1,32 @@
{
"Napi Callback": {
"prefix": "napicallback",
"body": [
"static napi_value CallbackSample(napi_env env, napi_callback_info info)",
"{",
" // Todo: you can use \"napiobjectin\" command that get object value from js.",
" // Todo: you can use \"napidoublein\" command that get double value from js.",
" // Business code.",
" double myRes = value0 + value1;",
" // Todo: you can use \"napidoubleout\" command that return double to js.",
" napi_value result;",
" // Invokes a js function with the provided arguments and returns the result.",
" napi_call_function(env, nullptr, args[2], 1, &doubleOut, &result);",
" return nullptr;",
"}",
"EXTERN_C_START",
"// Initialize the module.",
"static napi_value Init(napi_env env, napi_value exports)",
"{",
" // Define properties and methods for a N-API object.",
" napi_property_descriptor desc[] = {",
" { \"callbackSample\", nullptr, CallbackSample, nullptr, nullptr, nullptr, napi_default, nullptr }",
" };",
" // Add an array of properties to a ArkTs object.",
" napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);",
" return exports;",
"}",
"EXTERN_C_END"
]
}
}