Skip to content

Commit 52e60bc

Browse files
javachefacebook-github-bot
authored andcommitted
Move event dispatch logic from FabricUIManager into SurfaceMountingManager
Summary: 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] Differential Revision: D103007280
1 parent 1181985 commit 52e60bc

3 files changed

Lines changed: 54 additions & 47 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
@@ -1206,42 +1206,22 @@ public void receiveEvent(
12061206
return;
12071207
}
12081208

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

12471227
@Override

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

Lines changed: 3 additions & 15 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,10 @@ internal class MountingManager(
338338
) {
339339
val smm = getSurfaceMountingManager(surfaceId, reactTag)
340340
if (smm == null) {
341-
FLog.d(
342-
TAG,
343-
"Cannot queue event without valid surface mounting manager for tag: %d, surfaceId: %d",
344-
reactTag,
345-
surfaceId,
346-
)
341+
FLog.i(TAG, "Unable to invoke event: %s for reactTag: %d", eventName, reactTag)
347342
return
348343
}
349-
smm.enqueuePendingEvent(
350-
reactTag,
351-
eventName,
352-
canCoalesceEvent,
353-
params,
354-
eventCategory,
355-
eventTimestamp,
356-
)
344+
smm.dispatchEvent(reactTag, eventName, canCoalesceEvent, params, eventCategory, eventTimestamp)
357345
}
358346

359347
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
@@ -1164,6 +1164,45 @@ internal constructor(
11641164
}
11651165
}
11661166

1167+
@AnyThread
1168+
internal fun dispatchEvent(
1169+
reactTag: Int,
1170+
eventName: String,
1171+
canCoalesceEvent: Boolean,
1172+
params: WritableMap?,
1173+
@EventCategoryDef eventCategory: Int,
1174+
eventTimestamp: Long,
1175+
) {
1176+
val viewState = tagToViewState[reactTag]
1177+
if (viewState == null) {
1178+
// This can happen if the view has disappeared from the screen (because of async events)
1179+
FLog.i(TAG, "Unable to invoke event: %s for reactTag: %d", eventName, reactTag)
1180+
return
1181+
}
1182+
1183+
val eventEmitter = viewState.eventEmitter
1184+
if (eventEmitter == null) {
1185+
// The view is pre-allocated and created. However, it hasn't been mounted yet. We will have
1186+
// access to the event emitter later when the view is mounted. For now just save the event
1187+
// in the view state and trigger it later.
1188+
enqueuePendingEvent(
1189+
reactTag,
1190+
eventName,
1191+
canCoalesceEvent,
1192+
params,
1193+
eventCategory,
1194+
eventTimestamp,
1195+
)
1196+
return
1197+
}
1198+
1199+
if (canCoalesceEvent) {
1200+
eventEmitter.dispatchUnique(eventName, params, eventTimestamp)
1201+
} else {
1202+
eventEmitter.dispatch(eventName, params, eventCategory, eventTimestamp)
1203+
}
1204+
}
1205+
11671206
public fun markActiveTouchForTag(reactTag: Int): Unit {
11681207
viewsWithActiveTouches.add(reactTag)
11691208
}

0 commit comments

Comments
 (0)