From 34351fd0b19d3dc21814dba5c0f6c643f967c5bb Mon Sep 17 00:00:00 2001 From: David Vacca Date: Thu, 14 Jan 2021 17:21:59 -0800 Subject: [PATCH] Refactor initialization of Fabric to avoid loading UIManagerModule Summary: This diff refactors the intialization of Fabric in order to avoid loading UIManagerModule as part of the creation of FabricJSIModuleProvider. One caveat is that now we are not taking into consideration the flag mLazyViewManagersEnabled master/xplat/js/react-native-github/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java177 if (mLazyViewManagersEnabled) { As a side effect of this diff view managers will be initialized twice if the user has fabric and paper enabled This diff was originally backed out in D25739854 (https://github.com/facebook/react-native/commit/4984c1e525e310f15c7d89230fdb2fa8fea91f05) because it produced a couple of bugs: https://fb.workplace.com/groups/rn.support/permalink/4917641074951135/ https://fb.workplace.com/groups/rn.support/permalink/4918163014898941/ These bugs are fixed by D25667987 (https://github.com/facebook/react-native/commit/2e631471092090e743245377742166ecae1d7e26). This diff was reverted a couple of times because of the change in the registration of eventDispatcher. That's why I'm gating that behavior change as part of the "StaticViewConfig" QE. changelog: [internal] internal Reviewed By: JoshuaGross Differential Revision: D25858934 fbshipit-source-id: a632799ccac728d4efca44ee685519713b4a7cbb --- .../react/fabric/FabricJSIModuleProvider.java | 30 ++++++++++++++----- .../react/uiapp/RNTesterApplication.java | 11 ++++++- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java index 86e5cf38373..c4db712c77c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java @@ -7,6 +7,8 @@ package com.facebook.react.fabric; +import static com.facebook.react.config.ReactFeatureFlags.enableExperimentalStaticViewConfigs; + import androidx.annotation.NonNull; import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.JSIModuleProvider; @@ -27,8 +29,10 @@ import com.facebook.react.fabric.mounting.mountitems.PreAllocateViewMountItem; import com.facebook.react.fabric.mounting.mountitems.SendAccessibilityEvent; import com.facebook.react.uimanager.StateWrapper; import com.facebook.react.uimanager.UIManagerModule; +import com.facebook.react.uimanager.ViewManagerRegistry; import com.facebook.react.uimanager.events.BatchEventDispatchedListener; import com.facebook.react.uimanager.events.EventDispatcher; +import com.facebook.react.uimanager.events.EventDispatcherImpl; import com.facebook.systrace.Systrace; public class FabricJSIModuleProvider implements JSIModuleProvider { @@ -36,14 +40,17 @@ public class FabricJSIModuleProvider implements JSIModuleProvider { @NonNull private final ReactApplicationContext mReactApplicationContext; @NonNull private final ComponentFactory mComponentFactory; @NonNull private final ReactNativeConfig mConfig; + @NonNull private final ViewManagerRegistry mViewManagerRegistry; public FabricJSIModuleProvider( @NonNull ReactApplicationContext reactApplicationContext, @NonNull ComponentFactory componentFactory, - @NonNull ReactNativeConfig config) { + @NonNull ReactNativeConfig config, + @NonNull ViewManagerRegistry viewManagerRegistry) { mReactApplicationContext = reactApplicationContext; mComponentFactory = componentFactory; mConfig = config; + mViewManagerRegistry = viewManagerRegistry; } @Override @@ -79,20 +86,27 @@ public class FabricJSIModuleProvider implements JSIModuleProvider { private FabricUIManager createUIManager(@NonNull EventBeatManager eventBeatManager) { Systrace.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "FabricJSIModuleProvider.createUIManager"); - UIManagerModule nativeModule = - Assertions.assertNotNull(mReactApplicationContext.getNativeModule(UIManagerModule.class)); - EventDispatcher eventDispatcher = nativeModule.getEventDispatcher(); + EventDispatcher eventDispatcher = getEventDispatcher(); FabricUIManager fabricUIManager = new FabricUIManager( - mReactApplicationContext, - nativeModule.getViewManagerRegistry_DO_NOT_USE(), - eventDispatcher, - eventBeatManager); + mReactApplicationContext, mViewManagerRegistry, eventDispatcher, eventBeatManager); Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); return fabricUIManager; } + private EventDispatcher getEventDispatcher() { + EventDispatcher eventDispatcher; + if (enableExperimentalStaticViewConfigs) { + eventDispatcher = new EventDispatcherImpl(mReactApplicationContext); + } else { + UIManagerModule nativeModule = + Assertions.assertNotNull(mReactApplicationContext.getNativeModule(UIManagerModule.class)); + eventDispatcher = nativeModule.getEventDispatcher(); + } + return eventDispatcher; + } + // TODO T31905686: eager load Fabric classes, this is temporary and it will be removed // in the near future private static void loadClasses() { diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java index b892f527c0e..e8eec3ae80b 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java @@ -34,6 +34,7 @@ import com.facebook.react.module.model.ReactModuleInfo; import com.facebook.react.module.model.ReactModuleInfoProvider; import com.facebook.react.shell.MainReactPackage; import com.facebook.react.turbomodule.core.TurboModuleManager; +import com.facebook.react.uimanager.ViewManagerRegistry; import com.facebook.react.views.text.ReactFontManager; import com.facebook.soloader.SoLoader; import java.lang.reflect.InvocationTargetException; @@ -168,6 +169,13 @@ public class RNTesterApplication extends Application implements ReactApplication public JSIModuleProvider getJSIModuleProvider() { final ComponentFactory ComponentFactory = new ComponentFactory(); CoreComponentsRegistry.register(ComponentFactory); + final ReactInstanceManager reactInstanceManager = getReactInstanceManager(); + + ViewManagerRegistry viewManagerRegistry = + new ViewManagerRegistry( + reactInstanceManager.getOrCreateViewManagers( + reactApplicationContext)); + return new FabricJSIModuleProvider( reactApplicationContext, ComponentFactory, @@ -192,7 +200,8 @@ public class RNTesterApplication extends Application implements ReactApplication public double getDouble(final String s) { return 0; } - }); + }, + viewManagerRegistry); } }); }