From 519e5b1f8cb2e906e75b5b08657910e638e6e676 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Wed, 6 Nov 2024 14:04:47 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0napi=20async=20callback,p?= =?UTF-8?q?romise=20snippets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/package.json | 4 + .../snippets/napi_async_snippets.json | 127 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/vscode_plugin/snippets/napi_async_snippets.json diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index c2eeda75..77ca48ea 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -111,6 +111,10 @@ { "language": "cpp", "path": "./snippets/napi_class_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_async_snippets.json" } ] }, diff --git a/src/vscode_plugin/snippets/napi_async_snippets.json b/src/vscode_plugin/snippets/napi_async_snippets.json new file mode 100644 index 00000000..b4015279 --- /dev/null +++ b/src/vscode_plugin/snippets/napi_async_snippets.json @@ -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(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(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(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(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" + ] + } +} \ No newline at end of file -- Gitee From 460f9fc0e4e5c4897ffd78ef42d7bf8e6fcdbf3f Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Wed, 6 Nov 2024 14:51:13 +0800 Subject: [PATCH 2/7] fix snippets bug Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/snippets/napi_async_snippets.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vscode_plugin/snippets/napi_async_snippets.json b/src/vscode_plugin/snippets/napi_async_snippets.json index b4015279..41d1882f 100644 --- a/src/vscode_plugin/snippets/napi_async_snippets.json +++ b/src/vscode_plugin/snippets/napi_async_snippets.json @@ -85,7 +85,7 @@ " CallbackData *callbackData = reinterpret_cast(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);", + " 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;", -- Gitee From 2a4987cefb7f9593b1993273f26f6f232f352596 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Thu, 7 Nov 2024 17:11:38 +0800 Subject: [PATCH 3/7] add async func snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_async_snippets.json | 123 ++++-------------- 1 file changed, 24 insertions(+), 99 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_async_snippets.json b/src/vscode_plugin/snippets/napi_async_snippets.json index 41d1882f..d4c03560 100644 --- a/src/vscode_plugin/snippets/napi_async_snippets.json +++ b/src/vscode_plugin/snippets/napi_async_snippets.json @@ -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(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(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(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(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(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(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);", -- Gitee From 471b023a483eaf2547652ae2a3b9e2e2126e5dab Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Thu, 7 Nov 2024 17:53:16 +0800 Subject: [PATCH 4/7] add napi promise snippets Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/package.json | 4 ++ .../snippets/napi_promise_snippets.json | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/vscode_plugin/snippets/napi_promise_snippets.json diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index 77ca48ea..eaaee85a 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -115,6 +115,10 @@ { "language": "cpp", "path": "./snippets/napi_async_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_promise_snippets.json" } ] }, diff --git a/src/vscode_plugin/snippets/napi_promise_snippets.json b/src/vscode_plugin/snippets/napi_promise_snippets.json new file mode 100644 index 00000000..0a01d6c2 --- /dev/null +++ b/src/vscode_plugin/snippets/napi_promise_snippets.json @@ -0,0 +1,41 @@ +{ + "Napi Promise": { + "prefix": "napipromise", + "body": [ + "static napi_value PromiseSample(napi_env env, napi_callback_info info)", + "{", + " 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.", + " bool isResolved = true;", + " if (isResolved) {", + " // Fulfill a js Promise with a given value.", + " napi_resolve_deferred(env, deferred, doubleOut);", + " } else {", + " // Reject a js Promise with a given reason.", + " napi_reject_deferred(env, deferred, doubleOut);", + " }", + " 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" + ] + } +} \ No newline at end of file -- Gitee From 13f7e4f0c337b4dae013cb6bfbaa0a86c4ffd5e6 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Thu, 7 Nov 2024 19:07:53 +0800 Subject: [PATCH 5/7] fix napi class snippets bug Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/snippets/napi_class_snippets.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index a5c03b40..b94eb08f 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -50,6 +50,7 @@ " // Retrieve obj (the C++ object) previously wrapped in jsThis (the ArkTS object), and perform subsequent operations.", " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", " // Todo: you can use \"napistringutf8out\" command that return string utf8 value to js.", + " return stringOut;", "}", "napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info)", "{", -- Gitee From 4274da8b038b799e7fb699e76cbd06a4beda9eaa Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Thu, 7 Nov 2024 19:11:45 +0800 Subject: [PATCH 6/7] add napi callback snippets Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/package.json | 4 +++ .../snippets/napi_callback_snippets.json | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/vscode_plugin/snippets/napi_callback_snippets.json diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index eaaee85a..3d361525 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -119,6 +119,10 @@ { "language": "cpp", "path": "./snippets/napi_promise_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_callback_snippets.json" } ] }, diff --git a/src/vscode_plugin/snippets/napi_callback_snippets.json b/src/vscode_plugin/snippets/napi_callback_snippets.json new file mode 100644 index 00000000..1e65a56b --- /dev/null +++ b/src/vscode_plugin/snippets/napi_callback_snippets.json @@ -0,0 +1,32 @@ +{ + "Napi Callback": { + "prefix": "napicallback", + "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 result;", + " // Invokes a js function with the provided arguments and returns the result.", + " napi_call_function(env, nullptr, args[2], 1, &doubleOut, &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" + ] + } +} \ No newline at end of file -- Gitee From 2f4bde8c7aced59427c85d1827d44b16addcdbe4 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Fri, 8 Nov 2024 10:06:29 +0800 Subject: [PATCH 7/7] modify napi async, callback, promise snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_async_snippets.json | 21 ++++++------------- .../snippets/napi_callback_snippets.json | 9 +++----- .../snippets/napi_promise_snippets.json | 10 +++------ 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_async_snippets.json b/src/vscode_plugin/snippets/napi_async_snippets.json index d4c03560..c9eabeb6 100644 --- a/src/vscode_plugin/snippets/napi_async_snippets.json +++ b/src/vscode_plugin/snippets/napi_async_snippets.json @@ -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(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(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);", diff --git a/src/vscode_plugin/snippets/napi_callback_snippets.json b/src/vscode_plugin/snippets/napi_callback_snippets.json index 1e65a56b..f8589865 100644 --- a/src/vscode_plugin/snippets/napi_callback_snippets.json +++ b/src/vscode_plugin/snippets/napi_callback_snippets.json @@ -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", diff --git a/src/vscode_plugin/snippets/napi_promise_snippets.json b/src/vscode_plugin/snippets/napi_promise_snippets.json index 0a01d6c2..7a11abbf 100644 --- a/src/vscode_plugin/snippets/napi_promise_snippets.json +++ b/src/vscode_plugin/snippets/napi_promise_snippets.json @@ -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;", "}", -- Gitee