Implement lazy discovery for ViewManagers.

Reviewed By: kathryngray

Differential Revision: D5865095

fbshipit-source-id: c94970e4cd7aafb20cf844c48feea053ac8b6b0f
This commit is contained in:
Dmitry Zakharov 2017-09-28 09:40:39 -07:00 коммит произвёл Facebook Github Bot
Родитель c4f7ce9afd
Коммит da30b04703
24 изменённых файлов: 469 добавлений и 169 удалений

Просмотреть файл

@ -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),
});
});
}

Просмотреть файл

@ -86,7 +86,7 @@ public class CatalystNativeJSToJavaParametersTestCase extends ReactIntegrationTe
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

Просмотреть файл

@ -63,7 +63,7 @@ public class CatalystNativeJavaToJSArgumentsTestCase extends ReactIntegrationTes
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

Просмотреть файл

@ -113,7 +113,7 @@ public class CatalystNativeJavaToJSReturnValuesTestCase extends ReactIntegration
final UIManagerModule mUIManager =
new UIManagerModule(
getContext(), new ArrayList<ViewManager>(), new UIImplementationProvider(), false, 0);
getContext(), new ArrayList<ViewManager>(), new UIImplementationProvider(), 0);
mAssertModule = new AssertModule();

Просмотреть файл

@ -79,7 +79,7 @@ public class CatalystUIManagerTestCase extends ReactIntegrationTestCase {
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() {

Просмотреть файл

@ -49,7 +49,6 @@ public class JSLocaleTest extends ReactIntegrationTestCase {
getContext(),
viewManagers,
new UIImplementationProvider(),
false,
0);
UiThreadUtil.runOnUiThread(
new Runnable() {

Просмотреть файл

@ -69,7 +69,7 @@ public class ProgressBarTestCase extends ReactIntegrationTestCase {
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

Просмотреть файл

@ -50,7 +50,7 @@ public class ViewRenderingTestCase extends ReactIntegrationTestCase {
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

Просмотреть файл

@ -9,23 +9,25 @@
package com.facebook.react;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
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 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 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<>();
@ -39,9 +41,7 @@ public class CompositeReactPackage extends ReactInstancePackage {
mChildReactPackages.add(arg1);
mChildReactPackages.add(arg2);
for (ReactPackage reactPackage: args) {
mChildReactPackages.add(reactPackage);
}
Collections.addAll(mChildReactPackages, args);
}
/**
@ -56,7 +56,7 @@ public class CompositeReactPackage extends ReactInstancePackage {
moduleMap.put(nativeModule.getName(), nativeModule);
}
}
return new ArrayList(moduleMap.values());
return new ArrayList<>(moduleMap.values());
}
/**
@ -81,7 +81,7 @@ public class CompositeReactPackage extends ReactInstancePackage {
moduleMap.put(nativeModule.getName(), nativeModule);
}
}
return new ArrayList(moduleMap.values());
return new ArrayList<>(moduleMap.values());
}
/**
@ -95,6 +95,44 @@ public class CompositeReactPackage extends ReactInstancePackage {
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;
}
}

Просмотреть файл

@ -38,6 +38,7 @@ import com.facebook.react.uimanager.ViewManager;
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
@ -183,18 +184,34 @@ import javax.inject.Provider;
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);

Просмотреть файл

@ -88,6 +88,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
/**
@ -268,33 +269,32 @@ public class ReactInstanceManager {
mMinTimeLeftInFrameForNonBatchedOperationMs = minTimeLeftInFrameForNonBatchedOperationMs;
mUseSeparateUIBackgroundThread = useSeparateUIBackgroundThread;
mMinNumShakes = minNumShakes;
if (!splitPackagesEnabled) {
CoreModulesPackage coreModulesPackage =
synchronized (mPackages) {
if (!splitPackagesEnabled) {
CoreModulesPackage coreModulesPackage =
new CoreModulesPackage(
this,
mBackBtnHandler,
mUIImplementationProvider,
mLazyViewManagersEnabled,
mMinTimeLeftInFrameForNonBatchedOperationMs);
mPackages.add(coreModulesPackage);
} else {
PrinterHolder.getPrinter().logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: Use Split Packages");
mPackages.add(new BridgeCorePackage(this, mBackBtnHandler));
if (mUseDeveloperSupport) {
mPackages.add(new DebugCorePackage());
}
if (!useOnlyDefaultPackages) {
mPackages.add(new ReactNativeCorePackage(
this,
mBackBtnHandler,
mUIImplementationProvider,
mLazyViewManagersEnabled,
mMinTimeLeftInFrameForNonBatchedOperationMs);
mPackages.add(coreModulesPackage);
} else {
PrinterHolder.getPrinter()
.logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: Use Split Packages");
mPackages.add(new BridgeCorePackage(this, mBackBtnHandler));
if (mUseDeveloperSupport) {
mPackages.add(new DebugCorePackage());
}
if (!useOnlyDefaultPackages) {
mPackages.add(
new ReactNativeCorePackage(
this,
mUIImplementationProvider,
mLazyViewManagersEnabled,
mMinTimeLeftInFrameForNonBatchedOperationMs));
mMinTimeLeftInFrameForNonBatchedOperationMs));
}
}
mPackages.addAll(packages);
}
mPackages.addAll(packages);
// Instantiate ReactChoreographer in UI thread.
ReactChoreographer.initialize();
@ -352,9 +352,11 @@ public class ReactInstanceManager {
// CatalystInstance hasn't been created, so add packages for later evaluation
if (!hasStartedCreatingInitialContext()) {
for (ReactPackage p : packages) {
if (!mPackages.contains(p)) {
mPackages.add(p);
synchronized (mPackages) {
for (ReactPackage p : packages) {
if (!mPackages.contains(p)) {
mPackages.add(p);
}
}
}
return;
@ -771,17 +773,55 @@ public class ReactInstanceManager {
ReactMarker.logMarker(CREATE_VIEW_MANAGERS_START);
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAllViewManagers");
try {
List<ViewManager> allViewManagers = new ArrayList<>();
for (ReactPackage reactPackage : mPackages) {
allViewManagers.addAll(reactPackage.createViewManagers(catalystApplicationContext));
synchronized (mPackages) {
List<ViewManager> allViewManagers = new ArrayList<>();
for (ReactPackage reactPackage : mPackages) {
allViewManagers.addAll(reactPackage.createViewManagers(catalystApplicationContext));
}
return allViewManagers;
}
return allViewManagers;
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_VIEW_MANAGERS_END);
}
}
public @Nullable ViewManager createViewManager(String viewManagerName) {
ReactApplicationContext context =
Assertions.assertNotNull((ReactApplicationContext) getCurrentReactContext());
synchronized (mPackages) {
for (ReactPackage reactPackage : mPackages) {
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
ViewManager viewManager =
((ViewManagerOnDemandReactPackage) reactPackage)
.createViewManager(context, viewManagerName);
if (viewManager != null) {
return viewManager;
}
}
}
}
return null;
}
public List<String> getViewManagerNames() {
ReactApplicationContext context =
Assertions.assertNotNull((ReactApplicationContext) getCurrentReactContext());
synchronized (mPackages) {
Set<String> uniqueNames = new HashSet<>();
for (ReactPackage reactPackage : mPackages) {
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
List<String> names =
((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(context);
if (names != null) {
uniqueNames.addAll(names);
}
}
}
return new ArrayList<>(uniqueNames);
}
}
/**
* Add a listener to be notified of react instance events.
*/
@ -1108,20 +1148,20 @@ public class ReactInstanceManager {
ReactMarker.logMarker(PROCESS_PACKAGES_START);
// TODO(6818138): Solve use-case of native modules overriding
for (ReactPackage reactPackage : packages) {
if (checkAndUpdatePackageMembership && mPackages.contains(reactPackage)) {
continue;
}
Systrace.beginSection(
TRACE_TAG_REACT_JAVA_BRIDGE,
"createAndProcessCustomReactPackage");
try {
if (checkAndUpdatePackageMembership) {
mPackages.add(reactPackage);
synchronized (mPackages) {
for (ReactPackage reactPackage : packages) {
if (checkAndUpdatePackageMembership && mPackages.contains(reactPackage)) {
continue;
}
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAndProcessCustomReactPackage");
try {
if (checkAndUpdatePackageMembership) {
mPackages.add(reactPackage);
}
processPackage(reactPackage, nativeModuleRegistryBuilder);
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
processPackage(reactPackage, nativeModuleRegistryBuilder);
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
ReactMarker.logMarker(PROCESS_PACKAGES_END);

Просмотреть файл

@ -24,6 +24,7 @@ import com.facebook.react.uimanager.ViewManager;
import com.facebook.systrace.Systrace;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import javax.inject.Provider;
/**
@ -77,18 +78,36 @@ public class ReactNativeCorePackage extends LazyReactPackage {
return reactModuleInfoProvider;
}
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 viewManagerResolver =
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,
viewManagerResolver,
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);

Просмотреть файл

@ -0,0 +1,28 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.List;
import javax.annotation.Nullable;
public interface ViewManagerOnDemandReactPackage {
/**
* Provides a list of names of ViewManagers with which these modules can be accessed from JS.
* Typically, this is ViewManager.getName().
*/
List<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.
*/
@Nullable ViewManager createViewManager(ReactApplicationContext reactContext, String viewManagerName);
}

Просмотреть файл

@ -11,6 +11,7 @@ package com.facebook.react.flat;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.events.EventDispatcher;
import java.util.List;
@ -43,4 +44,14 @@ public final class FlatUIImplementationProvider extends UIImplementationProvider
mMemoryImprovementEnabled,
minTimeLeftInFrameForNonBatchedOperationMs);
}
@Override
public FlatUIImplementation createUIImplementation(
ReactApplicationContext reactContext,
UIManagerModule.ViewManagerResolver viewManagerResolver,
EventDispatcher eventDispatcher,
int minTimeLeftInFrameForNonBatchedOperationMs) {
throw new UnsupportedOperationException(
"Lazy version of FlatUIImplementations are not supported");
}
}

Просмотреть файл

@ -55,6 +55,18 @@ public class UIImplementation {
private long mLastCalculateLayoutTime = 0;
public UIImplementation(
ReactApplicationContext reactContext,
UIManagerModule.ViewManagerResolver viewManagerResolver,
EventDispatcher eventDispatcher,
int minTimeLeftInFrameForNonBatchedOperationMs) {
this(
reactContext,
new ViewManagerRegistry(viewManagerResolver),
eventDispatcher,
minTimeLeftInFrameForNonBatchedOperationMs);
}
public UIImplementation(
ReactApplicationContext reactContext,
List<ViewManager> viewManagers,

Просмотреть файл

@ -18,10 +18,25 @@ import java.util.List;
public class UIImplementationProvider {
public UIImplementation createUIImplementation(
ReactApplicationContext reactContext,
List<ViewManager> viewManagers,
UIManagerModule.ViewManagerResolver viewManagerResolver,
EventDispatcher eventDispatcher,
int minTimeLeftInFrameForNonBatchedOperationMs) {
return new UIImplementation(
reactContext, viewManagers, eventDispatcher, minTimeLeftInFrameForNonBatchedOperationMs);
reactContext,
viewManagerResolver,
eventDispatcher,
minTimeLeftInFrameForNonBatchedOperationMs);
}
public UIImplementation createUIImplementation(
ReactApplicationContext reactContext,
List<ViewManager> viewManagerList,
EventDispatcher eventDispatcher,
int minTimeLeftInFrameForNonBatchedOperationMs) {
return new UIImplementation(
reactContext,
viewManagerList,
eventDispatcher,
minTimeLeftInFrameForNonBatchedOperationMs);
}
}

Просмотреть файл

@ -17,7 +17,9 @@ import android.content.res.Configuration;
import com.facebook.common.logging.FLog;
import com.facebook.debug.holder.PrinterHolder;
import com.facebook.debug.tags.ReactDebugOverlayTags;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.animation.Animation;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.GuardedRunnable;
import com.facebook.react.bridge.LifecycleEventListener;
@ -29,6 +31,7 @@ import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.debug.NotThreadSafeViewHierarchyUpdateDebugListener;
@ -72,6 +75,21 @@ import javax.annotation.Nullable;
public class UIManagerModule extends ReactContextBaseJavaModule implements
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter {
/**
* Enables lazy discovery of a specific {@link ViewManager} by its name.
*/
public interface ViewManagerResolver {
/**
* {@class UIManagerModule} class uses this method to get a ViewManager by its name.
* This is the same name that comes from JS by {@code UIManager.ViewManagerName} call.
*/
@Nullable ViewManager getViewManager(String viewManagerName);
/**
* Provides a list of view manager names to register in JS as {@code UIManager.ViewManagerName}
*/
List<String> getViewManagerNames();
}
/**
* Resolves a name coming from native side to a name of the event that is exposed to JS.
@ -90,6 +108,7 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
private final EventDispatcher mEventDispatcher;
private final Map<String, Object> mModuleConstants;
private final Map<String, Object> mCustomDirectEvents;
private final UIImplementation mUIImplementation;
private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback();
@ -97,24 +116,45 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
public UIManagerModule(
ReactApplicationContext reactContext,
List<ViewManager> viewManagerList,
ViewManagerResolver viewManagerResolver,
UIImplementationProvider uiImplementationProvider,
boolean lazyViewManagersEnabled,
int minTimeLeftInFrameForNonBatchedOperationMs) {
super(reactContext);
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(reactContext);
mEventDispatcher = new EventDispatcher(reactContext);
mModuleConstants = createConstants(viewManagerList, lazyViewManagersEnabled);
mModuleConstants = createConstants(viewManagerResolver);
mCustomDirectEvents = UIManagerModuleConstants.getDirectEventTypeConstants();
mUIImplementation =
uiImplementationProvider.createUIImplementation(
reactContext,
viewManagerList,
viewManagerResolver,
mEventDispatcher,
minTimeLeftInFrameForNonBatchedOperationMs);
reactContext.addLifecycleEventListener(this);
}
public UIManagerModule(
ReactApplicationContext reactContext,
List<ViewManager> viewManagersList,
UIImplementationProvider uiImplementationProvider,
int minTimeLeftInFrameForNonBatchedOperationMs) {
super(reactContext);
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(reactContext);
mEventDispatcher = new EventDispatcher(reactContext);
mModuleConstants = createConstants(viewManagersList);
mCustomDirectEvents =
(Map<String, Object>) mModuleConstants.get(
UIManagerModuleConstantsHelper.CUSTOM_DIRECT_EVENTS_KEY);
mUIImplementation =
uiImplementationProvider.createUIImplementation(
reactContext,
viewManagersList,
mEventDispatcher,
minTimeLeftInFrameForNonBatchedOperationMs);
reactContext.addLifecycleEventListener(this);
}
/**
* This method gives an access to the {@link UIImplementation} object that can be used to execute
* operations on the view hierarchy.
@ -163,21 +203,55 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
ViewManagerPropertyUpdater.clear();
}
private static Map<String, Object> createConstants(
List<ViewManager> viewManagerList,
boolean lazyViewManagersEnabled) {
private static Map<String, Object> createConstants(ViewManagerResolver viewManagerResolver) {
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_START);
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateUIManagerConstants");
try {
return UIManagerModuleConstantsHelper.createConstants(
viewManagerList,
lazyViewManagersEnabled);
return UIManagerModuleConstantsHelper.createConstants(viewManagerResolver);
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_END);
}
}
private static Map<String, Object> createConstants(List<ViewManager> viewManagers) {
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_START);
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateUIManagerConstants");
try {
return UIManagerModuleConstantsHelper.createConstants(viewManagers);
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_END);
}
}
@DoNotStrip
@ReactMethod(isBlockingSynchronousMethod = true)
public @Nullable WritableMap getConstantsForViewManager(final String viewManagerName) {
ViewManager targetView =
viewManagerName != null ? mUIImplementation.resolveViewManager(viewManagerName) : null;
if (targetView == null) {
return null;
}
SystraceMessage.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "constants for ViewManager")
.arg("ViewManager", targetView.getName())
.arg("Lazy", true)
.flush();
try {
Map<String, Object> viewManagerConstants =
UIManagerModuleConstantsHelper.createConstantsForViewManager(
targetView,
UIManagerModuleConstants.getBubblingEventTypeConstants(),
UIManagerModuleConstants.getDirectEventTypeConstants(),
null,
mCustomDirectEvents);
return viewManagerConstants != null ? Arguments.makeNativeMap(viewManagerConstants) : null;
} finally {
SystraceMessage.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* Resolves Direct Event name exposed to JS from the one known to the Native side.
*/
@ -185,14 +259,10 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
return new CustomEventNamesResolver() {
@Override
public @Nullable String resolveCustomEventName(String eventName) {
Map<String, Map> directEventTypes =
(Map<String, Map>) getConstants().get(
UIManagerModuleConstantsHelper.CUSTOM_DIRECT_EVENT_TYPES_KEY);
if (directEventTypes != null) {
Map<String, String> customEventType = (Map<String, String>) directEventTypes.get(eventName);
if (customEventType != null) {
return customEventType.get("registrationName");
}
Map<String, String> customEventType =
(Map<String, String>) mCustomDirectEvents.get(eventName);
if (customEventType != null) {
return customEventType.get("registrationName");
}
return eventName;
}

Просмотреть файл

@ -9,14 +9,14 @@
package com.facebook.react.uimanager;
import java.util.List;
import java.util.Map;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
import com.facebook.react.common.MapBuilder;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Helps generate constants map for {@link UIManagerModule} by collecting and merging constants from
@ -24,8 +24,22 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
*/
/* package */ class UIManagerModuleConstantsHelper {
/* package */ static final String CUSTOM_BUBBLING_EVENT_TYPES_KEY = "customBubblingEventTypes";
/* package */ static final String CUSTOM_DIRECT_EVENT_TYPES_KEY = "customDirectEventTypes";
/* package */ static final String CUSTOM_BUBBLING_EVENTS_KEY = "customBubblingEventTypes";
/* package */ static final String CUSTOM_DIRECT_EVENTS_KEY = "customDirectEventTypes";
/**
* Generates a lazy discovery enabled version of {@link UIManagerModule} constants. It only
* contains a list of view manager names, so that JS side is aware of the managers there are.
* Actual ViewManager instantiation happens when {@code UIManager.SpecificViewManager} call happens.
* The View Manager is then registered on the JS side with the help of
* {@code UIManagerModule.getConstantsForViewManager}.
*/
/* package */ static Map<String, Object> createConstants(
UIManagerModule.ViewManagerResolver resolver) {
Map<String, Object> constants = UIManagerModuleConstants.getConstants();
constants.put("ViewManagerNames", resolver.getViewManagerNames());
return constants;
}
/**
* Generates map of constants that is then exposed by {@link UIManagerModule}.
@ -39,8 +53,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
* {@link UIManagerModuleConstants}.
* TODO(6845124): Create a test for this
*/
/* package */ static Map<String, Object> createConstants(
List<ViewManager> viewManagers, boolean lazyViewManagersEnabled) {
/* package */ static Map<String, Object> createConstants(List<ViewManager> viewManagers) {
Map<String, Object> constants = UIManagerModuleConstants.getConstants();
// Generic/default event types:
@ -58,43 +71,22 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
allDirectEventTypes.putAll(genericDirectEventTypes);
for (ViewManager viewManager : viewManagers) {
final String viewManagerName = viewManager.getName();
SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "constants for ViewManager")
.arg("ViewManager", viewManager.getName())
.flush();
.arg("ViewManager", viewManagerName)
.arg("Lazy", false)
.flush();
try {
Map viewManagerConstants = MapBuilder.newHashMap();
Map viewManagerBubblingEvents = viewManager.getExportedCustomBubblingEventTypeConstants();
if (viewManagerBubblingEvents != null) {
recursiveMerge(allBubblingEventTypes, viewManagerBubblingEvents);
recursiveMerge(viewManagerBubblingEvents, genericBubblingEventTypes);
} else {
viewManagerBubblingEvents = genericBubblingEventTypes;
}
viewManagerConstants.put("bubblingEventTypes", viewManagerBubblingEvents);
Map viewManagerDirectEvents = viewManager.getExportedCustomDirectEventTypeConstants();
if (viewManagerDirectEvents != null) {
recursiveMerge(allDirectEventTypes, viewManagerDirectEvents);
recursiveMerge(viewManagerDirectEvents, genericDirectEventTypes);
} else {
viewManagerDirectEvents = genericDirectEventTypes;
}
viewManagerConstants.put("directEventTypes", viewManagerDirectEvents);
Map customViewConstants = viewManager.getExportedViewConstants();
if (customViewConstants != null) {
viewManagerConstants.put("Constants", customViewConstants);
}
Map viewManagerCommands = viewManager.getCommandsMap();
if (viewManagerCommands != null) {
viewManagerConstants.put("Commands", viewManagerCommands);
}
Map<String, String> viewManagerNativeProps = viewManager.getNativeProps();
if (!viewManagerNativeProps.isEmpty()) {
viewManagerConstants.put("NativeProps", viewManagerNativeProps);
}
Map viewManagerConstants = createConstantsForViewManager(
viewManager,
genericBubblingEventTypes,
genericDirectEventTypes,
allBubblingEventTypes,
allDirectEventTypes);
if (!viewManagerConstants.isEmpty()) {
constants.put(viewManager.getName(), viewManagerConstants);
constants.put(viewManagerName, viewManagerConstants);
}
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
@ -102,13 +94,57 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
}
// Used by https://fburl.com/6nskr82o
constants.put(CUSTOM_BUBBLING_EVENT_TYPES_KEY, allBubblingEventTypes);
constants.put(CUSTOM_DIRECT_EVENT_TYPES_KEY, allDirectEventTypes);
constants.put("AndroidLazyViewManagersEnabled", lazyViewManagersEnabled);
constants.put(CUSTOM_BUBBLING_EVENTS_KEY, allBubblingEventTypes);
constants.put(CUSTOM_DIRECT_EVENTS_KEY, allDirectEventTypes);
return constants;
}
/* package */ static Map<String, Object> createConstantsForViewManager(
ViewManager viewManager,
Map defaultBubblingEvents,
Map defaultDirectEvents,
@Nullable Map cumulativeBubblingEventTypes,
@Nullable Map cumulativeDirectEventTypes) {
Map<String, Object> viewManagerConstants = MapBuilder.newHashMap();
Map viewManagerBubblingEvents = viewManager.getExportedCustomBubblingEventTypeConstants();
if (viewManagerBubblingEvents != null) {
if (cumulativeBubblingEventTypes != null) {
recursiveMerge(cumulativeBubblingEventTypes, viewManagerBubblingEvents);
}
recursiveMerge(viewManagerBubblingEvents, defaultBubblingEvents);
} else {
viewManagerBubblingEvents = defaultBubblingEvents;
}
viewManagerConstants.put("bubblingEventTypes", viewManagerBubblingEvents);
Map viewManagerDirectEvents = viewManager.getExportedCustomDirectEventTypeConstants();
if (viewManagerDirectEvents != null) {
if (cumulativeDirectEventTypes != null) {
recursiveMerge(cumulativeDirectEventTypes, viewManagerBubblingEvents);
}
recursiveMerge(viewManagerDirectEvents, defaultDirectEvents);
} else {
viewManagerDirectEvents = defaultDirectEvents;
}
viewManagerConstants.put("directEventTypes", viewManagerDirectEvents);
Map customViewConstants = viewManager.getExportedViewConstants();
if (customViewConstants != null) {
viewManagerConstants.put("Constants", customViewConstants);
}
Map viewManagerCommands = viewManager.getCommandsMap();
if (viewManagerCommands != null) {
viewManagerConstants.put("Commands", viewManagerCommands);
}
Map<String, String> viewManagerNativeProps = viewManager.getNativeProps();
if (!viewManagerNativeProps.isEmpty()) {
viewManagerConstants.put("NativeProps", viewManagerNativeProps);
}
return viewManagerConstants;
}
/**
* Merges {@param source} map into {@param dest} map recursively
*/

Просмотреть файл

@ -9,35 +9,53 @@
package com.facebook.react.uimanager;
import java.util.HashMap;
import com.facebook.react.common.MapBuilder;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Class that stores the mapping between native view name used in JS and the corresponding instance
* of {@link ViewManager}.
*/
public class ViewManagerRegistry {
public final class ViewManagerRegistry {
private final Map<String, ViewManager> mViewManagers;
private final @Nullable UIManagerModule.ViewManagerResolver mViewManagerResolver;
public ViewManagerRegistry(UIManagerModule.ViewManagerResolver viewManagerResolver) {
mViewManagers = MapBuilder.newHashMap();
mViewManagerResolver = viewManagerResolver;
}
public ViewManagerRegistry(List<ViewManager> viewManagerList) {
mViewManagers = new HashMap<>();
Map<String, ViewManager> viewManagerMap = MapBuilder.newHashMap();
for (ViewManager viewManager : viewManagerList) {
mViewManagers.put(viewManager.getName(), viewManager);
viewManagerMap.put(viewManager.getName(), viewManager);
}
mViewManagers = viewManagerMap;
mViewManagerResolver = null;
}
public ViewManagerRegistry(Map<String, ViewManager> viewManagerMap) {
mViewManagers = viewManagerMap;
mViewManagers =
viewManagerMap != null ? viewManagerMap : MapBuilder.<String, ViewManager>newHashMap();
mViewManagerResolver = null;
}
public ViewManager get(String className) {
ViewManager viewManager = mViewManagers.get(className);
if (viewManager != null) {
return viewManager;
} else {
throw new IllegalViewOperationException("No ViewManager defined for class " + className);
}
if (mViewManagerResolver != null) {
viewManager = mViewManagerResolver.getViewManager(className);
if (viewManager != null) {
mViewManagers.put(className, viewManager);
return viewManager;
}
}
throw new IllegalViewOperationException("No ViewManager defined for class " + className);
}
}

Просмотреть файл

@ -143,7 +143,7 @@ public class ReactPropConstantsTest {
List<ViewManager> viewManagers = Arrays.<ViewManager>asList(new ViewManagerUnderTest());
ReactApplicationContext reactContext = new ReactApplicationContext(RuntimeEnvironment.application);
UIManagerModule uiManagerModule =
new UIManagerModule(reactContext, viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(reactContext, viewManagers, new UIImplementationProvider(), 0);
Map<String, String> constants =
(Map) valueAtPath(uiManagerModule.getConstants(), "SomeView", "NativeProps");
assertThat(constants).isEqualTo(

Просмотреть файл

@ -69,7 +69,7 @@ public class UIManagerModuleConstantsTest {
public void testNoCustomConstants() {
List<ViewManager> viewManagers = Arrays.asList(mock(ViewManager.class));
UIManagerModule uiManagerModule =
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, false, 0);
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, 0);
Map<String, Object> constants = uiManagerModule.getConstants();
assertThat(constants)
.containsKey(CUSTOM_BUBBLING_EVENT_TYPES)
@ -84,7 +84,7 @@ public class UIManagerModuleConstantsTest {
when(mockViewManager.getExportedCustomBubblingEventTypeConstants())
.thenReturn(MapBuilder.of("onTwirl", TWIRL_BUBBLING_EVENT_MAP));
UIManagerModule uiManagerModule =
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, false, 0);
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, 0);
Map<String, Object> constants = uiManagerModule.getConstants();
assertThat((Map) constants.get(CUSTOM_BUBBLING_EVENT_TYPES))
.contains(MapEntry.entry("onTwirl", TWIRL_BUBBLING_EVENT_MAP))
@ -98,7 +98,7 @@ public class UIManagerModuleConstantsTest {
when(mockViewManager.getExportedCustomDirectEventTypeConstants())
.thenReturn(MapBuilder.of("onTwirl", TWIRL_DIRECT_EVENT_MAP));
UIManagerModule uiManagerModule =
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, false, 0);
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, 0);
Map<String, Object> constants = uiManagerModule.getConstants();
assertThat((Map) constants.get(CUSTOM_DIRECT_EVENT_TYPES))
.contains(MapEntry.entry("onTwirl", TWIRL_DIRECT_EVENT_MAP))
@ -113,7 +113,7 @@ public class UIManagerModuleConstantsTest {
when(mockViewManager.getExportedViewConstants())
.thenReturn(MapBuilder.of("PhotoSizeType", MapBuilder.of("Small", 1, "Large", 2)));
UIManagerModule uiManagerModule =
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, false, 0);
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, 0);
Map<String, Object> constants = uiManagerModule.getConstants();
assertThat(constants).containsKey("RedPandaPhotoOfTheDayView");
assertThat((Map) constants.get("RedPandaPhotoOfTheDayView")).containsKey("Constants");
@ -129,7 +129,7 @@ public class UIManagerModuleConstantsTest {
when(mockViewManager.getNativeProps())
.thenReturn(MapBuilder.of("fooProp", "number"));
UIManagerModule uiManagerModule =
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, false, 0);
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, 0);
Map<String, Object> constants = uiManagerModule.getConstants();
assertThat((String) valueAtPath(constants, "SomeView", "NativeProps", "fooProp"))
.isEqualTo("number");
@ -161,7 +161,7 @@ public class UIManagerModuleConstantsTest {
List<ViewManager> viewManagers = Arrays.asList(managerX, managerY);
UIManagerModule uiManagerModule =
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, false, 0);
new UIManagerModule(mReactContext, viewManagers, mUIImplementationProvider, 0);
Map<String, Object> constants = uiManagerModule.getConstants();
assertThat((Map) constants.get(CUSTOM_DIRECT_EVENT_TYPES)).containsKey("onTwirl");

Просмотреть файл

@ -816,7 +816,7 @@ public class UIManagerModuleTest {
new ReactTextViewManager(),
new ReactRawTextManager());
UIManagerModule uiManagerModule =
new UIManagerModule(mReactContext, viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(mReactContext, viewManagers, new UIImplementationProvider(), 0);
uiManagerModule.onHostResume();
return uiManagerModule;
}

Просмотреть файл

@ -430,7 +430,7 @@ public class ReactTextTest {
new ReactRawTextManager(),
});
UIManagerModule uiManagerModule =
new UIManagerModule(reactContext, viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(reactContext, viewManagers, new UIImplementationProvider(), 0);
uiManagerModule.onHostResume();
return uiManagerModule;
}

Просмотреть файл

@ -180,7 +180,7 @@ public class TextInputTest {
new ReactTextInputManager(),
});
UIManagerModule uiManagerModule =
new UIManagerModule(reactContext, viewManagers, new UIImplementationProvider(), false, 0);
new UIManagerModule(reactContext, viewManagers, new UIImplementationProvider(), 0);
uiManagerModule.onHostResume();
return uiManagerModule;
}