mirror of
https://gitee.com/openharmony/napi_generator
synced 2024-11-23 08:20:01 +00:00
modify napi async, callback, promise snippets
Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
parent
4274da8b03
commit
2f4bde8c7a
@ -5,26 +5,17 @@
|
||||
"struct AsyncData{",
|
||||
" napi_async_work work;",
|
||||
" napi_ref callbackRef;",
|
||||
" double args[2] = {0}; // save async work param in.",
|
||||
" double result; // save async work result.",
|
||||
" // save async work param in.",
|
||||
" // save async work result.",
|
||||
"};",
|
||||
"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);",
|
||||
" 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);",
|
||||
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"CompleteAsyncWork. asyncData->result: %{public}f\", asyncData->result);",
|
||||
" OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"CompleteAsyncWork\");",
|
||||
"}",
|
||||
"napi_value StartAsyncWork(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.",
|
||||
" AsyncData* asyncData = new AsyncData();",
|
||||
" asyncData->args[0] = value0;",
|
||||
" asyncData->args[1] = value1;",
|
||||
" asyncData->result = 0;",
|
||||
"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);",
|
||||
|
@ -4,14 +4,11 @@
|
||||
"body": [
|
||||
"static napi_value CallbackSample(napi_env env, napi_callback_info info)",
|
||||
"{",
|
||||
" // Todo: you can use \"napiobjectin\" command that get object value from js.",
|
||||
" // Todo: you can use \"napidoublein\" command that get double value from js.",
|
||||
" // Business code.",
|
||||
" double myRes = value0 + value1;",
|
||||
" // Todo: you can use \"napidoubleout\" command that return double to js.",
|
||||
" 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, args[2], 1, &doubleOut, &result);",
|
||||
" napi_call_function(env, nullptr, callback, 1, callbackArg, &result);",
|
||||
" return nullptr;",
|
||||
"}",
|
||||
"EXTERN_C_START",
|
||||
|
@ -6,20 +6,16 @@
|
||||
"{",
|
||||
" napi_value promise;",
|
||||
" napi_deferred deferred;",
|
||||
" // Todo: you can use \"napiobjectin\" command that get object value from js.",
|
||||
" // Todo: you can use \"napidoublein\" command that get double value from js.",
|
||||
" // Business code. ",
|
||||
" double myRes = value0 + value1;",
|
||||
" // Create a new js Promise.",
|
||||
" napi_create_promise(env, &deferred, &promise);",
|
||||
" // Todo: you can use \"napidoubleout\" command that return double to js.",
|
||||
" napi_value valueOut = nullptr;",
|
||||
" bool isResolved = true;",
|
||||
" if (isResolved) {",
|
||||
" // Fulfill a js Promise with a given value.",
|
||||
" napi_resolve_deferred(env, deferred, doubleOut);",
|
||||
" napi_resolve_deferred(env, deferred, valueOut);",
|
||||
" } else {",
|
||||
" // Reject a js Promise with a given reason.",
|
||||
" napi_reject_deferred(env, deferred, doubleOut);",
|
||||
" napi_reject_deferred(env, deferred, valueOut);",
|
||||
" }",
|
||||
" return promise;",
|
||||
"}",
|
||||
|
Loading…
Reference in New Issue
Block a user