Event should infer UIManagerType by presence of SurfaceId

Summary:
See comments inline for motivation. It's not safe to use viewtag of an Event to infer whether or not the view is in a Fabric or non-Fabric RootView.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D28365566

fbshipit-source-id: 187ddcc5d5a43a31a71232fdb2f1f5b334bec8c2
This commit is contained in:
Joshua Gross
2021-05-12 09:14:14 -07:00
committed by Facebook GitHub Bot
parent 263478b91c
commit 52b45a44b4
2 changed files with 21 additions and 2 deletions
@@ -535,7 +535,7 @@ import java.util.Queue;
return;
}
UIManager uiManager =
UIManagerHelper.getUIManagerForReactTag(mReactApplicationContext, event.getViewTag());
UIManagerHelper.getUIManager(mReactApplicationContext, event.getUIManagerType());
if (uiManager == null) {
return;
}
@@ -11,6 +11,7 @@ import androidx.annotation.Nullable;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.SystemClock;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.uimanager.common.UIManagerType;
/**
* A UI event that can be dispatched to JS.
@@ -33,6 +34,7 @@ public abstract class Event<T extends Event> {
private static int sUniqueID = 0;
private boolean mInitialized;
private @UIManagerType int mUIManagerType;
private int mSurfaceId;
private int mViewTag;
private long mTimestampMs;
@@ -67,10 +69,23 @@ public abstract class Event<T extends Event> {
protected void init(int surfaceId, int viewTag) {
mSurfaceId = surfaceId;
mViewTag = viewTag;
mInitialized = true;
// We infer UIManagerType. Even though it's not passed in explicitly, we have a
// contract that Fabric events *always* have a SurfaceId passed in, and non-Fabric events
// NEVER have a SurfaceId passed in (the default/placeholder of -1 is passed in instead).
// Why does this matter?
// Events can be sent to Views that are part of the View hierarchy *but not directly managed
// by React Native*. For example, embedded custom hierachies, Litho hierachies, etc.
// In those cases it's important to konw that the Event should be sent to the Fabric or
// non-Fabric UIManager, and we cannot use the ViewTag for inference since it's not controlled
// by RN and is essentially a random number.
// At some point it would be great to pass the SurfaceContext here instead.
mUIManagerType = (surfaceId == -1 ? UIManagerType.DEFAULT : UIManagerType.FABRIC);
// This is a *relative* time. See `getUnixTimestampMs`.
mTimestampMs = SystemClock.uptimeMillis();
mInitialized = true;
}
/** @return the view id for the view that generated this event */
@@ -142,6 +157,10 @@ public abstract class Event<T extends Event> {
onDispose();
}
public final @UIManagerType int getUIManagerType() {
return mUIManagerType;
}
/** @return the name of this event as registered in JS */
public abstract String getEventName();