Skip to content

Commit 97fa2a4

Browse files
javachemeta-codesync[bot]
authored andcommitted
Move event dispatch logic from FabricUIManager into SurfaceMountingManager (facebook#56659)
Summary: Pull Request resolved: facebook#56659 Refactor: consolidate the event dispatch decision (enqueue vs direct dispatch) into `SurfaceMountingManager.dispatchEvent`, removing the 3-way branching from `FabricUIManager.receiveEvent`. The sync event path remains in `FabricUIManager`. This moves `getEventEmitter`, `getViewExists`, and `enqueuePendingEvent` calls behind a single `dispatchEvent` entry point, making the ordering logic fully encapsulated in `SurfaceMountingManager`. Changelog: [Internal] Reviewed By: zeyap Differential Revision: D103007280 fbshipit-source-id: 15eed04c0d936836d2fedeaff09254266c0f0528
1 parent ca83ef3 commit 97fa2a4

3 files changed

Lines changed: 56 additions & 43 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,42 +1199,22 @@ public void receiveEvent(
11991199
return;
12001200
}
12011201

1202-
EventEmitterWrapper eventEmitter = mMountingManager.getEventEmitter(surfaceId, reactTag);
1203-
if (eventEmitter == null) {
1204-
if (mMountingManager.getViewExists(reactTag)) {
1205-
// The view is pre-allocated and created. However, it hasn't been mounted yet. We will have
1206-
// access to the event emitter later when the view is mounted. For now just save the event
1207-
// in the view state and trigger it later.
1208-
mMountingManager.enqueuePendingEvent(
1209-
surfaceId,
1210-
reactTag,
1211-
eventName,
1212-
canCoalesceEvent,
1213-
params,
1214-
eventCategory,
1215-
eventTimestamp);
1216-
} else {
1217-
// This can happen if the view has disappeared from the screen (because of async events)
1218-
FLog.i(TAG, "Unable to invoke event: " + eventName + " for reactTag: " + reactTag);
1219-
}
1220-
return;
1221-
}
1222-
12231202
if (experimentalIsSynchronous) {
12241203
UiThreadUtil.assertOnUiThread();
1225-
// add() returns true only if there are no equivalent events already in the set
1226-
boolean firstEventForFrame =
1227-
mSynchronousEvents.add(new SynchronousEvent(surfaceId, reactTag, eventName));
1228-
if (firstEventForFrame) {
1229-
eventEmitter.dispatchEventSynchronously(eventName, params, eventTimestamp);
1230-
}
1231-
} else {
1232-
if (canCoalesceEvent) {
1233-
eventEmitter.dispatchUnique(eventName, params, eventTimestamp);
1234-
} else {
1235-
eventEmitter.dispatch(eventName, params, eventCategory, eventTimestamp);
1204+
EventEmitterWrapper eventEmitter = mMountingManager.getEventEmitter(surfaceId, reactTag);
1205+
if (eventEmitter != null) {
1206+
// add() returns true only if there are no equivalent events already in the set
1207+
boolean firstEventForFrame =
1208+
mSynchronousEvents.add(new SynchronousEvent(surfaceId, reactTag, eventName));
1209+
if (firstEventForFrame) {
1210+
eventEmitter.dispatchEventSynchronously(eventName, params, eventTimestamp);
1211+
}
1212+
return;
12361213
}
12371214
}
1215+
1216+
mMountingManager.dispatchEvent(
1217+
surfaceId, reactTag, eventName, canCoalesceEvent, params, eventCategory, eventTimestamp);
12381218
}
12391219

12401220
@Override

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ internal class MountingManager(
327327
attachmentsPositions,
328328
)
329329

330-
fun enqueuePendingEvent(
330+
fun dispatchEvent(
331331
surfaceId: Int,
332332
reactTag: Int,
333333
eventName: String,
@@ -338,22 +338,16 @@ internal class MountingManager(
338338
) {
339339
val smm = getSurfaceMountingManager(surfaceId, reactTag)
340340
if (smm == null) {
341-
FLog.d(
341+
FLog.i(
342342
TAG,
343-
"Cannot queue event without valid surface mounting manager for tag: %d, surfaceId: %d",
343+
"Unable to invoke event %s for tag [%d] in surfaceId [%d]",
344+
eventName,
344345
reactTag,
345346
surfaceId,
346347
)
347348
return
348349
}
349-
smm.enqueuePendingEvent(
350-
reactTag,
351-
eventName,
352-
canCoalesceEvent,
353-
params,
354-
eventCategory,
355-
eventTimestamp,
356-
)
350+
smm.dispatchEvent(reactTag, eventName, canCoalesceEvent, params, eventCategory, eventTimestamp)
357351
}
358352

359353
private fun getSurfaceMountingManager(surfaceId: Int, reactTag: Int): SurfaceMountingManager? =

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,45 @@ internal constructor(
12411241
}
12421242
}
12431243

1244+
@AnyThread
1245+
internal fun dispatchEvent(
1246+
reactTag: Int,
1247+
eventName: String,
1248+
canCoalesceEvent: Boolean,
1249+
params: WritableMap?,
1250+
@EventCategoryDef eventCategory: Int,
1251+
eventTimestamp: Long,
1252+
) {
1253+
val viewState = registryGet(reactTag)
1254+
if (viewState == null) {
1255+
// This can happen if the view has disappeared from the screen (because of async events)
1256+
FLog.i(TAG, "Unable to invoke event: %s for reactTag: %d", eventName, reactTag)
1257+
return
1258+
}
1259+
1260+
val eventEmitter = viewState.eventEmitter
1261+
if (eventEmitter == null) {
1262+
// The view is pre-allocated and created. However, it hasn't been mounted yet. We will have
1263+
// access to the event emitter later when the view is mounted. For now just save the event
1264+
// in the view state and trigger it later.
1265+
enqueuePendingEvent(
1266+
reactTag,
1267+
eventName,
1268+
canCoalesceEvent,
1269+
params,
1270+
eventCategory,
1271+
eventTimestamp,
1272+
)
1273+
return
1274+
}
1275+
1276+
if (canCoalesceEvent) {
1277+
eventEmitter.dispatchUnique(eventName, params, eventTimestamp)
1278+
} else {
1279+
eventEmitter.dispatch(eventName, params, eventCategory, eventTimestamp)
1280+
}
1281+
}
1282+
12441283
public fun markActiveTouchForTag(reactTag: Int): Unit {
12451284
viewsWithActiveTouches.add(reactTag)
12461285
}

0 commit comments

Comments
 (0)