From 3cbc36138afd50bdbbdf2ad54b7a71a80b319bbc Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Mon, 25 Sep 2017 23:07:01 -0700 Subject: [PATCH] Native Animated - Allow events that are dispatched from any thread Summary: Instead of preventing events from working when not on the UI Thread we can just dispatch to it instead. **Test plan** Tested manually that animated events still work in RNTester Closes https://github.com/facebook/react-native/pull/15953 Differential Revision: D5909816 Pulled By: shergin fbshipit-source-id: 48d02b6aa9f2bc3bcb638e8852fccaac3f205276 --- .../NativeAnimation/RCTNativeAnimatedModule.m | 10 +++++----- .../animated/NativeAnimatedNodesManager.java | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Libraries/NativeAnimation/RCTNativeAnimatedModule.m b/Libraries/NativeAnimation/RCTNativeAnimatedModule.m index 5e182ab68..38ba6395b 100644 --- a/Libraries/NativeAnimation/RCTNativeAnimatedModule.m +++ b/Libraries/NativeAnimation/RCTNativeAnimatedModule.m @@ -239,11 +239,11 @@ RCT_EXPORT_METHOD(removeAnimatedEventFromView:(nonnull NSNumber *)viewTag - (void)eventDispatcherWillDispatchEvent:(id)event { - // Native animated events only work for events dispatched from the main queue. - if (!RCTIsMainQueue()) { - return; - } - return [_nodesManager handleAnimatedEvent:event]; + // Events can be dispatched from any queue so we have to make sure handleAnimatedEvent + // is run from the main queue. + RCTExecuteOnMainQueue(^{ + [self->_nodesManager handleAnimatedEvent:event]; + }); } @end 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 c80266335..68b0ad62a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java @@ -361,12 +361,22 @@ import javax.annotation.Nullable; } @Override - public void onEventDispatch(Event event) { - // Only support events dispatched from the UI thread. - if (!UiThreadUtil.isOnUiThread()) { - return; + public void onEventDispatch(final Event event) { + // Events can be dispatched from any thread so we have to make sure handleEvent is run from the + // UI thread. + if (UiThreadUtil.isOnUiThread()) { + handleEvent(event); + } else { + UiThreadUtil.runOnUiThread(new Runnable() { + @Override + public void run() { + handleEvent(event); + } + }); } + } + private void handleEvent(Event event) { if (!mEventDrivers.isEmpty()) { // If the event has a different name in native convert it to it's JS name. String eventName = mCustomEventNamesResolver.resolveCustomEventName(event.getEventName());