From 397e0dc25a604ab73ed7062efc4bdc4bda586be2 Mon Sep 17 00:00:00 2001 From: zhouyongfei Date: Wed, 20 Oct 2021 10:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8ANapi=E6=8E=A5=E5=8F=A3=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=94=BE=E5=88=B0ui=E7=9A=84=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=B8=AD=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhouyongfei --- .../include/event_target.h | 11 +-- .../inputmethod_ability/src/event_target.cpp | 68 +++++++++---------- .../src/input_method_ability.cpp | 4 +- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/frameworks/inputmethod_ability/include/event_target.h b/frameworks/inputmethod_ability/include/event_target.h index 96b99ec..afd0c49 100644 --- a/frameworks/inputmethod_ability/include/event_target.h +++ b/frameworks/inputmethod_ability/include/event_target.h @@ -17,6 +17,7 @@ #define INPUT_METHOD_NAPI_EVENT_TARGET_H #include "napi/native_api.h" +#include "napi/native_node_api.h" #include "global.h" #include "input_method_ability.h" @@ -37,7 +38,7 @@ namespace MiscServices { virtual void Once(const char *type, napi_value handler); virtual void Off(const char *type, napi_value handler); virtual void Off(const char *type); - virtual void Emit(const char *type, Event *event); + virtual void Emit(sptr &eventTarget, const char *type, Event *event); protected: napi_env env_; @@ -45,14 +46,6 @@ namespace MiscServices { EventListener *first_; EventListener *last_; }; - - class UvWorkMsg{ - public: - UvWorkMsg(const char *type, Event *event); - ~UvWorkMsg(){}; - char *type_; - Event *event_; - }; } } #endif // INPUT_METHOD_NAPI_EVENT_TARGET_H \ No newline at end of file diff --git a/frameworks/inputmethod_ability/src/event_target.cpp b/frameworks/inputmethod_ability/src/event_target.cpp index 0d95845..4c6f1b0 100644 --- a/frameworks/inputmethod_ability/src/event_target.cpp +++ b/frameworks/inputmethod_ability/src/event_target.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include #include "event_target.h" #include "securec.h" @@ -29,6 +29,15 @@ namespace MiscServices { EventListener *back = nullptr; EventListener *next = nullptr; }; + struct EventTargetCB { + napi_env env; + napi_ref thisVarRef; + EventListener *first; + EventListener *last; + sptr &eventTarget; + const char *type; + Event *event; + }; EventTarget::EventTarget(napi_env env, napi_value thisVar) { IMSA_HILOGI("EventTarget::EventTarget"); @@ -159,7 +168,7 @@ namespace MiscServices { } } - void EventTarget::Emit(const char *type, Event *event) + void EventTarget::Emit(sptr &eventTarget, const char *type, Event *event) { IMSA_HILOGI("EventTarget::Emit"); uv_loop_s *loop = nullptr; @@ -169,66 +178,53 @@ namespace MiscServices { return; } - uv_work_t *work1 = new (std::nothrow) uv_work_t; - if (work1 == nullptr) { - IMSA_HILOGI("EventTarget::Emit No memory work1 == nullptr"); + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + IMSA_HILOGI("EventTarget::Emit No memory work == nullptr"); return; } - UvWorkMsg *workData = new (std::nothrow) UvWorkMsg(type, event); - if (workData == nullptr) { - IMSA_HILOGI("EventTarget::Emit No memory workData == nullptr"); - delete work1; - return; - } + EventTargetCB *eventTargetCB = new (std::nothrow) EventTargetCB {.env = env_, .thisVarRef = thisVarRef_, + .first = first_, .last = last_, .type = type, .event = event, .eventTarget = eventTarget}; - work1->data = reinterpret_cast(workData); - int ret = uv_queue_work(loop, work1, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { + work->data = (void *)eventTargetCB; + + int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { //Js Thread if (work == nullptr) { IMSA_HILOGI("EventTarget::Emit work == nullptr"); return; } - UvWorkMsg *workMsg = reinterpret_cast(work->data); + EventTargetCB *eventTargetCB = (EventTargetCB *)work->data; do { - if (!workMsg) { - IMSA_HILOGI("EventTarget::Emit workMsg is nullptr"); - break; - } - napi_handle_scope scope = nullptr; - napi_open_handle_scope(env_, &scope); + napi_open_handle_scope(eventTargetCB->env, &scope); napi_value thisVar = nullptr; - napi_get_reference_value(env_, thisVarRef_, &thisVar); - for (EventListener *eventListener = first_; eventListener != nullptr; eventListener = eventListener->next) { - if (strcmp(eventListener->type, workMsg->type) == 0) { - napi_value jsEvent = event ? event->ToJsObject() : nullptr; + napi_get_reference_value(eventTargetCB->env, eventTargetCB->thisVarRef, &thisVar); + for (EventListener *eventListener = eventTargetCB->first; eventListener != nullptr; eventListener = eventListener->next) { + if (strcmp(eventListener->type, eventTargetCB->type) == 0) { + napi_value jsEvent = eventTargetCB->event ? eventTargetCB->event->ToJsObject() : nullptr; napi_value handler = nullptr; napi_value result = nullptr; - napi_get_reference_value(env_, eventListener->handlerRef, &handler); - napi_call_function(env_, thisVar, handler, jsEvent ? 1 : 0, jsEvent ? &jsEvent : nullptr, &result); + napi_get_reference_value(eventTargetCB->env, eventListener->handlerRef, &handler); + napi_call_function(eventTargetCB->env, thisVar, handler, jsEvent ? 1 : 0, jsEvent ? &jsEvent : nullptr, &result); if (eventListener->isOnce) { - Off(workMsg->type, handler); + eventTargetCB->eventTarget->Off(eventTargetCB->type, handler); } } } - napi_close_handle_scope(env_, scope); + napi_close_handle_scope(eventTargetCB->env, scope); } while (0); - if (workMsg) { - delete workMsg; + if (eventTargetCB) { + delete eventTargetCB; } delete work; }); if (ret != 0) { IMSA_HILOGI("EventTarget::Emit failed to execute libuv work queue"); - delete workData; - delete work1; + delete work; } } - - UvWorkMsg::UvWorkMsg(const char *type, Event *event) : type_(type), event_(event) - { - } } } \ No newline at end of file diff --git a/frameworks/inputmethod_ability/src/input_method_ability.cpp b/frameworks/inputmethod_ability/src/input_method_ability.cpp index 6be1a4d..3d7c395 100644 --- a/frameworks/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/inputmethod_ability/src/input_method_ability.cpp @@ -240,13 +240,13 @@ namespace MiscServices { void InputMethodAbility::ShowInputWindow() { IMSA_HILOGI("InputMethodAbility::ShowInputWindow"); - eventTarget_->Emit("keyboardShow", nullptr); + eventTarget_->Emit(eventTarget_, "keyboardShow", nullptr); } void InputMethodAbility::DissmissInputWindow() { IMSA_HILOGI("InputMethodAbility::DissmissInputWindow"); - eventTarget_->Emit("keyboardHide", nullptr); + eventTarget_->Emit(eventTarget_, "keyboardHide", nullptr); } bool InputMethodAbility::InsertText(const std::string text)