From e0a30ea34f8438c2136a2706d617545dbbb87d93 Mon Sep 17 00:00:00 2001 From: ma-shaoyin Date: Fri, 4 Aug 2023 20:17:26 +0800 Subject: [PATCH] Signed-off-by: ma-shaoyin Changes to be committed: --- frameworks/js/napi/common/BUILD.gn | 5 +++- .../js/napi/common/js_callback_object.cpp | 26 +++++++++++++++++++ .../js/napi/common/js_callback_object.h | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/common/BUILD.gn b/frameworks/js/napi/common/BUILD.gn index 63fc5f46..ba78e211 100644 --- a/frameworks/js/napi/common/BUILD.gn +++ b/frameworks/js/napi/common/BUILD.gn @@ -27,7 +27,10 @@ config("inputmethod_js_common_config") { config("inputmethod_js_common_public_config") { visibility = [ "./*" ] - include_dirs = [ "./" ] + include_dirs = [ + "./", + "${inputmethod_path}/frameworks/common", + ] } ohos_static_library("inputmethod_js_common") { diff --git a/frameworks/js/napi/common/js_callback_object.cpp b/frameworks/js/napi/common/js_callback_object.cpp index 8ef70bca..340b64ba 100644 --- a/frameworks/js/napi/common/js_callback_object.cpp +++ b/frameworks/js/napi/common/js_callback_object.cpp @@ -13,10 +13,13 @@ * limitations under the License. */ +#include + #include "js_callback_object.h" namespace OHOS { namespace MiscServices { +constexpr int32_t MAX_TIMEOUT = 2000; JSCallbackObject::JSCallbackObject(napi_env env, napi_value callback, std::thread::id threadId) : env_(env), threadId_(threadId) { @@ -26,8 +29,31 @@ JSCallbackObject::JSCallbackObject(napi_env env, napi_value callback, std::threa JSCallbackObject::~JSCallbackObject() { if (callback_ != nullptr) { + if (threadId_ == std::this_thread::get_id()) { + napi_delete_reference(env_, callback_); + env_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + return; + } + isDone_ = std::make_shared>(MAX_TIMEOUT, false); + work->data = this; + uv_loop_s *loop = nullptr; napi_delete_reference(env_, callback_); + napi_get_uv_event_loop(env_, &loop); + uv_queue_work_with_qos( + loop, work, [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + JSCallbackObject *jsObject = static_cast(work->data); + napi_delete_reference(jsObject->env_, jsObject->callback_); + bool isFinish = true; + jsObject->isDone_->SetValue(isFinish); + }, + uv_qos_user_initiated); } + isDone_->GetValue(); env_ = nullptr; } } // namespace MiscServices diff --git a/frameworks/js/napi/common/js_callback_object.h b/frameworks/js/napi/common/js_callback_object.h index 45c4f5b7..5f9a5ca3 100644 --- a/frameworks/js/napi/common/js_callback_object.h +++ b/frameworks/js/napi/common/js_callback_object.h @@ -17,6 +17,7 @@ #include +#include "block_data.h" #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -29,6 +30,7 @@ public: napi_ref callback_ = nullptr; napi_env env_{}; std::thread::id threadId_; + std::shared_ptr> isDone_; }; } // namespace MiscServices } // namespace OHOS