Skip to content

Commit d15bc8f

Browse files
luluwu2032facebook-github-bot
authored andcommitted
Fall back to eager view manage loading (#41165)
Summary: Currently Bridgeless forces lazy view manager loading, each ReactPackage must implement ```ViewManagerOnDemandReactPackage```. This can bring extra hassle for OSS users in migration. This diff add backward compatibility by falling back to eager view manage loading, after detecting any ReactPackage of current application NOT a subclass of ```ViewManagerOnDemandReactPackage```. Changelog: [Android][Changed] - Fall back to eager view manage loading for Bridgeless Reviewed By: cortinico Differential Revision: D50556405
1 parent dffd9d2 commit d15bc8f

File tree

1 file changed

+41
-4
lines changed
  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime

1 file changed

+41
-4
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import java.util.List;
7272
import java.util.Map;
7373
import java.util.Set;
74+
import java.util.concurrent.ConcurrentHashMap;
7475
import javax.annotation.Nullable;
7576
import kotlin.jvm.functions.Function1;
7677

@@ -94,6 +95,7 @@ final class ReactInstance {
9495
private final TurboModuleManager mTurboModuleManager;
9596
private final FabricUIManager mFabricUIManager;
9697
private final JavaTimerManager mJavaTimerManager;
98+
private final Map<String, ViewManager> mViewManagers = new ConcurrentHashMap<>();
9799

98100
@DoNotStrip @Nullable private ComponentNameResolverManager mComponentNameResolverManager;
99101
@DoNotStrip @Nullable private UIConstantsProviderManager mUIConstantsProviderManager;
@@ -497,8 +499,12 @@ public void registerSegment(int segmentId, String path) {
497499
}
498500

499501
private @Nullable ViewManager createViewManager(String viewManagerName) {
502+
// Return cached view manager if available, no matter it's eagerly or lazily loaded
503+
if (mViewManagers.containsKey(viewManagerName)) {
504+
return mViewManagers.get(viewManagerName);
505+
}
506+
List<ReactPackage> packages = mReactPackages;
500507
if (mDelegate != null) {
501-
List<ReactPackage> packages = mReactPackages;
502508
if (packages != null) {
503509
synchronized (packages) {
504510
for (ReactPackage reactPackage : packages) {
@@ -507,6 +513,7 @@ public void registerSegment(int segmentId, String path) {
507513
((ViewManagerOnDemandReactPackage) reactPackage)
508514
.createViewManager(mBridgelessReactContext, viewManagerName);
509515
if (viewManager != null) {
516+
mViewManagers.put(viewManagerName, viewManager);
510517
return viewManager;
511518
}
512519
}
@@ -515,7 +522,17 @@ public void registerSegment(int segmentId, String path) {
515522
}
516523
}
517524

518-
return null;
525+
// Once a view manager is not found in all react packages via lazy loading, fall back to default
526+
// implementation: eagerly initialize all view managers
527+
for (ReactPackage reactPackage : packages) {
528+
List<ViewManager> viewManagersInPackage =
529+
reactPackage.createViewManagers(mBridgelessReactContext);
530+
for (ViewManager viewManager : viewManagersInPackage) {
531+
mViewManagers.put(viewManager.getName(), viewManager);
532+
}
533+
}
534+
535+
return mViewManagers.get(viewManagerName);
519536
}
520537

521538
private @NonNull Collection<String> getViewManagerNames() {
@@ -542,8 +559,28 @@ public void registerSegment(int segmentId, String path) {
542559

543560
private @NonNull NativeMap getUIManagerConstants() {
544561
List<ViewManager> viewManagers = new ArrayList<ViewManager>();
545-
for (String viewManagerName : getViewManagerNames()) {
546-
viewManagers.add(createViewManager(viewManagerName));
562+
boolean canLoadViewManagersLazily = true;
563+
564+
List<ReactPackage> packages = mReactPackages;
565+
for (ReactPackage reactPackage : packages) {
566+
if (!(reactPackage instanceof ViewManagerOnDemandReactPackage)) {
567+
canLoadViewManagersLazily = false;
568+
break;
569+
}
570+
}
571+
// 1, Retrive view managers via on demand loading
572+
if (canLoadViewManagersLazily) {
573+
for (String viewManagerName : getViewManagerNames()) {
574+
viewManagers.add(createViewManager(viewManagerName));
575+
}
576+
} else {
577+
// 2, There are packages that don't implement ViewManagerOnDemandReactPackage so we retrieve
578+
// view managers via eager loading
579+
for (ReactPackage reactPackage : packages) {
580+
List<ViewManager> viewManagersInPackage =
581+
reactPackage.createViewManagers(mBridgelessReactContext);
582+
viewManagers.addAll(viewManagersInPackage);
583+
}
547584
}
548585
Map<String, Object> constants =
549586
UIManagerModule.createConstants(viewManagers, new HashMap<>(), new HashMap<>());

0 commit comments

Comments
 (0)