From 4ef3fafa7c8bdac9f2dd80eb77fd7de3e9e44135 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Tue, 10 Dec 2019 20:30:08 -0800 Subject: [PATCH] Extend UIManagerHelper class to retrieve EventDispatcher associated with a ReactContext Summary: This diff extends the UIManagerHelper class to expose the EventDispatcher associated to a tag / UImanagerType Changelog: [internal] Reviewed By: JoshuaGross Differential Revision: D18862863 fbshipit-source-id: 14ce7a6a33f20a94a6296320924dbe3544eadd85 --- .../react/uimanager/UIManagerHelper.java | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java index 3632fe840d7..8384d767007 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java @@ -9,14 +9,14 @@ package com.facebook.react.uimanager; import static com.facebook.react.uimanager.common.UIManagerType.FABRIC; import static com.facebook.react.uimanager.common.ViewUtil.getUIManagerType; - -import androidx.annotation.Nullable; import com.facebook.react.bridge.CatalystInstance; +import androidx.annotation.Nullable; import com.facebook.react.bridge.JSIModuleType; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactSoftException; import com.facebook.react.bridge.UIManager; import com.facebook.react.uimanager.common.UIManagerType; +import com.facebook.react.uimanager.events.EventDispatcher; /** Helper class for {@link UIManager}. */ public class UIManagerHelper { @@ -30,15 +30,39 @@ public class UIManagerHelper { /** @return a {@link UIManager} that can handle the react tag received by parameter. */ @Nullable public static UIManager getUIManager(ReactContext context, @UIManagerType int uiManagerType) { - if (!context.hasActiveCatalystInstance()) { - ReactSoftException.logSoftException( - "UIManagerHelper", - new RuntimeException("Cannot get UIManager: no active Catalyst instance")); - return null; + if (context.isBridgeless()) { + return (UIManager) context.getJSIModule(JSIModuleType.UIManager); + } else { + if (!context.hasActiveCatalystInstance()) { + ReactSoftException.logSoftException( + "UIManagerHelper", + new RuntimeException("Cannot get UIManager: no active Catalyst instance")); + return null; + } + CatalystInstance catalystInstance = context.getCatalystInstance(); + return uiManagerType == FABRIC + ? (UIManager) catalystInstance.getJSIModule(JSIModuleType.UIManager) + : catalystInstance.getNativeModule(UIManagerModule.class); } - CatalystInstance catalystInstance = context.getCatalystInstance(); - return uiManagerType == FABRIC - ? (UIManager) catalystInstance.getJSIModule(JSIModuleType.UIManager) - : catalystInstance.getNativeModule(UIManagerModule.class); + } + + /** + * @return the {@link EventDispatcher} that handles events for the reactTag received as a + * parameter. + */ + @Nullable + public static EventDispatcher getEventDispatcherForReactTag(ReactContext context, int reactTag) { + return getEventDispatcher(context, getUIManagerType(reactTag)); + } + + /** + * @return the {@link EventDispatcher} that handles events for the {@link UIManagerType} received + * as a parameter. + */ + @Nullable + public static EventDispatcher getEventDispatcher( + ReactContext context, @UIManagerType int uiManagerType) { + UIManager uiManager = getUIManager(context, uiManagerType); + return uiManager == null ? null : (EventDispatcher) uiManager.getEventDispatcher(); } }