Skip to content

Commit

Permalink
Create UIManager interface and extract common classes in uimanager/co…
Browse files Browse the repository at this point in the history
…mmon

Reviewed By: achen1

Differential Revision: D7102674

fbshipit-source-id: e14b6782ad102ec1c3d37988df4bbd4190511f09
  • Loading branch information
mdvacca authored and facebook-github-bot committed Mar 1, 2018
1 parent b181b77 commit 6b45fb2
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMarkerConstants;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec;
import com.facebook.react.common.LifecycleState;
Expand Down Expand Up @@ -328,40 +329,6 @@ public void createReactContextInBackground() {
recreateReactContextInBackgroundInner();
}

@ThreadConfined(UI)
public void registerAdditionalPackages(List<ReactPackage> packages) {
if (packages == null || packages.isEmpty()) {
return;
}

// CatalystInstance hasn't been created, so add packages for later evaluation
if (!hasStartedCreatingInitialContext()) {
synchronized (mPackages) {
for (ReactPackage p : packages) {
if (!mPackages.contains(p)) {
mPackages.add(p);
}
}
}
return;
}

ReactContext context = getCurrentReactContext();
CatalystInstance catalystInstance = context != null ? context.getCatalystInstance() : null;

Assertions.assertNotNull(catalystInstance, "CatalystInstance null after hasStartedCreatingInitialContext true.");

final ReactApplicationContext reactContext = getReactApplicationContext();

NativeModuleRegistry nativeModuleRegistry = processPackages(reactContext, packages, true);
catalystInstance.extendNativeModules(nativeModuleRegistry);
}

@VisibleForTesting
public @Nullable ReactApplicationContext getReactApplicationContext() {
return new ReactApplicationContext(mApplicationContext);
}

/**
* Recreate the react application and context. This should be called if configuration has changed
* or the developer has requested the app to be reloaded. It should only be called after an
Expand Down Expand Up @@ -1040,8 +1007,7 @@ private void attachRootViewToInstance(
CatalystInstance catalystInstance) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.attachRootViewToInstance()");
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachRootViewToInstance");
UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
final int rootTag = uiManagerModule.addRootView(rootView);
final int rootTag = catalystInstance.getNativeModule(UIManagerModule.class).addRootView(rootView);
rootView.setRootViewTag(rootTag);
rootView.invokeJSEntryPoint();
Systrace.beginAsyncSection(
Expand Down Expand Up @@ -1097,7 +1063,7 @@ private ReactApplicationContext createReactContext(
JSBundleLoader jsBundleLoader) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.createReactContext()");
ReactMarker.logMarker(CREATE_REACT_CONTEXT_START);
final ReactApplicationContext reactContext = getReactApplicationContext();
final ReactApplicationContext reactContext = new ReactApplicationContext(mApplicationContext);

if (mUseDeveloperSupport) {
reactContext.setNativeModuleCallExceptionHandler(mDevSupportManager);
Expand All @@ -1113,8 +1079,7 @@ private ReactApplicationContext createReactContext(
.setJSExecutor(jsExecutor)
.setRegistry(nativeModuleRegistry)
.setJSBundleLoader(jsBundleLoader)
.setNativeModuleCallExceptionHandler(exceptionHandler)
.setBridgeListener(mBridgeListener);
.setNativeModuleCallExceptionHandler(exceptionHandler);

ReactMarker.logMarker(CREATE_CATALYST_INSTANCE_START);
// CREATE_CATALYST_INSTANCE_END is in JSCExecutor.cpp
Expand All @@ -1126,6 +1091,9 @@ private ReactApplicationContext createReactContext(
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_CATALYST_INSTANCE_END);
}
if (mBridgeListener != null) {
mBridgeListener.onBridgeStarted(reactContext, catalystInstance);
}

if (mBridgeIdleDebugListener != null) {
catalystInstance.addBridgeIdleDebugListener(mBridgeIdleDebugListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.uimanager.JSTouchDispatcher;
import com.facebook.react.uimanager.MeasureSpecProvider;
import com.facebook.react.uimanager.common.MeasureSpecProvider;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.RootView;
import com.facebook.react.uimanager.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.systrace.Systrace;
Expand Down
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ rn_android_library(
react_native_target("java/com/facebook/debug/holder:holder"),
react_native_target("java/com/facebook/react/common:common"),
react_native_target("java/com/facebook/react/module/model:model"),
react_native_target("java/com/facebook/react/uimanager/common:common"),
] + ([react_native_target("jni/react/jni:jni")] if not IS_OSS_BUILD else []),
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public interface BridgeListener {
* Called right after the RN Bridge is initialized
* @param catalystInstance {@link CatalystInstance} bridge
*/
void onBridgeStarted(CatalystInstance catalystInstance);
void onBridgeStarted(ReactApplicationContext reactApplicationContext, CatalystInstance catalystInstance);

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ void callFunction(
* synchronized(jsContext) { nativeThingNeedingJsContext(jsContext.get()); }
*/
JavaScriptContextHolder getJavaScriptContextHolder();

<T extends UIManager> void setFabricUIManager(T fabricUIManager);

<T extends UIManager> T getFabricUIManager();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public String toString() {
private volatile boolean mAcceptCalls = false;

private boolean mJSBundleHasLoaded;
private UIManager mFabricUIManager;
private @Nullable String mSourceURL;

private JavaScriptContextHolder mJavaScriptContextHolder;
Expand All @@ -98,8 +99,7 @@ private CatalystInstanceImpl(
final JavaScriptExecutor jsExecutor,
final NativeModuleRegistry nativeModuleRegistry,
final JSBundleLoader jsBundleLoader,
NativeModuleCallExceptionHandler nativeModuleCallExceptionHandler,
final BridgeListener bridgeListener) {
NativeModuleCallExceptionHandler nativeModuleCallExceptionHandler) {
Log.d(ReactConstants.TAG, "Initializing React Xplat Bridge.");
mHybridData = initHybrid();

Expand All @@ -125,9 +125,6 @@ private CatalystInstanceImpl(
Log.d(ReactConstants.TAG, "Initializing React Xplat Bridge after initializeBridge");

mJavaScriptContextHolder = new JavaScriptContextHolder(getJavaScriptContext());
if (bridgeListener != null) {
bridgeListener.onBridgeStarted(this);
}
}

private static class BridgeCallback implements ReactCallback {
Expand Down Expand Up @@ -456,6 +453,14 @@ public JavaScriptContextHolder getJavaScriptContextHolder() {
return mJavaScriptContextHolder;
}

public UIManager getFabricUIManager() {
return mFabricUIManager;
}

public void setFabricUIManager(UIManager fabricUIManager) {
mFabricUIManager = fabricUIManager;
}

private native long getJavaScriptContext();

private void incrementPendingJSCalls() {
Expand Down Expand Up @@ -555,7 +560,6 @@ public static class Builder {
private @Nullable NativeModuleRegistry mRegistry;
private @Nullable JavaScriptExecutor mJSExecutor;
private @Nullable NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler;
private @Nullable BridgeListener mBridgeListener;


public Builder setReactQueueConfigurationSpec(
Expand Down Expand Up @@ -585,20 +589,13 @@ public Builder setNativeModuleCallExceptionHandler(
return this;
}

public Builder setBridgeListener(
BridgeListener listener) {
mBridgeListener = listener;
return this;
}

public CatalystInstanceImpl build() {
return new CatalystInstanceImpl(
Assertions.assertNotNull(mReactQueueConfigurationSpec),
Assertions.assertNotNull(mJSExecutor),
Assertions.assertNotNull(mRegistry),
Assertions.assertNotNull(mJSBundleLoader),
Assertions.assertNotNull(mNativeModuleCallExceptionHandler),
mBridgeListener);
Assertions.assertNotNull(mNativeModuleCallExceptionHandler));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.facebook.react.bridge;

import com.facebook.react.uimanager.common.MeasureSpecProvider;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;

public interface UIManager {

/**
* Registers a new root view.
*/
<T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView(final T rootView);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableNativeMap;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.uimanager.MeasureSpecProvider;
import com.facebook.react.uimanager.ReactRootViewTagGenerator;
import com.facebook.react.uimanager.ReactShadowNode;
import com.facebook.react.uimanager.ReactShadowNodeImpl;
import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.ViewManagerRegistry;
import com.facebook.react.uimanager.common.MeasureSpecProvider;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
Expand All @@ -24,7 +24,7 @@
* Fabric API.
*/
@SuppressWarnings("unused") // used from JNI
public class FabricUIManagerModule implements UIModule {
public class FabricUIManagerModule implements UIManager {

private final RootShadowNodeRegistry mRootShadowNodeRegistry = new RootShadowNodeRegistry();
private final ReactApplicationContext mReactApplicationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import android.view.ViewGroup;

import com.facebook.react.uimanager.NativeViewHierarchyManager;
import com.facebook.react.uimanager.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewManagerRegistry;
Expand Down
2 changes: 2 additions & 0 deletions ReactAndroid/src/main/java/com/facebook/react/uimanager/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rn_android_library(
exported_deps = [
":DisplayMetrics",
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("java/com/facebook/react/uimanager/common:common"),
],
provided_deps = [
react_native_dep("third-party/android/support/v4:lib-support-v4"),
Expand All @@ -40,6 +41,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/touch:touch"),
react_native_target("java/com/facebook/react/uimanager/annotations:annotations"),
react_native_target("java/com/facebook/react/uimanager/util:util"),
react_native_target("java/com/facebook/react/uimanager/common:common"),
react_native_target("res:uimanager"),
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.facebook.react.bridge.SoftAssertions;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.touch.JSResponderHandler;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.layoutanimation.LayoutAnimationController;
import com.facebook.react.uimanager.layoutanimation.LayoutAnimationListener;
import com.facebook.systrace.Systrace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.react.uimanager;

import android.view.ViewGroup;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;

/**
* View manager for ReactRootView components.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.uimanager.common.MeasureSpecProvider;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.debug.NotThreadSafeViewHierarchyUpdateDebugListener;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.systrace.Systrace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.common.MeasureSpecProvider;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.debug.NotThreadSafeViewHierarchyUpdateDebugListener;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.systrace.Systrace;
Expand Down Expand Up @@ -72,7 +75,7 @@
*/
@ReactModule(name = UIManagerModule.NAME)
public class UIManagerModule extends ReactContextBaseJavaModule implements
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter, UIModule {
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter, UIManager {

/**
* Enables lazy discovery of a specific {@link ViewManager} by its name.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.modules.core.ReactChoreographer;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.debug.NotThreadSafeViewHierarchyUpdateDebugListener;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//ReactNative:DEFS.bzl", "rn_android_library", "react_native_dep", "react_native_target")

rn_android_library(
name = "common",
srcs = glob(["*.java"]),
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/jsr-305:jsr-305"),
],
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2004-present Facebook. All Rights Reserved.

package com.facebook.react.uimanager;
package com.facebook.react.uimanager.common;

import android.view.View;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.uimanager;
package com.facebook.react.uimanager.common;

import javax.annotation.Nullable;

Expand Down

2 comments on commit 6b45fb2

@codyi96
Copy link
Contributor

@codyi96 codyi96 commented on 6b45fb2 Mar 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removed registerAdditionalPackages function. It's very useful as some package should added after js run to some condition

@xiayezhicheng
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removed registerAdditionalPackages function? It's very useful as some package should added after js run to some condition,Are there any alternatives to registerAdditionalPackages ?

Please sign in to comment.