增加napi async callback,promise snippets

Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
chen-zhongwei050 2024-11-06 14:04:47 +08:00
parent a051ab8659
commit 519e5b1f8c
2 changed files with 131 additions and 0 deletions

View File

@ -111,6 +111,10 @@
{
"language": "cpp",
"path": "./snippets/napi_class_snippets.json"
},
{
"language": "cpp",
"path": "./snippets/napi_async_snippets.json"
}
]
},

View File

@ -0,0 +1,127 @@
{
"Napi Async Callback": {
"prefix": "napiasynccallback",
"body": [
"struct CallbackData {",
" napi_async_work asyncWork = nullptr;",
" napi_ref callbackRef = nullptr;",
" double args[2] = {0};",
" double result = 0;",
"};",
"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 ExecuteCBCallback(napi_env env, void *data)",
"{",
" CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);",
" // Todo: business code. eg:callbackData->result = callbackData->args[0] + callbackData->args[1];",
"}",
"napi_value AsyncWorkCallback(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);",
" 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);",
" // Create an asynchronous work object.",
" napi_create_async_work(env, nullptr, resourceName, ExecuteCBCallback, CompleteCBCallback, asyncContext, &asyncContext->asyncWork);",
" // Add the asynchronous work object to a queue.",
" napi_queue_async_work(env, asyncContext->asyncWork);",
" 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[] = {",
" { \"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, result);",
" // 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 }",
" };",
" // 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"
]
}
}