Skip to content

Commit

Permalink
Implement lazy discovery for ViewManagers.
Browse files Browse the repository at this point in the history
Reviewed By: kathryngray

Differential Revision: D5865095

fbshipit-source-id: c94970e4cd7aafb20cf844c48feea053ac8b6b0f
  • Loading branch information
Dmitry Zakharov authored and facebook-github-bot committed Sep 28, 2017
1 parent c4f7ce9 commit da30b04
Show file tree
Hide file tree
Showing 24 changed files with 466 additions and 166 deletions.
7 changes: 2 additions & 5 deletions Libraries/ReactNative/UIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,10 @@ if (Platform.OS === 'ios') {
});
}
});
} else if (
Platform.OS === 'android' &&
UIManager.AndroidLazyViewManagersEnabled
) {
} else if (Platform.OS === 'android' && UIManager.ViewManagerNames) {
UIManager.ViewManagerNames.forEach(viewManagerName => {
defineLazyObjectProperty(UIManager, viewManagerName, {
get: () => NativeModules[viewManagerName.replace(/^(RCT|RK)/, '')],
get: () => UIManager.getConstantsForViewManager(viewManagerName),
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected void setUp() throws Exception {
List<ViewManager> viewManagers = Arrays.<ViewManager>asList(
new ReactViewManager());
final UIManagerModule mUIManager =
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), 0);
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void setUp() throws Exception {
List<ViewManager> viewManagers = Arrays.<ViewManager>asList(
new ReactViewManager());
final UIManagerModule mUIManager =
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), 0);
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected void setUp() throws Exception {

final UIManagerModule mUIManager =
new UIManagerModule(
getContext(), new ArrayList<ViewManager>(), new UIImplementationProvider(), false, 0);
getContext(), new ArrayList<ViewManager>(), new UIImplementationProvider(), 0);

mAssertModule = new AssertModule();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void setUp() throws Exception {
new ReactTextViewManager(),
new ReactRawTextManager());
uiManager =
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), 0);
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ protected void setUp() throws Exception {
getContext(),
viewManagers,
new UIImplementationProvider(),
false,
0);
UiThreadUtil.runOnUiThread(
new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected void setUp() throws Exception {
new ReactViewManager(),
new ReactProgressBarViewManager());
mUIManager =
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), 0);
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void setUp() throws Exception {

List<ViewManager> viewManagers = Arrays.<ViewManager>asList(new ReactViewManager());
final UIManagerModule uiManager =
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(getContext(), viewManagers, new UIImplementationProvider(), 0);
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@

package com.facebook.react;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import javax.annotation.Nullable;

/**
* {@code CompositeReactPackage} allows to create a single package composed of views and modules
* from several other packages.
*/
public class CompositeReactPackage extends ReactInstancePackage {
public class CompositeReactPackage extends ReactInstancePackage
implements ViewManagerOnDemandReactPackage {

private final List<ReactPackage> mChildReactPackages = new ArrayList<>();

Expand All @@ -39,9 +41,7 @@ public CompositeReactPackage(ReactPackage arg1, ReactPackage arg2, ReactPackage.
mChildReactPackages.add(arg1);
mChildReactPackages.add(arg2);

for (ReactPackage reactPackage: args) {
mChildReactPackages.add(reactPackage);
}
Collections.addAll(mChildReactPackages, args);
}

/**
Expand All @@ -56,7 +56,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
moduleMap.put(nativeModule.getName(), nativeModule);
}
}
return new ArrayList(moduleMap.values());
return new ArrayList<>(moduleMap.values());
}

/**
Expand All @@ -81,7 +81,7 @@ public List<NativeModule> createNativeModules(
moduleMap.put(nativeModule.getName(), nativeModule);
}
}
return new ArrayList(moduleMap.values());
return new ArrayList<>(moduleMap.values());
}

/**
Expand All @@ -95,6 +95,44 @@ public List<ViewManager> createViewManagers(ReactApplicationContext reactContext
viewManagerMap.put(viewManager.getName(), viewManager);
}
}
return new ArrayList(viewManagerMap.values());
return new ArrayList<>(viewManagerMap.values());
}

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

/**
* {@inheritDoc}
*/
@Override
public @Nullable ViewManager createViewManager(
ReactApplicationContext reactContext, String viewManagerName) {
ListIterator<ReactPackage> iterator = mChildReactPackages.listIterator(mChildReactPackages.size());
while (iterator.hasPrevious()) {
ReactPackage reactPackage = iterator.previous();
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
ViewManager viewManager =
((ViewManagerOnDemandReactPackage) reactPackage).createViewManager(reactContext, viewManagerName);
if (viewManager != null) {
return viewManager;
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.facebook.systrace.Systrace;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import javax.inject.Provider;
/**
* This module should be removed following the completion of an experiment into splitting this into
Expand Down Expand Up @@ -183,18 +184,34 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() {
return LazyReactPackage.getReactModuleInfoProviderViaReflection(this);
}

private UIManagerModule createUIManager(ReactApplicationContext reactContext) {
private UIManagerModule createUIManager(final ReactApplicationContext reactContext) {
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_START);
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule");
try {
List<ViewManager> viewManagersList = mReactInstanceManager.createAllViewManagers(
reactContext);
return new UIManagerModule(
reactContext,
viewManagersList,
mUIImplementationProvider,
mLazyViewManagersEnabled,
mMinTimeLeftInFrameForNonBatchedOperationMs);
if (mLazyViewManagersEnabled) {
UIManagerModule.ViewManagerResolver resolver = new UIManagerModule.ViewManagerResolver() {
@Override
public @Nullable ViewManager getViewManager(String viewManagerName) {
return mReactInstanceManager.createViewManager(viewManagerName);
}
@Override
public List<String> getViewManagerNames() {
return mReactInstanceManager.getViewManagerNames();
}
};

return new UIManagerModule(
reactContext,
resolver,
mUIImplementationProvider,
mMinTimeLeftInFrameForNonBatchedOperationMs);
} else {
return new UIManagerModule(
reactContext,
mReactInstanceManager.createAllViewManagers(reactContext),
mUIImplementationProvider,
mMinTimeLeftInFrameForNonBatchedOperationMs);
}
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_END);
Expand Down
Loading

0 comments on commit da30b04

Please sign in to comment.