Refactor BridgeListener into JSIModulesProvider

Reviewed By: achen1

Differential Revision: D7140944

fbshipit-source-id: 092ea8569af5b5f90e005d6dc2c1819c1c9cf58f
This commit is contained in:
David Vacca 2018-03-05 10:55:19 -08:00 committed by Facebook Github Bot
parent 9f239d7914
commit 4466b6fa7c
11 changed files with 89 additions and 41 deletions

View File

@ -43,10 +43,10 @@ import com.facebook.debug.tags.ReactDebugOverlayTags;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.infer.annotation.ThreadSafe;
import com.facebook.react.bridge.BridgeListener;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.CatalystInstanceImpl;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JSIModulesProvider;
import com.facebook.react.bridge.JavaJSExecutor;
import com.facebook.react.bridge.JavaScriptExecutor;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
@ -71,11 +71,11 @@ import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
import com.facebook.react.modules.appregistry.AppRegistry;
import com.facebook.react.modules.fabric.ReactFabric;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.core.ReactChoreographer;
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
import com.facebook.react.modules.fabric.ReactFabric;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.uimanager.UIManagerModule;
@ -157,7 +157,7 @@ public class ReactInstanceManager {
private final @Nullable NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler;
private final boolean mLazyNativeModulesEnabled;
private final boolean mDelayViewManagerClassLoadsEnabled;
private final @Nullable BridgeListener mBridgeListener;
private final @Nullable JSIModulesProvider mJSIModulesProvider;
private List<ViewManager> mViewManagers;
private class ReactContextInitParams {
@ -207,7 +207,7 @@ public class ReactInstanceManager {
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes,
int minTimeLeftInFrameForNonBatchedOperationMs,
@Nullable BridgeListener bridgeListener) {
@Nullable JSIModulesProvider jsiModulesProvider) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.ctor()");
initializeSoLoaderIfNecessary(applicationContext);
@ -256,7 +256,7 @@ public class ReactInstanceManager {
}
mPackages.addAll(packages);
}
mBridgeListener = bridgeListener;
mJSIModulesProvider = jsiModulesProvider;
// Instantiate ReactChoreographer in UI thread.
ReactChoreographer.initialize();
@ -1008,7 +1008,7 @@ public class ReactInstanceManager {
CatalystInstance catalystInstance) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.attachRootViewToInstance()");
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachRootViewToInstance");
UIManager uiManagerModule = rootView.isFabric() ? catalystInstance.getFabricUIManager() : catalystInstance.getNativeModule(UIManagerModule.class);
UIManager uiManagerModule = rootView.isFabric() ? catalystInstance.getJSIModule(UIManager.class) : catalystInstance.getNativeModule(UIManagerModule.class);
final int rootTag = uiManagerModule.addRootView(rootView);
rootView.setRootViewTag(rootTag);
rootView.invokeJSEntryPoint();
@ -1099,8 +1099,9 @@ public class ReactInstanceManager {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_CATALYST_INSTANCE_END);
}
if (mBridgeListener != null) {
mBridgeListener.onBridgeStarted(reactContext, catalystInstance);
if (mJSIModulesProvider != null) {
catalystInstance.addJSIModules(mJSIModulesProvider
.getJSIModules(reactContext, catalystInstance.getJavaScriptContextHolder()));
}
if (mBridgeIdleDebugListener != null) {

View File

@ -7,8 +7,7 @@ import static com.facebook.react.modules.systeminfo.AndroidInfoHelpers.getFriend
import android.app.Activity;
import android.app.Application;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.BridgeListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.JSIModulesProvider;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JSCJavaScriptExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
@ -50,7 +49,7 @@ public class ReactInstanceManagerBuilder {
private @Nullable JavaScriptExecutorFactory mJavaScriptExecutorFactory;
private int mMinNumShakes = 1;
private int mMinTimeLeftInFrameForNonBatchedOperationMs = -1;
private @Nullable BridgeListener mBridgeListener;
private @Nullable JSIModulesProvider mJSIModulesProvider;
/* package protected */ ReactInstanceManagerBuilder() {
}
@ -65,9 +64,9 @@ public class ReactInstanceManagerBuilder {
return this;
}
public ReactInstanceManagerBuilder setBridgeListener(
@Nullable BridgeListener listener) {
mBridgeListener = listener;
public ReactInstanceManagerBuilder setJSIModulesProvider(
@Nullable JSIModulesProvider jsiModulesProvider) {
mJSIModulesProvider = jsiModulesProvider;
return this;
}
@ -290,6 +289,6 @@ public class ReactInstanceManagerBuilder {
mDevBundleDownloadListener,
mMinNumShakes,
mMinTimeLeftInFrameForNonBatchedOperationMs,
mBridgeListener);
mJSIModulesProvider);
}
}

View File

@ -7,6 +7,7 @@
package com.facebook.react;
import com.facebook.react.bridge.JSIModulesProvider;
import javax.annotation.Nullable;
import java.util.List;
@ -14,7 +15,7 @@ import java.util.List;
import android.app.Application;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.BridgeListener;
import com.facebook.react.bridge.JSIModulesProvider;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMarkerConstants;
@ -75,7 +76,7 @@ public abstract class ReactNativeHost {
.setRedBoxHandler(getRedBoxHandler())
.setJavaScriptExecutorFactory(getJavaScriptExecutorFactory())
.setUIImplementationProvider(getUIImplementationProvider())
.setBridgeListener(getBridgeListener())
.setJSIModulesProvider(getJSIModulesProvider())
.setInitialLifecycleState(LifecycleState.BEFORE_CREATE);
for (ReactPackage reactPackage : getPackages()) {
@ -122,7 +123,8 @@ public abstract class ReactNativeHost {
return new UIImplementationProvider();
}
protected @Nullable BridgeListener getBridgeListener() {
protected @Nullable
JSIModulesProvider getJSIModulesProvider() {
return null;
}

View File

@ -1,14 +0,0 @@
package com.facebook.react.bridge;
/**
* Interface to listen to bridge events.
*/
public interface BridgeListener {
/**
* Called right after the RN Bridge is initialized
* @param catalystInstance {@link CatalystInstance} bridge
*/
void onBridgeStarted(ReactApplicationContext reactApplicationContext, CatalystInstance catalystInstance);
}

View File

@ -11,6 +11,7 @@ import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.queue.ReactQueueConfiguration;
import com.facebook.react.common.annotations.VisibleForTesting;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
/**
@ -62,6 +63,7 @@ public interface CatalystInstance
<T extends JavaScriptModule> T getJSModule(Class<T> jsInterface);
<T extends NativeModule> boolean hasNativeModule(Class<T> nativeModuleInterface);
<T extends NativeModule> T getNativeModule(Class<T> nativeModuleInterface);
<T extends JSIModule> T getJSIModule(Class<T> jsiModuleInterface);
Collection<NativeModule> getNativeModules();
/**
@ -99,7 +101,5 @@ public interface CatalystInstance
*/
JavaScriptContextHolder getJavaScriptContextHolder();
<T extends UIManager> void setFabricUIManager(T fabricUIManager);
<T extends UIManager> T getFabricUIManager();
void addJSIModules(List<JSIModuleHolder> jsiModules);
}

View File

@ -26,6 +26,7 @@ import com.facebook.systrace.TraceListener;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
@ -79,13 +80,13 @@ public class CatalystInstanceImpl implements CatalystInstance {
private final Object mJSCallsPendingInitLock = new Object();
private final NativeModuleRegistry mNativeModuleRegistry;
private final JSIModuleRegistry mJSIModuleRegistry = new JSIModuleRegistry();
private final NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler;
private final MessageQueueThread mNativeModulesQueueThread;
private boolean mInitialized = false;
private volatile boolean mAcceptCalls = false;
private boolean mJSBundleHasLoaded;
private UIManager mFabricUIManager;
private @Nullable String mSourceURL;
private JavaScriptContextHolder mJavaScriptContextHolder;
@ -453,12 +454,14 @@ public class CatalystInstanceImpl implements CatalystInstance {
return mJavaScriptContextHolder;
}
public UIManager getFabricUIManager() {
return mFabricUIManager;
@Override
public void addJSIModules(List<JSIModuleHolder> jsiModules) {
mJSIModuleRegistry.registerModules(jsiModules);
}
public void setFabricUIManager(UIManager fabricUIManager) {
mFabricUIManager = fabricUIManager;
@Override
public <T extends JSIModule> T getJSIModule(Class<T> jsiModuleInterface) {
return mJSIModuleRegistry.getModule(jsiModuleInterface);
}
private native long getJavaScriptContext();

View File

@ -0,0 +1,7 @@
package com.facebook.react.bridge;
/**
* Marker interface used to represent a JSI Module.
*/
public interface JSIModule {
}

View File

@ -0,0 +1,12 @@
package com.facebook.react.bridge;
/**
* Holder class used to register {@link JSIModule} into JSI Bridge.
*/
public interface JSIModuleHolder {
Class<? extends JSIModule> getJSIModuleClass();
<T extends JSIModule> T getJSIModule();
}

View File

@ -0,0 +1,23 @@
package com.facebook.react.bridge;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.facebook.infer.annotation.Assertions;
public class JSIModuleRegistry {
private final Map<Class, JSIModule> mModules = new HashMap<>();
public JSIModuleRegistry() { }
public <T extends JSIModule> T getModule(Class<T> moduleClass) {
return (T) Assertions.assertNotNull(mModules.get(moduleClass));
}
public void registerModules(List<JSIModuleHolder> jsiModules) {
for (JSIModuleHolder holder : jsiModules) {
mModules.put(holder.getJSIModuleClass(), holder.getJSIModule());
}
}
}

View File

@ -0,0 +1,15 @@
package com.facebook.react.bridge;
import java.util.List;
/**
* Interface used to initialize JSI Modules into the JSI Bridge.
*/
public interface JSIModulesProvider {
/**
* @return a {@link List<JSIModuleHolder>} that contain the list of JSI Modules.
*/
List<JSIModuleHolder> getJSIModules(ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext);
}

View File

@ -3,7 +3,7 @@ package com.facebook.react.bridge;
import com.facebook.react.uimanager.common.MeasureSpecProvider;
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
public interface UIManager {
public interface UIManager extends JSIModule {
/**
* Registers a new root view.