Skip to content

Commit

Permalink
Simplify ViewManagerOnDemandReactPackage.getViewManagerNames to retur…
Browse files Browse the repository at this point in the history
…n Collection

Summary:
This way we can avoid unnecessary ArrayList copies.

Changelog: [Android][Changed] Generalized the return type of ViewManagerOnDemandReactPackage.getViewManagerNames

Reviewed By: nlutsenko

Differential Revision: D36131516

fbshipit-source-id: 6a42c76cadbcce4c3720875d80062e1ee237bc2f
  • Loading branch information
javache authored and facebook-github-bot committed May 13, 2022
1 parent d7921f0 commit 51e029e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -89,18 +90,18 @@ public List<ViewManager> createViewManagers(ReactApplicationContext reactContext

/** {@inheritDoc} */
@Override
public List<String> getViewManagerNames(ReactApplicationContext reactContext) {
public Collection<String> getViewManagerNames(ReactApplicationContext reactContext) {
Set<String> uniqueNames = new HashSet<>();
for (ReactPackage reactPackage : mChildReactPackages) {
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
List<String> names =
Collection<String> names =
((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(reactContext);
if (names != null) {
uniqueNames.addAll(names);
}
}
}
return new ArrayList<>(uniqueNames);
return uniqueNames;
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.ViewManagerResolver;
import com.facebook.systrace.Systrace;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ private UIManagerModule createUIManager(final ReactApplicationContext reactConte
}

@Override
public List<String> getViewManagerNames() {
public Collection<String> getViewManagerNames() {
return mReactInstanceManager.getViewManagerNames();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public interface ReactInstanceEventListener
private final JavaScriptExecutorFactory mJavaScriptExecutorFactory;

// See {@code ReactInstanceManagerBuilder} for description of all flags here.
private @Nullable List<String> mViewManagerNames = null;
private @Nullable Collection<String> mViewManagerNames = null;
private final @Nullable JSBundleLoader mBundleLoader;
private final @Nullable String mJSMainModulePath; /* path to JS bundle root on Metro */
private final List<ReactPackage> mPackages;
Expand Down Expand Up @@ -962,17 +962,17 @@ public List<ViewManager> getOrCreateViewManagers(
return null;
}

public @Nullable List<String> getViewManagerNames() {
public Collection<String> getViewManagerNames() {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactInstanceManager.getViewManagerNames");
List<String> viewManagerNames = mViewManagerNames;
Collection<String> viewManagerNames = mViewManagerNames;
if (viewManagerNames != null) {
return viewManagerNames;
}
ReactApplicationContext context;
synchronized (mReactContextLock) {
context = (ReactApplicationContext) getCurrentReactContext();
if (context == null || !context.hasActiveReactInstance()) {
return null;
return Collections.emptyList();
}
}

Expand All @@ -985,7 +985,7 @@ public List<ViewManager> getOrCreateViewManagers(
.arg("Package", reactPackage.getClass().getSimpleName())
.flush();
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
List<String> names =
Collection<String> names =
((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(context);
if (names != null) {
uniqueNames.addAll(names);
Expand All @@ -994,7 +994,7 @@ public List<ViewManager> getOrCreateViewManagers(
SystraceMessage.endSection(TRACE_TAG_REACT_JAVA_BRIDGE).flush();
}
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
mViewManagerNames = new ArrayList<>(uniqueNames);
mViewManagerNames = uniqueNames;
}
return mViewManagerNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
import androidx.annotation.Nullable;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.List;
import java.util.Collection;

public interface ViewManagerOnDemandReactPackage {
/**
* Provides a list of names of ViewManagers with which these modules can be accessed from JS.
* Typically, this is ViewManager.getName().
*/
@Nullable
List<String> getViewManagerNames(ReactApplicationContext reactContext);
Collection<String> getViewManagerNames(ReactApplicationContext reactContext);
/**
* Creates and returns a ViewManager with a specific name {@param viewManagerName}. It's up to an
* implementing package how to interpret the name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import androidx.annotation.Nullable;
import com.facebook.react.common.MapBuilder;
import com.facebook.systrace.SystraceMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -33,7 +34,7 @@
*/
/* package */ static Map<String, Object> createConstants(ViewManagerResolver resolver) {
Map<String, Object> constants = UIManagerModuleConstants.getConstants();
constants.put("ViewManagerNames", resolver.getViewManagerNames());
constants.put("ViewManagerNames", new ArrayList<>(resolver.getViewManagerNames()));
constants.put("LazyViewManagersEnabled", true);
return constants;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package com.facebook.react.uimanager;

import java.util.List;
import java.util.Collection;
import javax.annotation.Nullable;

/** Enables lazy discovery of a specific {@link ViewManager} by its name. */
Expand All @@ -22,5 +22,5 @@ public interface ViewManagerResolver {
/**
* Provides a list of view manager names to register in JS as {@code UIManager.ViewManagerName}
*/
List<String> getViewManagerNames();
Collection<String> getViewManagerNames();
}

0 comments on commit 51e029e

Please sign in to comment.