Skip to content

Commit 3cbc361

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
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 #15953 Differential Revision: D5909816 Pulled By: shergin fbshipit-source-id: 48d02b6aa9f2bc3bcb638e8852fccaac3f205276
1 parent b694f96 commit 3cbc361

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

Libraries/NativeAnimation/RCTNativeAnimatedModule.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ - (void)animatedNode:(RCTValueAnimatedNode *)node didUpdateValue:(CGFloat)value
239239

240240
- (void)eventDispatcherWillDispatchEvent:(id<RCTEvent>)event
241241
{
242-
// Native animated events only work for events dispatched from the main queue.
243-
if (!RCTIsMainQueue()) {
244-
return;
245-
}
246-
return [_nodesManager handleAnimatedEvent:event];
242+
// Events can be dispatched from any queue so we have to make sure handleAnimatedEvent
243+
// is run from the main queue.
244+
RCTExecuteOnMainQueue(^{
245+
[self->_nodesManager handleAnimatedEvent:event];
246+
});
247247
}
248248

249249
@end

ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,22 @@ public void removeAnimatedEventFromView(int viewTag, String eventName, int anima
361361
}
362362

363363
@Override
364-
public void onEventDispatch(Event event) {
365-
// Only support events dispatched from the UI thread.
366-
if (!UiThreadUtil.isOnUiThread()) {
367-
return;
364+
public void onEventDispatch(final Event event) {
365+
// Events can be dispatched from any thread so we have to make sure handleEvent is run from the
366+
// UI thread.
367+
if (UiThreadUtil.isOnUiThread()) {
368+
handleEvent(event);
369+
} else {
370+
UiThreadUtil.runOnUiThread(new Runnable() {
371+
@Override
372+
public void run() {
373+
handleEvent(event);
374+
}
375+
});
368376
}
377+
}
369378

379+
private void handleEvent(Event event) {
370380
if (!mEventDrivers.isEmpty()) {
371381
// If the event has a different name in native convert it to it's JS name.
372382
String eventName = mCustomEventNamesResolver.resolveCustomEventName(event.getEventName());

0 commit comments

Comments
 (0)