mirror of
https://gitee.com/openharmony/napi_generator
synced 2024-11-23 08:20:01 +00:00
!479 增加napi enum, struct snippets
Merge pull request !479 from 苟晶晶/master
This commit is contained in:
commit
8649f09934
@ -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"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -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"
|
||||
"}"
|
||||
]
|
||||
}
|
||||
}
|
@ -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);"
|
||||
]
|
||||
}
|
||||
}
|
11
src/vscode_plugin/snippets/napi_enum_snippets.json
Normal file
11
src/vscode_plugin/snippets/napi_enum_snippets.json
Normal 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"
|
||||
]
|
||||
}
|
||||
}
|
@ -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);"
|
||||
]
|
||||
}
|
||||
}
|
13
src/vscode_plugin/snippets/napi_struct_snippets.json
Normal file
13
src/vscode_plugin/snippets/napi_struct_snippets.json
Normal 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."
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user