From dacada7750638734cad7f446ef4ad141d5551594 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Tue, 29 Oct 2024 13:18:50 -0700 Subject: [PATCH] use move semantics in TouchEventEmitter (#47232) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/47232 changelog: [internal] TouchEventEmitter APIs take ownership of TouchEvent and PointerEvent, the use of const & qualifiers is misleading and may lead it unnecessary copy. Reviewed By: rubennorte Differential Revision: D65040506 fbshipit-source-id: 941e2eab6a057e31066751e26387604858f03e51 --- .../React/Fabric/RCTSurfacePointerHandler.mm | 12 +-- .../components/view/TouchEventEmitter.cpp | 84 +++++++++++-------- .../components/view/TouchEventEmitter.h | 36 ++++---- 3 files changed, 72 insertions(+), 60 deletions(-) diff --git a/packages/react-native/React/Fabric/RCTSurfacePointerHandler.mm b/packages/react-native/React/Fabric/RCTSurfacePointerHandler.mm index 7206b5c7fa4a49..1dce206826273b 100644 --- a/packages/react-native/React/Fabric/RCTSurfacePointerHandler.mm +++ b/packages/react-native/React/Fabric/RCTSurfacePointerHandler.mm @@ -620,22 +620,22 @@ - (void)_dispatchActivePointers:(std::vector)activePointers event if (eventEmitter != nil) { switch (eventType) { case RCTPointerEventTypeStart: { - eventEmitter->onPointerDown(pointerEvent); + eventEmitter->onPointerDown(std::move(pointerEvent)); break; } case RCTPointerEventTypeMove: { - eventEmitter->onPointerMove(pointerEvent); + eventEmitter->onPointerMove(std::move(pointerEvent)); break; } case RCTPointerEventTypeEnd: { eventEmitter->onPointerUp(pointerEvent); if (pointerEvent.isPrimary && pointerEvent.button == 0 && IsPointerWithinInitialTree(activePointer)) { - eventEmitter->onClick(pointerEvent); + eventEmitter->onClick(std::move(pointerEvent)); } break; } case RCTPointerEventTypeCancel: { - eventEmitter->onPointerCancel(pointerEvent); + eventEmitter->onPointerCancel(std::move(pointerEvent)); break; } } @@ -761,10 +761,10 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer if (eventEmitter != nil) { switch (recognizer.state) { case UIGestureRecognizerStateEnded: - eventEmitter->onPointerLeave(event); + eventEmitter->onPointerLeave(std::move(event)); break; default: - eventEmitter->onPointerMove(event); + eventEmitter->onPointerMove(std::move(event)); break; } } diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.cpp index d147e5db54112c..6264d5fe50d62d 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.cpp @@ -44,11 +44,11 @@ static jsi::Value touchEventPayload( void TouchEventEmitter::dispatchTouchEvent( std::string type, - const TouchEvent& event, + TouchEvent event, RawEvent::Category category) const { dispatchEvent( std::move(type), - [event](jsi::Runtime& runtime) { + [event = std::move(event)](jsi::Runtime& runtime) { return touchEventPayload(runtime, event); }, category); @@ -56,80 +56,92 @@ void TouchEventEmitter::dispatchTouchEvent( void TouchEventEmitter::dispatchPointerEvent( std::string type, - const PointerEvent& event, + PointerEvent event, RawEvent::Category category) const { dispatchEvent( - std::move(type), std::make_shared(event), category); + std::move(type), + std::make_shared(std::move(event)), + category); } -void TouchEventEmitter::onTouchStart(const TouchEvent& event) const { - dispatchTouchEvent("touchStart", event, RawEvent::Category::ContinuousStart); +void TouchEventEmitter::onTouchStart(TouchEvent event) const { + dispatchTouchEvent( + "touchStart", std::move(event), RawEvent::Category::ContinuousStart); } -void TouchEventEmitter::onTouchMove(const TouchEvent& event) const { - dispatchUniqueEvent("touchMove", [event](jsi::Runtime& runtime) { - return touchEventPayload(runtime, event); - }); +void TouchEventEmitter::onTouchMove(TouchEvent event) const { + dispatchUniqueEvent( + "touchMove", [event = std::move(event)](jsi::Runtime& runtime) { + return touchEventPayload(runtime, event); + }); } -void TouchEventEmitter::onTouchEnd(const TouchEvent& event) const { - dispatchTouchEvent("touchEnd", event, RawEvent::Category::ContinuousEnd); +void TouchEventEmitter::onTouchEnd(TouchEvent event) const { + dispatchTouchEvent( + "touchEnd", std::move(event), RawEvent::Category::ContinuousEnd); } -void TouchEventEmitter::onTouchCancel(const TouchEvent& event) const { - dispatchTouchEvent("touchCancel", event, RawEvent::Category::ContinuousEnd); +void TouchEventEmitter::onTouchCancel(TouchEvent event) const { + dispatchTouchEvent( + "touchCancel", std::move(event), RawEvent::Category::ContinuousEnd); } -void TouchEventEmitter::onClick(const PointerEvent& event) const { - dispatchPointerEvent("click", event, RawEvent::Category::Discrete); +void TouchEventEmitter::onClick(PointerEvent event) const { + dispatchPointerEvent("click", std::move(event), RawEvent::Category::Discrete); } -void TouchEventEmitter::onPointerCancel(const PointerEvent& event) const { +void TouchEventEmitter::onPointerCancel(PointerEvent event) const { dispatchPointerEvent( - "pointerCancel", event, RawEvent::Category::ContinuousEnd); + "pointerCancel", std::move(event), RawEvent::Category::ContinuousEnd); } -void TouchEventEmitter::onPointerDown(const PointerEvent& event) const { +void TouchEventEmitter::onPointerDown(PointerEvent event) const { dispatchPointerEvent( - "pointerDown", event, RawEvent::Category::ContinuousStart); + "pointerDown", std::move(event), RawEvent::Category::ContinuousStart); } -void TouchEventEmitter::onPointerMove(const PointerEvent& event) const { - dispatchUniqueEvent("pointerMove", std::make_shared(event)); +void TouchEventEmitter::onPointerMove(PointerEvent event) const { + dispatchUniqueEvent( + "pointerMove", std::make_shared(std::move(event))); } -void TouchEventEmitter::onPointerUp(const PointerEvent& event) const { - dispatchPointerEvent("pointerUp", event, RawEvent::Category::ContinuousEnd); +void TouchEventEmitter::onPointerUp(PointerEvent event) const { + dispatchPointerEvent( + "pointerUp", std::move(event), RawEvent::Category::ContinuousEnd); } -void TouchEventEmitter::onPointerEnter(const PointerEvent& event) const { +void TouchEventEmitter::onPointerEnter(PointerEvent event) const { dispatchPointerEvent( - "pointerEnter", event, RawEvent::Category::ContinuousStart); + "pointerEnter", std::move(event), RawEvent::Category::ContinuousStart); } -void TouchEventEmitter::onPointerLeave(const PointerEvent& event) const { +void TouchEventEmitter::onPointerLeave(PointerEvent event) const { dispatchPointerEvent( - "pointerLeave", event, RawEvent::Category::ContinuousEnd); + "pointerLeave", std::move(event), RawEvent::Category::ContinuousEnd); } -void TouchEventEmitter::onPointerOver(const PointerEvent& event) const { +void TouchEventEmitter::onPointerOver(PointerEvent event) const { dispatchPointerEvent( - "pointerOver", event, RawEvent::Category::ContinuousStart); + "pointerOver", std::move(event), RawEvent::Category::ContinuousStart); } -void TouchEventEmitter::onPointerOut(const PointerEvent& event) const { +void TouchEventEmitter::onPointerOut(PointerEvent event) const { dispatchPointerEvent( - "pointerOut", event, RawEvent::Category::ContinuousStart); + "pointerOut", std::move(event), RawEvent::Category::ContinuousStart); } -void TouchEventEmitter::onGotPointerCapture(const PointerEvent& event) const { +void TouchEventEmitter::onGotPointerCapture(PointerEvent event) const { dispatchPointerEvent( - "gotPointerCapture", event, RawEvent::Category::ContinuousStart); + "gotPointerCapture", + std::move(event), + RawEvent::Category::ContinuousStart); } -void TouchEventEmitter::onLostPointerCapture(const PointerEvent& event) const { +void TouchEventEmitter::onLostPointerCapture(PointerEvent event) const { dispatchPointerEvent( - "lostPointerCapture", event, RawEvent::Category::ContinuousEnd); + "lostPointerCapture", + std::move(event), + RawEvent::Category::ContinuousEnd); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.h b/packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.h index 869e3ac34054ba..e7dcfa7d84aa89 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.h @@ -24,31 +24,31 @@ class TouchEventEmitter : public EventEmitter { public: using EventEmitter::EventEmitter; - void onTouchStart(const TouchEvent& event) const; - void onTouchMove(const TouchEvent& event) const; - void onTouchEnd(const TouchEvent& event) const; - void onTouchCancel(const TouchEvent& event) const; - - void onClick(const PointerEvent& event) const; - void onPointerCancel(const PointerEvent& event) const; - void onPointerDown(const PointerEvent& event) const; - void onPointerMove(const PointerEvent& event) const; - void onPointerUp(const PointerEvent& event) const; - void onPointerEnter(const PointerEvent& event) const; - void onPointerLeave(const PointerEvent& event) const; - void onPointerOver(const PointerEvent& event) const; - void onPointerOut(const PointerEvent& event) const; - void onGotPointerCapture(const PointerEvent& event) const; - void onLostPointerCapture(const PointerEvent& event) const; + void onTouchStart(TouchEvent event) const; + void onTouchMove(TouchEvent event) const; + void onTouchEnd(TouchEvent event) const; + void onTouchCancel(TouchEvent event) const; + + void onClick(PointerEvent event) const; + void onPointerCancel(PointerEvent event) const; + void onPointerDown(PointerEvent event) const; + void onPointerMove(PointerEvent event) const; + void onPointerUp(PointerEvent event) const; + void onPointerEnter(PointerEvent event) const; + void onPointerLeave(PointerEvent event) const; + void onPointerOver(PointerEvent event) const; + void onPointerOut(PointerEvent event) const; + void onGotPointerCapture(PointerEvent event) const; + void onLostPointerCapture(PointerEvent event) const; private: void dispatchTouchEvent( std::string type, - const TouchEvent& event, + TouchEvent event, RawEvent::Category category) const; void dispatchPointerEvent( std::string type, - const PointerEvent& event, + PointerEvent event, RawEvent::Category category) const; };