fix: restore proper event dispatch order on Fabric (#42664)

Summary:
This PR fixes the cpp crash coming from events being consumed in `dispatchModern` before forwarding it to listeners:
https://github.com/facebook/react-native/blob/ccff2bb8d19b2db244f30293b4e8d68a524c2059/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/FabricEventDispatcher.java#L42-L45
Because of this, all listeners that require reading from the native map (like RNReanimated using event.toString()) cause a crash since the data is no longer available - [https://github.com/facebook/react-native/blob/49f6ffc92f56953b68f340783e326145a377[…]/react-native/ReactAndroid/src/main/jni/react/jni/NativeMap.cpp](https://github.com/facebook/react-native/blob/49f6ffc92f56953b68f340783e326145a377c3b0/packages/react-native/ReactAndroid/src/main/jni/react/jni/NativeMap.cpp#L16-L19)
Restoring the previous behavior, where the event was forwarded to the listeners first, and only then dispatched further solves the problem:
https://github.com/facebook/react-native/blob/ccff2bb8d19b2db244f30293b4e8d68a524c2059/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcherImpl.java#L116-L126
Was there a reason for changing the order here?

Or maybe there was another reason why the order was changed?

## Changelog:

[ANDROID] [FIXED] - Proper event dispatch order on Fabric

Pull Request resolved: https://github.com/facebook/react-native/pull/42664

Test Plan: Run: https://github.com/WoLewicki/NewArchStylingBug/tree/%40wolewicki/show-fabric-event-crash and try to move the square. It will cause a crash in here: https://github.com/facebook/react-native/blob/ccff2bb8d19b2db244f30293b4e8d68a524c2059/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.java#L110 when trying to read from a consumed map.

Reviewed By: sammy-SC, jessebwr

Differential Revision: D53089137

Pulled By: mdvacca

fbshipit-source-id: 8c282a417d1889732792070a404cf87acad98523
This commit is contained in:
Wojciech Lewicki
2024-01-26 12:41:50 -08:00
committed by Facebook GitHub Bot
parent d4c3311296
commit 751eae94d4
@@ -39,10 +39,10 @@ public class FabricEventDispatcher implements EventDispatcher, LifecycleEventLis
@Override
public void dispatchEvent(Event event) {
event.dispatchModern(mReactEventEmitter);
for (EventDispatcherListener listener : mListeners) {
listener.onEventDispatch(event);
}
event.dispatchModern(mReactEventEmitter);
event.dispose();
maybePostFrameCallbackFromNonUI();