!479 增加napi enum, struct snippets

Merge pull request !479 from 苟晶晶/master
This commit is contained in:
openharmony_ci 2024-11-19 01:59:48 +00:00 committed by Gitee
commit 8649f09934
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 59 additions and 74 deletions

View File

@ -114,7 +114,7 @@
},
{
"language": "cpp",
"path": "./snippets/napi_async_snippets.json"
"path": "./snippets/napi_asyncwork_snippets.json"
},
{
"language": "cpp",
@ -123,6 +123,14 @@
{
"language": "cpp",
"path": "./snippets/napi_callback_snippets.json"
},
{
"language": "cpp",
"path": "./snippets/napi_enum_snippets.json"
},
{
"language": "cpp",
"path": "./snippets/napi_struct_snippets.json"
}
]
},

View File

@ -1,6 +1,6 @@
{
"Napi Async Func": {
"prefix": "napiasyncfunc",
"prefix": "napiasyncwork",
"body": [
"struct AsyncData{",
" napi_async_work work;",
@ -9,35 +9,22 @@
" // save async work result.",
"};",
"static void ExecuteAsyncWork(napi_env env, void* data) {",
" std::this_thread::sleep_for(std::chrono::seconds(2));",
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"ExecuteAsyncWork\");",
"}",
"static void CompleteAsyncWork(napi_env env, napi_status status, void* data) {",
" // Todo: you can use \"napicallfunc\" command that execute the callback.",
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"CompleteAsyncWork\");",
"}",
"napi_value StartAsyncWork(napi_env env, napi_callback_info info)",
"{",
" napi_value resourceName = nullptr;",
" // Create a js string which is used as the name for the asynchronous work object being created.",
" napi_create_string_utf8(env, \"asyncWork\", NAPI_AUTO_LENGTH, &resourceName);",
" // Create an asynchronous work object.",
" napi_create_async_work(env, nullptr, resourceName, ExecuteAsyncWork, CompleteAsyncWork, asyncData, &asyncData->work);",
" // Add the asynchronous work object to a queue.",
" napi_queue_async_work(env, asyncData->work);",
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"Main thread.\");",
" 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[] = {",
" { \"startAsyncWork\", nullptr, StartAsyncWork, 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"
"}"
]
}
}

View File

@ -1,29 +1,9 @@
{
"Napi Callback": {
"prefix": "napicallback",
"prefix": "napicallfunc",
"body": [
"static napi_value CallbackSample(napi_env env, napi_callback_info info)",
"{",
" napi_value callback;",
" napi_value callbackArg[1] = nullptr;",
" napi_value result;",
" // Invokes a js function with the provided arguments and returns the result.",
" napi_call_function(env, nullptr, callback, 1, callbackArg, &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"
"// Invokes a js function with the provided arguments and returns the result.",
"napi_call_function(env, recv, callbackFunc, argc, callbackArg, &result);"
]
}
}

View File

@ -0,0 +1,11 @@
{
"Napi Enum": {
"prefix": "napienum",
"body": [
"// Enum in.",
"// Todo: you can use \"napiint32in\" or \"napistringutf8in\" command that get int32_t or string value from js.",
"// Enum out.",
"// Todo: you can use \"napiint32out\" or \"napistringutf8out\" command that return int32_t or string value to js"
]
}
}

View File

@ -1,37 +1,23 @@
{
"Napi Promise": {
"prefix": "napipromise",
"Napi create promise": {
"prefix": "napicreatepromise",
"body": [
"static napi_value PromiseSample(napi_env env, napi_callback_info info)",
"{",
" napi_value promise;",
" napi_deferred deferred;",
" // Create a new js Promise.",
" napi_create_promise(env, &deferred, &promise);",
" napi_value valueOut = nullptr;",
" bool isResolved = true;",
" if (isResolved) {",
" // Fulfill a js Promise with a given value.",
" napi_resolve_deferred(env, deferred, valueOut);",
" } else {",
" // Reject a js Promise with a given reason.",
" napi_reject_deferred(env, deferred, valueOut);",
" }",
" return promise;",
"}",
"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[] = {",
" { \"promiseSample\", nullptr, PromiseSample, 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"
"// Create a new js Promise.",
"napi_create_promise(env, &deferred, &promise);"
]
},
"Napi resolve deferred": {
"prefix": "napiresolvedeferred",
"body": [
"// Fulfill a js Promise with a given value.",
"napi_resolve_deferred(env, deferred, resolution);"
]
},
"Napi reject deferred": {
"prefix": "napirejectdeferred",
"body": [
"// Reject a js Promise with a given reason.",
"napi_reject_deferred(env, deferred, rejection);"
]
}
}

View File

@ -0,0 +1,13 @@
{
"Napi Struct": {
"prefix": "napistruct",
"body": [
"// Struct in",
"// Todo: you can use \"napiobjectin\" command that get object value from js.",
"// Todo: you can use \"napigetvaluefromobject\" command that get value from js object.",
"// Struct out",
"// Todo: you can use \"napiobjectout\" command that return object to js.",
"// Todo: you can use \"napisetvalue2object\" command that set value to objectOut."
]
}
}