From ed1ad6b0837f7824450fa0b4e39d8e3039c9cef0 Mon Sep 17 00:00:00 2001 From: youzhi92 Date: Thu, 14 Oct 2021 15:43:14 +0800 Subject: [PATCH] issueNo: I4DVKW Description:refactor event listener Feature or Bugfix:Bugfix Binary Source:No Signed-off-by: youzhi92 Change-Id: I17c89be34cd82bfd92e9b34c6e19f603b44f4f04 --- frameworks/src/core/base/event_util.cpp | 2 -- frameworks/src/core/components/component.cpp | 6 ++--- .../src/core/components/event_listener.cpp | 3 +-- .../src/core/components/event_listener.h | 23 +++++++++++-------- .../components/input_checkbox_component.cpp | 4 ++-- .../core/components/input_radio_component.cpp | 4 ++-- .../src/core/components/switch_component.cpp | 4 ++-- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/frameworks/src/core/base/event_util.cpp b/frameworks/src/core/base/event_util.cpp index 5612e08..9c0eed6 100755 --- a/frameworks/src/core/base/event_util.cpp +++ b/frameworks/src/core/base/event_util.cpp @@ -52,7 +52,6 @@ void CallbackExecutor(void *data) JSValue args[argsLength] = {params->arg}; JSRelease(JSFunction::Call(params->fn, params->vm, args, argsLength)); JSRelease(params->arg); - JSRelease(params->vm); delete params; params = nullptr; } @@ -127,7 +126,6 @@ void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event, cons HILOG_ERROR(HILOG_MODULE_ACE, "EventUtil::InvokeCallback failed: Async task dispatch failure."); delete params; params = nullptr; - JSRelease(vm); JSRelease(event); } } diff --git a/frameworks/src/core/components/component.cpp b/frameworks/src/core/components/component.cpp index 39ea5cb..0a8084e 100755 --- a/frameworks/src/core/components/component.cpp +++ b/frameworks/src/core/components/component.cpp @@ -1126,7 +1126,7 @@ void Component::ParseAttrs() void Component::SetClickEventListener(UIView &view, const jerry_value_t eventFunc, bool isStopPropagation) { - onClickListener_ = new ViewOnClickListener(eventFunc, isStopPropagation); + onClickListener_ = new ViewOnClickListener(viewModel_, eventFunc, isStopPropagation); if (onClickListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "click listener create failed"); return; @@ -1205,7 +1205,7 @@ void Component::SetKeyBoardEventListener(jerry_value_t eventFunc, uint16_t event void Component::SetLongPressEventListener(UIView &view, const jerry_value_t eventFunc, bool isStopPropagation) { - onLongPressListener_ = new ViewOnLongPressListener(eventFunc, isStopPropagation); + onLongPressListener_ = new ViewOnLongPressListener(viewModel_, eventFunc, isStopPropagation); if (onLongPressListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "long press listener create failed"); return; @@ -1217,7 +1217,7 @@ void Component::SetLongPressEventListener(UIView &view, const jerry_value_t even void Component::SetSwipeEventListener(UIView &view, jerry_value_t eventFunc, bool isStopPropagation) { - onSwipeListener_ = new ViewOnSwipeListener(eventFunc, isStopPropagation); + onSwipeListener_ = new ViewOnSwipeListener(viewModel_, eventFunc, isStopPropagation); if (onSwipeListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "swipe listener create failed"); return; diff --git a/frameworks/src/core/components/event_listener.cpp b/frameworks/src/core/components/event_listener.cpp index 24267ae..ed1c8d9 100755 --- a/frameworks/src/core/components/event_listener.cpp +++ b/frameworks/src/core/components/event_listener.cpp @@ -146,8 +146,7 @@ bool ViewOnSwipeListener::OnDragEnd(UIView& view, const DragEvent &event) HILOG_DEBUG(HILOG_MODULE_ACE, "OnDragEnd received"); JSValue arg = EventUtil::CreateSwipeEvent(view, event); - JSValue vm = GetRootAbilitySlice(); - EventUtil::InvokeCallback(vm, fn_, arg, this); + EventUtil::InvokeCallback(vm_, fn_, arg, this); return isStopPropagation_; } } // namespace ACELite diff --git a/frameworks/src/core/components/event_listener.h b/frameworks/src/core/components/event_listener.h index a95f832..8fdbb4e 100755 --- a/frameworks/src/core/components/event_listener.h +++ b/frameworks/src/core/components/event_listener.h @@ -87,14 +87,16 @@ class ViewOnClickListener final : public UIView::OnClickListener { public: ACE_DISALLOW_COPY_AND_MOVE(ViewOnClickListener); - ViewOnClickListener(jerry_value_t fn, bool isStopPropagation) + ViewOnClickListener(jerry_value_t vm, jerry_value_t fn, bool isStopPropagation) : changeListener_(nullptr), + vm_(jerry_acquire_value(vm)), fn_(jerry_acquire_value(fn)), isStopPropagation_(isStopPropagation) {} ~ViewOnClickListener() { AsyncTaskManager::GetInstance().CancelWithContext(this); + jerry_release_value(vm_); jerry_release_value(fn_); } @@ -108,8 +110,7 @@ public: return isStopPropagation_; } JSValue arg = EventUtil::CreateEvent(EventUtil::EVENT_CLICK, view, event); - JSValue vm = GetRootAbilitySlice(); - EventUtil::InvokeCallback(vm, fn_, arg, this); + EventUtil::InvokeCallback(vm_, fn_, arg, this); return isStopPropagation_; } @@ -121,6 +122,7 @@ public: private: StateChangeListener *changeListener_; + jerry_value_t vm_; jerry_value_t fn_; bool isStopPropagation_; }; @@ -128,12 +130,13 @@ private: class ViewOnLongPressListener final : public UIView::OnLongPressListener { public: ACE_DISALLOW_COPY_AND_MOVE(ViewOnLongPressListener); - ViewOnLongPressListener(jerry_value_t fn, bool isStopPropagation) - : fn_(jerry_acquire_value(fn)), isStopPropagation_(isStopPropagation) {} + ViewOnLongPressListener(jerry_value_t vm, jerry_value_t fn, bool isStopPropagation) + : vm_(jerry_acquire_value(vm)), fn_(jerry_acquire_value(fn)), isStopPropagation_(isStopPropagation) {} ~ViewOnLongPressListener() { AsyncTaskManager::GetInstance().CancelWithContext(this); + jerry_release_value(vm_); jerry_release_value(fn_); } @@ -144,12 +147,12 @@ public: } JSValue arg = EventUtil::CreateEvent(EventUtil::EVENT_LONGPRESS, view, event); - JSValue vm = GetRootAbilitySlice(); - EventUtil::InvokeCallback(vm, fn_, arg, this); + EventUtil::InvokeCallback(vm_, fn_, arg, this); return isStopPropagation_; } jerry_value_t fn_; + jerry_value_t vm_; bool isStopPropagation_; }; @@ -218,14 +221,15 @@ private: class ViewOnSwipeListener final : public UIView::OnDragListener { public: ACE_DISALLOW_COPY_AND_MOVE(ViewOnSwipeListener); - ViewOnSwipeListener(jerry_value_t fn, bool isStopPropagation) - : fn_(jerry_acquire_value(fn)), isStopPropagation_(isStopPropagation) + ViewOnSwipeListener(jerry_value_t vm, jerry_value_t fn, bool isStopPropagation) + : vm_(vm), fn_(jerry_acquire_value(fn)), isStopPropagation_(isStopPropagation) { } ~ViewOnSwipeListener() { AsyncTaskManager::GetInstance().CancelWithContext(this); + jerry_release_value(vm_); jerry_release_value(fn_); } @@ -235,6 +239,7 @@ public: bool OnDragEnd(UIView& view, const DragEvent &event) override; private: + jerry_value_t vm_; jerry_value_t fn_; bool isStopPropagation_; }; diff --git a/frameworks/src/core/components/input_checkbox_component.cpp b/frameworks/src/core/components/input_checkbox_component.cpp index 7c782b6..d613ec7 100755 --- a/frameworks/src/core/components/input_checkbox_component.cpp +++ b/frameworks/src/core/components/input_checkbox_component.cpp @@ -69,7 +69,7 @@ bool InputCheckboxComponent::RegisterPrivateEventListener(uint16_t eventTypeId, return true; } if (eventTypeId == K_CLICK) { - clickListener_ = new ViewOnClickListener(funcValue, isStopPropagation); + clickListener_ = new ViewOnClickListener(GetViewModel(), funcValue, isStopPropagation); if (clickListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "create click listener failed"); return false; @@ -116,7 +116,7 @@ void InputCheckboxComponent::DealEvent() if (clickListener_ == nullptr) { // trigger changeEvent - clickListener_ = new ViewOnClickListener(UNDEFINED, true); + clickListener_ = new ViewOnClickListener(GetViewModel(), UNDEFINED, true); if (clickListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "create click listener failed"); return; diff --git a/frameworks/src/core/components/input_radio_component.cpp b/frameworks/src/core/components/input_radio_component.cpp index 16ec00e..453486b 100755 --- a/frameworks/src/core/components/input_radio_component.cpp +++ b/frameworks/src/core/components/input_radio_component.cpp @@ -81,7 +81,7 @@ bool InputRadioComponent::RegisterPrivateEventListener(uint16_t eventTypeId, return true; } if (eventTypeId == K_CLICK) { - clickListener_ = new ViewOnClickListener(funcValue, isStopPropagation); + clickListener_ = new ViewOnClickListener(GetViewModel(), funcValue, isStopPropagation); if (clickListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "create click listener failed"); return false; @@ -127,7 +127,7 @@ void InputRadioComponent::DealEvent() if (clickListener_ == nullptr) { // trigger changeEvent - clickListener_ = new ViewOnClickListener(UNDEFINED, true); + clickListener_ = new ViewOnClickListener(GetViewModel(), UNDEFINED, true); if (clickListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "create click listener failed"); return; diff --git a/frameworks/src/core/components/switch_component.cpp b/frameworks/src/core/components/switch_component.cpp index 5737fb1..333d307 100644 --- a/frameworks/src/core/components/switch_component.cpp +++ b/frameworks/src/core/components/switch_component.cpp @@ -99,7 +99,7 @@ bool SwitchComponent::RegisterPrivateEventListener(uint16_t eventTypeId, return true; } if (eventTypeId == K_CLICK) { - clickListener_ = new ViewOnClickListener(funcValue, isStopPropagation); + clickListener_ = new ViewOnClickListener(GetViewModel(), funcValue, isStopPropagation); if (clickListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "create click listener failed"); return false; @@ -117,7 +117,7 @@ void SwitchComponent::PostRender() toggleButton_.SetOnChangeListener(changeListener_); if (clickListener_ == nullptr) { // trigger changeEvent - clickListener_ = new ViewOnClickListener(UNDEFINED, true); + clickListener_ = new ViewOnClickListener(GetViewModel(), UNDEFINED, true); if (clickListener_ == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "create click listener failed"); return;