add async func snippets

Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
chen-zhongwei050 2024-11-07 17:11:38 +08:00
parent 460f9fc0e4
commit 2a4987cefb

View File

@ -1,52 +1,37 @@
{
"Napi Async Callback": {
"prefix": "napiasynccallback",
"Napi Async Func": {
"prefix": "napiasyncfunc",
"body": [
"struct CallbackData {",
" napi_async_work asyncWork = nullptr;",
" napi_ref callbackRef = nullptr;",
" double args[2] = {0};",
" double result = 0;",
"struct AsyncData{",
" napi_async_work work;",
" napi_ref callbackRef;",
" double args[2] = {0}; // save async work param in.",
" double result; // save async work result.",
"};",
"static void CompleteCBCallback(napi_env env, napi_status status, void *data)",
"{",
" CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);",
" napi_value callbackArg[1] = {nullptr};",
" // Todo: you can use \"napidoubleout\" command that return double value to js.",
" napi_value callback = nullptr;",
" // Retrieve the ArkTs value associated with a reference created by napi_create_reference.",
" napi_get_reference_value(env, callbackData->callbackRef, &callback);",
" // Execute the callback.",
" napi_value result;",
" napi_value undefined;",
" napi_get_undefined(env, &undefined);",
" // Invoke a ArkTs function with the provided arguments in a given environment.",
" napi_call_function(env, undefined, callback, 1, callbackArg, &result);",
" // Delete the napi_ref object and asynchronous work object.",
" napi_delete_reference(env, callbackData->callbackRef);",
" // Clean up an asynchronous work request and its associated resources.",
" napi_delete_async_work(env, callbackData->asyncWork);",
" delete callbackData;",
"static void ExecuteAsyncWork(napi_env env, void* data) {",
" AsyncData* asyncData = static_cast<AsyncData*>(data);",
" // Business code example. Execute plus work.",
" asyncData->result = asyncData->args[0] + asyncData->args[1];",
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"ExecuteAsyncWork. asyncData->result: %{public}f\", asyncData->result);",
"}",
"static void ExecuteCBCallback(napi_env env, void *data)",
"{",
" CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);",
" // Todo: business code. eg:callbackData->result = callbackData->args[0] + callbackData->args[1];",
"static void CompleteAsyncWork(napi_env env, napi_status status, void* data) {",
" AsyncData* asyncData = static_cast<AsyncData*>(data);",
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"CompleteAsyncWork. asyncData->result: %{public}f\", asyncData->result);",
"}",
"napi_value AsyncWorkCallback(napi_env env, napi_callback_info info)",
"{",
"napi_value StartAsyncWork(napi_env env, napi_callback_info info) {",
" // Todo: you can use \"napiobjectin\" command that get object param from js.",
" auto asyncContext = new CallbackData();",
" // Todo: you can use \"napidoublein\" command that get double param from js.",
" // Convert the callback to napi_ref to extend its lifecycle to prevent it from being garbage-collected.",
" napi_create_reference(env, args[2], 1, &asyncContext->callbackRef);",
" AsyncData* asyncData = new AsyncData();",
" asyncData->args[0] = value0;",
" asyncData->args[1] = value1;",
" asyncData->result = 0;",
" 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, \"asyncWorkCallback\", NAPI_AUTO_LENGTH, &resourceName);",
" napi_create_string_utf8(env, \"asyncWork\", NAPI_AUTO_LENGTH, &resourceName);",
" // Create an asynchronous work object.",
" napi_create_async_work(env, nullptr, resourceName, ExecuteCBCallback, CompleteCBCallback, asyncContext, &asyncContext->asyncWork);",
" 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, asyncContext->asyncWork);",
" napi_queue_async_work(env, asyncData->work);",
" return nullptr;",
"}",
"EXTERN_C_START",
@ -55,67 +40,7 @@
"{",
" // Define properties and methods for a N-API object.",
" napi_property_descriptor desc[] = {",
" { \"asyncWorkCallback\", nullptr, AsyncWorkCallback, 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"
]
},
"Napi Async Promise": {
"prefix": "napiasyncpromise",
"body": [
"struct CallbackData {",
" napi_async_work asyncWork = nullptr;",
" napi_deferred deferred = nullptr;",
" napi_ref callback = nullptr;",
" double args[2] = {0};",
" double result = 0;",
"};",
"static void ExecuteCBPromise(napi_env env, void *data)",
"{",
" CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);",
" // Todo: business code. eg:callbackData->result = callbackData->args[0] + callbackData->args[1];",
"}",
"static void CompleteCBPromise(napi_env env, napi_status status, void *data)",
"{",
" CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);",
" // Todo: you can use \"napidoubleout\" command that return double value to js.",
" // Fulfill a ArkTs Promise with a given value.",
" napi_resolve_deferred(env, callbackData->deferred, doubleOut);",
" // Clean up an asynchronous work request and its associated resources.",
" napi_delete_async_work(env, callbackData->asyncWork);",
" delete callbackData;",
"}",
"static napi_value AsyncWorkPromise(napi_env env, napi_callback_info info)",
"{",
" // Todo: you can use \"napiobjectin\" command that get object param from js.",
" napi_value promise = nullptr;",
" napi_deferred deferred = nullptr;",
" // Initialize a new ArkTs Promise and retrieves its deferred completion handle.",
" napi_create_promise(env, &deferred, &promise);",
" auto callbackData = new CallbackData();",
" callbackData->deferred = deferred;",
" // Todo: you can use \"napidoublein\" command that get double param from js.",
" 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, \"asyncWorkPromise\", NAPI_AUTO_LENGTH, &resourceName);",
" // Create an asynchronous work object.",
" napi_create_async_work(env, nullptr, resourceName, ExecuteCBPromise, CompleteCBPromise, callbackData, &callbackData->asyncWork);",
" // Add the asynchronous work object to a queue.",
" napi_queue_async_work(env, callbackData->asyncWork);",
" return promise;",
"}",
"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[] = {",
" { \"asyncWorkPromise\", nullptr, AsyncWorkPromise, nullptr, nullptr, nullptr, napi_default, nullptr }",
" { \"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);",