From b49e7afe47856202d5871a5b341768c57f92c2c9 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 23 Nov 2016 05:35:34 -0800 Subject: [PATCH] Dispatch native handled events to JS Summary: When native events where handled they were not sent to JS as an optimization but this caused some issues. One of the major one is touches are not handled properly inside a ScrollView with an Animated.event because it doesn't receive scroll events so it can't cancel the touch if the user scrolled. Closes https://github.com/facebook/react-native/pull/10981 Differential Revision: D4226403 Pulled By: astreet fbshipit-source-id: 41278d3ed4b684af142d9e273b11b974eb679879 --- Libraries/NativeAnimation/RCTNativeAnimatedModule.m | 8 ++------ React/Base/RCTEventDispatcher.h | 5 ++--- React/Base/RCTEventDispatcher.m | 9 +-------- .../react/animated/NativeAnimatedNodesManager.java | 7 ++----- .../react/uimanager/events/EventDispatcher.java | 10 +--------- .../uimanager/events/EventDispatcherListener.java | 3 +-- 6 files changed, 9 insertions(+), 33 deletions(-) diff --git a/Libraries/NativeAnimation/RCTNativeAnimatedModule.m b/Libraries/NativeAnimation/RCTNativeAnimatedModule.m index 75357f6d679..819d3937e99 100644 --- a/Libraries/NativeAnimation/RCTNativeAnimatedModule.m +++ b/Libraries/NativeAnimation/RCTNativeAnimatedModule.m @@ -322,11 +322,11 @@ RCT_EXPORT_METHOD(removeAnimatedEventFromView:(nonnull NSNumber *)viewTag body:@{@"tag": node.nodeTag, @"value": @(value)}]; } -- (BOOL)eventDispatcherWillDispatchEvent:(id)event +- (void)eventDispatcherWillDispatchEvent:(id)event { // Native animated events only work for events dispatched from the main queue. if (!RCTIsMainQueue() || _eventAnimationDrivers.count == 0) { - return NO; + return; } NSString *key = [NSString stringWithFormat:@"%@%@", event.viewTag, event.eventName]; @@ -336,11 +336,7 @@ RCT_EXPORT_METHOD(removeAnimatedEventFromView:(nonnull NSNumber *)viewTag [driver updateWithEvent:event]; [self updateViewsProps]; [driver.valueNode cleanupAnimationUpdate]; - - return YES; } - - return NO; } - (void)updateViewsProps diff --git a/React/Base/RCTEventDispatcher.h b/React/Base/RCTEventDispatcher.h index b6a4d41bfd9..e65fd003438 100644 --- a/React/Base/RCTEventDispatcher.h +++ b/React/Base/RCTEventDispatcher.h @@ -58,10 +58,9 @@ RCT_EXTERN NSString *RCTNormalizeInputEventName(NSString *eventName); /** * Called before dispatching an event, on the same thread the event was - * dispatched from. Return YES if the event was handled and must not be - * sent to JS. + * dispatched from. */ -- (BOOL)eventDispatcherWillDispatchEvent:(id)event; +- (void)eventDispatcherWillDispatchEvent:(id)event; @end diff --git a/React/Base/RCTEventDispatcher.m b/React/Base/RCTEventDispatcher.m index 381aa265c58..8e9228049f8 100644 --- a/React/Base/RCTEventDispatcher.m +++ b/React/Base/RCTEventDispatcher.m @@ -146,19 +146,12 @@ RCT_EXPORT_MODULE() { [_observersLock lock]; - BOOL eventHandled = NO; for (id observer in _observers) { - if ([observer eventDispatcherWillDispatchEvent:event]) { - eventHandled = YES; - } + [observer eventDispatcherWillDispatchEvent:event]; } [_observersLock unlock]; - if (eventHandled) { - return; - } - [_eventQueueLock lock]; NSNumber *eventID = RCTGetEventID(event); diff --git a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java index 64f44d1285a..c59b60cc79e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java @@ -314,10 +314,10 @@ import javax.annotation.Nullable; } @Override - public boolean onEventDispatch(Event event) { + public void onEventDispatch(Event event) { // Only support events dispatched from the UI thread. if (!UiThreadUtil.isOnUiThread()) { - return false; + return; } if (!mEventDrivers.isEmpty()) { @@ -332,11 +332,8 @@ import javax.annotation.Nullable; if (eventDriver != null) { event.dispatch(eventDriver); mUpdatedNodes.put(eventDriver.mValueNode.mTag, eventDriver.mValueNode); - return true; } } - - return false; } /** diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java index ade36a58ca9..619ed445a14 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java @@ -114,16 +114,8 @@ public class EventDispatcher implements LifecycleEventListener { public void dispatchEvent(Event event) { Assertions.assertCondition(event.isInitialized(), "Dispatched event hasn't been initialized"); - boolean eventHandled = false; for (EventDispatcherListener listener : mListeners) { - if (listener.onEventDispatch(event)) { - eventHandled = true; - } - } - - // If the event was handled by one of the event listener don't send it to JS. - if (eventHandled) { - return; + listener.onEventDispatch(event); } synchronized (mEventsStagingLock) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcherListener.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcherListener.java index 20a9940e9a3..a3e3298eef5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcherListener.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcherListener.java @@ -10,7 +10,6 @@ public interface EventDispatcherListener { * Called on every time an event is dispatched using {#link EventDispatcher#dispatchEvent}. Will be * called from the same thread that the event is being dispatched from. * @param event Event that was dispatched - * @return If the event was handled. If true the event won't be sent to JS. */ - boolean onEventDispatch(Event event); + void onEventDispatch(Event event); }