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..58b3d36b 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,7 +29,26 @@ JSCallbackObject::JSCallbackObject(napi_env env, napi_value callback, std::threa JSCallbackObject::~JSCallbackObject() { if (callback_ != nullptr) { - napi_delete_reference(env_, callback_); + if (threadId_ == std::this_thread::get_id()) { + napi_delete_reference(env_, callback_); + env_ = nullptr; + return; + } + std::shared_ptr work = std::make_shared(); + isDone_ = std::make_shared>(MAX_TIMEOUT, false); + work->data = this; + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + uv_queue_work_with_qos( + loop, work.get(), [](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; } 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 diff --git a/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp index d25f5927..f8129b88 100644 --- a/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp +++ b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp @@ -35,12 +35,10 @@ std::shared_ptr PanelListenerImpl::GetInstance() PanelListenerImpl::~PanelListenerImpl() { - env_ = nullptr; } void PanelListenerImpl::SaveInfo(napi_env env, const std::string &type, napi_value callback, uint32_t windowId) { - env_ = env; std::shared_ptr cbObject = std::make_shared(env, callback, std::this_thread::get_id()); auto result = callbacks_.Find(windowId); @@ -93,7 +91,7 @@ void PanelListenerImpl::OnPanelStatus(uint32_t windowId, bool isShow) } work->data = new (std::nothrow) UvEntry(callback.second); uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); + napi_get_uv_event_loop(callback.second->env_, &loop); uv_queue_work_with_qos( loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { diff --git a/frameworks/js/napi/inputmethodability/panel_listener_impl.h b/frameworks/js/napi/inputmethodability/panel_listener_impl.h index 496a8446..0b02cb3f 100644 --- a/frameworks/js/napi/inputmethodability/panel_listener_impl.h +++ b/frameworks/js/napi/inputmethodability/panel_listener_impl.h @@ -40,7 +40,6 @@ public: std::shared_ptr cbCopy; explicit UvEntry(const std::shared_ptr &cb) : cbCopy(cb) {} }; - napi_env env_ = nullptr; ConcurrentMap>> callbacks_; static std::mutex listenerMutex_; static std::shared_ptr instance_;