Skip to content

Commit

Permalink
Dispatch native handled events to JS
Browse files Browse the repository at this point in the history
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 #10981

Differential Revision: D4226403

Pulled By: astreet

fbshipit-source-id: 41278d3ed4b684af142d9e273b11b974eb679879
  • Loading branch information
janicduplessis authored and Facebook Github Bot committed Nov 23, 2016
1 parent dad5204 commit b49e7af
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 33 deletions.
8 changes: 2 additions & 6 deletions Libraries/NativeAnimation/RCTNativeAnimatedModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ - (void)animatedNode:(RCTValueAnimatedNode *)node didUpdateValue:(CGFloat)value
body:@{@"tag": node.nodeTag, @"value": @(value)}];
}

- (BOOL)eventDispatcherWillDispatchEvent:(id<RCTEvent>)event
- (void)eventDispatcherWillDispatchEvent:(id<RCTEvent>)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];
Expand All @@ -336,11 +336,7 @@ - (BOOL)eventDispatcherWillDispatchEvent:(id<RCTEvent>)event
[driver updateWithEvent:event];
[self updateViewsProps];
[driver.valueNode cleanupAnimationUpdate];

return YES;
}

return NO;
}

- (void)updateViewsProps
Expand Down
5 changes: 2 additions & 3 deletions React/Base/RCTEventDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<RCTEvent>)event;
- (void)eventDispatcherWillDispatchEvent:(id<RCTEvent>)event;

@end

Expand Down
9 changes: 1 addition & 8 deletions React/Base/RCTEventDispatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,12 @@ - (void)sendEvent:(id<RCTEvent>)event
{
[_observersLock lock];

BOOL eventHandled = NO;
for (id<RCTEventDispatcherObserver> observer in _observers) {
if ([observer eventDispatcherWillDispatchEvent:event]) {
eventHandled = YES;
}
[observer eventDispatcherWillDispatchEvent:event];
}

[_observersLock unlock];

if (eventHandled) {
return;
}

[_eventQueueLock lock];

NSNumber *eventID = RCTGetEventID(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ public void removeAnimatedEventFromView(int viewTag, String eventName) {
}

@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()) {
Expand All @@ -332,11 +332,8 @@ public boolean onEventDispatch(Event event) {
if (eventDriver != null) {
event.dispatch(eventDriver);
mUpdatedNodes.put(eventDriver.mValueNode.mTag, eventDriver.mValueNode);
return true;
}
}

return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,8 @@ public EventDispatcher(ReactApplicationContext reactContext) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit b49e7af

Please sign in to comment.