Make JavaScriptExecutor.Factory a configurable property of ReactInstanceManager

Reviewed By: mhorowitz

Differential Revision: D5662431

fbshipit-source-id: 17dca2744de645740cef252efbf83902acde5046
This commit is contained in:
Michał Gregorczyk 2017-08-21 13:32:46 -07:00 committed by Facebook Github Bot
parent 606a876df7
commit 73f17908e6
8 changed files with 64 additions and 32 deletions

View File

@ -30,7 +30,7 @@ import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec;
import com.facebook.react.bridge.CatalystInstanceImpl;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JSCJavaScriptExecutor;
import com.facebook.react.bridge.JSCJavaScriptExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutor;
import com.facebook.react.modules.core.ReactChoreographer;
@ -73,7 +73,7 @@ public class ReactTestHelper {
}
JavaScriptExecutor executor = null;
try {
executor = new JSCJavaScriptExecutor.Factory(new WritableNativeMap()).create();
executor = new JSCJavaScriptExecutorFactory().create();
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -43,9 +43,9 @@ import com.facebook.infer.annotation.ThreadSafe;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.CatalystInstanceImpl;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JSCJavaScriptExecutor;
import com.facebook.react.bridge.JavaJSExecutor;
import com.facebook.react.bridge.JavaScriptExecutor;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.NativeArray;
import com.facebook.react.bridge.NativeModuleCallExceptionHandler;
import com.facebook.react.bridge.NativeModuleRegistry;
@ -127,7 +127,8 @@ public class ReactInstanceManager {
private volatile @Nullable Thread mCreateReactContextThread;
/* accessed from any thread */
private final @Nullable JSBundleLoader mBundleLoader; /* path to JS bundle on file system */
private final JavaScriptExecutorFactory mJavaScriptExecutorFactory;
private final @Nullable JSBundleLoader mBundleLoader;
private final @Nullable String mJSMainModulePath; /* path to JS bundle root on packager server */
private final List<ReactPackage> mPackages;
private final List<CatalystInstanceImpl.PendingJSCall> mInitFunctions;
@ -178,17 +179,17 @@ public class ReactInstanceManager {
};
private class ReactContextInitParams {
private final JavaScriptExecutor.Factory mJsExecutorFactory;
private final JavaScriptExecutorFactory mJsExecutorFactory;
private final JSBundleLoader mJsBundleLoader;
public ReactContextInitParams(
JavaScriptExecutor.Factory jsExecutorFactory,
JavaScriptExecutorFactory jsExecutorFactory,
JSBundleLoader jsBundleLoader) {
mJsExecutorFactory = Assertions.assertNotNull(jsExecutorFactory);
mJsBundleLoader = Assertions.assertNotNull(jsBundleLoader);
}
public JavaScriptExecutor.Factory getJsExecutorFactory() {
public JavaScriptExecutorFactory getJsExecutorFactory() {
return mJsExecutorFactory;
}
@ -208,6 +209,7 @@ public class ReactInstanceManager {
Context applicationContext,
@Nullable Activity currentActivity,
@Nullable DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler,
JavaScriptExecutorFactory javaScriptExecutorFactory,
@Nullable JSBundleLoader bundleLoader,
@Nullable String jsMainModulePath,
List<ReactPackage> packages,
@ -233,6 +235,7 @@ public class ReactInstanceManager {
mApplicationContext = applicationContext;
mCurrentActivity = currentActivity;
mDefaultBackButtonImpl = defaultHardwareBackBtnHandler;
mJavaScriptExecutorFactory = javaScriptExecutorFactory;
mBundleLoader = bundleLoader;
mJSMainModulePath = jsMainModulePath;
mPackages = new ArrayList<>();
@ -444,7 +447,7 @@ public class ReactInstanceManager {
Log.d(
ReactConstants.TAG,
"ReactInstanceManager.recreateReactContextInBackgroundFromBundleLoader()");
recreateReactContextInBackground(new JSCJavaScriptExecutor.Factory(), mBundleLoader);
recreateReactContextInBackground(mJavaScriptExecutorFactory, mBundleLoader);
}
/**
@ -790,14 +793,14 @@ public class ReactInstanceManager {
private void onJSBundleLoadedFromServer() {
Log.d(ReactConstants.TAG, "ReactInstanceManager.onJSBundleLoadedFromServer()");
recreateReactContextInBackground(
new JSCJavaScriptExecutor.Factory(),
mJavaScriptExecutorFactory,
JSBundleLoader.createCachedBundleFromNetworkLoader(
mDevSupportManager.getSourceUrl(), mDevSupportManager.getDownloadedJSBundleFile()));
}
@ThreadConfined(UI)
private void recreateReactContextInBackground(
JavaScriptExecutor.Factory jsExecutorFactory,
JavaScriptExecutorFactory jsExecutorFactory,
JSBundleLoader jsBundleLoader) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.recreateReactContextInBackground()");
UiThreadUtil.assertOnUiThread();

View File

@ -5,7 +5,9 @@ package com.facebook.react;
import android.app.Activity;
import android.app.Application;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JSCJavaScriptExecutorFactory;
import com.facebook.react.bridge.NativeModuleCallExceptionHandler;
import com.facebook.react.bridge.NotThreadSafeBridgeIdleDebugListener;
import com.facebook.react.common.LifecycleState;
@ -40,6 +42,7 @@ public class ReactInstanceManagerBuilder {
private boolean mLazyNativeModulesEnabled;
private boolean mLazyViewManagersEnabled;
private @Nullable DevBundleDownloadListener mDevBundleDownloadListener;
private @Nullable JavaScriptExecutorFactory mJavaScriptExecutorFactory;
private boolean mUseSeparateUIBackgroundThread;
private int mMinNumShakes = 1;
private boolean mEnableSplitPackage;
@ -59,6 +62,15 @@ public class ReactInstanceManagerBuilder {
return this;
}
/**
* Factory for desired implementation of JavaScriptExecutor.
*/
public ReactInstanceManagerBuilder setJavaScriptExecutorFactory(
@Nullable JavaScriptExecutorFactory javaScriptExecutorFactory) {
mJavaScriptExecutorFactory = javaScriptExecutorFactory;
return this;
}
/**
* Name of the JS bundle file to be loaded from application's raw assets.
* Example: {@code "index.android.js"}
@ -249,6 +261,9 @@ public class ReactInstanceManagerBuilder {
mApplication,
mCurrentActivity,
mDefaultHardwareBackBtnHandler,
mJavaScriptExecutorFactory == null
? new JSCJavaScriptExecutorFactory()
: mJavaScriptExecutorFactory,
(mJSBundleLoader == null && mJSBundleAssetUrl != null)
? JSBundleLoader.createAssetLoader(
mApplication, mJSBundleAssetUrl, false /*Asynchronous*/)

View File

@ -13,27 +13,12 @@ import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public class JSCJavaScriptExecutor extends JavaScriptExecutor {
public static class Factory implements JavaScriptExecutor.Factory {
private ReadableNativeMap mJSCConfig;
public Factory() {
WritableNativeMap jscConfig = new WritableNativeMap();
jscConfig.putString("OwnerIdentity", "ReactNative");
mJSCConfig = jscConfig;
}
@Override
public JavaScriptExecutor create() throws Exception {
return new JSCJavaScriptExecutor(mJSCConfig);
}
}
/* package */ class JSCJavaScriptExecutor extends JavaScriptExecutor {
static {
ReactBridge.staticInit();
}
private JSCJavaScriptExecutor(ReadableNativeMap jscConfig) {
/* package */ JSCJavaScriptExecutor(ReadableNativeMap jscConfig) {
super(initHybrid(jscConfig));
}

View File

@ -0,0 +1,19 @@
/**
* 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.bridge;
public class JSCJavaScriptExecutorFactory implements JavaScriptExecutorFactory {
@Override
public JavaScriptExecutor create() throws Exception {
WritableNativeMap jscConfig = new WritableNativeMap();
jscConfig.putString("OwnerIdentity", "ReactNative");
return new JSCJavaScriptExecutor(jscConfig);
}
}

View File

@ -14,10 +14,6 @@ import com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public abstract class JavaScriptExecutor {
public interface Factory {
JavaScriptExecutor create() throws Exception;
}
private final HybridData mHybridData;
protected JavaScriptExecutor(HybridData hybridData) {

View File

@ -0,0 +1,14 @@
/**
* 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.bridge;
public interface JavaScriptExecutorFactory {
JavaScriptExecutor create() throws Exception;
}

View File

@ -24,7 +24,7 @@ import com.facebook.proguard.annotations.DoNotStrip;
*/
@DoNotStrip
public class ProxyJavaScriptExecutor extends JavaScriptExecutor {
public static class Factory implements JavaScriptExecutor.Factory {
public static class Factory implements JavaScriptExecutorFactory {
private final JavaJSExecutor.Factory mJavaJSExecutorFactory;
public Factory(JavaJSExecutor.Factory javaJSExecutorFactory) {