From 7fff4679d2d412631f4ee9f08a81b56d3f426a67 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Thu, 23 Jan 2020 11:54:45 -0800 Subject: [PATCH] Refactor UIManagerHelper.getUIManager Summary: This diff refactors the UIManagerHelper.getUIManager to allow the caller determine if it should return null when catalyst Istance is not active. This is necessary in order to keep backward compatibility for the getEventDispatcher method. changelog: [internal] Reviewed By: makovkastar Differential Revision: D19383063 fbshipit-source-id: 8a46b61d212480be91ea78929bbfa7248d5f3ad9 --- .../react/uimanager/UIManagerHelper.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 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 295f45c19ce..c21ad088c85 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java @@ -34,14 +34,34 @@ 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) { + return getUIManager(context, uiManagerType, true); + } + + @Nullable + private static UIManager getUIManager( + ReactContext context, + @UIManagerType int uiManagerType, + boolean returnNullIfCatalystIsInactive) { if (context.isBridgeless()) { return (UIManager) context.getJSIModule(JSIModuleType.UIManager); } else { + if (!context.hasCatalystInstance()) { + ReactSoftException.logSoftException( + "UIManagerHelper", + new IllegalStateException( + "Cannot get UIManager because the context doesn't contain a CatalystInstance.")); + return null; + } + // TODO T60461551: add tests to verify emission of events when the ReactContext is being turn + // down. if (!context.hasActiveCatalystInstance()) { ReactSoftException.logSoftException( "UIManagerHelper", - new RuntimeException("Cannot get UIManager: no active Catalyst instance")); - return null; + new IllegalStateException( + "Cannot get UIManager because the context doesn't contain an active CatalystInstance.")); + if (returnNullIfCatalystIsInactive) { + return null; + } } CatalystInstance catalystInstance = context.getCatalystInstance(); return uiManagerType == FABRIC @@ -66,7 +86,7 @@ public class UIManagerHelper { @Nullable public static EventDispatcher getEventDispatcher( ReactContext context, @UIManagerType int uiManagerType) { - UIManager uiManager = getUIManager(context, uiManagerType); + UIManager uiManager = getUIManager(context, uiManagerType, false); return uiManager == null ? null : (EventDispatcher) uiManager.getEventDispatcher(); }