modify async, callback, promise, enum, struct snippets

Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
chen-zhongwei050 2024-11-13 18:00:38 +08:00
parent 8d79f07188
commit 7bf15248e8
7 changed files with 61 additions and 193 deletions

View File

@ -1,56 +0,0 @@
{
"Napi Async Func": {
"prefix": "napiasyncfunc",
"body": [
"struct AsyncData{",
" napi_async_work work;",
" napi_ref callbackRef;",
" // save async work param in.",
" // save async work result.",
"};",
"static void ExecuteAsyncWork(napi_env env, void* data) {",
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"ExecuteAsyncWork\");",
"}",
"static void CompleteAsyncWork(napi_env env, napi_status status, void* data) {",
" AsyncData* asyncData = static_cast<AsyncData*>(data);",
" napi_value callback = nullptr;",
" // Retrieve the js value associated with a reference.",
" napi_get_reference_value(env, asyncData->callbackRef, &callback);",
" // Todo: execute the callback: napi_call_function(env, nullptr, callback, 1, callbackArg, &result);",
" // Delete the napi_ref object and asynchronous work object.",
" napi_delete_reference(env, asyncData->callbackRef);",
" napi_delete_async_work(env, asyncData->work);",
" delete asyncData;",
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"CompleteAsyncWork\");",
"}",
"napi_value StartAsyncWork(napi_env env, napi_callback_info info)",
"{",
" AsyncData* asyncData = new AsyncData();",
" napi_value callback = nullptr;",
" // Convert the callback to napi_ref to extend its lifecycle to prevent it from being garbage-collected.",
" napi_create_reference(env, callback, 1, &asyncData->callbackRef);",
" 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);",
" 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

@ -0,0 +1,30 @@
{
"Napi Async Func": {
"prefix": "napiasyncfunc",
"body": [
"struct AsyncData{",
" napi_async_work work;",
" napi_ref callbackRef;",
" // save async work param in.",
" // 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 \"napicallthreadsafefunc\" 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)",
"{",
" // 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;",
"}"
]
}
}

View File

@ -2,28 +2,8 @@
"Napi Callback": {
"prefix": "napicallback",
"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

@ -2,34 +2,9 @@
"Napi Enum": {
"prefix": "napienum",
"body": [
"napi_value CreateEnumObjectToJs(napi_env env, napi_callback_info info) {",
" napi_status status;",
" // Todo: you can use \"napiint32out\" command that return int32_t value to js.",
" // Todo: you can use \"napiobjectout\" command that return object value to js.",
" // Todo: you can use \"napisetvalue2object\" command that set value to objectOut",
" return objectOut;",
"}",
"napi_value GetEnumValueFromJs(napi_env env, napi_callback_info info)",
"{",
" // Todo: you can use \"napiobjectin\" command that get object value from js.",
" int32_t enumValue;",
" // Todo: you can use \"napiint32in\" command that get int32_t value from js.",
" return nullptr;",
"}",
"EXTERN_C_START",
"// Initialize the module.",
"static napi_value Init(napi_env env, napi_value exports)",
"{",
" // Define promise method for a N-API object.",
" napi_property_descriptor desc[] = {",
" {\"createEnumObjectToJs\", nullptr, CreateEnumObjectToJs, nullptr, nullptr, nullptr, napi_default, nullptr},",
" {\"getEnumValueFromJs\", nullptr, GetEnumValueFromJs, 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"
]
"// Todo: you can use \"napiint32in\" or \"napistringutf8in\" command that get int32_t or string value from js.",
"// Todo: you can use \"napiint32out\" or \"napistringutf8out\" command that return int32_t or string value to js"
],
"description": "enum in 和 out是否要分开"
}
}

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

@ -2,32 +2,12 @@
"Napi Struct": {
"prefix": "napistruct",
"body": [
"napi_value GetStructValueFromJs(napi_env env, napi_callback_info info)",
"{",
" // Todo: you can use \"napiobjectin\" command that get object value from js.",
" // Todo: you can use \"napigetvaluefromobject\" command that get value from js object.",
" return nullptr;",
"}",
"napi_value CreateStructObjectToJs(napi_env env, napi_callback_info info)",
"{",
" // Todo: you can use \"napiobjectout\" command that return object to js.",
" // Todo: you can use \"napisetvalue2object\" command that set value to objectOut.",
" return objectOut.",
"}",
"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[] = {",
" { \"getStructValueFromJs\", nullptr, GetStructValueFromJs, nullptr, nullptr, nullptr, napi_default, nullptr },",
" { \"createStructObjectToJs\", nullptr, CreateStructObjectToJs, 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"
]
"// Todo: you can use \"napiobjectin\" command that get object value from js.",
"// Todo: you can use \"napigetvaluefromobject\" command that get value from js object.",
"// Todo: business code.",
"// Todo: you can use \"napiobjectout\" command that return object to js.",
"// Todo: you can use \"napisetvalue2object\" command that set value to objectOut."
],
"description": "是否需要将struct的in和out分开"
}
}

View File

@ -1,27 +0,0 @@
{
"Napi SyncFunc": {
"prefix": "napisyncfunc",
"body": [
"static napi_value Add(napi_env env, napi_callback_info info)",
"{",
" // Todo: you can use \"napiobjectin\" command that get object param from js.",
" // Todo: you can use \"napidoublein\" command that get double param from js.",
" // Todo: you can use \"napidoubleout\" command that return double value to js.",
" return doubleOut;",
"}",
"EXTERN_C_START",
"// Initialize the module.",
"static napi_value Init(napi_env env, napi_value exports)",
"{",
" // Define promise method for a N-API object.",
" napi_property_descriptor desc[] = {",
" {\"add\", nullptr, Add, 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"
]
}
}